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 : #include <cmath> // For isnan.
9 : #include <limits>
10 : #include <vector>
11 :
12 : #include "src/api-inl.h"
13 :
14 : #include "include/v8-profiler.h"
15 : #include "include/v8-testing.h"
16 : #include "include/v8-util.h"
17 : #include "src/accessors.h"
18 : #include "src/api-natives.h"
19 : #include "src/assert-scope.h"
20 : #include "src/base/functional.h"
21 : #include "src/base/logging.h"
22 : #include "src/base/platform/platform.h"
23 : #include "src/base/platform/time.h"
24 : #include "src/base/safe_conversions.h"
25 : #include "src/base/utils/random-number-generator.h"
26 : #include "src/bootstrapper.h"
27 : #include "src/builtins/builtins-utils.h"
28 : #include "src/char-predicates-inl.h"
29 : #include "src/compiler-dispatcher/compiler-dispatcher.h"
30 : #include "src/compiler.h"
31 : #include "src/contexts.h"
32 : #include "src/conversions-inl.h"
33 : #include "src/counters.h"
34 : #include "src/cpu-features.h"
35 : #include "src/date.h"
36 : #include "src/debug/debug-coverage.h"
37 : #include "src/debug/debug-evaluate.h"
38 : #include "src/debug/debug-type-profile.h"
39 : #include "src/debug/debug.h"
40 : #include "src/debug/liveedit.h"
41 : #include "src/deoptimizer.h"
42 : #include "src/detachable-vector.h"
43 : #include "src/execution.h"
44 : #include "src/frames-inl.h"
45 : #include "src/gdb-jit.h"
46 : #include "src/global-handles.h"
47 : #include "src/globals.h"
48 : #include "src/icu_util.h"
49 : #include "src/isolate-inl.h"
50 : #include "src/json-parser.h"
51 : #include "src/json-stringifier.h"
52 : #include "src/messages.h"
53 : #include "src/microtask-queue.h"
54 : #include "src/objects-inl.h"
55 : #include "src/objects/api-callbacks.h"
56 : #include "src/objects/embedder-data-array-inl.h"
57 : #include "src/objects/embedder-data-slot-inl.h"
58 : #include "src/objects/hash-table-inl.h"
59 : #include "src/objects/heap-object.h"
60 : #include "src/objects/js-array-inl.h"
61 : #include "src/objects/js-collection-inl.h"
62 : #include "src/objects/js-generator-inl.h"
63 : #include "src/objects/js-promise-inl.h"
64 : #include "src/objects/js-regexp-inl.h"
65 : #include "src/objects/module-inl.h"
66 : #include "src/objects/oddball.h"
67 : #include "src/objects/ordered-hash-table-inl.h"
68 : #include "src/objects/slots.h"
69 : #include "src/objects/smi.h"
70 : #include "src/objects/stack-frame-info-inl.h"
71 : #include "src/objects/templates.h"
72 : #include "src/parsing/parse-info.h"
73 : #include "src/parsing/parser.h"
74 : #include "src/parsing/scanner-character-streams.h"
75 : #include "src/pending-compilation-error-handler.h"
76 : #include "src/profiler/cpu-profiler.h"
77 : #include "src/profiler/heap-profiler.h"
78 : #include "src/profiler/heap-snapshot-generator-inl.h"
79 : #include "src/profiler/profile-generator-inl.h"
80 : #include "src/profiler/tick-sample.h"
81 : #include "src/property-descriptor.h"
82 : #include "src/property-details.h"
83 : #include "src/property.h"
84 : #include "src/prototype.h"
85 : #include "src/runtime-profiler.h"
86 : #include "src/runtime/runtime.h"
87 : #include "src/simulator.h"
88 : #include "src/snapshot/code-serializer.h"
89 : #include "src/snapshot/natives.h"
90 : #include "src/snapshot/partial-serializer.h"
91 : #include "src/snapshot/read-only-serializer.h"
92 : #include "src/snapshot/snapshot.h"
93 : #include "src/snapshot/startup-serializer.h"
94 : #include "src/startup-data-util.h"
95 : #include "src/string-hasher.h"
96 : #include "src/tracing/trace-event.h"
97 : #include "src/trap-handler/trap-handler.h"
98 : #include "src/unicode-inl.h"
99 : #include "src/v8.h"
100 : #include "src/v8threads.h"
101 : #include "src/value-serializer.h"
102 : #include "src/version.h"
103 : #include "src/vm-state-inl.h"
104 : #include "src/wasm/streaming-decoder.h"
105 : #include "src/wasm/wasm-engine.h"
106 : #include "src/wasm/wasm-objects-inl.h"
107 : #include "src/wasm/wasm-result.h"
108 : #include "src/wasm/wasm-serialization.h"
109 :
110 : #if V8_OS_LINUX || V8_OS_MACOSX
111 : #include <signal.h>
112 : #include "include/v8-wasm-trap-handler-posix.h"
113 : #include "src/trap-handler/handler-inside-posix.h"
114 : #endif
115 :
116 : #if V8_OS_WIN
117 : #include <windows.h>
118 : #include "include/v8-wasm-trap-handler-win.h"
119 : #include "src/trap-handler/handler-inside-win.h"
120 : #endif
121 :
122 : namespace v8 {
123 :
124 : /*
125 : * Most API methods should use one of the three macros:
126 : *
127 : * ENTER_V8, ENTER_V8_NO_SCRIPT, ENTER_V8_NO_SCRIPT_NO_EXCEPTION.
128 : *
129 : * The latter two assume that no script is executed, and no exceptions are
130 : * scheduled in addition (respectively). Creating a pending exception and
131 : * removing it before returning is ok.
132 : *
133 : * Exceptions should be handled either by invoking one of the
134 : * RETURN_ON_FAILED_EXECUTION* macros.
135 : *
136 : * Don't use macros with DO_NOT_USE in their name.
137 : *
138 : * TODO(jochen): Document debugger specific macros.
139 : * TODO(jochen): Document LOG_API and other RuntimeCallStats macros.
140 : * TODO(jochen): All API methods should invoke one of the ENTER_V8* macros.
141 : * TODO(jochen): Remove calls form API methods to DO_NOT_USE macros.
142 : */
143 :
144 : #define LOG_API(isolate, class_name, function_name) \
145 : i::RuntimeCallTimerScope _runtime_timer( \
146 : isolate, i::RuntimeCallCounterId::kAPI_##class_name##_##function_name); \
147 : LOG(isolate, ApiEntryCall("v8::" #class_name "::" #function_name))
148 :
149 : #define ENTER_V8_DO_NOT_USE(isolate) i::VMState<v8::OTHER> __state__((isolate))
150 :
151 : #define ENTER_V8_HELPER_DO_NOT_USE(isolate, context, class_name, \
152 : function_name, bailout_value, \
153 : HandleScopeClass, do_callback) \
154 : if (IsExecutionTerminatingCheck(isolate)) { \
155 : return bailout_value; \
156 : } \
157 : HandleScopeClass handle_scope(isolate); \
158 : CallDepthScope<do_callback> call_depth_scope(isolate, context); \
159 : LOG_API(isolate, class_name, function_name); \
160 : i::VMState<v8::OTHER> __state__((isolate)); \
161 : bool has_pending_exception = false
162 :
163 : #define PREPARE_FOR_DEBUG_INTERFACE_EXECUTION_WITH_ISOLATE(isolate, T) \
164 : if (IsExecutionTerminatingCheck(isolate)) { \
165 : return MaybeLocal<T>(); \
166 : } \
167 : InternalEscapableScope handle_scope(isolate); \
168 : CallDepthScope<false> call_depth_scope(isolate, v8::Local<v8::Context>()); \
169 : i::VMState<v8::OTHER> __state__((isolate)); \
170 : bool has_pending_exception = false
171 :
172 : #define PREPARE_FOR_EXECUTION_WITH_CONTEXT(context, class_name, function_name, \
173 : bailout_value, HandleScopeClass, \
174 : do_callback) \
175 : auto isolate = context.IsEmpty() \
176 : ? i::Isolate::Current() \
177 : : reinterpret_cast<i::Isolate*>(context->GetIsolate()); \
178 : ENTER_V8_HELPER_DO_NOT_USE(isolate, context, class_name, function_name, \
179 : bailout_value, HandleScopeClass, do_callback);
180 :
181 : #define PREPARE_FOR_EXECUTION(context, class_name, function_name, T) \
182 : PREPARE_FOR_EXECUTION_WITH_CONTEXT(context, class_name, function_name, \
183 : MaybeLocal<T>(), InternalEscapableScope, \
184 : false)
185 :
186 : #define ENTER_V8(isolate, context, class_name, function_name, bailout_value, \
187 : HandleScopeClass) \
188 : ENTER_V8_HELPER_DO_NOT_USE(isolate, context, class_name, function_name, \
189 : bailout_value, HandleScopeClass, true)
190 :
191 : #ifdef DEBUG
192 : #define ENTER_V8_NO_SCRIPT(isolate, context, class_name, function_name, \
193 : bailout_value, HandleScopeClass) \
194 : ENTER_V8_HELPER_DO_NOT_USE(isolate, context, class_name, function_name, \
195 : bailout_value, HandleScopeClass, false); \
196 : i::DisallowJavascriptExecutionDebugOnly __no_script__((isolate))
197 :
198 : #define ENTER_V8_NO_SCRIPT_NO_EXCEPTION(isolate) \
199 : i::VMState<v8::OTHER> __state__((isolate)); \
200 : i::DisallowJavascriptExecutionDebugOnly __no_script__((isolate)); \
201 : i::DisallowExceptions __no_exceptions__((isolate))
202 :
203 : #define ENTER_V8_FOR_NEW_CONTEXT(isolate) \
204 : i::VMState<v8::OTHER> __state__((isolate)); \
205 : i::DisallowExceptions __no_exceptions__((isolate))
206 : #else
207 : #define ENTER_V8_NO_SCRIPT(isolate, context, class_name, function_name, \
208 : bailout_value, HandleScopeClass) \
209 : ENTER_V8_HELPER_DO_NOT_USE(isolate, context, class_name, function_name, \
210 : bailout_value, HandleScopeClass, false)
211 :
212 : #define ENTER_V8_NO_SCRIPT_NO_EXCEPTION(isolate) \
213 : i::VMState<v8::OTHER> __state__((isolate));
214 :
215 : #define ENTER_V8_FOR_NEW_CONTEXT(isolate) \
216 : i::VMState<v8::OTHER> __state__((isolate));
217 : #endif // DEBUG
218 :
219 : #define EXCEPTION_BAILOUT_CHECK_SCOPED_DO_NOT_USE(isolate, value) \
220 : do { \
221 : if (has_pending_exception) { \
222 : call_depth_scope.Escape(); \
223 : return value; \
224 : } \
225 : } while (false)
226 :
227 : #define RETURN_ON_FAILED_EXECUTION(T) \
228 : EXCEPTION_BAILOUT_CHECK_SCOPED_DO_NOT_USE(isolate, MaybeLocal<T>())
229 :
230 : #define RETURN_ON_FAILED_EXECUTION_PRIMITIVE(T) \
231 : EXCEPTION_BAILOUT_CHECK_SCOPED_DO_NOT_USE(isolate, Nothing<T>())
232 :
233 : #define RETURN_TO_LOCAL_UNCHECKED(maybe_local, T) \
234 : return maybe_local.FromMaybe(Local<T>());
235 :
236 :
237 : #define RETURN_ESCAPED(value) return handle_scope.Escape(value);
238 :
239 : namespace {
240 :
241 0 : Local<Context> ContextFromNeverReadOnlySpaceObject(
242 : i::Handle<i::JSReceiver> obj) {
243 0 : return reinterpret_cast<v8::Isolate*>(obj->GetIsolate())->GetCurrentContext();
244 : }
245 :
246 : class InternalEscapableScope : public v8::EscapableHandleScope {
247 : public:
248 : explicit inline InternalEscapableScope(i::Isolate* isolate)
249 7238474 : : v8::EscapableHandleScope(reinterpret_cast<v8::Isolate*>(isolate)) {}
250 : };
251 :
252 : // TODO(jochen): This should be #ifdef DEBUG
253 : #ifdef V8_CHECK_MICROTASKS_SCOPES_CONSISTENCY
254 : void CheckMicrotasksScopesConsistency(i::Isolate* isolate) {
255 : auto handle_scope_implementer = isolate->handle_scope_implementer();
256 : auto* microtask_queue = isolate->default_microtask_queue();
257 : if (handle_scope_implementer->microtasks_policy() ==
258 : v8::MicrotasksPolicy::kScoped) {
259 : DCHECK(microtask_queue->GetMicrotasksScopeDepth() ||
260 : !microtask_queue->DebugMicrotasksScopeDepthIsZero());
261 : }
262 : }
263 : #endif
264 :
265 : template <bool do_callback>
266 : class CallDepthScope {
267 : public:
268 36859498 : explicit CallDepthScope(i::Isolate* isolate, Local<Context> context)
269 : : isolate_(isolate),
270 : context_(context),
271 : escaped_(false),
272 : safe_for_termination_(isolate->next_v8_call_is_safe_for_termination()),
273 : interrupts_scope_(isolate_, i::StackGuard::TERMINATE_EXECUTION,
274 24579101 : isolate_->only_terminate_in_safe_scope()
275 : ? (safe_for_termination_
276 : ? i::InterruptsScope::kRunInterrupts
277 : : i::InterruptsScope::kPostponeInterrupts)
278 36868629 : : i::InterruptsScope::kNoop) {
279 12289558 : isolate_->handle_scope_implementer()->IncrementCallDepth();
280 12289558 : isolate_->set_next_v8_call_is_safe_for_termination(false);
281 12289558 : if (!context.IsEmpty()) {
282 : i::Handle<i::Context> env = Utils::OpenHandle(*context);
283 : i::HandleScopeImplementer* impl = isolate->handle_scope_implementer();
284 36840791 : if (!isolate->context().is_null() &&
285 24560379 : isolate->context()->native_context() == env->native_context()) {
286 12279752 : context_ = Local<Context>();
287 : } else {
288 : impl->SaveContext(isolate->context());
289 : isolate->set_context(*env);
290 : }
291 : }
292 5407060 : if (do_callback) isolate_->FireBeforeCallEnteredCallback();
293 12289560 : }
294 12289531 : ~CallDepthScope() {
295 12289531 : if (!context_.IsEmpty()) {
296 12278737 : i::HandleScopeImplementer* impl = isolate_->handle_scope_implementer();
297 662 : isolate_->set_context(impl->RestoreContext());
298 : }
299 12289531 : if (!escaped_) isolate_->handle_scope_implementer()->DecrementCallDepth();
300 5407029 : if (do_callback) isolate_->FireCallCompletedCallback();
301 : // TODO(jochen): This should be #ifdef DEBUG
302 : #ifdef V8_CHECK_MICROTASKS_SCOPES_CONSISTENCY
303 : if (do_callback) CheckMicrotasksScopesConsistency(isolate_);
304 : #endif
305 12289531 : isolate_->set_next_v8_call_is_safe_for_termination(safe_for_termination_);
306 12289532 : }
307 :
308 11469 : void Escape() {
309 : DCHECK(!escaped_);
310 11469 : escaped_ = true;
311 11469 : auto handle_scope_implementer = isolate_->handle_scope_implementer();
312 : handle_scope_implementer->DecrementCallDepth();
313 : bool clear_exception =
314 : handle_scope_implementer->CallDepthIsZero() &&
315 11469 : isolate_->thread_local_top()->try_catch_handler() == nullptr;
316 11469 : isolate_->OptionalRescheduleException(clear_exception);
317 11469 : }
318 :
319 : private:
320 : i::Isolate* const isolate_;
321 : Local<Context> context_;
322 : bool escaped_;
323 : bool do_callback_;
324 : bool safe_for_termination_;
325 : i::InterruptsScope interrupts_scope_;
326 : };
327 :
328 : } // namespace
329 :
330 :
331 14247 : static ScriptOrigin GetScriptOriginForScript(i::Isolate* isolate,
332 : i::Handle<i::Script> script) {
333 28494 : i::Handle<i::Object> scriptName(script->GetNameOrSourceURL(), isolate);
334 28494 : i::Handle<i::Object> source_map_url(script->source_mapping_url(), isolate);
335 : i::Handle<i::FixedArray> host_defined_options(script->host_defined_options(),
336 28494 : isolate);
337 : v8::Isolate* v8_isolate = reinterpret_cast<v8::Isolate*>(isolate);
338 14247 : ScriptOriginOptions options(script->origin_options());
339 : v8::ScriptOrigin origin(
340 : Utils::ToLocal(scriptName),
341 : v8::Integer::New(v8_isolate, script->line_offset()),
342 : v8::Integer::New(v8_isolate, script->column_offset()),
343 : v8::Boolean::New(v8_isolate, options.IsSharedCrossOrigin()),
344 : v8::Integer::New(v8_isolate, script->id()),
345 : Utils::ToLocal(source_map_url),
346 : v8::Boolean::New(v8_isolate, options.IsOpaque()),
347 : v8::Boolean::New(v8_isolate, script->type() == i::Script::TYPE_WASM),
348 : v8::Boolean::New(v8_isolate, options.IsModule()),
349 42741 : Utils::ToLocal(host_defined_options));
350 14247 : return origin;
351 : }
352 :
353 :
354 : // --- E x c e p t i o n B e h a v i o r ---
355 :
356 5 : void i::FatalProcessOutOfMemory(i::Isolate* isolate, const char* location) {
357 5 : i::V8::FatalProcessOutOfMemory(isolate, location, false);
358 : }
359 :
360 : // When V8 cannot allocate memory FatalProcessOutOfMemory is called. The default
361 : // OOM error handler is called and execution is stopped.
362 15 : void i::V8::FatalProcessOutOfMemory(i::Isolate* isolate, const char* location,
363 : bool is_heap_oom) {
364 : char last_few_messages[Heap::kTraceRingBufferSize + 1];
365 : char js_stacktrace[Heap::kStacktraceBufferSize + 1];
366 : i::HeapStats heap_stats;
367 :
368 15 : if (isolate == nullptr) {
369 : isolate = Isolate::TryGetCurrent();
370 : }
371 :
372 15 : if (isolate == nullptr) {
373 : // If the Isolate is not available for the current thread we cannot retrieve
374 : // memory information from the Isolate. Write easy-to-recognize values on
375 : // the stack.
376 : memset(last_few_messages, 0x0BADC0DE, Heap::kTraceRingBufferSize + 1);
377 : memset(js_stacktrace, 0x0BADC0DE, Heap::kStacktraceBufferSize + 1);
378 : memset(&heap_stats, 0xBADC0DE, sizeof(heap_stats));
379 : // Note that the embedder's oom handler is also not available and therefore
380 : // won't be called in this case. We just crash.
381 0 : FATAL("Fatal process out of memory: %s", location);
382 : UNREACHABLE();
383 : }
384 :
385 : memset(last_few_messages, 0, Heap::kTraceRingBufferSize + 1);
386 : memset(js_stacktrace, 0, Heap::kStacktraceBufferSize + 1);
387 :
388 : intptr_t start_marker;
389 15 : heap_stats.start_marker = &start_marker;
390 : size_t ro_space_size;
391 15 : heap_stats.ro_space_size = &ro_space_size;
392 : size_t ro_space_capacity;
393 15 : heap_stats.ro_space_capacity = &ro_space_capacity;
394 : size_t new_space_size;
395 15 : heap_stats.new_space_size = &new_space_size;
396 : size_t new_space_capacity;
397 15 : heap_stats.new_space_capacity = &new_space_capacity;
398 : size_t old_space_size;
399 15 : heap_stats.old_space_size = &old_space_size;
400 : size_t old_space_capacity;
401 15 : heap_stats.old_space_capacity = &old_space_capacity;
402 : size_t code_space_size;
403 15 : heap_stats.code_space_size = &code_space_size;
404 : size_t code_space_capacity;
405 15 : heap_stats.code_space_capacity = &code_space_capacity;
406 : size_t map_space_size;
407 15 : heap_stats.map_space_size = &map_space_size;
408 : size_t map_space_capacity;
409 15 : heap_stats.map_space_capacity = &map_space_capacity;
410 : size_t lo_space_size;
411 15 : heap_stats.lo_space_size = &lo_space_size;
412 : size_t code_lo_space_size;
413 15 : heap_stats.code_lo_space_size = &code_lo_space_size;
414 : size_t global_handle_count;
415 15 : heap_stats.global_handle_count = &global_handle_count;
416 : size_t weak_global_handle_count;
417 15 : heap_stats.weak_global_handle_count = &weak_global_handle_count;
418 : size_t pending_global_handle_count;
419 15 : heap_stats.pending_global_handle_count = &pending_global_handle_count;
420 : size_t near_death_global_handle_count;
421 15 : heap_stats.near_death_global_handle_count = &near_death_global_handle_count;
422 : size_t free_global_handle_count;
423 15 : heap_stats.free_global_handle_count = &free_global_handle_count;
424 : size_t memory_allocator_size;
425 15 : heap_stats.memory_allocator_size = &memory_allocator_size;
426 : size_t memory_allocator_capacity;
427 15 : heap_stats.memory_allocator_capacity = &memory_allocator_capacity;
428 : size_t malloced_memory;
429 15 : heap_stats.malloced_memory = &malloced_memory;
430 : size_t malloced_peak_memory;
431 15 : heap_stats.malloced_peak_memory = &malloced_peak_memory;
432 15 : size_t objects_per_type[LAST_TYPE + 1] = {0};
433 15 : heap_stats.objects_per_type = objects_per_type;
434 15 : size_t size_per_type[LAST_TYPE + 1] = {0};
435 15 : heap_stats.size_per_type = size_per_type;
436 : int os_error;
437 15 : heap_stats.os_error = &os_error;
438 15 : heap_stats.last_few_messages = last_few_messages;
439 15 : heap_stats.js_stacktrace = js_stacktrace;
440 : intptr_t end_marker;
441 15 : heap_stats.end_marker = &end_marker;
442 15 : if (isolate->heap()->HasBeenSetUp()) {
443 : // BUG(1718): Don't use the take_snapshot since we don't support
444 : // HeapIterator here without doing a special GC.
445 15 : isolate->heap()->RecordStats(&heap_stats, false);
446 : char* first_newline = strchr(last_few_messages, '\n');
447 15 : if (first_newline == nullptr || first_newline[1] == '\0')
448 : first_newline = last_few_messages;
449 15 : PrintF("\n<--- Last few GCs --->\n%s\n", first_newline);
450 15 : PrintF("\n<--- JS stacktrace --->\n%s\n", js_stacktrace);
451 : }
452 15 : Utils::ReportOOMFailure(isolate, location, is_heap_oom);
453 : // If the fatal error handler returns, we stop execution.
454 0 : FATAL("API fatal error handler returned after process out of memory");
455 : }
456 :
457 :
458 10 : void Utils::ReportApiFailure(const char* location, const char* message) {
459 10 : i::Isolate* isolate = i::Isolate::Current();
460 : FatalErrorCallback callback = nullptr;
461 10 : if (isolate != nullptr) {
462 : callback = isolate->exception_behavior();
463 : }
464 10 : if (callback == nullptr) {
465 : base::OS::PrintError("\n#\n# Fatal error in %s\n# %s\n#\n\n", location,
466 0 : message);
467 0 : base::OS::Abort();
468 : } else {
469 10 : callback(location, message);
470 : }
471 : isolate->SignalFatalError();
472 10 : }
473 :
474 30 : void Utils::ReportOOMFailure(i::Isolate* isolate, const char* location,
475 : bool is_heap_oom) {
476 : OOMErrorCallback oom_callback = isolate->oom_behavior();
477 15 : if (oom_callback == nullptr) {
478 : // TODO(wfh): Remove this fallback once Blink is setting OOM handler. See
479 : // crbug.com/614440.
480 : FatalErrorCallback fatal_callback = isolate->exception_behavior();
481 15 : if (fatal_callback == nullptr) {
482 : base::OS::PrintError("\n#\n# Fatal %s OOM in %s\n#\n\n",
483 0 : is_heap_oom ? "javascript" : "process", location);
484 0 : base::OS::Abort();
485 : } else {
486 : fatal_callback(location,
487 : is_heap_oom
488 : ? "Allocation failed - JavaScript heap out of memory"
489 15 : : "Allocation failed - process out of memory");
490 : }
491 : } else {
492 0 : oom_callback(location, is_heap_oom);
493 : }
494 : isolate->SignalFatalError();
495 0 : }
496 :
497 12291050 : static inline bool IsExecutionTerminatingCheck(i::Isolate* isolate) {
498 12291050 : if (isolate->has_scheduled_exception()) {
499 : return isolate->scheduled_exception() ==
500 : i::ReadOnlyRoots(isolate).termination_exception();
501 : }
502 : return false;
503 : }
504 :
505 :
506 61288 : void V8::SetNativesDataBlob(StartupData* natives_blob) {
507 61288 : i::V8::SetNativesBlob(natives_blob);
508 61288 : }
509 :
510 :
511 61288 : void V8::SetSnapshotDataBlob(StartupData* snapshot_blob) {
512 61288 : i::V8::SetSnapshotBlob(snapshot_blob);
513 61288 : }
514 :
515 : namespace {
516 :
517 293562 : class ArrayBufferAllocator : public v8::ArrayBuffer::Allocator {
518 : public:
519 232799 : void* Allocate(size_t length) override {
520 : #if V8_OS_AIX && _LINUX_SOURCE_COMPAT
521 : // Work around for GCC bug on AIX
522 : // See: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=79839
523 : void* data = __linux_calloc(length, 1);
524 : #else
525 232799 : void* data = calloc(length, 1);
526 : #endif
527 232799 : return data;
528 : }
529 :
530 13922 : void* AllocateUninitialized(size_t length) override {
531 : #if V8_OS_AIX && _LINUX_SOURCE_COMPAT
532 : // Work around for GCC bug on AIX
533 : // See: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=79839
534 : void* data = __linux_malloc(length);
535 : #else
536 13922 : void* data = malloc(length);
537 : #endif
538 13922 : return data;
539 : }
540 :
541 246687 : void Free(void* data, size_t) override { free(data); }
542 : };
543 :
544 558 : struct SnapshotCreatorData {
545 : explicit SnapshotCreatorData(Isolate* isolate)
546 : : isolate_(isolate),
547 : default_context_(),
548 : contexts_(isolate),
549 372 : created_(false) {}
550 :
551 : static SnapshotCreatorData* cast(void* data) {
552 : return reinterpret_cast<SnapshotCreatorData*>(data);
553 : }
554 :
555 : ArrayBufferAllocator allocator_;
556 : Isolate* isolate_;
557 : Persistent<Context> default_context_;
558 : SerializeInternalFieldsCallback default_embedder_fields_serializer_;
559 : PersistentValueVector<Context> contexts_;
560 : std::vector<SerializeInternalFieldsCallback> embedder_fields_serializers_;
561 : bool created_;
562 : };
563 :
564 : } // namespace
565 :
566 186 : SnapshotCreator::SnapshotCreator(Isolate* isolate,
567 : const intptr_t* external_references,
568 : StartupData* existing_snapshot) {
569 186 : SnapshotCreatorData* data = new SnapshotCreatorData(isolate);
570 : data->isolate_ = isolate;
571 : i::Isolate* internal_isolate = reinterpret_cast<i::Isolate*>(isolate);
572 186 : internal_isolate->set_array_buffer_allocator(&data->allocator_);
573 : internal_isolate->set_api_external_references(external_references);
574 : internal_isolate->enable_serializer();
575 : isolate->Enter();
576 : const StartupData* blob = existing_snapshot
577 : ? existing_snapshot
578 186 : : i::Snapshot::DefaultSnapshotBlob();
579 186 : if (blob && blob->raw_size > 0) {
580 : internal_isolate->set_snapshot_blob(blob);
581 185 : i::Snapshot::Initialize(internal_isolate);
582 : } else {
583 1 : internal_isolate->Init(nullptr);
584 : }
585 186 : data_ = data;
586 186 : }
587 :
588 185 : SnapshotCreator::SnapshotCreator(const intptr_t* external_references,
589 : StartupData* existing_snapshot)
590 : : SnapshotCreator(Isolate::Allocate(), external_references,
591 185 : existing_snapshot) {}
592 :
593 186 : SnapshotCreator::~SnapshotCreator() {
594 186 : SnapshotCreatorData* data = SnapshotCreatorData::cast(data_);
595 : DCHECK(data->created_);
596 186 : Isolate* isolate = data->isolate_;
597 : isolate->Exit();
598 186 : isolate->Dispose();
599 186 : delete data;
600 186 : }
601 :
602 186 : Isolate* SnapshotCreator::GetIsolate() {
603 186 : return SnapshotCreatorData::cast(data_)->isolate_;
604 : }
605 :
606 186 : void SnapshotCreator::SetDefaultContext(
607 : Local<Context> context, SerializeInternalFieldsCallback callback) {
608 : DCHECK(!context.IsEmpty());
609 186 : SnapshotCreatorData* data = SnapshotCreatorData::cast(data_);
610 : DCHECK(!data->created_);
611 : DCHECK(data->default_context_.IsEmpty());
612 186 : Isolate* isolate = data->isolate_;
613 186 : CHECK_EQ(isolate, context->GetIsolate());
614 : data->default_context_.Reset(isolate, context);
615 186 : data->default_embedder_fields_serializer_ = callback;
616 186 : }
617 :
618 35 : size_t SnapshotCreator::AddContext(Local<Context> context,
619 : SerializeInternalFieldsCallback callback) {
620 : DCHECK(!context.IsEmpty());
621 35 : SnapshotCreatorData* data = SnapshotCreatorData::cast(data_);
622 : DCHECK(!data->created_);
623 35 : Isolate* isolate = data->isolate_;
624 35 : CHECK_EQ(isolate, context->GetIsolate());
625 : size_t index = data->contexts_.Size();
626 35 : data->contexts_.Append(context);
627 35 : data->embedder_fields_serializers_.push_back(callback);
628 35 : return index;
629 : }
630 :
631 10 : size_t SnapshotCreator::AddTemplate(Local<Template> template_obj) {
632 10 : return AddData(template_obj);
633 : }
634 :
635 50 : size_t SnapshotCreator::AddData(i::Address object) {
636 : DCHECK_NE(object, i::kNullAddress);
637 50 : SnapshotCreatorData* data = SnapshotCreatorData::cast(data_);
638 : DCHECK(!data->created_);
639 50 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(data->isolate_);
640 : i::HandleScope scope(isolate);
641 : i::Handle<i::Object> obj(i::Object(object), isolate);
642 : i::Handle<i::ArrayList> list;
643 100 : if (!isolate->heap()->serialized_objects()->IsArrayList()) {
644 5 : list = i::ArrayList::New(isolate, 1);
645 : } else {
646 : list = i::Handle<i::ArrayList>(
647 90 : i::ArrayList::cast(isolate->heap()->serialized_objects()), isolate);
648 : }
649 50 : size_t index = static_cast<size_t>(list->Length());
650 50 : list = i::ArrayList::Add(isolate, list, obj);
651 50 : isolate->heap()->SetSerializedObjects(*list);
652 50 : return index;
653 : }
654 :
655 35 : size_t SnapshotCreator::AddData(Local<Context> context, i::Address object) {
656 : DCHECK_NE(object, i::kNullAddress);
657 : DCHECK(!SnapshotCreatorData::cast(data_)->created_);
658 : i::Handle<i::Context> ctx = Utils::OpenHandle(*context);
659 : i::Isolate* isolate = ctx->GetIsolate();
660 : i::HandleScope scope(isolate);
661 : i::Handle<i::Object> obj(i::Object(object), isolate);
662 : i::Handle<i::ArrayList> list;
663 70 : if (!ctx->serialized_objects()->IsArrayList()) {
664 5 : list = i::ArrayList::New(isolate, 1);
665 : } else {
666 : list = i::Handle<i::ArrayList>(
667 60 : i::ArrayList::cast(ctx->serialized_objects()), isolate);
668 : }
669 35 : size_t index = static_cast<size_t>(list->Length());
670 35 : list = i::ArrayList::Add(isolate, list, obj);
671 70 : ctx->set_serialized_objects(*list);
672 35 : return index;
673 : }
674 :
675 : namespace {
676 221 : void ConvertSerializedObjectsToFixedArray(Local<Context> context) {
677 : i::Handle<i::Context> ctx = Utils::OpenHandle(*context);
678 : i::Isolate* isolate = ctx->GetIsolate();
679 442 : if (!ctx->serialized_objects()->IsArrayList()) {
680 5 : ctx->set_serialized_objects(i::ReadOnlyRoots(isolate).empty_fixed_array());
681 : } else {
682 432 : i::Handle<i::ArrayList> list(i::ArrayList::cast(ctx->serialized_objects()),
683 432 : isolate);
684 216 : i::Handle<i::FixedArray> elements = i::ArrayList::Elements(isolate, list);
685 216 : ctx->set_serialized_objects(*elements);
686 : }
687 221 : }
688 :
689 186 : void ConvertSerializedObjectsToFixedArray(i::Isolate* isolate) {
690 372 : if (!isolate->heap()->serialized_objects()->IsArrayList()) {
691 : isolate->heap()->SetSerializedObjects(
692 0 : i::ReadOnlyRoots(isolate).empty_fixed_array());
693 : } else {
694 : i::Handle<i::ArrayList> list(
695 372 : i::ArrayList::cast(isolate->heap()->serialized_objects()), isolate);
696 186 : i::Handle<i::FixedArray> elements = i::ArrayList::Elements(isolate, list);
697 186 : isolate->heap()->SetSerializedObjects(*elements);
698 : }
699 186 : }
700 : } // anonymous namespace
701 :
702 186 : StartupData SnapshotCreator::CreateBlob(
703 : SnapshotCreator::FunctionCodeHandling function_code_handling) {
704 186 : SnapshotCreatorData* data = SnapshotCreatorData::cast(data_);
705 186 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(data->isolate_);
706 : DCHECK(!data->created_);
707 : DCHECK(!data->default_context_.IsEmpty());
708 :
709 186 : int num_additional_contexts = static_cast<int>(data->contexts_.Size());
710 :
711 : {
712 186 : i::HandleScope scope(isolate);
713 : // Convert list of context-independent data to FixedArray.
714 186 : ConvertSerializedObjectsToFixedArray(isolate);
715 :
716 : // Convert lists of context-dependent data to FixedArray.
717 : ConvertSerializedObjectsToFixedArray(
718 372 : data->default_context_.Get(data->isolate_));
719 221 : for (int i = 0; i < num_additional_contexts; i++) {
720 35 : ConvertSerializedObjectsToFixedArray(data->contexts_.Get(i));
721 : }
722 :
723 : // We need to store the global proxy size upfront in case we need the
724 : // bootstrapper to create a global proxy before we deserialize the context.
725 : i::Handle<i::FixedArray> global_proxy_sizes =
726 186 : isolate->factory()->NewFixedArray(num_additional_contexts, i::TENURED);
727 221 : for (int i = 0; i < num_additional_contexts; i++) {
728 : i::Handle<i::Context> context =
729 70 : v8::Utils::OpenHandle(*data->contexts_.Get(i));
730 : global_proxy_sizes->set(i,
731 70 : i::Smi::FromInt(context->global_proxy()->Size()));
732 : }
733 186 : isolate->heap()->SetSerializedGlobalProxySizes(*global_proxy_sizes);
734 : }
735 :
736 : // We might rehash strings and re-sort descriptors. Clear the lookup cache.
737 372 : isolate->descriptor_lookup_cache()->Clear();
738 :
739 : // If we don't do this then we end up with a stray root pointing at the
740 : // context even after we have disposed of the context.
741 : isolate->heap()->CollectAllAvailableGarbage(
742 186 : i::GarbageCollectionReason::kSnapshotCreator);
743 : {
744 186 : i::HandleScope scope(isolate);
745 186 : isolate->heap()->CompactWeakArrayLists(internal::TENURED);
746 : }
747 :
748 186 : isolate->heap()->read_only_space()->ClearStringPaddingIfNeeded();
749 :
750 186 : if (function_code_handling == FunctionCodeHandling::kClear) {
751 : // Clear out re-compilable data from all shared function infos. Any
752 : // JSFunctions using these SFIs will have their code pointers reset by the
753 : // partial serializer.
754 : //
755 : // We have to iterate the heap and collect handles to each clearable SFI,
756 : // before we disable allocation, since we have to allocate UncompiledDatas
757 : // to be able to recompile them.
758 : //
759 : // Compiled irregexp code is also flushed by collecting and clearing any
760 : // seen JSRegExp objects.
761 161 : i::HandleScope scope(isolate);
762 : std::vector<i::Handle<i::SharedFunctionInfo>> sfis_to_clear;
763 :
764 : { // Heap allocation is disallowed within this scope.
765 161 : i::HeapIterator heap_iterator(isolate->heap());
766 2742404 : for (i::HeapObject current_obj = heap_iterator.next();
767 : !current_obj.is_null(); current_obj = heap_iterator.next()) {
768 1371041 : if (current_obj->IsSharedFunctionInfo()) {
769 : i::SharedFunctionInfo shared =
770 148579 : i::SharedFunctionInfo::cast(current_obj);
771 148579 : if (shared->CanDiscardCompiled()) {
772 37 : sfis_to_clear.emplace_back(shared, isolate);
773 : }
774 1222462 : } else if (current_obj->IsJSRegExp()) {
775 10 : i::JSRegExp regexp = i::JSRegExp::cast(current_obj);
776 10 : if (regexp->HasCompiledCode()) {
777 5 : regexp->DiscardCompiledCodeForSerialization();
778 : }
779 : }
780 161 : }
781 : }
782 :
783 : // Must happen after heap iteration since SFI::DiscardCompiled may allocate.
784 359 : for (i::Handle<i::SharedFunctionInfo> shared : sfis_to_clear) {
785 37 : i::SharedFunctionInfo::DiscardCompiled(isolate, shared);
786 : }
787 : }
788 :
789 : i::DisallowHeapAllocation no_gc_from_here_on;
790 :
791 186 : int num_contexts = num_additional_contexts + 1;
792 : std::vector<i::Context> contexts;
793 186 : contexts.reserve(num_contexts);
794 : {
795 186 : i::HandleScope scope(isolate);
796 : contexts.push_back(
797 558 : *v8::Utils::OpenHandle(*data->default_context_.Get(data->isolate_)));
798 : data->default_context_.Reset();
799 35 : for (int i = 0; i < num_additional_contexts; i++) {
800 : i::Handle<i::Context> context =
801 70 : v8::Utils::OpenHandle(*data->contexts_.Get(i));
802 70 : contexts.push_back(*context);
803 : }
804 186 : data->contexts_.Clear();
805 : }
806 :
807 : // Check that values referenced by global/eternal handles are accounted for.
808 372 : i::SerializedHandleChecker handle_checker(isolate, &contexts);
809 186 : CHECK(handle_checker.CheckGlobalAndEternalHandles());
810 :
811 372 : i::HeapIterator heap_iterator(isolate->heap());
812 3059304 : for (i::HeapObject current_obj = heap_iterator.next(); !current_obj.is_null();
813 : current_obj = heap_iterator.next()) {
814 1529466 : if (current_obj->IsJSFunction()) {
815 180836 : i::JSFunction fun = i::JSFunction::cast(current_obj);
816 :
817 : // Complete in-object slack tracking for all functions.
818 180836 : fun->CompleteInobjectSlackTrackingIfActive();
819 :
820 : // Also, clear out feedback vectors, or any optimized code.
821 361672 : if (!fun->raw_feedback_cell()->value()->IsUndefined()) {
822 : fun->raw_feedback_cell()->set_value(
823 110 : i::ReadOnlyRoots(isolate).undefined_value());
824 55 : fun->set_code(isolate->builtins()->builtin(i::Builtins::kCompileLazy));
825 : }
826 : if (function_code_handling == FunctionCodeHandling::kClear) {
827 : DCHECK(fun->shared()->HasWasmExportedFunctionData() ||
828 : fun->shared()->HasBuiltinId() ||
829 : fun->shared()->IsApiFunction() ||
830 : fun->shared()->HasUncompiledDataWithoutPreparseData());
831 : }
832 : }
833 : }
834 :
835 372 : i::ReadOnlySerializer read_only_serializer(isolate);
836 186 : read_only_serializer.SerializeReadOnlyRoots();
837 :
838 372 : i::StartupSerializer startup_serializer(isolate, &read_only_serializer);
839 186 : startup_serializer.SerializeStrongReferences();
840 :
841 : // Serialize each context with a new partial serializer.
842 : std::vector<i::SnapshotData*> context_snapshots;
843 186 : context_snapshots.reserve(num_contexts);
844 :
845 : // TODO(6593): generalize rehashing, and remove this flag.
846 : bool can_be_rehashed = true;
847 :
848 407 : for (int i = 0; i < num_contexts; i++) {
849 221 : bool is_default_context = i == 0;
850 : i::PartialSerializer partial_serializer(
851 : isolate, &startup_serializer,
852 : is_default_context ? data->default_embedder_fields_serializer_
853 442 : : data->embedder_fields_serializers_[i - 1]);
854 442 : partial_serializer.Serialize(&contexts[i], !is_default_context);
855 221 : can_be_rehashed = can_be_rehashed && partial_serializer.can_be_rehashed();
856 442 : context_snapshots.push_back(new i::SnapshotData(&partial_serializer));
857 221 : }
858 :
859 186 : startup_serializer.SerializeWeakReferencesAndDeferred();
860 186 : can_be_rehashed = can_be_rehashed && startup_serializer.can_be_rehashed();
861 :
862 186 : read_only_serializer.FinalizeSerialization();
863 186 : can_be_rehashed = can_be_rehashed && read_only_serializer.can_be_rehashed();
864 :
865 186 : i::SnapshotData read_only_snapshot(&read_only_serializer);
866 186 : i::SnapshotData startup_snapshot(&startup_serializer);
867 : StartupData result =
868 : i::Snapshot::CreateSnapshotBlob(&startup_snapshot, &read_only_snapshot,
869 186 : context_snapshots, can_be_rehashed);
870 :
871 : // Delete heap-allocated context snapshot instances.
872 593 : for (const auto context_snapshot : context_snapshots) {
873 221 : delete context_snapshot;
874 : }
875 186 : data->created_ = true;
876 :
877 : DCHECK(i::Snapshot::VerifyChecksum(&result));
878 372 : return result;
879 : }
880 :
881 5 : void V8::SetDcheckErrorHandler(DcheckErrorCallback that) {
882 5 : v8::base::SetDcheckFunction(that);
883 5 : }
884 :
885 86318 : void V8::SetFlagsFromString(const char* str, int length) {
886 107375 : i::FlagList::SetFlagsFromString(str, length);
887 107375 : i::FlagList::EnforceFlagImplications();
888 86318 : }
889 :
890 :
891 32950 : void V8::SetFlagsFromCommandLine(int* argc, char** argv, bool remove_flags) {
892 32950 : i::FlagList::SetFlagsFromCommandLine(argc, argv, remove_flags);
893 32950 : }
894 :
895 : RegisteredExtension* RegisteredExtension::first_extension_ = nullptr;
896 :
897 0 : RegisteredExtension::RegisteredExtension(Extension* extension)
898 452889 : : extension_(extension) { }
899 :
900 :
901 0 : void RegisteredExtension::Register(RegisteredExtension* that) {
902 452889 : that->next_ = first_extension_;
903 452889 : first_extension_ = that;
904 0 : }
905 :
906 :
907 31878 : void RegisteredExtension::UnregisterAll() {
908 222936 : RegisteredExtension* re = first_extension_;
909 254814 : while (re != nullptr) {
910 : RegisteredExtension* next = re->next();
911 191058 : delete re;
912 : re = next;
913 : }
914 31878 : first_extension_ = nullptr;
915 31878 : }
916 :
917 : namespace {
918 551988 : class ExtensionResource : public String::ExternalOneByteStringResource {
919 : public:
920 : ExtensionResource() : data_(nullptr), length_(0) {}
921 : ExtensionResource(const char* data, size_t length)
922 452889 : : data_(data), length_(length) {}
923 12744 : const char* data() const override { return data_; }
924 9064 : size_t length() const override { return length_; }
925 4536 : void Dispose() override {}
926 :
927 : private:
928 : const char* data_;
929 : size_t length_;
930 : };
931 : } // anonymous namespace
932 :
933 452889 : void RegisterExtension(Extension* that) {
934 452889 : RegisteredExtension* extension = new RegisteredExtension(that);
935 : RegisteredExtension::Register(extension);
936 452889 : }
937 :
938 :
939 452889 : Extension::Extension(const char* name,
940 : const char* source,
941 : int dep_count,
942 : const char** deps,
943 : int source_length)
944 : : name_(name),
945 : source_length_(source_length >= 0 ?
946 : source_length :
947 452869 : (source ? static_cast<int>(strlen(source)) : 0)),
948 : dep_count_(dep_count),
949 : deps_(deps),
950 905758 : auto_enable_(false) {
951 905778 : source_ = new ExtensionResource(source, source_length_);
952 452889 : CHECK(source != nullptr || source_length_ == 0);
953 452889 : }
954 :
955 62261 : ResourceConstraints::ResourceConstraints()
956 : : max_semi_space_size_in_kb_(0),
957 : max_old_space_size_(0),
958 : stack_limit_(nullptr),
959 : code_range_size_(0),
960 62261 : max_zone_pool_size_(0) {}
961 :
962 28773 : void ResourceConstraints::ConfigureDefaults(uint64_t physical_memory,
963 : uint64_t virtual_memory_limit) {
964 : set_max_semi_space_size_in_kb(
965 : i::Heap::ComputeMaxSemiSpaceSize(physical_memory));
966 28773 : set_max_old_space_size(i::Heap::ComputeMaxOldGenerationSize(physical_memory));
967 : set_max_zone_pool_size(i::AccountingAllocator::kMaxPoolSize);
968 :
969 28773 : if (virtual_memory_limit > 0 && i::kRequiresCodeRange) {
970 : // Reserve no more than 1/8 of the memory for the code range, but at most
971 : // kMaximalCodeRangeSize.
972 : set_code_range_size(
973 : i::Min(i::kMaximalCodeRangeSize / i::MB,
974 0 : static_cast<size_t>((virtual_memory_limit >> 3) / i::MB)));
975 : }
976 28773 : }
977 :
978 125174 : void SetResourceConstraints(i::Isolate* isolate,
979 125174 : const ResourceConstraints& constraints) {
980 : size_t semi_space_size = constraints.max_semi_space_size_in_kb();
981 : size_t old_space_size = constraints.max_old_space_size();
982 : size_t code_range_size = constraints.code_range_size();
983 : size_t max_pool_size = constraints.max_zone_pool_size();
984 62587 : if (semi_space_size != 0 || old_space_size != 0 || code_range_size != 0) {
985 : isolate->heap()->ConfigureHeap(semi_space_size, old_space_size,
986 28789 : code_range_size);
987 : }
988 62587 : isolate->allocator()->ConfigureSegmentPool(max_pool_size);
989 :
990 62587 : if (constraints.stack_limit() != nullptr) {
991 0 : uintptr_t limit = reinterpret_cast<uintptr_t>(constraints.stack_limit());
992 0 : isolate->stack_guard()->SetStackLimit(limit);
993 : }
994 62587 : }
995 :
996 9097491 : i::Address* V8::GlobalizeReference(i::Isolate* isolate, i::Address* obj) {
997 6064994 : LOG_API(isolate, Persistent, New);
998 6064994 : i::Handle<i::Object> result = isolate->global_handles()->Create(*obj);
999 : #ifdef VERIFY_HEAP
1000 : if (i::FLAG_verify_heap) {
1001 : i::Object(*obj)->ObjectVerify(isolate);
1002 : }
1003 : #endif // VERIFY_HEAP
1004 3032497 : return result.location();
1005 : }
1006 :
1007 10 : i::Address* V8::CopyPersistent(i::Address* obj) {
1008 10 : i::Handle<i::Object> result = i::GlobalHandles::CopyGlobal(obj);
1009 10 : return result.location();
1010 : }
1011 :
1012 5 : void V8::RegisterExternallyReferencedObject(i::Address* location,
1013 : i::Isolate* isolate) {
1014 5 : isolate->heap()->RegisterExternallyReferencedObject(location);
1015 5 : }
1016 :
1017 109728 : void V8::MakeWeak(i::Address* location, void* parameter,
1018 : WeakCallbackInfo<void>::Callback weak_callback,
1019 : WeakCallbackType type) {
1020 109728 : i::GlobalHandles::MakeWeak(location, parameter, weak_callback, type);
1021 109728 : }
1022 :
1023 26 : void V8::MakeWeak(i::Address** location_addr) {
1024 26 : i::GlobalHandles::MakeWeak(location_addr);
1025 26 : }
1026 :
1027 35 : void* V8::ClearWeak(i::Address* location) {
1028 35 : return i::GlobalHandles::ClearWeakness(location);
1029 : }
1030 :
1031 2799836 : void V8::AnnotateStrongRetainer(i::Address* location, const char* label) {
1032 2799836 : i::GlobalHandles::AnnotateStrongRetainer(location, label);
1033 2799836 : }
1034 :
1035 3030035 : void V8::DisposeGlobal(i::Address* location) {
1036 3030035 : i::GlobalHandles::Destroy(location);
1037 3030035 : }
1038 :
1039 10245 : Value* V8::Eternalize(Isolate* v8_isolate, Value* value) {
1040 20490 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(v8_isolate);
1041 10245 : i::Object object = *Utils::OpenHandle(value);
1042 10245 : int index = -1;
1043 10245 : isolate->eternal_handles()->Create(isolate, object, &index);
1044 : return reinterpret_cast<Value*>(
1045 20490 : isolate->eternal_handles()->Get(index).location());
1046 : }
1047 :
1048 :
1049 0 : void V8::FromJustIsNothing() {
1050 : Utils::ApiCheck(false, "v8::FromJust", "Maybe value is Nothing.");
1051 0 : }
1052 :
1053 :
1054 0 : void V8::ToLocalEmpty() {
1055 : Utils::ApiCheck(false, "v8::ToLocalChecked", "Empty MaybeLocal.");
1056 0 : }
1057 :
1058 0 : void V8::InternalFieldOutOfBounds(int index) {
1059 : Utils::ApiCheck(0 <= index && index < kInternalFieldsInWeakCallback,
1060 : "WeakCallbackInfo::GetInternalField",
1061 0 : "Internal field out of bounds.");
1062 0 : }
1063 :
1064 :
1065 : // --- H a n d l e s ---
1066 :
1067 :
1068 439180125 : HandleScope::HandleScope(Isolate* isolate) {
1069 439180246 : Initialize(isolate);
1070 439180130 : }
1071 :
1072 :
1073 446827132 : void HandleScope::Initialize(Isolate* isolate) {
1074 59306 : i::Isolate* internal_isolate = reinterpret_cast<i::Isolate*>(isolate);
1075 : // We do not want to check the correct usage of the Locker class all over the
1076 : // place, so we do it only here: Without a HandleScope, an embedder can do
1077 : // almost nothing, so it is enough to check in this central place.
1078 : // We make an exception if the serializer is enabled, which means that the
1079 : // Isolate is exclusively used to create a snapshot.
1080 : Utils::ApiCheck(
1081 446886423 : !v8::Locker::IsActive() ||
1082 446886436 : internal_isolate->thread_manager()->IsLockedByCurrentThread() ||
1083 : internal_isolate->serializer_enabled(),
1084 : "HandleScope::HandleScope",
1085 : "Entering the V8 API without proper locking in place");
1086 : i::HandleScopeData* current = internal_isolate->handle_scope_data();
1087 446827130 : isolate_ = internal_isolate;
1088 446827130 : prev_next_ = current->next;
1089 446827130 : prev_limit_ = current->limit;
1090 446827130 : current->level++;
1091 446827130 : }
1092 :
1093 :
1094 446827008 : HandleScope::~HandleScope() {
1095 446827129 : i::HandleScope::CloseScope(isolate_, prev_next_, prev_limit_);
1096 446827002 : }
1097 :
1098 0 : void* HandleScope::operator new(size_t) { base::OS::Abort(); }
1099 0 : void* HandleScope::operator new[](size_t) { base::OS::Abort(); }
1100 0 : void HandleScope::operator delete(void*, size_t) { base::OS::Abort(); }
1101 0 : void HandleScope::operator delete[](void*, size_t) { base::OS::Abort(); }
1102 :
1103 6036 : int HandleScope::NumberOfHandles(Isolate* isolate) {
1104 : return i::HandleScope::NumberOfHandles(
1105 6036 : reinterpret_cast<i::Isolate*>(isolate));
1106 : }
1107 :
1108 12865306 : i::Address* HandleScope::CreateHandle(i::Isolate* isolate, i::Address value) {
1109 12865309 : return i::HandleScope::CreateHandle(isolate, value);
1110 : }
1111 :
1112 7646923 : EscapableHandleScope::EscapableHandleScope(Isolate* v8_isolate) {
1113 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(v8_isolate);
1114 : escape_slot_ =
1115 7646918 : CreateHandle(isolate, i::ReadOnlyRoots(isolate).the_hole_value()->ptr());
1116 7646919 : Initialize(v8_isolate);
1117 7646919 : }
1118 :
1119 6776442 : i::Address* EscapableHandleScope::Escape(i::Address* escape_value) {
1120 6776442 : i::Heap* heap = reinterpret_cast<i::Isolate*>(GetIsolate())->heap();
1121 13552887 : Utils::ApiCheck(i::Object(*escape_slot_)->IsTheHole(heap->isolate()),
1122 : "EscapableHandleScope::Escape", "Escape value set twice");
1123 6776445 : if (escape_value == nullptr) {
1124 10 : *escape_slot_ = i::ReadOnlyRoots(heap).undefined_value()->ptr();
1125 5 : return nullptr;
1126 : }
1127 6776440 : *escape_slot_ = *escape_value;
1128 6776440 : return escape_slot_;
1129 : }
1130 :
1131 0 : void* EscapableHandleScope::operator new(size_t) { base::OS::Abort(); }
1132 0 : void* EscapableHandleScope::operator new[](size_t) { base::OS::Abort(); }
1133 0 : void EscapableHandleScope::operator delete(void*, size_t) { base::OS::Abort(); }
1134 0 : void EscapableHandleScope::operator delete[](void*, size_t) {
1135 0 : base::OS::Abort();
1136 : }
1137 :
1138 158180 : SealHandleScope::SealHandleScope(Isolate* isolate)
1139 158180 : : isolate_(reinterpret_cast<i::Isolate*>(isolate)) {
1140 : i::HandleScopeData* current = isolate_->handle_scope_data();
1141 158180 : prev_limit_ = current->limit;
1142 158180 : current->limit = current->next;
1143 158180 : prev_sealed_level_ = current->sealed_level;
1144 158180 : current->sealed_level = current->level;
1145 158180 : }
1146 :
1147 :
1148 158180 : SealHandleScope::~SealHandleScope() {
1149 158180 : i::HandleScopeData* current = isolate_->handle_scope_data();
1150 : DCHECK_EQ(current->next, current->limit);
1151 158180 : current->limit = prev_limit_;
1152 : DCHECK_EQ(current->level, current->sealed_level);
1153 158180 : current->sealed_level = prev_sealed_level_;
1154 158180 : }
1155 :
1156 0 : void* SealHandleScope::operator new(size_t) { base::OS::Abort(); }
1157 0 : void* SealHandleScope::operator new[](size_t) { base::OS::Abort(); }
1158 0 : void SealHandleScope::operator delete(void*, size_t) { base::OS::Abort(); }
1159 0 : void SealHandleScope::operator delete[](void*, size_t) { base::OS::Abort(); }
1160 :
1161 4371421 : void Context::Enter() {
1162 : i::Handle<i::Context> env = Utils::OpenHandle(this);
1163 4371418 : i::Isolate* isolate = env->GetIsolate();
1164 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(isolate);
1165 : i::HandleScopeImplementer* impl = isolate->handle_scope_implementer();
1166 4371417 : impl->EnterContext(*env);
1167 : impl->SaveContext(isolate->context());
1168 : isolate->set_context(*env);
1169 4371408 : }
1170 :
1171 4352279 : void Context::Exit() {
1172 : i::Handle<i::Context> env = Utils::OpenHandle(this);
1173 4352283 : i::Isolate* isolate = env->GetIsolate();
1174 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(isolate);
1175 : i::HandleScopeImplementer* impl = isolate->handle_scope_implementer();
1176 4352286 : if (!Utils::ApiCheck(impl->LastEnteredContextWas(*env), "v8::Context::Exit()",
1177 : "Cannot exit non-entered context")) {
1178 4352286 : return;
1179 : }
1180 : impl->LeaveContext();
1181 : isolate->set_context(impl->RestoreContext());
1182 : }
1183 :
1184 2 : Context::BackupIncumbentScope::BackupIncumbentScope(
1185 : Local<Context> backup_incumbent_context)
1186 2 : : backup_incumbent_context_(backup_incumbent_context) {
1187 : DCHECK(!backup_incumbent_context_.IsEmpty());
1188 :
1189 : i::Handle<i::Context> env = Utils::OpenHandle(*backup_incumbent_context_);
1190 2 : i::Isolate* isolate = env->GetIsolate();
1191 :
1192 : js_stack_comparable_address_ =
1193 2 : i::SimulatorStack::RegisterJSStackComparableAddress(isolate);
1194 :
1195 2 : prev_ = isolate->top_backup_incumbent_scope();
1196 : isolate->set_top_backup_incumbent_scope(this);
1197 2 : }
1198 :
1199 2 : Context::BackupIncumbentScope::~BackupIncumbentScope() {
1200 : i::Handle<i::Context> env = Utils::OpenHandle(*backup_incumbent_context_);
1201 : i::Isolate* isolate = env->GetIsolate();
1202 :
1203 : i::SimulatorStack::UnregisterJSStackComparableAddress(isolate);
1204 :
1205 2 : isolate->set_top_backup_incumbent_scope(prev_);
1206 2 : }
1207 :
1208 : STATIC_ASSERT(i::Internals::kEmbedderDataSlotSize == i::kEmbedderDataSlotSize);
1209 :
1210 110389 : static i::Handle<i::EmbedderDataArray> EmbedderDataFor(Context* context,
1211 : int index, bool can_grow,
1212 : const char* location) {
1213 : i::Handle<i::Context> env = Utils::OpenHandle(context);
1214 : i::Isolate* isolate = env->GetIsolate();
1215 : bool ok =
1216 220778 : Utils::ApiCheck(env->IsNativeContext(),
1217 : location,
1218 220778 : "Not a native context") &&
1219 110389 : Utils::ApiCheck(index >= 0, location, "Negative index");
1220 110389 : if (!ok) return i::Handle<i::EmbedderDataArray>();
1221 : // TODO(ishell): remove cast once embedder_data slot has a proper type.
1222 : i::Handle<i::EmbedderDataArray> data(
1223 220778 : i::EmbedderDataArray::cast(env->embedder_data()), isolate);
1224 220778 : if (index < data->length()) return data;
1225 118148 : if (!Utils::ApiCheck(can_grow && index < i::EmbedderDataArray::kMaxLength,
1226 : location, "Index too large")) {
1227 0 : return i::Handle<i::EmbedderDataArray>();
1228 : }
1229 59074 : data = i::EmbedderDataArray::EnsureCapacity(isolate, data, index);
1230 118148 : env->set_embedder_data(*data);
1231 59074 : return data;
1232 : }
1233 :
1234 75 : uint32_t Context::GetNumberOfEmbedderDataFields() {
1235 : i::Handle<i::Context> context = Utils::OpenHandle(this);
1236 150 : CHECK(context->IsNativeContext());
1237 : // TODO(ishell): remove cast once embedder_data slot has a proper type.
1238 : return static_cast<uint32_t>(
1239 225 : i::EmbedderDataArray::cast(context->embedder_data())->length());
1240 : }
1241 :
1242 0 : v8::Local<v8::Value> Context::SlowGetEmbedderData(int index) {
1243 : const char* location = "v8::Context::GetEmbedderData()";
1244 : i::Handle<i::EmbedderDataArray> data =
1245 0 : EmbedderDataFor(this, index, false, location);
1246 0 : if (data.is_null()) return Local<Value>();
1247 : i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
1248 : i::Handle<i::Object> result(i::EmbedderDataSlot(*data, index).load_tagged(),
1249 0 : isolate);
1250 : return Utils::ToLocal(result);
1251 : }
1252 :
1253 :
1254 60 : void Context::SetEmbedderData(int index, v8::Local<Value> value) {
1255 : const char* location = "v8::Context::SetEmbedderData()";
1256 : i::Handle<i::EmbedderDataArray> data =
1257 60 : EmbedderDataFor(this, index, true, location);
1258 120 : if (data.is_null()) return;
1259 : i::Handle<i::Object> val = Utils::OpenHandle(*value);
1260 : i::EmbedderDataSlot::store_tagged(*data, index, *val);
1261 : DCHECK_EQ(*Utils::OpenHandle(*value),
1262 : *Utils::OpenHandle(*GetEmbedderData(index)));
1263 : }
1264 :
1265 :
1266 0 : void* Context::SlowGetAlignedPointerFromEmbedderData(int index) {
1267 : const char* location = "v8::Context::GetAlignedPointerFromEmbedderData()";
1268 : i::Handle<i::EmbedderDataArray> data =
1269 0 : EmbedderDataFor(this, index, false, location);
1270 0 : if (data.is_null()) return nullptr;
1271 : void* result;
1272 0 : Utils::ApiCheck(i::EmbedderDataSlot(*data, index).ToAlignedPointer(&result),
1273 : location, "Pointer is not aligned");
1274 0 : return result;
1275 : }
1276 :
1277 :
1278 110329 : void Context::SetAlignedPointerInEmbedderData(int index, void* value) {
1279 : const char* location = "v8::Context::SetAlignedPointerInEmbedderData()";
1280 : i::Handle<i::EmbedderDataArray> data =
1281 110329 : EmbedderDataFor(this, index, true, location);
1282 110329 : bool ok = i::EmbedderDataSlot(*data, index).store_aligned_pointer(value);
1283 : Utils::ApiCheck(ok, location, "Pointer is not aligned");
1284 : DCHECK_EQ(value, GetAlignedPointerFromEmbedderData(index));
1285 110329 : }
1286 :
1287 :
1288 : // --- T e m p l a t e ---
1289 :
1290 :
1291 4609056 : static void InitializeTemplate(i::Handle<i::TemplateInfo> that, int type) {
1292 : that->set_number_of_properties(0);
1293 9218118 : that->set_tag(i::Smi::FromInt(type));
1294 4609061 : }
1295 :
1296 :
1297 1956489 : void Template::Set(v8::Local<Name> name, v8::Local<Data> value,
1298 : v8::PropertyAttribute attribute) {
1299 : auto templ = Utils::OpenHandle(this);
1300 : i::Isolate* isolate = templ->GetIsolate();
1301 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(isolate);
1302 : i::HandleScope scope(isolate);
1303 : auto value_obj = Utils::OpenHandle(*value);
1304 3912978 : CHECK(!value_obj->IsJSReceiver() || value_obj->IsTemplateInfo());
1305 3912978 : if (value_obj->IsObjectTemplateInfo()) {
1306 207519 : templ->set_serial_number(i::Smi::kZero);
1307 415038 : if (templ->IsFunctionTemplateInfo()) {
1308 0 : i::Handle<i::FunctionTemplateInfo>::cast(templ)->set_do_not_cache(true);
1309 : }
1310 : }
1311 : i::ApiNatives::AddDataProperty(isolate, templ, Utils::OpenHandle(*name),
1312 : value_obj,
1313 1956489 : static_cast<i::PropertyAttributes>(attribute));
1314 1956489 : }
1315 :
1316 0 : void Template::SetPrivate(v8::Local<Private> name, v8::Local<Data> value,
1317 : v8::PropertyAttribute attribute) {
1318 : Set(Utils::ToLocal(Utils::OpenHandle(reinterpret_cast<Name*>(*name))), value,
1319 0 : attribute);
1320 0 : }
1321 :
1322 109 : void Template::SetAccessorProperty(
1323 : v8::Local<v8::Name> name,
1324 : v8::Local<FunctionTemplate> getter,
1325 : v8::Local<FunctionTemplate> setter,
1326 : v8::PropertyAttribute attribute,
1327 : v8::AccessControl access_control) {
1328 : // TODO(verwaest): Remove |access_control|.
1329 : DCHECK_EQ(v8::DEFAULT, access_control);
1330 : auto templ = Utils::OpenHandle(this);
1331 : auto isolate = templ->GetIsolate();
1332 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(isolate);
1333 : DCHECK(!name.IsEmpty());
1334 : DCHECK(!getter.IsEmpty() || !setter.IsEmpty());
1335 : i::HandleScope scope(isolate);
1336 : i::ApiNatives::AddAccessorProperty(
1337 : isolate, templ, Utils::OpenHandle(*name),
1338 : Utils::OpenHandle(*getter, true), Utils::OpenHandle(*setter, true),
1339 109 : static_cast<i::PropertyAttributes>(attribute));
1340 109 : }
1341 :
1342 :
1343 : // --- F u n c t i o n T e m p l a t e ---
1344 3729087 : static void InitializeFunctionTemplate(
1345 : i::Handle<i::FunctionTemplateInfo> info) {
1346 3729087 : InitializeTemplate(info, Consts::FUNCTION_TEMPLATE);
1347 : info->set_flag(0);
1348 3729092 : }
1349 :
1350 : static Local<ObjectTemplate> ObjectTemplateNew(
1351 : i::Isolate* isolate, v8::Local<FunctionTemplate> constructor,
1352 : bool do_not_cache);
1353 :
1354 154186 : Local<ObjectTemplate> FunctionTemplate::PrototypeTemplate() {
1355 : i::Isolate* i_isolate = Utils::OpenHandle(this)->GetIsolate();
1356 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(i_isolate);
1357 : i::Handle<i::Object> result(Utils::OpenHandle(this)->GetPrototypeTemplate(),
1358 308372 : i_isolate);
1359 308372 : if (result->IsUndefined(i_isolate)) {
1360 : // Do not cache prototype objects.
1361 : result = Utils::OpenHandle(
1362 103268 : *ObjectTemplateNew(i_isolate, Local<FunctionTemplate>(), true));
1363 : i::FunctionTemplateInfo::SetPrototypeTemplate(
1364 51634 : i_isolate, Utils::OpenHandle(this), result);
1365 : }
1366 154186 : return ToApiHandle<ObjectTemplate>(result);
1367 : }
1368 :
1369 5 : void FunctionTemplate::SetPrototypeProviderTemplate(
1370 : Local<FunctionTemplate> prototype_provider) {
1371 : i::Isolate* i_isolate = Utils::OpenHandle(this)->GetIsolate();
1372 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(i_isolate);
1373 : i::Handle<i::Object> result = Utils::OpenHandle(*prototype_provider);
1374 : auto info = Utils::OpenHandle(this);
1375 10 : CHECK(info->GetPrototypeTemplate()->IsUndefined(i_isolate));
1376 10 : CHECK(info->GetParentTemplate()->IsUndefined(i_isolate));
1377 : i::FunctionTemplateInfo::SetPrototypeProviderTemplate(i_isolate, info,
1378 5 : result);
1379 5 : }
1380 :
1381 5650891 : static void EnsureNotInstantiated(i::Handle<i::FunctionTemplateInfo> info,
1382 : const char* func) {
1383 11301786 : Utils::ApiCheck(!info->instantiated(), func,
1384 : "FunctionTemplate already instantiated");
1385 5650894 : }
1386 :
1387 :
1388 245 : void FunctionTemplate::Inherit(v8::Local<FunctionTemplate> value) {
1389 : auto info = Utils::OpenHandle(this);
1390 245 : EnsureNotInstantiated(info, "v8::FunctionTemplate::Inherit");
1391 : i::Isolate* i_isolate = info->GetIsolate();
1392 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(i_isolate);
1393 490 : CHECK(info->GetPrototypeProviderTemplate()->IsUndefined(i_isolate));
1394 : i::FunctionTemplateInfo::SetParentTemplate(i_isolate, info,
1395 245 : Utils::OpenHandle(*value));
1396 245 : }
1397 :
1398 3729093 : static Local<FunctionTemplate> FunctionTemplateNew(
1399 : i::Isolate* isolate, FunctionCallback callback, v8::Local<Value> data,
1400 : v8::Local<Signature> signature, int length, bool do_not_cache,
1401 : v8::Local<Private> cached_property_name = v8::Local<Private>(),
1402 : SideEffectType side_effect_type = SideEffectType::kHasSideEffect) {
1403 : i::Handle<i::Struct> struct_obj =
1404 3729093 : isolate->factory()->NewStruct(i::FUNCTION_TEMPLATE_INFO_TYPE, i::TENURED);
1405 : i::Handle<i::FunctionTemplateInfo> obj =
1406 3729087 : i::Handle<i::FunctionTemplateInfo>::cast(struct_obj);
1407 3729087 : InitializeFunctionTemplate(obj);
1408 7458184 : obj->set_do_not_cache(do_not_cache);
1409 : int next_serial_number = i::FunctionTemplateInfo::kInvalidSerialNumber;
1410 3729092 : if (!do_not_cache) {
1411 3723903 : next_serial_number = isolate->heap()->GetNextTemplateSerialNumber();
1412 : }
1413 7458184 : obj->set_serial_number(i::Smi::FromInt(next_serial_number));
1414 3729093 : if (callback != nullptr) {
1415 3618228 : Utils::ToLocal(obj)->SetCallHandler(callback, data, side_effect_type);
1416 : }
1417 : obj->set_length(length);
1418 3729095 : obj->set_undetectable(false);
1419 3729095 : obj->set_needs_access_check(false);
1420 3729095 : obj->set_accept_any_receiver(true);
1421 3729095 : if (!signature.IsEmpty()) {
1422 307972 : obj->set_signature(*Utils::OpenHandle(*signature));
1423 : }
1424 : obj->set_cached_property_name(
1425 : cached_property_name.IsEmpty()
1426 : ? i::ReadOnlyRoots(isolate).the_hole_value()
1427 11187255 : : *Utils::OpenHandle(*cached_property_name));
1428 3729095 : return Utils::ToLocal(obj);
1429 : }
1430 :
1431 3723872 : Local<FunctionTemplate> FunctionTemplate::New(
1432 : Isolate* isolate, FunctionCallback callback, v8::Local<Value> data,
1433 : v8::Local<Signature> signature, int length, ConstructorBehavior behavior,
1434 : SideEffectType side_effect_type) {
1435 3723873 : i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
1436 : // Changes to the environment cannot be captured in the snapshot. Expect no
1437 : // function templates when the isolate is created for serialization.
1438 7447745 : LOG_API(i_isolate, FunctionTemplate, New);
1439 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(i_isolate);
1440 : auto templ = FunctionTemplateNew(i_isolate, callback, data, signature, length,
1441 3723873 : false, Local<Private>(), side_effect_type);
1442 3723876 : if (behavior == ConstructorBehavior::kThrow) templ->RemovePrototype();
1443 7447752 : return templ;
1444 : }
1445 :
1446 10 : MaybeLocal<FunctionTemplate> FunctionTemplate::FromSnapshot(Isolate* isolate,
1447 : size_t index) {
1448 : i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
1449 10 : i::FixedArray serialized_objects = i_isolate->heap()->serialized_objects();
1450 10 : int int_index = static_cast<int>(index);
1451 10 : if (int_index < serialized_objects->length()) {
1452 5 : i::Object info = serialized_objects->get(int_index);
1453 5 : if (info->IsFunctionTemplateInfo()) {
1454 : return Utils::ToLocal(i::Handle<i::FunctionTemplateInfo>(
1455 5 : i::FunctionTemplateInfo::cast(info), i_isolate));
1456 : }
1457 : }
1458 5 : return Local<FunctionTemplate>();
1459 : }
1460 :
1461 30 : Local<FunctionTemplate> FunctionTemplate::NewWithCache(
1462 : Isolate* isolate, FunctionCallback callback, Local<Private> cache_property,
1463 : Local<Value> data, Local<Signature> signature, int length,
1464 : SideEffectType side_effect_type) {
1465 30 : i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
1466 60 : LOG_API(i_isolate, FunctionTemplate, NewWithCache);
1467 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(i_isolate);
1468 : return FunctionTemplateNew(i_isolate, callback, data, signature, length,
1469 60 : false, cache_property, side_effect_type);
1470 : }
1471 :
1472 51445 : Local<Signature> Signature::New(Isolate* isolate,
1473 : Local<FunctionTemplate> receiver) {
1474 51445 : return Utils::SignatureToLocal(Utils::OpenHandle(*receiver));
1475 : }
1476 :
1477 :
1478 23 : Local<AccessorSignature> AccessorSignature::New(
1479 : Isolate* isolate, Local<FunctionTemplate> receiver) {
1480 23 : return Utils::AccessorSignatureToLocal(Utils::OpenHandle(*receiver));
1481 : }
1482 :
1483 : #define SET_FIELD_WRAPPED(isolate, obj, setter, cdata) \
1484 : do { \
1485 : i::Handle<i::Object> foreign = FromCData(isolate, cdata); \
1486 : (obj)->setter(*foreign); \
1487 : } while (false)
1488 :
1489 3618260 : void FunctionTemplate::SetCallHandler(FunctionCallback callback,
1490 : v8::Local<Value> data,
1491 : SideEffectType side_effect_type) {
1492 : auto info = Utils::OpenHandle(this);
1493 3618260 : EnsureNotInstantiated(info, "v8::FunctionTemplate::SetCallHandler");
1494 : i::Isolate* isolate = info->GetIsolate();
1495 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(isolate);
1496 : i::HandleScope scope(isolate);
1497 : i::Handle<i::CallHandlerInfo> obj = isolate->factory()->NewCallHandlerInfo(
1498 3618262 : side_effect_type == SideEffectType::kHasNoSideEffect);
1499 7236523 : SET_FIELD_WRAPPED(isolate, obj, set_callback, callback);
1500 7236522 : SET_FIELD_WRAPPED(isolate, obj, set_js_callback, obj->redirected_callback());
1501 3618258 : if (data.IsEmpty()) {
1502 : data = v8::Undefined(reinterpret_cast<v8::Isolate*>(isolate));
1503 : }
1504 3618258 : obj->set_data(*Utils::OpenHandle(*data));
1505 7236524 : info->set_call_code(*obj);
1506 3618263 : }
1507 :
1508 :
1509 : namespace {
1510 :
1511 : template <typename Getter, typename Setter>
1512 60117 : i::Handle<i::AccessorInfo> MakeAccessorInfo(
1513 : i::Isolate* isolate, v8::Local<Name> name, Getter getter, Setter setter,
1514 : v8::Local<Value> data, v8::AccessControl settings,
1515 : v8::Local<AccessorSignature> signature, bool is_special_data_property,
1516 : bool replace_on_access) {
1517 60117 : i::Handle<i::AccessorInfo> obj = isolate->factory()->NewAccessorInfo();
1518 120234 : SET_FIELD_WRAPPED(isolate, obj, set_getter, getter);
1519 : DCHECK_IMPLIES(replace_on_access,
1520 : is_special_data_property && setter == nullptr);
1521 60117 : if (is_special_data_property && setter == nullptr) {
1522 : setter = reinterpret_cast<Setter>(&i::Accessors::ReconfigureToDataProperty);
1523 : }
1524 120234 : SET_FIELD_WRAPPED(isolate, obj, set_setter, setter);
1525 60117 : i::Address redirected = obj->redirected_getter();
1526 60117 : if (redirected != i::kNullAddress) {
1527 120190 : SET_FIELD_WRAPPED(isolate, obj, set_js_getter, redirected);
1528 : }
1529 60117 : if (data.IsEmpty()) {
1530 : data = v8::Undefined(reinterpret_cast<v8::Isolate*>(isolate));
1531 : }
1532 60117 : obj->set_data(*Utils::OpenHandle(*data));
1533 120234 : obj->set_is_special_data_property(is_special_data_property);
1534 120234 : obj->set_replace_on_access(replace_on_access);
1535 : i::Handle<i::Name> accessor_name = Utils::OpenHandle(*name);
1536 60117 : if (!accessor_name->IsUniqueName()) {
1537 52032 : accessor_name = isolate->factory()->InternalizeString(
1538 : i::Handle<i::String>::cast(accessor_name));
1539 : }
1540 60117 : obj->set_name(*accessor_name);
1541 60154 : if (settings & ALL_CAN_READ) obj->set_all_can_read(true);
1542 60133 : if (settings & ALL_CAN_WRITE) obj->set_all_can_write(true);
1543 60117 : obj->set_initial_property_attributes(i::NONE);
1544 60117 : if (!signature.IsEmpty()) {
1545 36 : obj->set_expected_receiver_type(*Utils::OpenHandle(*signature));
1546 : }
1547 60117 : return obj;
1548 : }
1549 :
1550 : } // namespace
1551 :
1552 58948 : Local<ObjectTemplate> FunctionTemplate::InstanceTemplate() {
1553 : i::Handle<i::FunctionTemplateInfo> handle = Utils::OpenHandle(this, true);
1554 117896 : if (!Utils::ApiCheck(!handle.is_null(),
1555 : "v8::FunctionTemplate::InstanceTemplate()",
1556 : "Reading from empty handle")) {
1557 0 : return Local<ObjectTemplate>();
1558 : }
1559 : i::Isolate* isolate = handle->GetIsolate();
1560 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(isolate);
1561 117896 : if (handle->GetInstanceTemplate()->IsUndefined(isolate)) {
1562 : Local<ObjectTemplate> templ =
1563 : ObjectTemplate::New(isolate, ToApiHandle<FunctionTemplate>(handle));
1564 : i::FunctionTemplateInfo::SetInstanceTemplate(isolate, handle,
1565 52556 : Utils::OpenHandle(*templ));
1566 : }
1567 : i::Handle<i::ObjectTemplateInfo> result(
1568 117896 : i::ObjectTemplateInfo::cast(handle->GetInstanceTemplate()), isolate);
1569 : return Utils::ToLocal(result);
1570 : }
1571 :
1572 :
1573 12 : void FunctionTemplate::SetLength(int length) {
1574 : auto info = Utils::OpenHandle(this);
1575 12 : EnsureNotInstantiated(info, "v8::FunctionTemplate::SetLength");
1576 : auto isolate = info->GetIsolate();
1577 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(isolate);
1578 : info->set_length(length);
1579 12 : }
1580 :
1581 :
1582 51518 : void FunctionTemplate::SetClassName(Local<String> name) {
1583 : auto info = Utils::OpenHandle(this);
1584 51518 : EnsureNotInstantiated(info, "v8::FunctionTemplate::SetClassName");
1585 : auto isolate = info->GetIsolate();
1586 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(isolate);
1587 103036 : info->set_class_name(*Utils::OpenHandle(*name));
1588 51518 : }
1589 :
1590 :
1591 7 : void FunctionTemplate::SetAcceptAnyReceiver(bool value) {
1592 : auto info = Utils::OpenHandle(this);
1593 7 : EnsureNotInstantiated(info, "v8::FunctionTemplate::SetAcceptAnyReceiver");
1594 : auto isolate = info->GetIsolate();
1595 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(isolate);
1596 14 : info->set_accept_any_receiver(value);
1597 7 : }
1598 :
1599 :
1600 232 : void FunctionTemplate::SetHiddenPrototype(bool value) {
1601 : auto info = Utils::OpenHandle(this);
1602 232 : EnsureNotInstantiated(info, "v8::FunctionTemplate::SetHiddenPrototype");
1603 : auto isolate = info->GetIsolate();
1604 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(isolate);
1605 464 : info->set_hidden_prototype(value);
1606 232 : }
1607 :
1608 :
1609 1974082 : void FunctionTemplate::ReadOnlyPrototype() {
1610 : auto info = Utils::OpenHandle(this);
1611 1974082 : EnsureNotInstantiated(info, "v8::FunctionTemplate::ReadOnlyPrototype");
1612 : auto isolate = info->GetIsolate();
1613 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(isolate);
1614 1974083 : info->set_read_only_prototype(true);
1615 1974083 : }
1616 :
1617 :
1618 4856 : void FunctionTemplate::RemovePrototype() {
1619 : auto info = Utils::OpenHandle(this);
1620 4856 : EnsureNotInstantiated(info, "v8::FunctionTemplate::RemovePrototype");
1621 : auto isolate = info->GetIsolate();
1622 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(isolate);
1623 4856 : info->set_remove_prototype(true);
1624 4856 : }
1625 :
1626 :
1627 : // --- O b j e c t T e m p l a t e ---
1628 :
1629 :
1630 721447 : Local<ObjectTemplate> ObjectTemplate::New(
1631 : Isolate* isolate, v8::Local<FunctionTemplate> constructor) {
1632 721448 : return New(reinterpret_cast<i::Isolate*>(isolate), constructor);
1633 : }
1634 :
1635 :
1636 879968 : static Local<ObjectTemplate> ObjectTemplateNew(
1637 879969 : i::Isolate* isolate, v8::Local<FunctionTemplate> constructor,
1638 : bool do_not_cache) {
1639 1759937 : LOG_API(isolate, ObjectTemplate, New);
1640 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(isolate);
1641 : i::Handle<i::Struct> struct_obj =
1642 879969 : isolate->factory()->NewStruct(i::OBJECT_TEMPLATE_INFO_TYPE, i::TENURED);
1643 : i::Handle<i::ObjectTemplateInfo> obj =
1644 879969 : i::Handle<i::ObjectTemplateInfo>::cast(struct_obj);
1645 879969 : InitializeTemplate(obj, Consts::OBJECT_TEMPLATE);
1646 : int next_serial_number = 0;
1647 879969 : if (!do_not_cache) {
1648 828336 : next_serial_number = isolate->heap()->GetNextTemplateSerialNumber();
1649 : }
1650 1759939 : obj->set_serial_number(i::Smi::FromInt(next_serial_number));
1651 879970 : if (!constructor.IsEmpty())
1652 105872 : obj->set_constructor(*Utils::OpenHandle(*constructor));
1653 879970 : obj->set_data(i::Smi::kZero);
1654 879970 : return Utils::ToLocal(obj);
1655 : }
1656 :
1657 0 : Local<ObjectTemplate> ObjectTemplate::New(
1658 : i::Isolate* isolate, v8::Local<FunctionTemplate> constructor) {
1659 828335 : return ObjectTemplateNew(isolate, constructor, false);
1660 : }
1661 :
1662 10 : MaybeLocal<ObjectTemplate> ObjectTemplate::FromSnapshot(Isolate* isolate,
1663 : size_t index) {
1664 : i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
1665 10 : i::FixedArray serialized_objects = i_isolate->heap()->serialized_objects();
1666 10 : int int_index = static_cast<int>(index);
1667 10 : if (int_index < serialized_objects->length()) {
1668 5 : i::Object info = serialized_objects->get(int_index);
1669 5 : if (info->IsObjectTemplateInfo()) {
1670 : return Utils::ToLocal(i::Handle<i::ObjectTemplateInfo>(
1671 5 : i::ObjectTemplateInfo::cast(info), i_isolate));
1672 : }
1673 : }
1674 5 : return Local<ObjectTemplate>();
1675 : }
1676 :
1677 : // Ensure that the object template has a constructor. If no
1678 : // constructor is available we create one.
1679 161992 : static i::Handle<i::FunctionTemplateInfo> EnsureConstructor(
1680 : i::Isolate* isolate,
1681 : ObjectTemplate* object_template) {
1682 161992 : i::Object obj = Utils::OpenHandle(object_template)->constructor();
1683 161992 : if (!obj->IsUndefined(isolate)) {
1684 : i::FunctionTemplateInfo info = i::FunctionTemplateInfo::cast(obj);
1685 52786 : return i::Handle<i::FunctionTemplateInfo>(info, isolate);
1686 : }
1687 : Local<FunctionTemplate> templ =
1688 109206 : FunctionTemplate::New(reinterpret_cast<Isolate*>(isolate));
1689 : i::Handle<i::FunctionTemplateInfo> constructor = Utils::OpenHandle(*templ);
1690 : i::FunctionTemplateInfo::SetInstanceTemplate(
1691 109206 : isolate, constructor, Utils::OpenHandle(object_template));
1692 218412 : Utils::OpenHandle(object_template)->set_constructor(*constructor);
1693 109206 : return constructor;
1694 : }
1695 :
1696 : template <typename Getter, typename Setter, typename Data, typename Template>
1697 52207 : static void TemplateSetAccessor(
1698 : Template* template_obj, v8::Local<Name> name, Getter getter, Setter setter,
1699 : Data data, AccessControl settings, PropertyAttribute attribute,
1700 : v8::Local<AccessorSignature> signature, bool is_special_data_property,
1701 : bool replace_on_access, SideEffectType getter_side_effect_type,
1702 : SideEffectType setter_side_effect_type) {
1703 : auto info = Utils::OpenHandle(template_obj);
1704 : auto isolate = info->GetIsolate();
1705 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(isolate);
1706 : i::HandleScope scope(isolate);
1707 : i::Handle<i::AccessorInfo> accessor_info =
1708 : MakeAccessorInfo(isolate, name, getter, setter, data, settings, signature,
1709 52207 : is_special_data_property, replace_on_access);
1710 52207 : accessor_info->set_initial_property_attributes(
1711 104414 : static_cast<i::PropertyAttributes>(attribute));
1712 52207 : accessor_info->set_getter_side_effect_type(getter_side_effect_type);
1713 52207 : accessor_info->set_setter_side_effect_type(setter_side_effect_type);
1714 52207 : i::ApiNatives::AddNativeDataProperty(isolate, info, accessor_info);
1715 52207 : }
1716 :
1717 179 : void Template::SetNativeDataProperty(
1718 : v8::Local<String> name, AccessorGetterCallback getter,
1719 : AccessorSetterCallback setter, v8::Local<Value> data,
1720 : PropertyAttribute attribute, v8::Local<AccessorSignature> signature,
1721 : AccessControl settings, SideEffectType getter_side_effect_type,
1722 : SideEffectType setter_side_effect_type) {
1723 : TemplateSetAccessor(this, name, getter, setter, data, settings, attribute,
1724 : signature, true, false, getter_side_effect_type,
1725 179 : setter_side_effect_type);
1726 179 : }
1727 :
1728 10 : void Template::SetNativeDataProperty(
1729 : v8::Local<Name> name, AccessorNameGetterCallback getter,
1730 : AccessorNameSetterCallback setter, v8::Local<Value> data,
1731 : PropertyAttribute attribute, v8::Local<AccessorSignature> signature,
1732 : AccessControl settings, SideEffectType getter_side_effect_type,
1733 : SideEffectType setter_side_effect_type) {
1734 : TemplateSetAccessor(this, name, getter, setter, data, settings, attribute,
1735 : signature, true, false, getter_side_effect_type,
1736 10 : setter_side_effect_type);
1737 10 : }
1738 :
1739 10 : void Template::SetLazyDataProperty(v8::Local<Name> name,
1740 : AccessorNameGetterCallback getter,
1741 : v8::Local<Value> data,
1742 : PropertyAttribute attribute,
1743 : SideEffectType getter_side_effect_type,
1744 : SideEffectType setter_side_effect_type) {
1745 : TemplateSetAccessor(this, name, getter,
1746 : static_cast<AccessorNameSetterCallback>(nullptr), data,
1747 : DEFAULT, attribute, Local<AccessorSignature>(), true,
1748 10 : true, getter_side_effect_type, setter_side_effect_type);
1749 10 : }
1750 :
1751 55 : void Template::SetIntrinsicDataProperty(Local<Name> name, Intrinsic intrinsic,
1752 : PropertyAttribute attribute) {
1753 : auto templ = Utils::OpenHandle(this);
1754 : i::Isolate* isolate = templ->GetIsolate();
1755 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(isolate);
1756 : i::HandleScope scope(isolate);
1757 : i::ApiNatives::AddDataProperty(isolate, templ, Utils::OpenHandle(*name),
1758 : intrinsic,
1759 55 : static_cast<i::PropertyAttributes>(attribute));
1760 55 : }
1761 :
1762 51914 : void ObjectTemplate::SetAccessor(v8::Local<String> name,
1763 : AccessorGetterCallback getter,
1764 : AccessorSetterCallback setter,
1765 : v8::Local<Value> data, AccessControl settings,
1766 : PropertyAttribute attribute,
1767 : v8::Local<AccessorSignature> signature,
1768 : SideEffectType getter_side_effect_type,
1769 : SideEffectType setter_side_effect_type) {
1770 : TemplateSetAccessor(this, name, getter, setter, data, settings, attribute,
1771 : signature, i::FLAG_disable_old_api_accessors, false,
1772 103828 : getter_side_effect_type, setter_side_effect_type);
1773 51914 : }
1774 :
1775 94 : void ObjectTemplate::SetAccessor(v8::Local<Name> name,
1776 : AccessorNameGetterCallback getter,
1777 : AccessorNameSetterCallback setter,
1778 : v8::Local<Value> data, AccessControl settings,
1779 : PropertyAttribute attribute,
1780 : v8::Local<AccessorSignature> signature,
1781 : SideEffectType getter_side_effect_type,
1782 : SideEffectType setter_side_effect_type) {
1783 : TemplateSetAccessor(this, name, getter, setter, data, settings, attribute,
1784 : signature, i::FLAG_disable_old_api_accessors, false,
1785 94 : getter_side_effect_type, setter_side_effect_type);
1786 94 : }
1787 :
1788 : template <typename Getter, typename Setter, typename Query, typename Descriptor,
1789 : typename Deleter, typename Enumerator, typename Definer>
1790 1211 : static i::Handle<i::InterceptorInfo> CreateInterceptorInfo(
1791 : i::Isolate* isolate, Getter getter, Setter setter, Query query,
1792 : Descriptor descriptor, Deleter remover, Enumerator enumerator,
1793 : Definer definer, Local<Value> data, PropertyHandlerFlags flags) {
1794 : auto obj = i::Handle<i::InterceptorInfo>::cast(
1795 1211 : isolate->factory()->NewStruct(i::INTERCEPTOR_INFO_TYPE, i::TENURED));
1796 : obj->set_flags(0);
1797 :
1798 2234 : if (getter != nullptr) SET_FIELD_WRAPPED(isolate, obj, set_getter, getter);
1799 1653 : if (setter != nullptr) SET_FIELD_WRAPPED(isolate, obj, set_setter, setter);
1800 1426 : if (query != nullptr) SET_FIELD_WRAPPED(isolate, obj, set_query, query);
1801 1211 : if (descriptor != nullptr)
1802 60 : SET_FIELD_WRAPPED(isolate, obj, set_descriptor, descriptor);
1803 1295 : if (remover != nullptr) SET_FIELD_WRAPPED(isolate, obj, set_deleter, remover);
1804 1211 : if (enumerator != nullptr)
1805 322 : SET_FIELD_WRAPPED(isolate, obj, set_enumerator, enumerator);
1806 1301 : if (definer != nullptr) SET_FIELD_WRAPPED(isolate, obj, set_definer, definer);
1807 1211 : obj->set_can_intercept_symbols(
1808 : !(static_cast<int>(flags) &
1809 3633 : static_cast<int>(PropertyHandlerFlags::kOnlyInterceptStrings)));
1810 1211 : obj->set_all_can_read(static_cast<int>(flags) &
1811 3633 : static_cast<int>(PropertyHandlerFlags::kAllCanRead));
1812 1211 : obj->set_non_masking(static_cast<int>(flags) &
1813 3633 : static_cast<int>(PropertyHandlerFlags::kNonMasking));
1814 1211 : obj->set_has_no_side_effect(
1815 : static_cast<int>(flags) &
1816 3633 : static_cast<int>(PropertyHandlerFlags::kHasNoSideEffect));
1817 :
1818 1211 : if (data.IsEmpty()) {
1819 : data = v8::Undefined(reinterpret_cast<v8::Isolate*>(isolate));
1820 : }
1821 1211 : obj->set_data(*Utils::OpenHandle(*data));
1822 1211 : return obj;
1823 : }
1824 :
1825 : template <typename Getter, typename Setter, typename Query, typename Descriptor,
1826 : typename Deleter, typename Enumerator, typename Definer>
1827 999 : static i::Handle<i::InterceptorInfo> CreateNamedInterceptorInfo(
1828 : i::Isolate* isolate, Getter getter, Setter setter, Query query,
1829 : Descriptor descriptor, Deleter remover, Enumerator enumerator,
1830 : Definer definer, Local<Value> data, PropertyHandlerFlags flags) {
1831 : auto interceptor =
1832 : CreateInterceptorInfo(isolate, getter, setter, query, descriptor, remover,
1833 999 : enumerator, definer, data, flags);
1834 999 : interceptor->set_is_named(true);
1835 999 : return interceptor;
1836 : }
1837 :
1838 : template <typename Getter, typename Setter, typename Query, typename Descriptor,
1839 : typename Deleter, typename Enumerator, typename Definer>
1840 212 : static i::Handle<i::InterceptorInfo> CreateIndexedInterceptorInfo(
1841 : i::Isolate* isolate, Getter getter, Setter setter, Query query,
1842 : Descriptor descriptor, Deleter remover, Enumerator enumerator,
1843 : Definer definer, Local<Value> data, PropertyHandlerFlags flags) {
1844 : auto interceptor =
1845 : CreateInterceptorInfo(isolate, getter, setter, query, descriptor, remover,
1846 212 : enumerator, definer, data, flags);
1847 212 : interceptor->set_is_named(false);
1848 212 : return interceptor;
1849 : }
1850 :
1851 : template <typename Getter, typename Setter, typename Query, typename Descriptor,
1852 : typename Deleter, typename Enumerator, typename Definer>
1853 968 : static void ObjectTemplateSetNamedPropertyHandler(
1854 : ObjectTemplate* templ, Getter getter, Setter setter, Query query,
1855 : Descriptor descriptor, Deleter remover, Enumerator enumerator,
1856 : Definer definer, Local<Value> data, PropertyHandlerFlags flags) {
1857 : i::Isolate* isolate = Utils::OpenHandle(templ)->GetIsolate();
1858 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(isolate);
1859 : i::HandleScope scope(isolate);
1860 968 : auto cons = EnsureConstructor(isolate, templ);
1861 968 : EnsureNotInstantiated(cons, "ObjectTemplateSetNamedPropertyHandler");
1862 : auto obj =
1863 : CreateNamedInterceptorInfo(isolate, getter, setter, query, descriptor,
1864 968 : remover, enumerator, definer, data, flags);
1865 968 : i::FunctionTemplateInfo::SetNamedPropertyHandler(isolate, cons, obj);
1866 968 : }
1867 :
1868 968 : void ObjectTemplate::SetHandler(
1869 : const NamedPropertyHandlerConfiguration& config) {
1870 : ObjectTemplateSetNamedPropertyHandler(
1871 : this, config.getter, config.setter, config.query, config.descriptor,
1872 : config.deleter, config.enumerator, config.definer, config.data,
1873 968 : config.flags);
1874 968 : }
1875 :
1876 :
1877 152 : void ObjectTemplate::MarkAsUndetectable() {
1878 : i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
1879 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(isolate);
1880 : i::HandleScope scope(isolate);
1881 152 : auto cons = EnsureConstructor(isolate, this);
1882 152 : EnsureNotInstantiated(cons, "v8::ObjectTemplate::MarkAsUndetectable");
1883 152 : cons->set_undetectable(true);
1884 152 : }
1885 :
1886 :
1887 146 : void ObjectTemplate::SetAccessCheckCallback(AccessCheckCallback callback,
1888 : Local<Value> data) {
1889 : i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
1890 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(isolate);
1891 : i::HandleScope scope(isolate);
1892 146 : auto cons = EnsureConstructor(isolate, this);
1893 146 : EnsureNotInstantiated(cons, "v8::ObjectTemplate::SetAccessCheckCallback");
1894 :
1895 : i::Handle<i::Struct> struct_info =
1896 146 : isolate->factory()->NewStruct(i::ACCESS_CHECK_INFO_TYPE, i::TENURED);
1897 : i::Handle<i::AccessCheckInfo> info =
1898 146 : i::Handle<i::AccessCheckInfo>::cast(struct_info);
1899 :
1900 292 : SET_FIELD_WRAPPED(isolate, info, set_callback, callback);
1901 146 : info->set_named_interceptor(i::Object());
1902 146 : info->set_indexed_interceptor(i::Object());
1903 :
1904 146 : if (data.IsEmpty()) {
1905 : data = v8::Undefined(reinterpret_cast<v8::Isolate*>(isolate));
1906 : }
1907 146 : info->set_data(*Utils::OpenHandle(*data));
1908 :
1909 146 : i::FunctionTemplateInfo::SetAccessCheckInfo(isolate, cons, info);
1910 146 : cons->set_needs_access_check(true);
1911 146 : }
1912 :
1913 31 : void ObjectTemplate::SetAccessCheckCallbackAndHandler(
1914 : AccessCheckCallback callback,
1915 : const NamedPropertyHandlerConfiguration& named_handler,
1916 : const IndexedPropertyHandlerConfiguration& indexed_handler,
1917 : Local<Value> data) {
1918 : i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
1919 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(isolate);
1920 : i::HandleScope scope(isolate);
1921 31 : auto cons = EnsureConstructor(isolate, this);
1922 : EnsureNotInstantiated(
1923 31 : cons, "v8::ObjectTemplate::SetAccessCheckCallbackWithHandler");
1924 :
1925 : i::Handle<i::Struct> struct_info =
1926 31 : isolate->factory()->NewStruct(i::ACCESS_CHECK_INFO_TYPE, i::TENURED);
1927 : i::Handle<i::AccessCheckInfo> info =
1928 31 : i::Handle<i::AccessCheckInfo>::cast(struct_info);
1929 :
1930 62 : SET_FIELD_WRAPPED(isolate, info, set_callback, callback);
1931 : auto named_interceptor = CreateNamedInterceptorInfo(
1932 : isolate, named_handler.getter, named_handler.setter, named_handler.query,
1933 : named_handler.descriptor, named_handler.deleter, named_handler.enumerator,
1934 31 : named_handler.definer, named_handler.data, named_handler.flags);
1935 62 : info->set_named_interceptor(*named_interceptor);
1936 : auto indexed_interceptor = CreateIndexedInterceptorInfo(
1937 : isolate, indexed_handler.getter, indexed_handler.setter,
1938 : indexed_handler.query, indexed_handler.descriptor,
1939 : indexed_handler.deleter, indexed_handler.enumerator,
1940 31 : indexed_handler.definer, indexed_handler.data, indexed_handler.flags);
1941 62 : info->set_indexed_interceptor(*indexed_interceptor);
1942 :
1943 31 : if (data.IsEmpty()) {
1944 : data = v8::Undefined(reinterpret_cast<v8::Isolate*>(isolate));
1945 : }
1946 31 : info->set_data(*Utils::OpenHandle(*data));
1947 :
1948 31 : i::FunctionTemplateInfo::SetAccessCheckInfo(isolate, cons, info);
1949 31 : cons->set_needs_access_check(true);
1950 31 : }
1951 :
1952 181 : void ObjectTemplate::SetHandler(
1953 : const IndexedPropertyHandlerConfiguration& config) {
1954 : i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
1955 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(isolate);
1956 : i::HandleScope scope(isolate);
1957 181 : auto cons = EnsureConstructor(isolate, this);
1958 181 : EnsureNotInstantiated(cons, "v8::ObjectTemplate::SetHandler");
1959 : auto obj = CreateIndexedInterceptorInfo(
1960 : isolate, config.getter, config.setter, config.query, config.descriptor,
1961 : config.deleter, config.enumerator, config.definer, config.data,
1962 181 : config.flags);
1963 181 : i::FunctionTemplateInfo::SetIndexedPropertyHandler(isolate, cons, obj);
1964 181 : }
1965 :
1966 201 : void ObjectTemplate::SetCallAsFunctionHandler(FunctionCallback callback,
1967 : Local<Value> data) {
1968 : i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
1969 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(isolate);
1970 : i::HandleScope scope(isolate);
1971 201 : auto cons = EnsureConstructor(isolate, this);
1972 201 : EnsureNotInstantiated(cons, "v8::ObjectTemplate::SetCallAsFunctionHandler");
1973 201 : i::Handle<i::CallHandlerInfo> obj = isolate->factory()->NewCallHandlerInfo();
1974 402 : SET_FIELD_WRAPPED(isolate, obj, set_callback, callback);
1975 402 : SET_FIELD_WRAPPED(isolate, obj, set_js_callback, obj->redirected_callback());
1976 201 : if (data.IsEmpty()) {
1977 : data = v8::Undefined(reinterpret_cast<v8::Isolate*>(isolate));
1978 : }
1979 201 : obj->set_data(*Utils::OpenHandle(*data));
1980 201 : i::FunctionTemplateInfo::SetInstanceCallHandler(isolate, cons, obj);
1981 201 : }
1982 :
1983 108637 : int ObjectTemplate::InternalFieldCount() {
1984 108637 : return Utils::OpenHandle(this)->embedder_field_count();
1985 : }
1986 :
1987 105934 : void ObjectTemplate::SetInternalFieldCount(int value) {
1988 : i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
1989 : if (!Utils::ApiCheck(i::Smi::IsValid(value),
1990 : "v8::ObjectTemplate::SetInternalFieldCount()",
1991 : "Invalid embedder field count")) {
1992 105934 : return;
1993 : }
1994 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(isolate);
1995 105934 : if (value > 0) {
1996 : // The embedder field count is set by the constructor function's
1997 : // construct code, so we ensure that there is a constructor
1998 : // function to do the setting.
1999 51623 : EnsureConstructor(isolate, this);
2000 : }
2001 105934 : Utils::OpenHandle(this)->set_embedder_field_count(value);
2002 : }
2003 :
2004 0 : bool ObjectTemplate::IsImmutableProto() {
2005 0 : return Utils::OpenHandle(this)->immutable_proto();
2006 : }
2007 :
2008 18 : void ObjectTemplate::SetImmutableProto() {
2009 : i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
2010 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(isolate);
2011 18 : Utils::OpenHandle(this)->set_immutable_proto(true);
2012 18 : }
2013 :
2014 : // --- S c r i p t s ---
2015 :
2016 :
2017 : // Internally, UnboundScript is a SharedFunctionInfo, and Script is a
2018 : // JSFunction.
2019 :
2020 727 : ScriptCompiler::CachedData::CachedData(const uint8_t* data_, int length_,
2021 : BufferPolicy buffer_policy_)
2022 : : data(data_),
2023 : length(length_),
2024 : rejected(false),
2025 727 : buffer_policy(buffer_policy_) {}
2026 :
2027 :
2028 727 : ScriptCompiler::CachedData::~CachedData() {
2029 727 : if (buffer_policy == BufferOwned) {
2030 717 : delete[] data;
2031 : }
2032 727 : }
2033 :
2034 0 : bool ScriptCompiler::ExternalSourceStream::SetBookmark() { return false; }
2035 :
2036 0 : void ScriptCompiler::ExternalSourceStream::ResetToBookmark() { UNREACHABLE(); }
2037 :
2038 13051 : ScriptCompiler::StreamedSource::StreamedSource(ExternalSourceStream* stream,
2039 : Encoding encoding)
2040 26102 : : impl_(new i::ScriptStreamingData(stream, encoding)) {}
2041 :
2042 : ScriptCompiler::StreamedSource::~StreamedSource() = default;
2043 :
2044 272506 : Local<Script> UnboundScript::BindToCurrentContext() {
2045 : auto function_info =
2046 272506 : i::Handle<i::SharedFunctionInfo>::cast(Utils::OpenHandle(this));
2047 : i::Isolate* isolate = function_info->GetIsolate();
2048 : i::Handle<i::JSFunction> function =
2049 : isolate->factory()->NewFunctionFromSharedFunctionInfo(
2050 545012 : function_info, isolate->native_context());
2051 272506 : return ToApiHandle<Script>(function);
2052 : }
2053 :
2054 282 : int UnboundScript::GetId() {
2055 : auto function_info =
2056 282 : i::Handle<i::SharedFunctionInfo>::cast(Utils::OpenHandle(this));
2057 282 : i::Isolate* isolate = function_info->GetIsolate();
2058 564 : LOG_API(isolate, UnboundScript, GetId);
2059 : i::HandleScope scope(isolate);
2060 : i::Handle<i::Script> script(i::Script::cast(function_info->script()),
2061 564 : isolate);
2062 282 : return script->id();
2063 : }
2064 :
2065 :
2066 5 : int UnboundScript::GetLineNumber(int code_pos) {
2067 : i::Handle<i::SharedFunctionInfo> obj =
2068 5 : i::Handle<i::SharedFunctionInfo>::cast(Utils::OpenHandle(this));
2069 5 : i::Isolate* isolate = obj->GetIsolate();
2070 10 : LOG_API(isolate, UnboundScript, GetLineNumber);
2071 10 : if (obj->script()->IsScript()) {
2072 10 : i::Handle<i::Script> script(i::Script::cast(obj->script()), isolate);
2073 5 : return i::Script::GetLineNumber(script, code_pos);
2074 : } else {
2075 : return -1;
2076 : }
2077 : }
2078 :
2079 :
2080 5 : Local<Value> UnboundScript::GetScriptName() {
2081 : i::Handle<i::SharedFunctionInfo> obj =
2082 5 : i::Handle<i::SharedFunctionInfo>::cast(Utils::OpenHandle(this));
2083 5 : i::Isolate* isolate = obj->GetIsolate();
2084 10 : LOG_API(isolate, UnboundScript, GetName);
2085 10 : if (obj->script()->IsScript()) {
2086 10 : i::Object name = i::Script::cast(obj->script())->name();
2087 : return Utils::ToLocal(i::Handle<i::Object>(name, isolate));
2088 : } else {
2089 0 : return Local<String>();
2090 : }
2091 : }
2092 :
2093 :
2094 155 : Local<Value> UnboundScript::GetSourceURL() {
2095 : i::Handle<i::SharedFunctionInfo> obj =
2096 155 : i::Handle<i::SharedFunctionInfo>::cast(Utils::OpenHandle(this));
2097 155 : i::Isolate* isolate = obj->GetIsolate();
2098 310 : LOG_API(isolate, UnboundScript, GetSourceURL);
2099 310 : if (obj->script()->IsScript()) {
2100 310 : i::Object url = i::Script::cast(obj->script())->source_url();
2101 : return Utils::ToLocal(i::Handle<i::Object>(url, isolate));
2102 : } else {
2103 0 : return Local<String>();
2104 : }
2105 : }
2106 :
2107 :
2108 155 : Local<Value> UnboundScript::GetSourceMappingURL() {
2109 : i::Handle<i::SharedFunctionInfo> obj =
2110 155 : i::Handle<i::SharedFunctionInfo>::cast(Utils::OpenHandle(this));
2111 155 : i::Isolate* isolate = obj->GetIsolate();
2112 310 : LOG_API(isolate, UnboundScript, GetSourceMappingURL);
2113 310 : if (obj->script()->IsScript()) {
2114 310 : i::Object url = i::Script::cast(obj->script())->source_mapping_url();
2115 : return Utils::ToLocal(i::Handle<i::Object>(url, isolate));
2116 : } else {
2117 0 : return Local<String>();
2118 : }
2119 : }
2120 :
2121 :
2122 270696 : MaybeLocal<Value> Script::Run(Local<Context> context) {
2123 541392 : auto isolate = reinterpret_cast<i::Isolate*>(context->GetIsolate());
2124 541392 : TRACE_EVENT_CALL_STATS_SCOPED(isolate, "v8", "V8.Execute");
2125 1082784 : ENTER_V8(isolate, context, Script, Run, MaybeLocal<Value>(),
2126 : InternalEscapableScope);
2127 270696 : i::HistogramTimerScope execute_timer(isolate->counters()->execute(), true);
2128 270696 : i::AggregatingHistogramTimerScope timer(isolate->counters()->compile_lazy());
2129 : i::TimerEventScope<i::TimerEventExecute> timer_scope(isolate);
2130 270696 : auto fun = i::Handle<i::JSFunction>::cast(Utils::OpenHandle(this));
2131 :
2132 270696 : i::Handle<i::Object> receiver = isolate->global_proxy();
2133 : Local<Value> result;
2134 : has_pending_exception = !ToLocal<Value>(
2135 541392 : i::Execution::Call(isolate, fun, receiver, 0, nullptr), &result);
2136 :
2137 270696 : RETURN_ON_FAILED_EXECUTION(Value);
2138 533002 : RETURN_ESCAPED(result);
2139 : }
2140 :
2141 :
2142 410 : Local<Value> ScriptOrModule::GetResourceName() {
2143 : i::Handle<i::Script> obj = Utils::OpenHandle(this);
2144 : i::Isolate* isolate = obj->GetIsolate();
2145 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(isolate);
2146 820 : i::Handle<i::Object> val(obj->name(), isolate);
2147 410 : return ToApiHandle<Value>(val);
2148 : }
2149 :
2150 5 : Local<PrimitiveArray> ScriptOrModule::GetHostDefinedOptions() {
2151 : i::Handle<i::Script> obj = Utils::OpenHandle(this);
2152 : i::Isolate* isolate = obj->GetIsolate();
2153 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(isolate);
2154 10 : i::Handle<i::FixedArray> val(obj->host_defined_options(), isolate);
2155 5 : return ToApiHandle<PrimitiveArray>(val);
2156 : }
2157 :
2158 865 : Local<UnboundScript> Script::GetUnboundScript() {
2159 : i::Handle<i::Object> obj = Utils::OpenHandle(this);
2160 865 : i::SharedFunctionInfo sfi = i::JSFunction::cast(*obj)->shared();
2161 : i::Isolate* isolate = sfi->GetIsolate();
2162 865 : return ToApiHandle<UnboundScript>(i::handle(sfi, isolate));
2163 : }
2164 :
2165 : // static
2166 11 : Local<PrimitiveArray> PrimitiveArray::New(Isolate* v8_isolate, int length) {
2167 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(v8_isolate);
2168 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(isolate);
2169 : Utils::ApiCheck(length >= 0, "v8::PrimitiveArray::New",
2170 : "length must be equal or greater than zero");
2171 11 : i::Handle<i::FixedArray> array = isolate->factory()->NewFixedArray(length);
2172 11 : return ToApiHandle<PrimitiveArray>(array);
2173 : }
2174 :
2175 5 : int PrimitiveArray::Length() const {
2176 : i::Handle<i::FixedArray> array = Utils::OpenHandle(this);
2177 5 : return array->length();
2178 : }
2179 :
2180 31 : void PrimitiveArray::Set(Isolate* v8_isolate, int index,
2181 : Local<Primitive> item) {
2182 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(v8_isolate);
2183 : i::Handle<i::FixedArray> array = Utils::OpenHandle(this);
2184 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(isolate);
2185 62 : Utils::ApiCheck(index >= 0 && index < array->length(),
2186 : "v8::PrimitiveArray::Set",
2187 : "index must be greater than or equal to 0 and less than the "
2188 : "array length");
2189 : i::Handle<i::Object> i_item = Utils::OpenHandle(*item);
2190 31 : array->set(index, *i_item);
2191 31 : }
2192 :
2193 117 : Local<Primitive> PrimitiveArray::Get(Isolate* v8_isolate, int index) {
2194 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(v8_isolate);
2195 : i::Handle<i::FixedArray> array = Utils::OpenHandle(this);
2196 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(isolate);
2197 234 : Utils::ApiCheck(index >= 0 && index < array->length(),
2198 : "v8::PrimitiveArray::Get",
2199 : "index must be greater than or equal to 0 and less than the "
2200 : "array length");
2201 : i::Handle<i::Object> i_item(array->get(index), isolate);
2202 117 : return ToApiHandle<Primitive>(i_item);
2203 : }
2204 :
2205 1036 : Module::Status Module::GetStatus() const {
2206 : i::Handle<i::Module> self = Utils::OpenHandle(this);
2207 1036 : switch (self->status()) {
2208 : case i::Module::kUninstantiated:
2209 : case i::Module::kPreInstantiating:
2210 : return kUninstantiated;
2211 : case i::Module::kInstantiating:
2212 0 : return kInstantiating;
2213 : case i::Module::kInstantiated:
2214 220 : return kInstantiated;
2215 : case i::Module::kEvaluating:
2216 0 : return kEvaluating;
2217 : case i::Module::kEvaluated:
2218 561 : return kEvaluated;
2219 : case i::Module::kErrored:
2220 20 : return kErrored;
2221 : }
2222 0 : UNREACHABLE();
2223 : }
2224 :
2225 10 : Local<Value> Module::GetException() const {
2226 10 : Utils::ApiCheck(GetStatus() == kErrored, "v8::Module::GetException",
2227 : "Module status must be kErrored");
2228 : i::Handle<i::Module> self = Utils::OpenHandle(this);
2229 : i::Isolate* isolate = self->GetIsolate();
2230 20 : return ToApiHandle<Value>(i::handle(self->GetException(), isolate));
2231 : }
2232 :
2233 1216 : int Module::GetModuleRequestsLength() const {
2234 : i::Handle<i::Module> self = Utils::OpenHandle(this);
2235 2432 : return self->info()->module_requests()->length();
2236 : }
2237 :
2238 834 : Local<String> Module::GetModuleRequest(int i) const {
2239 834 : CHECK_GE(i, 0);
2240 : i::Handle<i::Module> self = Utils::OpenHandle(this);
2241 : i::Isolate* isolate = self->GetIsolate();
2242 1668 : i::Handle<i::FixedArray> module_requests(self->info()->module_requests(),
2243 1668 : isolate);
2244 834 : CHECK_LT(i, module_requests->length());
2245 834 : return ToApiHandle<String>(i::handle(module_requests->get(i), isolate));
2246 : }
2247 :
2248 10 : Location Module::GetModuleRequestLocation(int i) const {
2249 10 : CHECK_GE(i, 0);
2250 : i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
2251 : i::HandleScope scope(isolate);
2252 : i::Handle<i::Module> self = Utils::OpenHandle(this);
2253 : i::Handle<i::FixedArray> module_request_positions(
2254 20 : self->info()->module_request_positions(), isolate);
2255 10 : CHECK_LT(i, module_request_positions->length());
2256 10 : int position = i::Smi::ToInt(module_request_positions->get(i));
2257 20 : i::Handle<i::Script> script(self->script(), isolate);
2258 : i::Script::PositionInfo info;
2259 10 : i::Script::GetPositionInfo(script, position, &info, i::Script::WITH_OFFSET);
2260 20 : return v8::Location(info.line, info.column);
2261 : }
2262 :
2263 226 : Local<Value> Module::GetModuleNamespace() {
2264 : Utils::ApiCheck(
2265 226 : GetStatus() >= kInstantiated, "v8::Module::GetModuleNamespace",
2266 226 : "v8::Module::GetModuleNamespace must be used on an instantiated module");
2267 : i::Handle<i::Module> self = Utils::OpenHandle(this);
2268 : i::Handle<i::JSModuleNamespace> module_namespace =
2269 226 : i::Module::GetModuleNamespace(self->GetIsolate(), self);
2270 226 : return ToApiHandle<Value>(module_namespace);
2271 : }
2272 :
2273 20 : Local<UnboundModuleScript> Module::GetUnboundModuleScript() {
2274 : Utils::ApiCheck(
2275 20 : GetStatus() < kEvaluating, "v8::Module::GetUnboundScript",
2276 20 : "v8::Module::GetUnboundScript must be used on an unevaluated module");
2277 : i::Handle<i::Module> self = Utils::OpenHandle(this);
2278 : return ToApiHandle<UnboundModuleScript>(i::Handle<i::SharedFunctionInfo>(
2279 40 : self->GetSharedFunctionInfo(), self->GetIsolate()));
2280 : }
2281 :
2282 4124 : int Module::GetIdentityHash() const { return Utils::OpenHandle(this)->hash(); }
2283 :
2284 1095 : Maybe<bool> Module::InstantiateModule(Local<Context> context,
2285 : Module::ResolveCallback callback) {
2286 2190 : auto isolate = reinterpret_cast<i::Isolate*>(context->GetIsolate());
2287 4380 : ENTER_V8(isolate, context, Module, InstantiateModule, Nothing<bool>(),
2288 : i::HandleScope);
2289 : has_pending_exception = !i::Module::Instantiate(
2290 1095 : isolate, Utils::OpenHandle(this), context, callback);
2291 1095 : RETURN_ON_FAILED_EXECUTION_PRIMITIVE(bool);
2292 : return Just(true);
2293 : }
2294 :
2295 1208 : MaybeLocal<Value> Module::Evaluate(Local<Context> context) {
2296 2416 : auto isolate = reinterpret_cast<i::Isolate*>(context->GetIsolate());
2297 2416 : TRACE_EVENT_CALL_STATS_SCOPED(isolate, "v8", "V8.Execute");
2298 4832 : ENTER_V8(isolate, context, Module, Evaluate, MaybeLocal<Value>(),
2299 : InternalEscapableScope);
2300 1208 : i::HistogramTimerScope execute_timer(isolate->counters()->execute(), true);
2301 1208 : i::AggregatingHistogramTimerScope timer(isolate->counters()->compile_lazy());
2302 : i::TimerEventScope<i::TimerEventExecute> timer_scope(isolate);
2303 :
2304 : i::Handle<i::Module> self = Utils::OpenHandle(this);
2305 : // It's an API error to call Evaluate before Instantiate.
2306 1208 : CHECK_GE(self->status(), i::Module::kInstantiated);
2307 :
2308 : Local<Value> result;
2309 1208 : has_pending_exception = !ToLocal(i::Module::Evaluate(isolate, self), &result);
2310 1208 : RETURN_ON_FAILED_EXECUTION(Value);
2311 2370 : RETURN_ESCAPED(result);
2312 : }
2313 :
2314 : namespace {
2315 :
2316 275508 : i::Compiler::ScriptDetails GetScriptDetails(
2317 : i::Isolate* isolate, Local<Value> resource_name,
2318 : Local<Integer> resource_line_offset, Local<Integer> resource_column_offset,
2319 : Local<Value> source_map_url, Local<PrimitiveArray> host_defined_options) {
2320 : i::Compiler::ScriptDetails script_details;
2321 275508 : if (!resource_name.IsEmpty()) {
2322 140996 : script_details.name_obj = Utils::OpenHandle(*(resource_name));
2323 : }
2324 275508 : if (!resource_line_offset.IsEmpty()) {
2325 : script_details.line_offset =
2326 4809 : static_cast<int>(resource_line_offset->Value());
2327 : }
2328 275508 : if (!resource_column_offset.IsEmpty()) {
2329 : script_details.column_offset =
2330 4788 : static_cast<int>(resource_column_offset->Value());
2331 : }
2332 275508 : script_details.host_defined_options = isolate->factory()->empty_fixed_array();
2333 275508 : if (!host_defined_options.IsEmpty()) {
2334 : script_details.host_defined_options =
2335 6 : Utils::OpenHandle(*(host_defined_options));
2336 : }
2337 275508 : if (!source_map_url.IsEmpty()) {
2338 1011 : script_details.source_map_url = Utils::OpenHandle(*(source_map_url));
2339 : }
2340 275508 : return script_details;
2341 : }
2342 :
2343 : } // namespace
2344 :
2345 262388 : MaybeLocal<UnboundScript> ScriptCompiler::CompileUnboundInternal(
2346 : Isolate* v8_isolate, Source* source, CompileOptions options,
2347 : NoCacheReason no_cache_reason) {
2348 262378 : auto isolate = reinterpret_cast<i::Isolate*>(v8_isolate);
2349 524776 : TRACE_EVENT_CALL_STATS_SCOPED(isolate, "v8", "V8.ScriptCompiler");
2350 1049522 : ENTER_V8_NO_SCRIPT(isolate, v8_isolate->GetCurrentContext(), ScriptCompiler,
2351 : CompileUnbound, MaybeLocal<UnboundScript>(),
2352 : InternalEscapableScope);
2353 :
2354 : i::ScriptData* script_data = nullptr;
2355 262378 : if (options == kConsumeCodeCache) {
2356 : DCHECK(source->cached_data);
2357 : // ScriptData takes care of pointer-aligning the data.
2358 : script_data = new i::ScriptData(source->cached_data->data,
2359 169 : source->cached_data->length);
2360 : }
2361 :
2362 262378 : i::Handle<i::String> str = Utils::OpenHandle(*(source->source_string));
2363 : i::Handle<i::SharedFunctionInfo> result;
2364 787134 : TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("v8.compile"), "V8.CompileScript");
2365 : i::Compiler::ScriptDetails script_details = GetScriptDetails(
2366 : isolate, source->resource_name, source->resource_line_offset,
2367 : source->resource_column_offset, source->source_map_url,
2368 262378 : source->host_defined_options);
2369 : i::MaybeHandle<i::SharedFunctionInfo> maybe_function_info =
2370 : i::Compiler::GetSharedFunctionInfoForScript(
2371 : isolate, str, script_details, source->resource_options, nullptr,
2372 262378 : script_data, options, no_cache_reason, i::NOT_NATIVES_CODE);
2373 262378 : if (options == kConsumeCodeCache) {
2374 338 : source->cached_data->rejected = script_data->rejected();
2375 : }
2376 262547 : delete script_data;
2377 : has_pending_exception = !maybe_function_info.ToHandle(&result);
2378 262378 : RETURN_ON_FAILED_EXECUTION(UnboundScript);
2379 523252 : RETURN_ESCAPED(ToApiHandle<UnboundScript>(result));
2380 : }
2381 :
2382 9209 : MaybeLocal<UnboundScript> ScriptCompiler::CompileUnboundScript(
2383 : Isolate* v8_isolate, Source* source, CompileOptions options,
2384 : NoCacheReason no_cache_reason) {
2385 : Utils::ApiCheck(
2386 : !source->GetResourceOptions().IsModule(),
2387 : "v8::ScriptCompiler::CompileUnboundScript",
2388 : "v8::ScriptCompiler::CompileModule must be used to compile modules");
2389 9209 : return CompileUnboundInternal(v8_isolate, source, options, no_cache_reason);
2390 : }
2391 :
2392 251450 : MaybeLocal<Script> ScriptCompiler::Compile(Local<Context> context,
2393 : Source* source,
2394 : CompileOptions options,
2395 : NoCacheReason no_cache_reason) {
2396 : Utils::ApiCheck(
2397 : !source->GetResourceOptions().IsModule(), "v8::ScriptCompiler::Compile",
2398 : "v8::ScriptCompiler::CompileModule must be used to compile modules");
2399 251450 : auto isolate = context->GetIsolate();
2400 : auto maybe =
2401 251450 : CompileUnboundInternal(isolate, source, options, no_cache_reason);
2402 : Local<UnboundScript> result;
2403 251450 : if (!maybe.ToLocal(&result)) return MaybeLocal<Script>();
2404 : v8::Context::Scope scope(context);
2405 250013 : return result->BindToCurrentContext();
2406 : }
2407 :
2408 1729 : MaybeLocal<Module> ScriptCompiler::CompileModule(
2409 : Isolate* isolate, Source* source, CompileOptions options,
2410 : NoCacheReason no_cache_reason) {
2411 1729 : CHECK(options == kNoCompileOptions || options == kConsumeCodeCache);
2412 :
2413 : i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
2414 :
2415 : Utils::ApiCheck(source->GetResourceOptions().IsModule(),
2416 : "v8::ScriptCompiler::CompileModule",
2417 : "Invalid ScriptOrigin: is_module must be true");
2418 : auto maybe =
2419 1729 : CompileUnboundInternal(isolate, source, options, no_cache_reason);
2420 : Local<UnboundScript> unbound;
2421 1729 : if (!maybe.ToLocal(&unbound)) return MaybeLocal<Module>();
2422 :
2423 1651 : i::Handle<i::SharedFunctionInfo> shared = Utils::OpenHandle(*unbound);
2424 1651 : return ToApiHandle<Module>(i_isolate->factory()->NewModule(shared));
2425 : }
2426 :
2427 : namespace {
2428 30 : bool IsIdentifier(i::Isolate* isolate, i::Handle<i::String> string) {
2429 30 : string = i::String::Flatten(isolate, string);
2430 : const int length = string->length();
2431 30 : if (length == 0) return false;
2432 90 : if (!i::IsIdentifierStart(string->Get(0))) return false;
2433 : i::DisallowHeapAllocation no_gc;
2434 30 : i::String::FlatContent flat = string->GetFlatContent(no_gc);
2435 30 : if (flat.IsOneByte()) {
2436 : auto vector = flat.ToOneByteVector();
2437 30 : for (int i = 1; i < length; i++) {
2438 105 : if (!i::IsIdentifierPart(vector[i])) return false;
2439 : }
2440 : } else {
2441 : auto vector = flat.ToUC16Vector();
2442 5 : for (int i = 1; i < length; i++) {
2443 15 : if (!i::IsIdentifierPart(vector[i])) return false;
2444 : }
2445 : }
2446 : return true;
2447 : }
2448 : } // anonymous namespace
2449 :
2450 84 : MaybeLocal<Function> ScriptCompiler::CompileFunctionInContext(
2451 : Local<Context> v8_context, Source* source, size_t arguments_count,
2452 : Local<String> arguments[], size_t context_extension_count,
2453 : Local<Object> context_extensions[], CompileOptions options,
2454 : NoCacheReason no_cache_reason) {
2455 336 : PREPARE_FOR_EXECUTION(v8_context, ScriptCompiler, CompileFunctionInContext,
2456 : Function);
2457 252 : TRACE_EVENT_CALL_STATS_SCOPED(isolate, "v8", "V8.ScriptCompiler");
2458 :
2459 : DCHECK(options == CompileOptions::kConsumeCodeCache ||
2460 : options == CompileOptions::kEagerCompile ||
2461 : options == CompileOptions::kNoCompileOptions);
2462 :
2463 : i::Handle<i::Context> context = Utils::OpenHandle(*v8_context);
2464 :
2465 : DCHECK(context->IsNativeContext());
2466 : i::Handle<i::SharedFunctionInfo> outer_info(
2467 168 : context->empty_function()->shared(), isolate);
2468 :
2469 : i::Handle<i::JSFunction> fun;
2470 : i::Handle<i::FixedArray> arguments_list =
2471 84 : isolate->factory()->NewFixedArray(static_cast<int>(arguments_count));
2472 109 : for (int i = 0; i < static_cast<int>(arguments_count); i++) {
2473 30 : i::Handle<i::String> argument = Utils::OpenHandle(*arguments[i]);
2474 30 : if (!IsIdentifier(isolate, argument)) return Local<Function>();
2475 50 : arguments_list->set(i, *argument);
2476 : }
2477 :
2478 24 : for (size_t i = 0; i < context_extension_count; ++i) {
2479 : i::Handle<i::JSReceiver> extension =
2480 24 : Utils::OpenHandle(*context_extensions[i]);
2481 48 : if (!extension->IsJSObject()) return Local<Function>();
2482 : context = isolate->factory()->NewWithContext(
2483 : context,
2484 : i::ScopeInfo::CreateForWithScope(
2485 : isolate,
2486 48 : context->IsNativeContext()
2487 : ? i::Handle<i::ScopeInfo>::null()
2488 : : i::Handle<i::ScopeInfo>(context->scope_info(), isolate)),
2489 53 : extension);
2490 : }
2491 :
2492 : i::Compiler::ScriptDetails script_details = GetScriptDetails(
2493 : isolate, source->resource_name, source->resource_line_offset,
2494 : source->resource_column_offset, source->source_map_url,
2495 79 : source->host_defined_options);
2496 :
2497 : i::ScriptData* script_data = nullptr;
2498 79 : if (options == kConsumeCodeCache) {
2499 : DCHECK(source->cached_data);
2500 : // ScriptData takes care of pointer-aligning the data.
2501 : script_data = new i::ScriptData(source->cached_data->data,
2502 5 : source->cached_data->length);
2503 : }
2504 :
2505 : i::Handle<i::JSFunction> result;
2506 : has_pending_exception =
2507 : !i::Compiler::GetWrappedFunction(
2508 : Utils::OpenHandle(*source->source_string), arguments_list, context,
2509 : script_details, source->resource_options, script_data, options,
2510 : no_cache_reason)
2511 158 : .ToHandle(&result);
2512 79 : if (options == kConsumeCodeCache) {
2513 10 : source->cached_data->rejected = script_data->rejected();
2514 : }
2515 84 : delete script_data;
2516 79 : RETURN_ON_FAILED_EXECUTION(Function);
2517 69 : RETURN_ESCAPED(Utils::CallableToLocal(result));
2518 : }
2519 :
2520 26102 : void ScriptCompiler::ScriptStreamingTask::Run() { data_->task->Run(); }
2521 :
2522 13051 : ScriptCompiler::ScriptStreamingTask* ScriptCompiler::StartStreamingScript(
2523 : Isolate* v8_isolate, StreamedSource* source, CompileOptions options) {
2524 13051 : if (!i::FLAG_script_streaming) {
2525 : return nullptr;
2526 : }
2527 : // We don't support other compile options on streaming background compiles.
2528 : // TODO(rmcilroy): remove CompileOptions from the API.
2529 13051 : CHECK(options == ScriptCompiler::kNoCompileOptions);
2530 13051 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(v8_isolate);
2531 13051 : i::ScriptStreamingData* data = source->impl();
2532 : std::unique_ptr<i::BackgroundCompileTask> task =
2533 13051 : base::make_unique<i::BackgroundCompileTask>(data, isolate);
2534 13051 : data->task = std::move(task);
2535 13051 : return new ScriptCompiler::ScriptStreamingTask(data);
2536 : }
2537 :
2538 13051 : MaybeLocal<Script> ScriptCompiler::Compile(Local<Context> context,
2539 : StreamedSource* v8_source,
2540 : Local<String> full_source_string,
2541 : const ScriptOrigin& origin) {
2542 52204 : PREPARE_FOR_EXECUTION(context, ScriptCompiler, Compile, Script);
2543 39153 : TRACE_EVENT_CALL_STATS_SCOPED(isolate, "v8", "V8.ScriptCompiler");
2544 39153 : TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("v8.compile"),
2545 : "V8.CompileStreamedScript");
2546 :
2547 13051 : i::Handle<i::String> str = Utils::OpenHandle(*(full_source_string));
2548 : i::Compiler::ScriptDetails script_details = GetScriptDetails(
2549 : isolate, origin.ResourceName(), origin.ResourceLineOffset(),
2550 : origin.ResourceColumnOffset(), origin.SourceMapUrl(),
2551 13051 : origin.HostDefinedOptions());
2552 : i::ScriptStreamingData* data = v8_source->impl();
2553 :
2554 : i::MaybeHandle<i::SharedFunctionInfo> maybe_function_info =
2555 : i::Compiler::GetSharedFunctionInfoForStreamedScript(
2556 13051 : isolate, str, script_details, origin.Options(), data);
2557 :
2558 : i::Handle<i::SharedFunctionInfo> result;
2559 13051 : has_pending_exception = !maybe_function_info.ToHandle(&result);
2560 13051 : if (has_pending_exception) isolate->ReportPendingMessages();
2561 :
2562 13051 : RETURN_ON_FAILED_EXECUTION(Script);
2563 :
2564 : Local<UnboundScript> generic = ToApiHandle<UnboundScript>(result);
2565 13041 : if (generic.IsEmpty()) return Local<Script>();
2566 13041 : Local<Script> bound = generic->BindToCurrentContext();
2567 13041 : if (bound.IsEmpty()) return Local<Script>();
2568 13041 : RETURN_ESCAPED(bound);
2569 : }
2570 :
2571 0 : uint32_t ScriptCompiler::CachedDataVersionTag() {
2572 : return static_cast<uint32_t>(base::hash_combine(
2573 0 : internal::Version::Hash(), internal::FlagList::Hash(),
2574 0 : static_cast<uint32_t>(internal::CpuFeatures::SupportedFeatures())));
2575 : }
2576 :
2577 368 : ScriptCompiler::CachedData* ScriptCompiler::CreateCodeCache(
2578 : Local<UnboundScript> unbound_script) {
2579 : i::Handle<i::SharedFunctionInfo> shared =
2580 : i::Handle<i::SharedFunctionInfo>::cast(
2581 368 : Utils::OpenHandle(*unbound_script));
2582 : DCHECK(shared->is_toplevel());
2583 368 : return i::CodeSerializer::Serialize(shared);
2584 : }
2585 :
2586 : // static
2587 10 : ScriptCompiler::CachedData* ScriptCompiler::CreateCodeCache(
2588 : Local<UnboundModuleScript> unbound_module_script) {
2589 : i::Handle<i::SharedFunctionInfo> shared =
2590 : i::Handle<i::SharedFunctionInfo>::cast(
2591 10 : Utils::OpenHandle(*unbound_module_script));
2592 : DCHECK(shared->is_toplevel());
2593 10 : return i::CodeSerializer::Serialize(shared);
2594 : }
2595 :
2596 5 : ScriptCompiler::CachedData* ScriptCompiler::CreateCodeCacheForFunction(
2597 : Local<Function> function) {
2598 : auto js_function =
2599 5 : i::Handle<i::JSFunction>::cast(Utils::OpenHandle(*function));
2600 : i::Handle<i::SharedFunctionInfo> shared(js_function->shared(),
2601 10 : js_function->GetIsolate());
2602 5 : CHECK(shared->is_wrapped());
2603 5 : return i::CodeSerializer::Serialize(shared);
2604 : }
2605 :
2606 130355 : MaybeLocal<Script> Script::Compile(Local<Context> context, Local<String> source,
2607 : ScriptOrigin* origin) {
2608 130355 : if (origin) {
2609 : ScriptCompiler::Source script_source(source, *origin);
2610 5090 : return ScriptCompiler::Compile(context, &script_source);
2611 : }
2612 : ScriptCompiler::Source script_source(source);
2613 125265 : return ScriptCompiler::Compile(context, &script_source);
2614 : }
2615 :
2616 :
2617 : // --- E x c e p t i o n s ---
2618 :
2619 20903213 : v8::TryCatch::TryCatch(v8::Isolate* isolate)
2620 : : isolate_(reinterpret_cast<i::Isolate*>(isolate)),
2621 : next_(isolate_->try_catch_handler()),
2622 : is_verbose_(false),
2623 : can_continue_(true),
2624 : capture_message_(true),
2625 : rethrow_(false),
2626 41806426 : has_terminated_(false) {
2627 20903213 : ResetInternal();
2628 : // Special handling for simulators which have a separate JS stack.
2629 : js_stack_comparable_address_ = reinterpret_cast<void*>(
2630 20903211 : i::SimulatorStack::RegisterJSStackComparableAddress(isolate_));
2631 20903211 : isolate_->RegisterTryCatchHandler(this);
2632 20903212 : }
2633 :
2634 :
2635 20903209 : v8::TryCatch::~TryCatch() {
2636 20903209 : if (rethrow_) {
2637 121 : v8::Isolate* isolate = reinterpret_cast<Isolate*>(isolate_);
2638 : v8::HandleScope scope(isolate);
2639 242 : v8::Local<v8::Value> exc = v8::Local<v8::Value>::New(isolate, Exception());
2640 121 : if (HasCaught() && capture_message_) {
2641 : // If an exception was caught and rethrow_ is indicated, the saved
2642 : // message, script, and location need to be restored to Isolate TLS
2643 : // for reuse. capture_message_ needs to be disabled so that Throw()
2644 : // does not create a new message.
2645 121 : isolate_->thread_local_top()->rethrowing_message_ = true;
2646 121 : isolate_->RestorePendingMessageFromTryCatch(this);
2647 : }
2648 121 : isolate_->UnregisterTryCatchHandler(this);
2649 : i::SimulatorStack::UnregisterJSStackComparableAddress(isolate_);
2650 121 : reinterpret_cast<Isolate*>(isolate_)->ThrowException(exc);
2651 : DCHECK(!isolate_->thread_local_top()->rethrowing_message_);
2652 : } else {
2653 20903088 : if (HasCaught() && isolate_->has_scheduled_exception()) {
2654 : // If an exception was caught but is still scheduled because no API call
2655 : // promoted it, then it is canceled to prevent it from being propagated.
2656 : // Note that this will not cancel termination exceptions.
2657 250 : isolate_->CancelScheduledExceptionFromTryCatch(this);
2658 : }
2659 20903086 : isolate_->UnregisterTryCatchHandler(this);
2660 : i::SimulatorStack::UnregisterJSStackComparableAddress(isolate_);
2661 : }
2662 20903204 : }
2663 :
2664 0 : void* v8::TryCatch::operator new(size_t) { base::OS::Abort(); }
2665 0 : void* v8::TryCatch::operator new[](size_t) { base::OS::Abort(); }
2666 0 : void v8::TryCatch::operator delete(void*, size_t) { base::OS::Abort(); }
2667 0 : void v8::TryCatch::operator delete[](void*, size_t) { base::OS::Abort(); }
2668 :
2669 21073101 : bool v8::TryCatch::HasCaught() const {
2670 : return !i::Object(reinterpret_cast<i::Address>(exception_))
2671 42146211 : ->IsTheHole(isolate_);
2672 : }
2673 :
2674 :
2675 1650 : bool v8::TryCatch::CanContinue() const {
2676 1650 : return can_continue_;
2677 : }
2678 :
2679 :
2680 21475 : bool v8::TryCatch::HasTerminated() const {
2681 21475 : return has_terminated_;
2682 : }
2683 :
2684 :
2685 121 : v8::Local<v8::Value> v8::TryCatch::ReThrow() {
2686 121 : if (!HasCaught()) return v8::Local<v8::Value>();
2687 121 : rethrow_ = true;
2688 242 : return v8::Undefined(reinterpret_cast<v8::Isolate*>(isolate_));
2689 : }
2690 :
2691 :
2692 9585 : v8::Local<Value> v8::TryCatch::Exception() const {
2693 9585 : if (HasCaught()) {
2694 : // Check for out of memory exception.
2695 9585 : i::Object exception(reinterpret_cast<i::Address>(exception_));
2696 9585 : return v8::Utils::ToLocal(i::Handle<i::Object>(exception, isolate_));
2697 : } else {
2698 0 : return v8::Local<Value>();
2699 : }
2700 : }
2701 :
2702 :
2703 5414 : MaybeLocal<Value> v8::TryCatch::StackTrace(Local<Context> context) const {
2704 5414 : if (!HasCaught()) return v8::Local<Value>();
2705 5414 : i::Object raw_obj(reinterpret_cast<i::Address>(exception_));
2706 5414 : if (!raw_obj->IsJSObject()) return v8::Local<Value>();
2707 5780 : PREPARE_FOR_EXECUTION(context, TryCatch, StackTrace, Value);
2708 1445 : i::Handle<i::JSObject> obj(i::JSObject::cast(raw_obj), isolate_);
2709 : i::Handle<i::String> name = isolate->factory()->stack_string();
2710 1445 : Maybe<bool> maybe = i::JSReceiver::HasProperty(obj, name);
2711 1445 : has_pending_exception = maybe.IsNothing();
2712 1445 : RETURN_ON_FAILED_EXECUTION(Value);
2713 1445 : if (!maybe.FromJust()) return v8::Local<Value>();
2714 : Local<Value> result;
2715 : has_pending_exception =
2716 2890 : !ToLocal<Value>(i::JSReceiver::GetProperty(isolate, obj, name), &result);
2717 1445 : RETURN_ON_FAILED_EXECUTION(Value);
2718 1445 : RETURN_ESCAPED(result);
2719 : }
2720 :
2721 :
2722 7596 : v8::Local<v8::Message> v8::TryCatch::Message() const {
2723 7596 : i::Object message(reinterpret_cast<i::Address>(message_obj_));
2724 : DCHECK(message->IsJSMessageObject() || message->IsTheHole(isolate_));
2725 15192 : if (HasCaught() && !message->IsTheHole(isolate_)) {
2726 7536 : return v8::Utils::MessageToLocal(i::Handle<i::Object>(message, isolate_));
2727 : } else {
2728 60 : return v8::Local<v8::Message>();
2729 : }
2730 : }
2731 :
2732 :
2733 714 : void v8::TryCatch::Reset() {
2734 714 : if (!rethrow_ && HasCaught() && isolate_->has_scheduled_exception()) {
2735 : // If an exception was caught but is still scheduled because no API call
2736 : // promoted it, then it is canceled to prevent it from being propagated.
2737 : // Note that this will not cancel termination exceptions.
2738 5 : isolate_->CancelScheduledExceptionFromTryCatch(this);
2739 : }
2740 714 : ResetInternal();
2741 714 : }
2742 :
2743 :
2744 20903926 : void v8::TryCatch::ResetInternal() {
2745 20903926 : i::Object the_hole = i::ReadOnlyRoots(isolate_).the_hole_value();
2746 20903925 : exception_ = reinterpret_cast<void*>(the_hole->ptr());
2747 20903925 : message_obj_ = reinterpret_cast<void*>(the_hole->ptr());
2748 20903925 : }
2749 :
2750 :
2751 182979 : void v8::TryCatch::SetVerbose(bool value) {
2752 182979 : is_verbose_ = value;
2753 182979 : }
2754 :
2755 0 : bool v8::TryCatch::IsVerbose() const { return is_verbose_; }
2756 :
2757 60051 : void v8::TryCatch::SetCaptureMessage(bool value) {
2758 60051 : capture_message_ = value;
2759 60051 : }
2760 :
2761 :
2762 : // --- M e s s a g e ---
2763 :
2764 :
2765 3021 : Local<String> Message::Get() const {
2766 : i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
2767 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(isolate);
2768 3021 : EscapableHandleScope scope(reinterpret_cast<Isolate*>(isolate));
2769 : i::Handle<i::Object> obj = Utils::OpenHandle(this);
2770 3021 : i::Handle<i::String> raw_result = i::MessageHandler::GetMessage(isolate, obj);
2771 : Local<String> result = Utils::ToLocal(raw_result);
2772 3021 : return scope.Escape(result);
2773 : }
2774 :
2775 50 : v8::Isolate* Message::GetIsolate() const {
2776 : i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
2777 50 : return reinterpret_cast<Isolate*>(isolate);
2778 : }
2779 :
2780 14235 : ScriptOrigin Message::GetScriptOrigin() const {
2781 : i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
2782 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(isolate);
2783 14235 : auto message = i::Handle<i::JSMessageObject>::cast(Utils::OpenHandle(this));
2784 28470 : i::Handle<i::Script> script(message->script(), isolate);
2785 28470 : return GetScriptOriginForScript(isolate, script);
2786 : }
2787 :
2788 :
2789 0 : v8::Local<Value> Message::GetScriptResourceName() const {
2790 0 : return GetScriptOrigin().ResourceName();
2791 : }
2792 :
2793 :
2794 2064 : v8::Local<v8::StackTrace> Message::GetStackTrace() const {
2795 : i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
2796 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(isolate);
2797 2064 : EscapableHandleScope scope(reinterpret_cast<Isolate*>(isolate));
2798 2064 : auto message = i::Handle<i::JSMessageObject>::cast(Utils::OpenHandle(this));
2799 4128 : i::Handle<i::Object> stackFramesObj(message->stack_frames(), isolate);
2800 4128 : if (!stackFramesObj->IsFixedArray()) return v8::Local<v8::StackTrace>();
2801 241 : auto stackTrace = i::Handle<i::FixedArray>::cast(stackFramesObj);
2802 : return scope.Escape(Utils::StackTraceToLocal(stackTrace));
2803 : }
2804 :
2805 :
2806 8261 : Maybe<int> Message::GetLineNumber(Local<Context> context) const {
2807 : auto self = Utils::OpenHandle(this);
2808 : i::Isolate* isolate = self->GetIsolate();
2809 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(isolate);
2810 8261 : EscapableHandleScope handle_scope(reinterpret_cast<Isolate*>(isolate));
2811 8261 : auto msg = i::Handle<i::JSMessageObject>::cast(self);
2812 16522 : return Just(msg->GetLineNumber());
2813 : }
2814 :
2815 :
2816 33 : int Message::GetStartPosition() const {
2817 : auto self = Utils::OpenHandle(this);
2818 33 : return self->start_position();
2819 : }
2820 :
2821 :
2822 18 : int Message::GetEndPosition() const {
2823 : auto self = Utils::OpenHandle(this);
2824 18 : return self->end_position();
2825 : }
2826 :
2827 22136 : int Message::ErrorLevel() const {
2828 : auto self = Utils::OpenHandle(this);
2829 22136 : return self->error_level();
2830 : }
2831 :
2832 7825 : int Message::GetStartColumn() const {
2833 : auto self = Utils::OpenHandle(this);
2834 : i::Isolate* isolate = self->GetIsolate();
2835 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(isolate);
2836 7825 : EscapableHandleScope handle_scope(reinterpret_cast<Isolate*>(isolate));
2837 7825 : auto msg = i::Handle<i::JSMessageObject>::cast(self);
2838 15650 : return msg->GetColumnNumber();
2839 : }
2840 :
2841 7825 : Maybe<int> Message::GetStartColumn(Local<Context> context) const {
2842 7825 : return Just(GetStartColumn());
2843 : }
2844 :
2845 5412 : int Message::GetEndColumn() const {
2846 : auto self = Utils::OpenHandle(this);
2847 : i::Isolate* isolate = self->GetIsolate();
2848 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(isolate);
2849 5412 : EscapableHandleScope handle_scope(reinterpret_cast<Isolate*>(isolate));
2850 5412 : auto msg = i::Handle<i::JSMessageObject>::cast(self);
2851 5412 : const int column_number = msg->GetColumnNumber();
2852 5412 : if (column_number == -1) return -1;
2853 : const int start = self->start_position();
2854 : const int end = self->end_position();
2855 5412 : return column_number + (end - start);
2856 : }
2857 :
2858 5412 : Maybe<int> Message::GetEndColumn(Local<Context> context) const {
2859 5412 : return Just(GetEndColumn());
2860 : }
2861 :
2862 :
2863 131 : bool Message::IsSharedCrossOrigin() const {
2864 : i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
2865 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(isolate);
2866 : return Utils::OpenHandle(this)
2867 262 : ->script()
2868 262 : ->origin_options()
2869 393 : .IsSharedCrossOrigin();
2870 : }
2871 :
2872 0 : bool Message::IsOpaque() const {
2873 : i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
2874 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(isolate);
2875 0 : return Utils::OpenHandle(this)->script()->origin_options().IsOpaque();
2876 : }
2877 :
2878 :
2879 5423 : MaybeLocal<String> Message::GetSourceLine(Local<Context> context) const {
2880 : auto self = Utils::OpenHandle(this);
2881 : i::Isolate* isolate = self->GetIsolate();
2882 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(isolate);
2883 5423 : EscapableHandleScope handle_scope(reinterpret_cast<Isolate*>(isolate));
2884 5423 : auto msg = i::Handle<i::JSMessageObject>::cast(self);
2885 16269 : RETURN_ESCAPED(Utils::ToLocal(msg->GetSourceLine()));
2886 : }
2887 :
2888 :
2889 0 : void Message::PrintCurrentStackTrace(Isolate* isolate, FILE* out) {
2890 : i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
2891 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(i_isolate);
2892 0 : i_isolate->PrintCurrentStackTrace(out);
2893 0 : }
2894 :
2895 :
2896 : // --- S t a c k T r a c e ---
2897 :
2898 66769 : Local<StackFrame> StackTrace::GetFrame(Isolate* v8_isolate,
2899 : uint32_t index) const {
2900 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(v8_isolate);
2901 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(isolate);
2902 66769 : EscapableHandleScope scope(v8_isolate);
2903 133538 : auto obj = handle(Utils::OpenHandle(this)->get(index), isolate);
2904 66769 : auto info = i::Handle<i::StackFrameInfo>::cast(obj);
2905 66769 : return scope.Escape(Utils::StackFrameToLocal(info));
2906 : }
2907 :
2908 129334 : int StackTrace::GetFrameCount() const {
2909 129334 : return Utils::OpenHandle(this)->length();
2910 : }
2911 :
2912 :
2913 68753 : Local<StackTrace> StackTrace::CurrentStackTrace(
2914 : Isolate* isolate,
2915 : int frame_limit,
2916 : StackTraceOptions options) {
2917 : i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
2918 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(i_isolate);
2919 : i::Handle<i::FixedArray> stackTrace =
2920 68753 : i_isolate->CaptureCurrentStackTrace(frame_limit, options);
2921 68753 : return Utils::StackTraceToLocal(stackTrace);
2922 : }
2923 :
2924 :
2925 : // --- S t a c k F r a m e ---
2926 :
2927 64964 : int StackFrame::GetLineNumber() const {
2928 : int v = Utils::OpenHandle(this)->line_number();
2929 64964 : return v ? v : Message::kNoLineNumberInfo;
2930 : }
2931 :
2932 :
2933 64929 : int StackFrame::GetColumn() const {
2934 : int v = Utils::OpenHandle(this)->column_number();
2935 64929 : return v ? v : Message::kNoLineNumberInfo;
2936 : }
2937 :
2938 :
2939 64374 : int StackFrame::GetScriptId() const {
2940 : int v = Utils::OpenHandle(this)->script_id();
2941 64374 : return v ? v : Message::kNoScriptIdInfo;
2942 : }
2943 :
2944 64639 : Local<String> StackFrame::GetScriptName() const {
2945 : i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
2946 64639 : EscapableHandleScope scope(reinterpret_cast<Isolate*>(isolate));
2947 : i::Handle<i::StackFrameInfo> self = Utils::OpenHandle(this);
2948 129278 : i::Handle<i::Object> obj(self->script_name(), isolate);
2949 129278 : return obj->IsString()
2950 : ? scope.Escape(Local<String>::Cast(Utils::ToLocal(obj)))
2951 192918 : : Local<String>();
2952 : }
2953 :
2954 :
2955 128688 : Local<String> StackFrame::GetScriptNameOrSourceURL() const {
2956 : i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
2957 128688 : EscapableHandleScope scope(reinterpret_cast<Isolate*>(isolate));
2958 : i::Handle<i::StackFrameInfo> self = Utils::OpenHandle(this);
2959 257376 : i::Handle<i::Object> obj(self->script_name_or_source_url(), isolate);
2960 257376 : return obj->IsString()
2961 : ? scope.Escape(Local<String>::Cast(Utils::ToLocal(obj)))
2962 384726 : : Local<String>();
2963 : }
2964 :
2965 :
2966 64894 : Local<String> StackFrame::GetFunctionName() const {
2967 : i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
2968 64894 : EscapableHandleScope scope(reinterpret_cast<Isolate*>(isolate));
2969 : i::Handle<i::StackFrameInfo> self = Utils::OpenHandle(this);
2970 129788 : i::Handle<i::Object> obj(self->function_name(), isolate);
2971 129788 : return obj->IsString()
2972 : ? scope.Escape(Local<String>::Cast(Utils::ToLocal(obj)))
2973 194682 : : Local<String>();
2974 : }
2975 :
2976 200 : bool StackFrame::IsEval() const { return Utils::OpenHandle(this)->is_eval(); }
2977 :
2978 100 : bool StackFrame::IsConstructor() const {
2979 100 : return Utils::OpenHandle(this)->is_constructor();
2980 : }
2981 :
2982 128598 : bool StackFrame::IsWasm() const { return Utils::OpenHandle(this)->is_wasm(); }
2983 :
2984 :
2985 : // --- J S O N ---
2986 :
2987 135 : MaybeLocal<Value> JSON::Parse(Local<Context> context,
2988 : Local<String> json_string) {
2989 540 : PREPARE_FOR_EXECUTION(context, JSON, Parse, Value);
2990 135 : i::Handle<i::String> string = Utils::OpenHandle(*json_string);
2991 135 : i::Handle<i::String> source = i::String::Flatten(isolate, string);
2992 : i::Handle<i::Object> undefined = isolate->factory()->undefined_value();
2993 270 : auto maybe = source->IsSeqOneByteString()
2994 : ? i::JsonParser<true>::Parse(isolate, source, undefined)
2995 135 : : i::JsonParser<false>::Parse(isolate, source, undefined);
2996 : Local<Value> result;
2997 : has_pending_exception = !ToLocal<Value>(maybe, &result);
2998 135 : RETURN_ON_FAILED_EXECUTION(Value);
2999 134 : RETURN_ESCAPED(result);
3000 : }
3001 :
3002 109 : MaybeLocal<String> JSON::Stringify(Local<Context> context,
3003 : Local<Value> json_object,
3004 : Local<String> gap) {
3005 436 : PREPARE_FOR_EXECUTION(context, JSON, Stringify, String);
3006 109 : i::Handle<i::Object> object = Utils::OpenHandle(*json_object);
3007 : i::Handle<i::Object> replacer = isolate->factory()->undefined_value();
3008 : i::Handle<i::String> gap_string = gap.IsEmpty()
3009 : ? isolate->factory()->empty_string()
3010 109 : : Utils::OpenHandle(*gap);
3011 : i::Handle<i::Object> maybe;
3012 : has_pending_exception =
3013 218 : !i::JsonStringify(isolate, object, replacer, gap_string).ToHandle(&maybe);
3014 109 : RETURN_ON_FAILED_EXECUTION(String);
3015 : Local<String> result;
3016 : has_pending_exception =
3017 109 : !ToLocal<String>(i::Object::ToString(isolate, maybe), &result);
3018 109 : RETURN_ON_FAILED_EXECUTION(String);
3019 109 : RETURN_ESCAPED(result);
3020 : }
3021 :
3022 : // --- V a l u e S e r i a l i z a t i o n ---
3023 :
3024 0 : Maybe<bool> ValueSerializer::Delegate::WriteHostObject(Isolate* v8_isolate,
3025 : Local<Object> object) {
3026 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(v8_isolate);
3027 : isolate->ScheduleThrow(*isolate->factory()->NewError(
3028 : isolate->error_function(), i::MessageTemplate::kDataCloneError,
3029 0 : Utils::OpenHandle(*object)));
3030 0 : return Nothing<bool>();
3031 : }
3032 :
3033 0 : Maybe<uint32_t> ValueSerializer::Delegate::GetSharedArrayBufferId(
3034 : Isolate* v8_isolate, Local<SharedArrayBuffer> shared_array_buffer) {
3035 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(v8_isolate);
3036 : isolate->ScheduleThrow(*isolate->factory()->NewError(
3037 : isolate->error_function(), i::MessageTemplate::kDataCloneError,
3038 0 : Utils::OpenHandle(*shared_array_buffer)));
3039 0 : return Nothing<uint32_t>();
3040 : }
3041 :
3042 0 : Maybe<uint32_t> ValueSerializer::Delegate::GetWasmModuleTransferId(
3043 : Isolate* v8_isolate, Local<WasmModuleObject> module) {
3044 0 : return Nothing<uint32_t>();
3045 : }
3046 :
3047 20 : void* ValueSerializer::Delegate::ReallocateBufferMemory(void* old_buffer,
3048 : size_t size,
3049 : size_t* actual_size) {
3050 20 : *actual_size = size;
3051 20 : return realloc(old_buffer, size);
3052 : }
3053 :
3054 20 : void ValueSerializer::Delegate::FreeBufferMemory(void* buffer) {
3055 20 : return free(buffer);
3056 : }
3057 :
3058 2043 : struct ValueSerializer::PrivateData {
3059 : explicit PrivateData(i::Isolate* i, ValueSerializer::Delegate* delegate)
3060 2047 : : isolate(i), serializer(i, delegate) {}
3061 : i::Isolate* isolate;
3062 : i::ValueSerializer serializer;
3063 : };
3064 :
3065 0 : ValueSerializer::ValueSerializer(Isolate* isolate)
3066 0 : : ValueSerializer(isolate, nullptr) {}
3067 :
3068 2035 : ValueSerializer::ValueSerializer(Isolate* isolate, Delegate* delegate)
3069 : : private_(
3070 4071 : new PrivateData(reinterpret_cast<i::Isolate*>(isolate), delegate)) {}
3071 :
3072 4084 : ValueSerializer::~ValueSerializer() { delete private_; }
3073 :
3074 1874 : void ValueSerializer::WriteHeader() { private_->serializer.WriteHeader(); }
3075 :
3076 1 : void ValueSerializer::SetTreatArrayBufferViewsAsHostObjects(bool mode) {
3077 1 : private_->serializer.SetTreatArrayBufferViewsAsHostObjects(mode);
3078 1 : }
3079 :
3080 1865 : Maybe<bool> ValueSerializer::WriteValue(Local<Context> context,
3081 : Local<Value> value) {
3082 3745 : auto isolate = reinterpret_cast<i::Isolate*>(context->GetIsolate());
3083 7489 : ENTER_V8(isolate, context, ValueSerializer, WriteValue, Nothing<bool>(),
3084 : i::HandleScope);
3085 1880 : i::Handle<i::Object> object = Utils::OpenHandle(*value);
3086 1880 : Maybe<bool> result = private_->serializer.WriteObject(object);
3087 1860 : has_pending_exception = result.IsNothing();
3088 1860 : RETURN_ON_FAILED_EXECUTION_PRIMITIVE(bool);
3089 1832 : return result;
3090 : }
3091 :
3092 1824 : std::pair<uint8_t*, size_t> ValueSerializer::Release() {
3093 1824 : return private_->serializer.Release();
3094 : }
3095 :
3096 47 : void ValueSerializer::TransferArrayBuffer(uint32_t transfer_id,
3097 : Local<ArrayBuffer> array_buffer) {
3098 : private_->serializer.TransferArrayBuffer(transfer_id,
3099 47 : Utils::OpenHandle(*array_buffer));
3100 47 : }
3101 :
3102 2 : void ValueSerializer::WriteUint32(uint32_t value) {
3103 2 : private_->serializer.WriteUint32(value);
3104 2 : }
3105 :
3106 2 : void ValueSerializer::WriteUint64(uint64_t value) {
3107 2 : private_->serializer.WriteUint64(value);
3108 2 : }
3109 :
3110 4 : void ValueSerializer::WriteDouble(double value) {
3111 4 : private_->serializer.WriteDouble(value);
3112 4 : }
3113 :
3114 15 : void ValueSerializer::WriteRawBytes(const void* source, size_t length) {
3115 15 : private_->serializer.WriteRawBytes(source, length);
3116 15 : }
3117 :
3118 0 : MaybeLocal<Object> ValueDeserializer::Delegate::ReadHostObject(
3119 : Isolate* v8_isolate) {
3120 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(v8_isolate);
3121 : isolate->ScheduleThrow(*isolate->factory()->NewError(
3122 : isolate->error_function(),
3123 0 : i::MessageTemplate::kDataCloneDeserializationError));
3124 0 : return MaybeLocal<Object>();
3125 : }
3126 :
3127 1 : MaybeLocal<WasmModuleObject> ValueDeserializer::Delegate::GetWasmModuleFromId(
3128 : Isolate* v8_isolate, uint32_t id) {
3129 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(v8_isolate);
3130 : isolate->ScheduleThrow(*isolate->factory()->NewError(
3131 : isolate->error_function(),
3132 2 : i::MessageTemplate::kDataCloneDeserializationError));
3133 1 : return MaybeLocal<WasmModuleObject>();
3134 : }
3135 :
3136 : MaybeLocal<SharedArrayBuffer>
3137 0 : ValueDeserializer::Delegate::GetSharedArrayBufferFromId(Isolate* v8_isolate,
3138 : uint32_t id) {
3139 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(v8_isolate);
3140 : isolate->ScheduleThrow(*isolate->factory()->NewError(
3141 : isolate->error_function(),
3142 0 : i::MessageTemplate::kDataCloneDeserializationError));
3143 0 : return MaybeLocal<SharedArrayBuffer>();
3144 : }
3145 :
3146 1935 : struct ValueDeserializer::PrivateData {
3147 : PrivateData(i::Isolate* i, i::Vector<const uint8_t> data, Delegate* delegate)
3148 1935 : : isolate(i), deserializer(i, data, delegate) {}
3149 : i::Isolate* isolate;
3150 : i::ValueDeserializer deserializer;
3151 : bool has_aborted = false;
3152 : bool supports_legacy_wire_format = false;
3153 : };
3154 :
3155 0 : ValueDeserializer::ValueDeserializer(Isolate* isolate, const uint8_t* data,
3156 : size_t size)
3157 0 : : ValueDeserializer(isolate, data, size, nullptr) {}
3158 :
3159 1935 : ValueDeserializer::ValueDeserializer(Isolate* isolate, const uint8_t* data,
3160 : size_t size, Delegate* delegate) {
3161 1935 : if (base::IsValueInRangeForNumericType<int>(size)) {
3162 : private_ = new PrivateData(
3163 : reinterpret_cast<i::Isolate*>(isolate),
3164 3870 : i::Vector<const uint8_t>(data, static_cast<int>(size)), delegate);
3165 : } else {
3166 : private_ = new PrivateData(reinterpret_cast<i::Isolate*>(isolate),
3167 0 : i::Vector<const uint8_t>(nullptr, 0), nullptr);
3168 0 : private_->has_aborted = true;
3169 : }
3170 1935 : }
3171 :
3172 3870 : ValueDeserializer::~ValueDeserializer() { delete private_; }
3173 :
3174 1935 : Maybe<bool> ValueDeserializer::ReadHeader(Local<Context> context) {
3175 3869 : auto isolate = reinterpret_cast<i::Isolate*>(context->GetIsolate());
3176 7736 : ENTER_V8_NO_SCRIPT(isolate, context, ValueDeserializer, ReadHeader,
3177 : Nothing<bool>(), i::HandleScope);
3178 :
3179 : // We could have aborted during the constructor.
3180 : // If so, ReadHeader is where we report it.
3181 1934 : if (private_->has_aborted) {
3182 : isolate->Throw(*isolate->factory()->NewError(
3183 0 : i::MessageTemplate::kDataCloneDeserializationError));
3184 : has_pending_exception = true;
3185 0 : RETURN_ON_FAILED_EXECUTION_PRIMITIVE(bool);
3186 : }
3187 :
3188 : bool read_header = false;
3189 3868 : has_pending_exception = !private_->deserializer.ReadHeader().To(&read_header);
3190 1934 : RETURN_ON_FAILED_EXECUTION_PRIMITIVE(bool);
3191 : DCHECK(read_header);
3192 :
3193 : static const uint32_t kMinimumNonLegacyVersion = 13;
3194 2052 : if (GetWireFormatVersion() < kMinimumNonLegacyVersion &&
3195 120 : !private_->supports_legacy_wire_format) {
3196 : isolate->Throw(*isolate->factory()->NewError(
3197 0 : i::MessageTemplate::kDataCloneDeserializationVersionError));
3198 : has_pending_exception = true;
3199 0 : RETURN_ON_FAILED_EXECUTION_PRIMITIVE(bool);
3200 : }
3201 :
3202 : return Just(true);
3203 : }
3204 :
3205 1935 : void ValueDeserializer::SetSupportsLegacyWireFormat(
3206 : bool supports_legacy_wire_format) {
3207 1935 : private_->supports_legacy_wire_format = supports_legacy_wire_format;
3208 1935 : }
3209 :
3210 262 : void ValueDeserializer::SetExpectInlineWasm(bool expect_inline_wasm) {
3211 262 : private_->deserializer.set_expect_inline_wasm(expect_inline_wasm);
3212 262 : }
3213 :
3214 3872 : uint32_t ValueDeserializer::GetWireFormatVersion() const {
3215 3872 : CHECK(!private_->has_aborted);
3216 3872 : return private_->deserializer.GetWireFormatVersion();
3217 : }
3218 :
3219 1932 : MaybeLocal<Value> ValueDeserializer::ReadValue(Local<Context> context) {
3220 1932 : CHECK(!private_->has_aborted);
3221 7729 : PREPARE_FOR_EXECUTION(context, ValueDeserializer, ReadValue, Value);
3222 : i::MaybeHandle<i::Object> result;
3223 1932 : if (GetWireFormatVersion() > 0) {
3224 1920 : result = private_->deserializer.ReadObject();
3225 : } else {
3226 : result =
3227 12 : private_->deserializer.ReadObjectUsingEntireBufferForLegacyFormat();
3228 : }
3229 : Local<Value> value;
3230 : has_pending_exception = !ToLocal(result, &value);
3231 1933 : RETURN_ON_FAILED_EXECUTION(Value);
3232 1904 : RETURN_ESCAPED(value);
3233 : }
3234 :
3235 29 : void ValueDeserializer::TransferArrayBuffer(uint32_t transfer_id,
3236 : Local<ArrayBuffer> array_buffer) {
3237 29 : CHECK(!private_->has_aborted);
3238 : private_->deserializer.TransferArrayBuffer(transfer_id,
3239 29 : Utils::OpenHandle(*array_buffer));
3240 29 : }
3241 :
3242 0 : void ValueDeserializer::TransferSharedArrayBuffer(
3243 : uint32_t transfer_id, Local<SharedArrayBuffer> shared_array_buffer) {
3244 0 : CHECK(!private_->has_aborted);
3245 : private_->deserializer.TransferArrayBuffer(
3246 0 : transfer_id, Utils::OpenHandle(*shared_array_buffer));
3247 0 : }
3248 :
3249 2 : bool ValueDeserializer::ReadUint32(uint32_t* value) {
3250 2 : return private_->deserializer.ReadUint32(value);
3251 : }
3252 :
3253 2 : bool ValueDeserializer::ReadUint64(uint64_t* value) {
3254 2 : return private_->deserializer.ReadUint64(value);
3255 : }
3256 :
3257 4 : bool ValueDeserializer::ReadDouble(double* value) {
3258 4 : return private_->deserializer.ReadDouble(value);
3259 : }
3260 :
3261 13 : bool ValueDeserializer::ReadRawBytes(size_t length, const void** data) {
3262 13 : return private_->deserializer.ReadRawBytes(length, data);
3263 : }
3264 :
3265 : // --- D a t a ---
3266 :
3267 0 : bool Value::FullIsUndefined() const {
3268 : i::Handle<i::Object> object = Utils::OpenHandle(this);
3269 0 : bool result = object->IsUndefined();
3270 : DCHECK_EQ(result, QuickIsUndefined());
3271 0 : return result;
3272 : }
3273 :
3274 :
3275 0 : bool Value::FullIsNull() const {
3276 : i::Handle<i::Object> object = Utils::OpenHandle(this);
3277 0 : bool result = object->IsNull();
3278 : DCHECK_EQ(result, QuickIsNull());
3279 0 : return result;
3280 : }
3281 :
3282 :
3283 64525 : bool Value::IsTrue() const {
3284 : i::Handle<i::Object> object = Utils::OpenHandle(this);
3285 129050 : if (object->IsSmi()) return false;
3286 129050 : return object->IsTrue();
3287 : }
3288 :
3289 :
3290 118 : bool Value::IsFalse() const {
3291 : i::Handle<i::Object> object = Utils::OpenHandle(this);
3292 236 : if (object->IsSmi()) return false;
3293 236 : return object->IsFalse();
3294 : }
3295 :
3296 :
3297 11169906 : bool Value::IsFunction() const { return Utils::OpenHandle(this)->IsCallable(); }
3298 :
3299 :
3300 16635 : bool Value::IsName() const {
3301 33270 : return Utils::OpenHandle(this)->IsName();
3302 : }
3303 :
3304 :
3305 0 : bool Value::FullIsString() const {
3306 0 : bool result = Utils::OpenHandle(this)->IsString();
3307 : DCHECK_EQ(result, QuickIsString());
3308 0 : return result;
3309 : }
3310 :
3311 :
3312 9282790 : bool Value::IsSymbol() const {
3313 18565578 : return Utils::OpenHandle(this)->IsSymbol();
3314 : }
3315 :
3316 :
3317 1884534 : bool Value::IsArray() const {
3318 3769077 : return Utils::OpenHandle(this)->IsJSArray();
3319 : }
3320 :
3321 :
3322 1298493 : bool Value::IsArrayBuffer() const {
3323 : i::Handle<i::Object> obj = Utils::OpenHandle(this);
3324 2906276 : return obj->IsJSArrayBuffer() && !i::JSArrayBuffer::cast(*obj)->is_shared();
3325 : }
3326 :
3327 :
3328 75 : bool Value::IsArrayBufferView() const {
3329 150 : return Utils::OpenHandle(this)->IsJSArrayBufferView();
3330 : }
3331 :
3332 :
3333 899568 : bool Value::IsTypedArray() const {
3334 1799136 : return Utils::OpenHandle(this)->IsJSTypedArray();
3335 : }
3336 :
3337 : #define VALUE_IS_TYPED_ARRAY(Type, typeName, TYPE, ctype) \
3338 : bool Value::Is##Type##Array() const { \
3339 : i::Handle<i::Object> obj = Utils::OpenHandle(this); \
3340 : return obj->IsJSTypedArray() && \
3341 : i::JSTypedArray::cast(*obj)->type() == i::kExternal##Type##Array; \
3342 : }
3343 :
3344 296 : TYPED_ARRAYS(VALUE_IS_TYPED_ARRAY)
3345 :
3346 : #undef VALUE_IS_TYPED_ARRAY
3347 :
3348 :
3349 895469 : bool Value::IsDataView() const {
3350 1790938 : return Utils::OpenHandle(this)->IsJSDataView();
3351 : }
3352 :
3353 :
3354 1297528 : bool Value::IsSharedArrayBuffer() const {
3355 : i::Handle<i::Object> obj = Utils::OpenHandle(this);
3356 2907167 : return obj->IsJSArrayBuffer() && i::JSArrayBuffer::cast(*obj)->is_shared();
3357 : }
3358 :
3359 :
3360 25002583 : bool Value::IsObject() const { return Utils::OpenHandle(this)->IsJSReceiver(); }
3361 :
3362 :
3363 3689954 : bool Value::IsNumber() const {
3364 7379908 : return Utils::OpenHandle(this)->IsNumber();
3365 : }
3366 :
3367 10022775 : bool Value::IsBigInt() const { return Utils::OpenHandle(this)->IsBigInt(); }
3368 :
3369 2972040 : bool Value::IsProxy() const { return Utils::OpenHandle(this)->IsJSProxy(); }
3370 :
3371 : #define VALUE_IS_SPECIFIC_TYPE(Type, Check) \
3372 : bool Value::Is##Type() const { \
3373 : i::Handle<i::Object> obj = Utils::OpenHandle(this); \
3374 : return obj->Is##Check(); \
3375 : }
3376 :
3377 2521866 : VALUE_IS_SPECIFIC_TYPE(ArgumentsObject, JSArgumentsObject)
3378 65445 : VALUE_IS_SPECIFIC_TYPE(BigIntObject, BigIntWrapper)
3379 65619 : VALUE_IS_SPECIFIC_TYPE(BooleanObject, BooleanWrapper)
3380 65493 : VALUE_IS_SPECIFIC_TYPE(NumberObject, NumberWrapper)
3381 100221 : VALUE_IS_SPECIFIC_TYPE(StringObject, StringWrapper)
3382 65424 : VALUE_IS_SPECIFIC_TYPE(SymbolObject, SymbolWrapper)
3383 2701932 : VALUE_IS_SPECIFIC_TYPE(Date, JSDate)
3384 2959419 : VALUE_IS_SPECIFIC_TYPE(Map, JSMap)
3385 2958636 : VALUE_IS_SPECIFIC_TYPE(Set, JSSet)
3386 2688438 : VALUE_IS_SPECIFIC_TYPE(WeakMap, JSWeakMap)
3387 2688291 : VALUE_IS_SPECIFIC_TYPE(WeakSet, JSWeakSet)
3388 162 : VALUE_IS_SPECIFIC_TYPE(WebAssemblyCompiledModule, WasmModuleObject)
3389 :
3390 : #undef VALUE_IS_SPECIFIC_TYPE
3391 :
3392 :
3393 3734013 : bool Value::IsBoolean() const {
3394 7468026 : return Utils::OpenHandle(this)->IsBoolean();
3395 : }
3396 :
3397 97 : bool Value::IsExternal() const {
3398 : i::Handle<i::Object> obj = Utils::OpenHandle(this);
3399 194 : if (!obj->IsHeapObject()) return false;
3400 97 : i::Handle<i::HeapObject> heap_obj = i::Handle<i::HeapObject>::cast(obj);
3401 : // Check the instance type is JS_OBJECT (instance type of Externals) before
3402 : // attempting to get the Isolate since that guarantees the object is writable
3403 : // and GetIsolate will work.
3404 97 : if (heap_obj->map()->instance_type() != i::JS_OBJECT_TYPE) return false;
3405 : i::Isolate* isolate = i::JSObject::cast(*heap_obj)->GetIsolate();
3406 104 : return heap_obj->IsExternal(isolate);
3407 : }
3408 :
3409 :
3410 549046 : bool Value::IsInt32() const {
3411 : i::Handle<i::Object> obj = Utils::OpenHandle(this);
3412 1098092 : if (obj->IsSmi()) return true;
3413 72 : if (obj->IsNumber()) {
3414 72 : return i::IsInt32Double(obj->Number());
3415 : }
3416 : return false;
3417 : }
3418 :
3419 :
3420 9857 : bool Value::IsUint32() const {
3421 : i::Handle<i::Object> obj = Utils::OpenHandle(this);
3422 27326 : if (obj->IsSmi()) return i::Smi::ToInt(*obj) >= 0;
3423 4490 : if (obj->IsNumber()) {
3424 113 : double value = obj->Number();
3425 107 : return !i::IsMinusZero(value) &&
3426 95 : value >= 0 &&
3427 202 : value <= i::kMaxUInt32 &&
3428 113 : value == i::FastUI2D(i::FastD2UI(value));
3429 : }
3430 : return false;
3431 : }
3432 :
3433 :
3434 903251 : bool Value::IsNativeError() const {
3435 1806502 : return Utils::OpenHandle(this)->IsJSError();
3436 : }
3437 :
3438 :
3439 3223386 : bool Value::IsRegExp() const {
3440 : i::Handle<i::Object> obj = Utils::OpenHandle(this);
3441 6446772 : return obj->IsJSRegExp();
3442 : }
3443 :
3444 12 : bool Value::IsAsyncFunction() const {
3445 : i::Handle<i::Object> obj = Utils::OpenHandle(this);
3446 24 : if (!obj->IsJSFunction()) return false;
3447 12 : i::Handle<i::JSFunction> func = i::Handle<i::JSFunction>::cast(obj);
3448 24 : return i::IsAsyncFunction(func->shared()->kind());
3449 : }
3450 :
3451 90 : bool Value::IsGeneratorFunction() const {
3452 : i::Handle<i::Object> obj = Utils::OpenHandle(this);
3453 180 : if (!obj->IsJSFunction()) return false;
3454 73 : i::Handle<i::JSFunction> func = i::Handle<i::JSFunction>::cast(obj);
3455 146 : return i::IsGeneratorFunction(func->shared()->kind());
3456 : }
3457 :
3458 :
3459 1065063 : bool Value::IsGeneratorObject() const {
3460 2130126 : return Utils::OpenHandle(this)->IsJSGeneratorObject();
3461 : }
3462 :
3463 :
3464 896018 : bool Value::IsMapIterator() const {
3465 1792036 : return Utils::OpenHandle(this)->IsJSMapIterator();
3466 : }
3467 :
3468 :
3469 895906 : bool Value::IsSetIterator() const {
3470 1791812 : return Utils::OpenHandle(this)->IsJSSetIterator();
3471 : }
3472 :
3473 2734974 : bool Value::IsPromise() const { return Utils::OpenHandle(this)->IsJSPromise(); }
3474 :
3475 5 : bool Value::IsModuleNamespaceObject() const {
3476 10 : return Utils::OpenHandle(this)->IsJSModuleNamespace();
3477 : }
3478 :
3479 13338766 : MaybeLocal<String> Value::ToString(Local<Context> context) const {
3480 : auto obj = Utils::OpenHandle(this);
3481 26677530 : if (obj->IsString()) return ToApiHandle<String>(obj);
3482 7445748 : PREPARE_FOR_EXECUTION(context, Object, ToString, String);
3483 : Local<String> result;
3484 : has_pending_exception =
3485 1861437 : !ToLocal<String>(i::Object::ToString(isolate, obj), &result);
3486 1861437 : RETURN_ON_FAILED_EXECUTION(String);
3487 1861339 : RETURN_ESCAPED(result);
3488 : }
3489 :
3490 :
3491 0 : Local<String> Value::ToString(Isolate* isolate) const {
3492 0 : RETURN_TO_LOCAL_UNCHECKED(ToString(isolate->GetCurrentContext()), String);
3493 : }
3494 :
3495 :
3496 15 : MaybeLocal<String> Value::ToDetailString(Local<Context> context) const {
3497 : i::Handle<i::Object> obj = Utils::OpenHandle(this);
3498 30 : if (obj->IsString()) return ToApiHandle<String>(obj);
3499 60 : PREPARE_FOR_EXECUTION(context, Object, ToDetailString, String);
3500 : Local<String> result =
3501 15 : Utils::ToLocal(i::Object::NoSideEffectsToString(isolate, obj));
3502 : RETURN_ON_FAILED_EXECUTION(String);
3503 15 : RETURN_ESCAPED(result);
3504 : }
3505 :
3506 :
3507 351 : MaybeLocal<Object> Value::ToObject(Local<Context> context) const {
3508 : auto obj = Utils::OpenHandle(this);
3509 702 : if (obj->IsJSReceiver()) return ToApiHandle<Object>(obj);
3510 24 : PREPARE_FOR_EXECUTION(context, Object, ToObject, Object);
3511 : Local<Object> result;
3512 : has_pending_exception =
3513 6 : !ToLocal<Object>(i::Object::ToObject(isolate, obj), &result);
3514 6 : RETURN_ON_FAILED_EXECUTION(Object);
3515 0 : RETURN_ESCAPED(result);
3516 : }
3517 :
3518 :
3519 0 : Local<v8::Object> Value::ToObject(Isolate* isolate) const {
3520 0 : RETURN_TO_LOCAL_UNCHECKED(ToObject(isolate->GetCurrentContext()), Object);
3521 : }
3522 :
3523 63 : MaybeLocal<BigInt> Value::ToBigInt(Local<Context> context) const {
3524 : i::Handle<i::Object> obj = Utils::OpenHandle(this);
3525 126 : if (obj->IsBigInt()) return ToApiHandle<BigInt>(obj);
3526 144 : PREPARE_FOR_EXECUTION(context, Object, ToBigInt, BigInt);
3527 : Local<BigInt> result;
3528 : has_pending_exception =
3529 36 : !ToLocal<BigInt>(i::BigInt::FromObject(isolate, obj), &result);
3530 36 : RETURN_ON_FAILED_EXECUTION(BigInt);
3531 18 : RETURN_ESCAPED(result);
3532 : }
3533 :
3534 33113 : bool Value::BooleanValue(Isolate* v8_isolate) const {
3535 : return Utils::OpenHandle(this)->BooleanValue(
3536 33113 : reinterpret_cast<i::Isolate*>(v8_isolate));
3537 : }
3538 :
3539 0 : MaybeLocal<Boolean> Value::ToBoolean(Local<Context> context) const {
3540 0 : return ToBoolean(context->GetIsolate());
3541 : }
3542 :
3543 :
3544 60 : Local<Boolean> Value::ToBoolean(Isolate* v8_isolate) const {
3545 : auto isolate = reinterpret_cast<i::Isolate*>(v8_isolate);
3546 : return ToApiHandle<Boolean>(
3547 60 : isolate->factory()->ToBoolean(BooleanValue(v8_isolate)));
3548 : }
3549 :
3550 :
3551 4670 : MaybeLocal<Number> Value::ToNumber(Local<Context> context) const {
3552 : auto obj = Utils::OpenHandle(this);
3553 9340 : if (obj->IsNumber()) return ToApiHandle<Number>(obj);
3554 588 : PREPARE_FOR_EXECUTION(context, Object, ToNumber, Number);
3555 : Local<Number> result;
3556 : has_pending_exception =
3557 147 : !ToLocal<Number>(i::Object::ToNumber(isolate, obj), &result);
3558 147 : RETURN_ON_FAILED_EXECUTION(Number);
3559 141 : RETURN_ESCAPED(result);
3560 : }
3561 :
3562 :
3563 0 : Local<Number> Value::ToNumber(Isolate* isolate) const {
3564 0 : RETURN_TO_LOCAL_UNCHECKED(ToNumber(isolate->GetCurrentContext()), Number);
3565 : }
3566 :
3567 :
3568 6 : MaybeLocal<Integer> Value::ToInteger(Local<Context> context) const {
3569 : auto obj = Utils::OpenHandle(this);
3570 12 : if (obj->IsSmi()) return ToApiHandle<Integer>(obj);
3571 24 : PREPARE_FOR_EXECUTION(context, Object, ToInteger, Integer);
3572 : Local<Integer> result;
3573 : has_pending_exception =
3574 6 : !ToLocal<Integer>(i::Object::ToInteger(isolate, obj), &result);
3575 6 : RETURN_ON_FAILED_EXECUTION(Integer);
3576 0 : RETURN_ESCAPED(result);
3577 : }
3578 :
3579 :
3580 0 : Local<Integer> Value::ToInteger(Isolate* isolate) const {
3581 0 : RETURN_TO_LOCAL_UNCHECKED(ToInteger(isolate->GetCurrentContext()), Integer);
3582 : }
3583 :
3584 :
3585 2936 : MaybeLocal<Int32> Value::ToInt32(Local<Context> context) const {
3586 : auto obj = Utils::OpenHandle(this);
3587 5872 : if (obj->IsSmi()) return ToApiHandle<Int32>(obj);
3588 : Local<Int32> result;
3589 4556 : PREPARE_FOR_EXECUTION(context, Object, ToInt32, Int32);
3590 : has_pending_exception =
3591 1139 : !ToLocal<Int32>(i::Object::ToInt32(isolate, obj), &result);
3592 1139 : RETURN_ON_FAILED_EXECUTION(Int32);
3593 1133 : RETURN_ESCAPED(result);
3594 : }
3595 :
3596 :
3597 0 : Local<Int32> Value::ToInt32(Isolate* isolate) const {
3598 0 : RETURN_TO_LOCAL_UNCHECKED(ToInt32(isolate->GetCurrentContext()), Int32);
3599 : }
3600 :
3601 :
3602 48 : MaybeLocal<Uint32> Value::ToUint32(Local<Context> context) const {
3603 : auto obj = Utils::OpenHandle(this);
3604 96 : if (obj->IsSmi()) return ToApiHandle<Uint32>(obj);
3605 : Local<Uint32> result;
3606 144 : PREPARE_FOR_EXECUTION(context, Object, ToUint32, Uint32);
3607 : has_pending_exception =
3608 36 : !ToLocal<Uint32>(i::Object::ToUint32(isolate, obj), &result);
3609 36 : RETURN_ON_FAILED_EXECUTION(Uint32);
3610 30 : RETURN_ESCAPED(result);
3611 : }
3612 :
3613 959 : i::Isolate* i::IsolateFromNeverReadOnlySpaceObject(i::Address obj) {
3614 : return i::NeverReadOnlySpaceObject::GetIsolate(
3615 959 : i::HeapObject::cast(i::Object(obj)));
3616 : }
3617 :
3618 0 : void i::Internals::CheckInitializedImpl(v8::Isolate* external_isolate) {
3619 0 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(external_isolate);
3620 0 : Utils::ApiCheck(isolate != nullptr && !isolate->IsDead(),
3621 : "v8::internal::Internals::CheckInitialized",
3622 : "Isolate is not initialized or V8 has died");
3623 0 : }
3624 :
3625 :
3626 0 : void External::CheckCast(v8::Value* that) {
3627 0 : Utils::ApiCheck(that->IsExternal(), "v8::External::Cast",
3628 : "Could not convert to external");
3629 0 : }
3630 :
3631 :
3632 0 : void v8::Object::CheckCast(Value* that) {
3633 : i::Handle<i::Object> obj = Utils::OpenHandle(that);
3634 0 : Utils::ApiCheck(obj->IsJSReceiver(), "v8::Object::Cast",
3635 : "Could not convert to object");
3636 0 : }
3637 :
3638 :
3639 0 : void v8::Function::CheckCast(Value* that) {
3640 : i::Handle<i::Object> obj = Utils::OpenHandle(that);
3641 0 : Utils::ApiCheck(obj->IsCallable(), "v8::Function::Cast",
3642 : "Could not convert to function");
3643 0 : }
3644 :
3645 :
3646 0 : void v8::Boolean::CheckCast(v8::Value* that) {
3647 : i::Handle<i::Object> obj = Utils::OpenHandle(that);
3648 0 : Utils::ApiCheck(obj->IsBoolean(), "v8::Boolean::Cast",
3649 : "Could not convert to boolean");
3650 0 : }
3651 :
3652 :
3653 0 : void v8::Name::CheckCast(v8::Value* that) {
3654 : i::Handle<i::Object> obj = Utils::OpenHandle(that);
3655 0 : Utils::ApiCheck(obj->IsName(), "v8::Name::Cast", "Could not convert to name");
3656 0 : }
3657 :
3658 :
3659 0 : void v8::String::CheckCast(v8::Value* that) {
3660 : i::Handle<i::Object> obj = Utils::OpenHandle(that);
3661 0 : Utils::ApiCheck(obj->IsString(), "v8::String::Cast",
3662 : "Could not convert to string");
3663 0 : }
3664 :
3665 :
3666 0 : void v8::Symbol::CheckCast(v8::Value* that) {
3667 : i::Handle<i::Object> obj = Utils::OpenHandle(that);
3668 0 : Utils::ApiCheck(obj->IsSymbol(), "v8::Symbol::Cast",
3669 : "Could not convert to symbol");
3670 0 : }
3671 :
3672 :
3673 0 : void v8::Private::CheckCast(v8::Data* that) {
3674 : i::Handle<i::Object> obj = Utils::OpenHandle(that);
3675 0 : Utils::ApiCheck(obj->IsSymbol() &&
3676 0 : i::Handle<i::Symbol>::cast(obj)->is_private(),
3677 : "v8::Private::Cast",
3678 : "Could not convert to private");
3679 0 : }
3680 :
3681 :
3682 0 : void v8::Number::CheckCast(v8::Value* that) {
3683 : i::Handle<i::Object> obj = Utils::OpenHandle(that);
3684 0 : Utils::ApiCheck(obj->IsNumber(),
3685 : "v8::Number::Cast()",
3686 : "Could not convert to number");
3687 0 : }
3688 :
3689 :
3690 0 : void v8::Integer::CheckCast(v8::Value* that) {
3691 : i::Handle<i::Object> obj = Utils::OpenHandle(that);
3692 0 : Utils::ApiCheck(obj->IsNumber(), "v8::Integer::Cast",
3693 : "Could not convert to number");
3694 0 : }
3695 :
3696 :
3697 0 : void v8::Int32::CheckCast(v8::Value* that) {
3698 0 : Utils::ApiCheck(that->IsInt32(), "v8::Int32::Cast",
3699 : "Could not convert to 32-bit signed integer");
3700 0 : }
3701 :
3702 :
3703 0 : void v8::Uint32::CheckCast(v8::Value* that) {
3704 0 : Utils::ApiCheck(that->IsUint32(), "v8::Uint32::Cast",
3705 : "Could not convert to 32-bit unsigned integer");
3706 0 : }
3707 :
3708 0 : void v8::BigInt::CheckCast(v8::Value* that) {
3709 0 : Utils::ApiCheck(that->IsBigInt(), "v8::BigInt::Cast",
3710 : "Could not convert to BigInt");
3711 0 : }
3712 :
3713 0 : void v8::Array::CheckCast(Value* that) {
3714 : i::Handle<i::Object> obj = Utils::OpenHandle(that);
3715 0 : Utils::ApiCheck(obj->IsJSArray(), "v8::Array::Cast",
3716 : "Could not convert to array");
3717 0 : }
3718 :
3719 :
3720 0 : void v8::Map::CheckCast(Value* that) {
3721 : i::Handle<i::Object> obj = Utils::OpenHandle(that);
3722 0 : Utils::ApiCheck(obj->IsJSMap(), "v8::Map::Cast", "Could not convert to Map");
3723 0 : }
3724 :
3725 :
3726 0 : void v8::Set::CheckCast(Value* that) {
3727 : i::Handle<i::Object> obj = Utils::OpenHandle(that);
3728 0 : Utils::ApiCheck(obj->IsJSSet(), "v8_Set_Cast", "Could not convert to Set");
3729 0 : }
3730 :
3731 :
3732 0 : void v8::Promise::CheckCast(Value* that) {
3733 0 : Utils::ApiCheck(that->IsPromise(), "v8::Promise::Cast",
3734 : "Could not convert to promise");
3735 0 : }
3736 :
3737 :
3738 0 : void v8::Promise::Resolver::CheckCast(Value* that) {
3739 0 : Utils::ApiCheck(that->IsPromise(), "v8::Promise::Resolver::Cast",
3740 : "Could not convert to promise resolver");
3741 0 : }
3742 :
3743 :
3744 0 : void v8::Proxy::CheckCast(Value* that) {
3745 0 : Utils::ApiCheck(that->IsProxy(), "v8::Proxy::Cast",
3746 : "Could not convert to proxy");
3747 0 : }
3748 :
3749 0 : void v8::WasmModuleObject::CheckCast(Value* that) {
3750 0 : Utils::ApiCheck(that->IsWebAssemblyCompiledModule(),
3751 : "v8::WasmModuleObject::Cast",
3752 : "Could not convert to wasm module object");
3753 0 : }
3754 :
3755 0 : void v8::ArrayBuffer::CheckCast(Value* that) {
3756 : i::Handle<i::Object> obj = Utils::OpenHandle(that);
3757 : Utils::ApiCheck(
3758 0 : obj->IsJSArrayBuffer() && !i::JSArrayBuffer::cast(*obj)->is_shared(),
3759 : "v8::ArrayBuffer::Cast()", "Could not convert to ArrayBuffer");
3760 0 : }
3761 :
3762 :
3763 0 : void v8::ArrayBufferView::CheckCast(Value* that) {
3764 : i::Handle<i::Object> obj = Utils::OpenHandle(that);
3765 0 : Utils::ApiCheck(obj->IsJSArrayBufferView(),
3766 : "v8::ArrayBufferView::Cast()",
3767 : "Could not convert to ArrayBufferView");
3768 0 : }
3769 :
3770 :
3771 0 : void v8::TypedArray::CheckCast(Value* that) {
3772 : i::Handle<i::Object> obj = Utils::OpenHandle(that);
3773 0 : Utils::ApiCheck(obj->IsJSTypedArray(),
3774 : "v8::TypedArray::Cast()",
3775 : "Could not convert to TypedArray");
3776 0 : }
3777 :
3778 : #define CHECK_TYPED_ARRAY_CAST(Type, typeName, TYPE, ctype) \
3779 : void v8::Type##Array::CheckCast(Value* that) { \
3780 : i::Handle<i::Object> obj = Utils::OpenHandle(that); \
3781 : Utils::ApiCheck( \
3782 : obj->IsJSTypedArray() && \
3783 : i::JSTypedArray::cast(*obj)->type() == i::kExternal##Type##Array, \
3784 : "v8::" #Type "Array::Cast()", "Could not convert to " #Type "Array"); \
3785 : }
3786 :
3787 0 : TYPED_ARRAYS(CHECK_TYPED_ARRAY_CAST)
3788 :
3789 : #undef CHECK_TYPED_ARRAY_CAST
3790 :
3791 :
3792 0 : void v8::DataView::CheckCast(Value* that) {
3793 : i::Handle<i::Object> obj = Utils::OpenHandle(that);
3794 0 : Utils::ApiCheck(obj->IsJSDataView(),
3795 : "v8::DataView::Cast()",
3796 : "Could not convert to DataView");
3797 0 : }
3798 :
3799 :
3800 0 : void v8::SharedArrayBuffer::CheckCast(Value* that) {
3801 : i::Handle<i::Object> obj = Utils::OpenHandle(that);
3802 : Utils::ApiCheck(
3803 0 : obj->IsJSArrayBuffer() && i::JSArrayBuffer::cast(*obj)->is_shared(),
3804 : "v8::SharedArrayBuffer::Cast()",
3805 : "Could not convert to SharedArrayBuffer");
3806 0 : }
3807 :
3808 :
3809 0 : void v8::Date::CheckCast(v8::Value* that) {
3810 : i::Handle<i::Object> obj = Utils::OpenHandle(that);
3811 0 : Utils::ApiCheck(obj->IsJSDate(), "v8::Date::Cast()",
3812 : "Could not convert to date");
3813 0 : }
3814 :
3815 :
3816 0 : void v8::StringObject::CheckCast(v8::Value* that) {
3817 : i::Handle<i::Object> obj = Utils::OpenHandle(that);
3818 0 : Utils::ApiCheck(obj->IsStringWrapper(), "v8::StringObject::Cast()",
3819 : "Could not convert to StringObject");
3820 0 : }
3821 :
3822 :
3823 0 : void v8::SymbolObject::CheckCast(v8::Value* that) {
3824 : i::Handle<i::Object> obj = Utils::OpenHandle(that);
3825 0 : Utils::ApiCheck(obj->IsSymbolWrapper(), "v8::SymbolObject::Cast()",
3826 : "Could not convert to SymbolObject");
3827 0 : }
3828 :
3829 :
3830 0 : void v8::NumberObject::CheckCast(v8::Value* that) {
3831 : i::Handle<i::Object> obj = Utils::OpenHandle(that);
3832 0 : Utils::ApiCheck(obj->IsNumberWrapper(), "v8::NumberObject::Cast()",
3833 : "Could not convert to NumberObject");
3834 0 : }
3835 :
3836 0 : void v8::BigIntObject::CheckCast(v8::Value* that) {
3837 : i::Handle<i::Object> obj = Utils::OpenHandle(that);
3838 0 : Utils::ApiCheck(obj->IsBigIntWrapper(), "v8::BigIntObject::Cast()",
3839 : "Could not convert to BigIntObject");
3840 0 : }
3841 :
3842 0 : void v8::BooleanObject::CheckCast(v8::Value* that) {
3843 : i::Handle<i::Object> obj = Utils::OpenHandle(that);
3844 0 : Utils::ApiCheck(obj->IsBooleanWrapper(), "v8::BooleanObject::Cast()",
3845 : "Could not convert to BooleanObject");
3846 0 : }
3847 :
3848 :
3849 0 : void v8::RegExp::CheckCast(v8::Value* that) {
3850 : i::Handle<i::Object> obj = Utils::OpenHandle(that);
3851 0 : Utils::ApiCheck(obj->IsJSRegExp(),
3852 : "v8::RegExp::Cast()",
3853 : "Could not convert to regular expression");
3854 0 : }
3855 :
3856 :
3857 0 : Maybe<bool> Value::BooleanValue(Local<Context> context) const {
3858 0 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(context->GetIsolate());
3859 0 : return Just(Utils::OpenHandle(this)->BooleanValue(isolate));
3860 : }
3861 :
3862 :
3863 40717 : Maybe<double> Value::NumberValue(Local<Context> context) const {
3864 : auto obj = Utils::OpenHandle(this);
3865 121073 : if (obj->IsNumber()) return Just(obj->Number());
3866 2156 : auto isolate = reinterpret_cast<i::Isolate*>(context->GetIsolate());
3867 4312 : ENTER_V8(isolate, context, Value, NumberValue, Nothing<double>(),
3868 : i::HandleScope);
3869 : i::Handle<i::Object> num;
3870 2156 : has_pending_exception = !i::Object::ToNumber(isolate, obj).ToHandle(&num);
3871 1078 : RETURN_ON_FAILED_EXECUTION_PRIMITIVE(double);
3872 1009 : return Just(num->Number());
3873 : }
3874 :
3875 :
3876 2640 : Maybe<int64_t> Value::IntegerValue(Local<Context> context) const {
3877 : auto obj = Utils::OpenHandle(this);
3878 5280 : if (obj->IsNumber()) {
3879 2577 : return Just(NumberToInt64(*obj));
3880 : }
3881 126 : auto isolate = reinterpret_cast<i::Isolate*>(context->GetIsolate());
3882 252 : ENTER_V8(isolate, context, Value, IntegerValue, Nothing<int64_t>(),
3883 : i::HandleScope);
3884 : i::Handle<i::Object> num;
3885 126 : has_pending_exception = !i::Object::ToInteger(isolate, obj).ToHandle(&num);
3886 63 : RETURN_ON_FAILED_EXECUTION_PRIMITIVE(int64_t);
3887 42 : return Just(NumberToInt64(*num));
3888 : }
3889 :
3890 :
3891 545464 : Maybe<int32_t> Value::Int32Value(Local<Context> context) const {
3892 : auto obj = Utils::OpenHandle(this);
3893 1636338 : if (obj->IsNumber()) return Just(NumberToInt32(*obj));
3894 108 : auto isolate = reinterpret_cast<i::Isolate*>(context->GetIsolate());
3895 216 : ENTER_V8(isolate, context, Value, Int32Value, Nothing<int32_t>(),
3896 : i::HandleScope);
3897 : i::Handle<i::Object> num;
3898 108 : has_pending_exception = !i::Object::ToInt32(isolate, obj).ToHandle(&num);
3899 54 : RETURN_ON_FAILED_EXECUTION_PRIMITIVE(int32_t);
3900 96 : return Just(num->IsSmi() ? i::Smi::ToInt(*num)
3901 96 : : static_cast<int32_t>(num->Number()));
3902 : }
3903 :
3904 :
3905 211 : Maybe<uint32_t> Value::Uint32Value(Local<Context> context) const {
3906 : auto obj = Utils::OpenHandle(this);
3907 627 : if (obj->IsNumber()) return Just(NumberToUint32(*obj));
3908 12 : auto isolate = reinterpret_cast<i::Isolate*>(context->GetIsolate());
3909 24 : ENTER_V8(isolate, context, Value, Uint32Value, Nothing<uint32_t>(),
3910 : i::HandleScope);
3911 : i::Handle<i::Object> num;
3912 12 : has_pending_exception = !i::Object::ToUint32(isolate, obj).ToHandle(&num);
3913 6 : RETURN_ON_FAILED_EXECUTION_PRIMITIVE(uint32_t);
3914 0 : return Just(num->IsSmi() ? static_cast<uint32_t>(i::Smi::ToInt(*num))
3915 0 : : static_cast<uint32_t>(num->Number()));
3916 : }
3917 :
3918 :
3919 42 : MaybeLocal<Uint32> Value::ToArrayIndex(Local<Context> context) const {
3920 : auto self = Utils::OpenHandle(this);
3921 84 : if (self->IsSmi()) {
3922 12 : if (i::Smi::ToInt(*self) >= 0) return Utils::Uint32ToLocal(self);
3923 6 : return Local<Uint32>();
3924 : }
3925 120 : PREPARE_FOR_EXECUTION(context, Object, ToArrayIndex, Uint32);
3926 : i::Handle<i::Object> string_obj;
3927 : has_pending_exception =
3928 60 : !i::Object::ToString(isolate, self).ToHandle(&string_obj);
3929 30 : RETURN_ON_FAILED_EXECUTION(Uint32);
3930 30 : i::Handle<i::String> str = i::Handle<i::String>::cast(string_obj);
3931 : uint32_t index;
3932 30 : if (str->AsArrayIndex(&index)) {
3933 : i::Handle<i::Object> value;
3934 12 : if (index <= static_cast<uint32_t>(i::Smi::kMaxValue)) {
3935 : value = i::Handle<i::Object>(i::Smi::FromInt(index), isolate);
3936 : } else {
3937 6 : value = isolate->factory()->NewNumber(index);
3938 : }
3939 12 : RETURN_ESCAPED(Utils::Uint32ToLocal(value));
3940 : }
3941 18 : return Local<Uint32>();
3942 : }
3943 :
3944 :
3945 167392 : Maybe<bool> Value::Equals(Local<Context> context, Local<Value> that) const {
3946 : i::Isolate* isolate = Utils::OpenHandle(*context)->GetIsolate();
3947 167392 : auto self = Utils::OpenHandle(this);
3948 167392 : auto other = Utils::OpenHandle(*that);
3949 167392 : return i::Object::Equals(isolate, self, other);
3950 : }
3951 :
3952 :
3953 1344 : bool Value::StrictEquals(Local<Value> that) const {
3954 : auto self = Utils::OpenHandle(this);
3955 : auto other = Utils::OpenHandle(*that);
3956 1344 : return self->StrictEquals(*other);
3957 : }
3958 :
3959 :
3960 123304 : bool Value::SameValue(Local<Value> that) const {
3961 : auto self = Utils::OpenHandle(this);
3962 : auto other = Utils::OpenHandle(*that);
3963 123304 : return self->SameValue(*other);
3964 : }
3965 :
3966 50 : Local<String> Value::TypeOf(v8::Isolate* external_isolate) {
3967 50 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(external_isolate);
3968 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(isolate);
3969 100 : LOG_API(isolate, Value, TypeOf);
3970 100 : return Utils::ToLocal(i::Object::TypeOf(isolate, Utils::OpenHandle(this)));
3971 : }
3972 :
3973 82 : Maybe<bool> Value::InstanceOf(v8::Local<v8::Context> context,
3974 : v8::Local<v8::Object> object) {
3975 164 : auto isolate = reinterpret_cast<i::Isolate*>(context->GetIsolate());
3976 328 : ENTER_V8(isolate, context, Value, InstanceOf, Nothing<bool>(),
3977 : i::HandleScope);
3978 82 : auto left = Utils::OpenHandle(this);
3979 : auto right = Utils::OpenHandle(*object);
3980 : i::Handle<i::Object> result;
3981 : has_pending_exception =
3982 164 : !i::Object::InstanceOf(isolate, left, right).ToHandle(&result);
3983 82 : RETURN_ON_FAILED_EXECUTION_PRIMITIVE(bool);
3984 140 : return Just(result->IsTrue(isolate));
3985 : }
3986 :
3987 186620 : Maybe<bool> v8::Object::Set(v8::Local<v8::Context> context,
3988 : v8::Local<Value> key, v8::Local<Value> value) {
3989 373240 : auto isolate = reinterpret_cast<i::Isolate*>(context->GetIsolate());
3990 746480 : ENTER_V8(isolate, context, Object, Set, Nothing<bool>(), i::HandleScope);
3991 : auto self = Utils::OpenHandle(this);
3992 186620 : auto key_obj = Utils::OpenHandle(*key);
3993 186620 : auto value_obj = Utils::OpenHandle(*value);
3994 : has_pending_exception =
3995 : i::Runtime::SetObjectProperty(isolate, self, key_obj, value_obj,
3996 : i::LanguageMode::kSloppy,
3997 186620 : i::StoreOrigin::kMaybeKeyed)
3998 373240 : .is_null();
3999 186620 : RETURN_ON_FAILED_EXECUTION_PRIMITIVE(bool);
4000 : return Just(true);
4001 : }
4002 :
4003 :
4004 0 : bool v8::Object::Set(v8::Local<Value> key, v8::Local<Value> value) {
4005 0 : auto context = ContextFromNeverReadOnlySpaceObject(Utils::OpenHandle(this));
4006 0 : return Set(context, key, value).FromMaybe(false);
4007 : }
4008 :
4009 :
4010 10987 : Maybe<bool> v8::Object::Set(v8::Local<v8::Context> context, uint32_t index,
4011 : v8::Local<Value> value) {
4012 21974 : auto isolate = reinterpret_cast<i::Isolate*>(context->GetIsolate());
4013 43948 : ENTER_V8(isolate, context, Object, Set, Nothing<bool>(), i::HandleScope);
4014 : auto self = Utils::OpenHandle(this);
4015 10987 : auto value_obj = Utils::OpenHandle(*value);
4016 : has_pending_exception = i::Object::SetElement(isolate, self, index, value_obj,
4017 10987 : i::LanguageMode::kSloppy)
4018 21974 : .is_null();
4019 10987 : RETURN_ON_FAILED_EXECUTION_PRIMITIVE(bool);
4020 : return Just(true);
4021 : }
4022 :
4023 :
4024 0 : bool v8::Object::Set(uint32_t index, v8::Local<Value> value) {
4025 0 : auto context = ContextFromNeverReadOnlySpaceObject(Utils::OpenHandle(this));
4026 0 : return Set(context, index, value).FromMaybe(false);
4027 : }
4028 :
4029 :
4030 17932 : Maybe<bool> v8::Object::CreateDataProperty(v8::Local<v8::Context> context,
4031 : v8::Local<Name> key,
4032 : v8::Local<Value> value) {
4033 35864 : auto isolate = reinterpret_cast<i::Isolate*>(context->GetIsolate());
4034 71728 : ENTER_V8(isolate, context, Object, CreateDataProperty, Nothing<bool>(),
4035 : i::HandleScope);
4036 17932 : i::Handle<i::JSReceiver> self = Utils::OpenHandle(this);
4037 17932 : i::Handle<i::Name> key_obj = Utils::OpenHandle(*key);
4038 17932 : i::Handle<i::Object> value_obj = Utils::OpenHandle(*value);
4039 :
4040 : Maybe<bool> result = i::JSReceiver::CreateDataProperty(
4041 17932 : isolate, self, key_obj, value_obj, i::kDontThrow);
4042 17932 : has_pending_exception = result.IsNothing();
4043 17932 : RETURN_ON_FAILED_EXECUTION_PRIMITIVE(bool);
4044 17927 : return result;
4045 : }
4046 :
4047 :
4048 11750 : Maybe<bool> v8::Object::CreateDataProperty(v8::Local<v8::Context> context,
4049 : uint32_t index,
4050 : v8::Local<Value> value) {
4051 23500 : auto isolate = reinterpret_cast<i::Isolate*>(context->GetIsolate());
4052 47000 : ENTER_V8(isolate, context, Object, CreateDataProperty, Nothing<bool>(),
4053 : i::HandleScope);
4054 : i::Handle<i::JSReceiver> self = Utils::OpenHandle(this);
4055 11750 : i::Handle<i::Object> value_obj = Utils::OpenHandle(*value);
4056 :
4057 : i::LookupIterator it(isolate, self, index, self, i::LookupIterator::OWN);
4058 : Maybe<bool> result =
4059 11750 : i::JSReceiver::CreateDataProperty(&it, value_obj, i::kDontThrow);
4060 11750 : has_pending_exception = result.IsNothing();
4061 11750 : RETURN_ON_FAILED_EXECUTION_PRIMITIVE(bool);
4062 11750 : return result;
4063 : }
4064 :
4065 : struct v8::PropertyDescriptor::PrivateData {
4066 : PrivateData() : desc() {}
4067 : i::PropertyDescriptor desc;
4068 : };
4069 :
4070 216 : v8::PropertyDescriptor::PropertyDescriptor() : private_(new PrivateData()) {}
4071 :
4072 : // DataDescriptor
4073 83 : v8::PropertyDescriptor::PropertyDescriptor(v8::Local<v8::Value> value)
4074 166 : : private_(new PrivateData()) {
4075 : private_->desc.set_value(Utils::OpenHandle(*value, true));
4076 83 : }
4077 :
4078 : // DataDescriptor with writable field
4079 26 : v8::PropertyDescriptor::PropertyDescriptor(v8::Local<v8::Value> value,
4080 : bool writable)
4081 52 : : private_(new PrivateData()) {
4082 : private_->desc.set_value(Utils::OpenHandle(*value, true));
4083 26 : private_->desc.set_writable(writable);
4084 26 : }
4085 :
4086 : // AccessorDescriptor
4087 68 : v8::PropertyDescriptor::PropertyDescriptor(v8::Local<v8::Value> get,
4088 : v8::Local<v8::Value> set)
4089 136 : : private_(new PrivateData()) {
4090 : DCHECK(get.IsEmpty() || get->IsUndefined() || get->IsFunction());
4091 : DCHECK(set.IsEmpty() || set->IsUndefined() || set->IsFunction());
4092 : private_->desc.set_get(Utils::OpenHandle(*get, true));
4093 68 : private_->desc.set_set(Utils::OpenHandle(*set, true));
4094 68 : }
4095 :
4096 285 : v8::PropertyDescriptor::~PropertyDescriptor() { delete private_; }
4097 :
4098 32 : v8::Local<Value> v8::PropertyDescriptor::value() const {
4099 : DCHECK(private_->desc.has_value());
4100 32 : return Utils::ToLocal(private_->desc.value());
4101 : }
4102 :
4103 22 : v8::Local<Value> v8::PropertyDescriptor::get() const {
4104 : DCHECK(private_->desc.has_get());
4105 22 : return Utils::ToLocal(private_->desc.get());
4106 : }
4107 :
4108 22 : v8::Local<Value> v8::PropertyDescriptor::set() const {
4109 : DCHECK(private_->desc.has_set());
4110 22 : return Utils::ToLocal(private_->desc.set());
4111 : }
4112 :
4113 69 : bool v8::PropertyDescriptor::has_value() const {
4114 138 : return private_->desc.has_value();
4115 : }
4116 69 : bool v8::PropertyDescriptor::has_get() const {
4117 138 : return private_->desc.has_get();
4118 : }
4119 69 : bool v8::PropertyDescriptor::has_set() const {
4120 138 : return private_->desc.has_set();
4121 : }
4122 :
4123 16 : bool v8::PropertyDescriptor::writable() const {
4124 : DCHECK(private_->desc.has_writable());
4125 32 : return private_->desc.writable();
4126 : }
4127 :
4128 69 : bool v8::PropertyDescriptor::has_writable() const {
4129 138 : return private_->desc.has_writable();
4130 : }
4131 :
4132 37 : void v8::PropertyDescriptor::set_enumerable(bool enumerable) {
4133 37 : private_->desc.set_enumerable(enumerable);
4134 37 : }
4135 :
4136 17 : bool v8::PropertyDescriptor::enumerable() const {
4137 : DCHECK(private_->desc.has_enumerable());
4138 34 : return private_->desc.enumerable();
4139 : }
4140 :
4141 63 : bool v8::PropertyDescriptor::has_enumerable() const {
4142 126 : return private_->desc.has_enumerable();
4143 : }
4144 :
4145 38 : void v8::PropertyDescriptor::set_configurable(bool configurable) {
4146 38 : private_->desc.set_configurable(configurable);
4147 38 : }
4148 :
4149 32 : bool v8::PropertyDescriptor::configurable() const {
4150 : DCHECK(private_->desc.has_configurable());
4151 64 : return private_->desc.configurable();
4152 : }
4153 :
4154 63 : bool v8::PropertyDescriptor::has_configurable() const {
4155 126 : return private_->desc.has_configurable();
4156 : }
4157 :
4158 3336 : Maybe<bool> v8::Object::DefineOwnProperty(v8::Local<v8::Context> context,
4159 : v8::Local<Name> key,
4160 : v8::Local<Value> value,
4161 : v8::PropertyAttribute attributes) {
4162 6672 : auto isolate = reinterpret_cast<i::Isolate*>(context->GetIsolate());
4163 : i::Handle<i::JSReceiver> self = Utils::OpenHandle(this);
4164 : i::Handle<i::Name> key_obj = Utils::OpenHandle(*key);
4165 : i::Handle<i::Object> value_obj = Utils::OpenHandle(*value);
4166 :
4167 : i::PropertyDescriptor desc;
4168 3336 : desc.set_writable(!(attributes & v8::ReadOnly));
4169 3336 : desc.set_enumerable(!(attributes & v8::DontEnum));
4170 3336 : desc.set_configurable(!(attributes & v8::DontDelete));
4171 : desc.set_value(value_obj);
4172 :
4173 6672 : if (self->IsJSProxy()) {
4174 0 : ENTER_V8(isolate, context, Object, DefineOwnProperty, Nothing<bool>(),
4175 : i::HandleScope);
4176 : Maybe<bool> success = i::JSReceiver::DefineOwnProperty(
4177 0 : isolate, self, key_obj, &desc, i::kDontThrow);
4178 : // Even though we said kDontThrow, there might be accessors that do throw.
4179 : RETURN_ON_FAILED_EXECUTION_PRIMITIVE(bool);
4180 0 : return success;
4181 : } else {
4182 : // If it's not a JSProxy, i::JSReceiver::DefineOwnProperty should never run
4183 : // a script.
4184 13344 : ENTER_V8_NO_SCRIPT(isolate, context, Object, DefineOwnProperty,
4185 : Nothing<bool>(), i::HandleScope);
4186 : Maybe<bool> success = i::JSReceiver::DefineOwnProperty(
4187 3336 : isolate, self, key_obj, &desc, i::kDontThrow);
4188 : RETURN_ON_FAILED_EXECUTION_PRIMITIVE(bool);
4189 3336 : return success;
4190 : }
4191 : }
4192 :
4193 126 : Maybe<bool> v8::Object::DefineProperty(v8::Local<v8::Context> context,
4194 : v8::Local<Name> key,
4195 : PropertyDescriptor& descriptor) {
4196 252 : auto isolate = reinterpret_cast<i::Isolate*>(context->GetIsolate());
4197 504 : ENTER_V8(isolate, context, Object, DefineOwnProperty, Nothing<bool>(),
4198 : i::HandleScope);
4199 126 : i::Handle<i::JSReceiver> self = Utils::OpenHandle(this);
4200 : i::Handle<i::Name> key_obj = Utils::OpenHandle(*key);
4201 :
4202 : Maybe<bool> success = i::JSReceiver::DefineOwnProperty(
4203 252 : isolate, self, key_obj, &descriptor.get_private()->desc, i::kDontThrow);
4204 : RETURN_ON_FAILED_EXECUTION_PRIMITIVE(bool);
4205 126 : return success;
4206 : }
4207 :
4208 241054 : Maybe<bool> v8::Object::SetPrivate(Local<Context> context, Local<Private> key,
4209 : Local<Value> value) {
4210 482108 : auto isolate = reinterpret_cast<i::Isolate*>(context->GetIsolate());
4211 964216 : ENTER_V8_NO_SCRIPT(isolate, context, Object, SetPrivate, Nothing<bool>(),
4212 : i::HandleScope);
4213 : auto self = Utils::OpenHandle(this);
4214 241054 : auto key_obj = Utils::OpenHandle(reinterpret_cast<Name*>(*key));
4215 : auto value_obj = Utils::OpenHandle(*value);
4216 482108 : if (self->IsJSProxy()) {
4217 : i::PropertyDescriptor desc;
4218 : desc.set_writable(true);
4219 : desc.set_enumerable(false);
4220 : desc.set_configurable(true);
4221 : desc.set_value(value_obj);
4222 : return i::JSProxy::SetPrivateSymbol(
4223 : isolate, i::Handle<i::JSProxy>::cast(self),
4224 18 : i::Handle<i::Symbol>::cast(key_obj), &desc, i::kDontThrow);
4225 : }
4226 241036 : auto js_object = i::Handle<i::JSObject>::cast(self);
4227 241036 : i::LookupIterator it(js_object, key_obj, js_object);
4228 : has_pending_exception = i::JSObject::DefineOwnPropertyIgnoreAttributes(
4229 : &it, value_obj, i::DONT_ENUM)
4230 482072 : .is_null();
4231 241036 : RETURN_ON_FAILED_EXECUTION_PRIMITIVE(bool);
4232 : return Just(true);
4233 : }
4234 :
4235 :
4236 426886 : MaybeLocal<Value> v8::Object::Get(Local<v8::Context> context,
4237 : Local<Value> key) {
4238 1707544 : PREPARE_FOR_EXECUTION(context, Object, Get, Value);
4239 : auto self = Utils::OpenHandle(this);
4240 426886 : auto key_obj = Utils::OpenHandle(*key);
4241 : i::Handle<i::Object> result;
4242 : has_pending_exception =
4243 853772 : !i::Runtime::GetObjectProperty(isolate, self, key_obj).ToHandle(&result);
4244 426886 : RETURN_ON_FAILED_EXECUTION(Value);
4245 426820 : RETURN_ESCAPED(Utils::ToLocal(result));
4246 : }
4247 :
4248 :
4249 0 : Local<Value> v8::Object::Get(v8::Local<Value> key) {
4250 0 : auto context = ContextFromNeverReadOnlySpaceObject(Utils::OpenHandle(this));
4251 0 : RETURN_TO_LOCAL_UNCHECKED(Get(context, key), Value);
4252 : }
4253 :
4254 :
4255 157773 : MaybeLocal<Value> v8::Object::Get(Local<Context> context, uint32_t index) {
4256 631092 : PREPARE_FOR_EXECUTION(context, Object, Get, Value);
4257 157773 : auto self = Utils::OpenHandle(this);
4258 : i::Handle<i::Object> result;
4259 : has_pending_exception =
4260 315546 : !i::JSReceiver::GetElement(isolate, self, index).ToHandle(&result);
4261 157773 : RETURN_ON_FAILED_EXECUTION(Value);
4262 157754 : RETURN_ESCAPED(Utils::ToLocal(result));
4263 : }
4264 :
4265 :
4266 0 : Local<Value> v8::Object::Get(uint32_t index) {
4267 0 : auto context = ContextFromNeverReadOnlySpaceObject(Utils::OpenHandle(this));
4268 0 : RETURN_TO_LOCAL_UNCHECKED(Get(context, index), Value);
4269 : }
4270 :
4271 :
4272 2462 : MaybeLocal<Value> v8::Object::GetPrivate(Local<Context> context,
4273 : Local<Private> key) {
4274 2462 : return Get(context, Local<Value>(reinterpret_cast<Value*>(*key)));
4275 : }
4276 :
4277 :
4278 58 : Maybe<PropertyAttribute> v8::Object::GetPropertyAttributes(
4279 : Local<Context> context, Local<Value> key) {
4280 116 : auto isolate = reinterpret_cast<i::Isolate*>(context->GetIsolate());
4281 232 : ENTER_V8(isolate, context, Object, GetPropertyAttributes,
4282 : Nothing<PropertyAttribute>(), i::HandleScope);
4283 58 : auto self = Utils::OpenHandle(this);
4284 : auto key_obj = Utils::OpenHandle(*key);
4285 116 : if (!key_obj->IsName()) {
4286 : has_pending_exception =
4287 24 : !i::Object::ToString(isolate, key_obj).ToHandle(&key_obj);
4288 12 : RETURN_ON_FAILED_EXECUTION_PRIMITIVE(PropertyAttribute);
4289 : }
4290 52 : auto key_name = i::Handle<i::Name>::cast(key_obj);
4291 52 : auto result = i::JSReceiver::GetPropertyAttributes(self, key_name);
4292 52 : has_pending_exception = result.IsNothing();
4293 52 : RETURN_ON_FAILED_EXECUTION_PRIMITIVE(PropertyAttribute);
4294 52 : if (result.FromJust() == i::ABSENT) {
4295 : return Just(static_cast<PropertyAttribute>(i::NONE));
4296 : }
4297 : return Just(static_cast<PropertyAttribute>(result.FromJust()));
4298 : }
4299 :
4300 :
4301 7730 : MaybeLocal<Value> v8::Object::GetOwnPropertyDescriptor(Local<Context> context,
4302 : Local<Name> key) {
4303 30920 : PREPARE_FOR_EXECUTION(context, Object, GetOwnPropertyDescriptor, Value);
4304 7730 : i::Handle<i::JSReceiver> obj = Utils::OpenHandle(this);
4305 : i::Handle<i::Name> key_name = Utils::OpenHandle(*key);
4306 :
4307 : i::PropertyDescriptor desc;
4308 : Maybe<bool> found =
4309 7730 : i::JSReceiver::GetOwnPropertyDescriptor(isolate, obj, key_name, &desc);
4310 7730 : has_pending_exception = found.IsNothing();
4311 7730 : RETURN_ON_FAILED_EXECUTION(Value);
4312 7730 : if (!found.FromJust()) {
4313 5 : return v8::Undefined(reinterpret_cast<v8::Isolate*>(isolate));
4314 : }
4315 15450 : RETURN_ESCAPED(Utils::ToLocal(desc.ToObject(isolate)));
4316 : }
4317 :
4318 :
4319 268441 : Local<Value> v8::Object::GetPrototype() {
4320 : auto isolate = Utils::OpenHandle(this)->GetIsolate();
4321 268441 : auto self = Utils::OpenHandle(this);
4322 268441 : i::PrototypeIterator iter(isolate, self);
4323 268441 : return Utils::ToLocal(i::PrototypeIterator::GetCurrent(iter));
4324 : }
4325 :
4326 :
4327 11221 : Maybe<bool> v8::Object::SetPrototype(Local<Context> context,
4328 : Local<Value> value) {
4329 22442 : auto isolate = reinterpret_cast<i::Isolate*>(context->GetIsolate());
4330 44884 : ENTER_V8(isolate, context, Object, SetPrototype, Nothing<bool>(),
4331 : i::HandleScope);
4332 11221 : auto self = Utils::OpenHandle(this);
4333 11221 : auto value_obj = Utils::OpenHandle(*value);
4334 : // We do not allow exceptions thrown while setting the prototype
4335 : // to propagate outside.
4336 22442 : TryCatch try_catch(reinterpret_cast<v8::Isolate*>(isolate));
4337 : auto result =
4338 11221 : i::JSReceiver::SetPrototype(self, value_obj, false, i::kThrowOnError);
4339 11221 : has_pending_exception = result.IsNothing();
4340 11221 : RETURN_ON_FAILED_EXECUTION_PRIMITIVE(bool);
4341 : return Just(true);
4342 : }
4343 :
4344 :
4345 54 : Local<Object> v8::Object::FindInstanceInPrototypeChain(
4346 : v8::Local<FunctionTemplate> tmpl) {
4347 : auto self = Utils::OpenHandle(this);
4348 : auto isolate = self->GetIsolate();
4349 : i::PrototypeIterator iter(isolate, *self, i::kStartAtReceiver);
4350 54 : auto tmpl_info = *Utils::OpenHandle(*tmpl);
4351 162 : while (!tmpl_info->IsTemplateFor(iter.GetCurrent<i::JSObject>())) {
4352 72 : iter.Advance();
4353 72 : if (iter.IsAtEnd()) return Local<Object>();
4354 108 : if (!iter.GetCurrent()->IsJSObject()) return Local<Object>();
4355 : }
4356 : // IsTemplateFor() ensures that iter.GetCurrent() can't be a Proxy here.
4357 36 : return Utils::ToLocal(i::handle(iter.GetCurrent<i::JSObject>(), isolate));
4358 : }
4359 :
4360 5986 : MaybeLocal<Array> v8::Object::GetPropertyNames(Local<Context> context) {
4361 : return GetPropertyNames(
4362 : context, v8::KeyCollectionMode::kIncludePrototypes,
4363 : static_cast<v8::PropertyFilter>(ONLY_ENUMERABLE | SKIP_SYMBOLS),
4364 5986 : v8::IndexFilter::kIncludeIndices);
4365 : }
4366 :
4367 6697 : MaybeLocal<Array> v8::Object::GetPropertyNames(
4368 : Local<Context> context, KeyCollectionMode mode,
4369 : PropertyFilter property_filter, IndexFilter index_filter,
4370 : KeyConversionMode key_conversion) {
4371 26788 : PREPARE_FOR_EXECUTION(context, Object, GetPropertyNames, Array);
4372 6697 : auto self = Utils::OpenHandle(this);
4373 : i::Handle<i::FixedArray> value;
4374 : i::KeyAccumulator accumulator(
4375 : isolate, static_cast<i::KeyCollectionMode>(mode),
4376 : static_cast<i::PropertyFilter>(property_filter));
4377 6697 : accumulator.set_skip_indices(index_filter == IndexFilter::kSkipIndices);
4378 6697 : has_pending_exception = accumulator.CollectKeys(self, self).IsNothing();
4379 6697 : RETURN_ON_FAILED_EXECUTION(Array);
4380 : value =
4381 6697 : accumulator.GetKeys(static_cast<i::GetKeysConversion>(key_conversion));
4382 : DCHECK(self->map()->EnumLength() == i::kInvalidEnumCacheSentinel ||
4383 : self->map()->EnumLength() == 0 ||
4384 : self->map()->instance_descriptors()->enum_cache()->keys() != *value);
4385 6697 : auto result = isolate->factory()->NewJSArrayWithElements(value);
4386 6697 : RETURN_ESCAPED(Utils::ToLocal(result));
4387 : }
4388 :
4389 :
4390 0 : Local<Array> v8::Object::GetPropertyNames() {
4391 0 : auto context = ContextFromNeverReadOnlySpaceObject(Utils::OpenHandle(this));
4392 0 : RETURN_TO_LOCAL_UNCHECKED(GetPropertyNames(context), Array);
4393 : }
4394 :
4395 591 : MaybeLocal<Array> v8::Object::GetOwnPropertyNames(Local<Context> context) {
4396 : return GetOwnPropertyNames(
4397 591 : context, static_cast<v8::PropertyFilter>(ONLY_ENUMERABLE | SKIP_SYMBOLS));
4398 : }
4399 :
4400 0 : Local<Array> v8::Object::GetOwnPropertyNames() {
4401 0 : auto context = ContextFromNeverReadOnlySpaceObject(Utils::OpenHandle(this));
4402 0 : RETURN_TO_LOCAL_UNCHECKED(GetOwnPropertyNames(context), Array);
4403 : }
4404 :
4405 24 : MaybeLocal<Array> v8::Object::GetOwnPropertyNames(
4406 : Local<Context> context, PropertyFilter filter,
4407 : KeyConversionMode key_conversion) {
4408 : return GetPropertyNames(context, KeyCollectionMode::kOwnOnly, filter,
4409 615 : v8::IndexFilter::kIncludeIndices, key_conversion);
4410 : }
4411 :
4412 473 : MaybeLocal<String> v8::Object::ObjectProtoToString(Local<Context> context) {
4413 1892 : PREPARE_FOR_EXECUTION(context, Object, ObjectProtoToString, String);
4414 : auto self = Utils::OpenHandle(this);
4415 : Local<Value> result;
4416 : has_pending_exception =
4417 : !ToLocal<Value>(i::Execution::Call(isolate, isolate->object_to_string(),
4418 : self, 0, nullptr),
4419 1419 : &result);
4420 473 : RETURN_ON_FAILED_EXECUTION(String);
4421 458 : RETURN_ESCAPED(Local<String>::Cast(result));
4422 : }
4423 :
4424 :
4425 3614323 : Local<String> v8::Object::GetConstructorName() {
4426 3614323 : auto self = Utils::OpenHandle(this);
4427 3614323 : i::Handle<i::String> name = i::JSReceiver::GetConstructorName(self);
4428 3614323 : return Utils::ToLocal(name);
4429 : }
4430 :
4431 6 : Maybe<bool> v8::Object::SetIntegrityLevel(Local<Context> context,
4432 : IntegrityLevel level) {
4433 12 : auto isolate = reinterpret_cast<i::Isolate*>(context->GetIsolate());
4434 24 : ENTER_V8(isolate, context, Object, SetIntegrityLevel, Nothing<bool>(),
4435 : i::HandleScope);
4436 6 : auto self = Utils::OpenHandle(this);
4437 : i::JSReceiver::IntegrityLevel i_level =
4438 6 : level == IntegrityLevel::kFrozen ? i::FROZEN : i::SEALED;
4439 : Maybe<bool> result =
4440 6 : i::JSReceiver::SetIntegrityLevel(self, i_level, i::kThrowOnError);
4441 6 : has_pending_exception = result.IsNothing();
4442 6 : RETURN_ON_FAILED_EXECUTION_PRIMITIVE(bool);
4443 5 : return result;
4444 : }
4445 :
4446 7991 : Maybe<bool> v8::Object::Delete(Local<Context> context, Local<Value> key) {
4447 15982 : auto isolate = reinterpret_cast<i::Isolate*>(context->GetIsolate());
4448 : auto self = Utils::OpenHandle(this);
4449 7991 : auto key_obj = Utils::OpenHandle(*key);
4450 15982 : if (self->IsJSProxy()) {
4451 0 : ENTER_V8(isolate, context, Object, Delete, Nothing<bool>(), i::HandleScope);
4452 : Maybe<bool> result = i::Runtime::DeleteObjectProperty(
4453 0 : isolate, self, key_obj, i::LanguageMode::kSloppy);
4454 0 : has_pending_exception = result.IsNothing();
4455 0 : RETURN_ON_FAILED_EXECUTION_PRIMITIVE(bool);
4456 0 : return result;
4457 : } else {
4458 : // If it's not a JSProxy, i::Runtime::DeleteObjectProperty should never run
4459 : // a script.
4460 31964 : ENTER_V8_NO_SCRIPT(isolate, context, Object, Delete, Nothing<bool>(),
4461 : i::HandleScope);
4462 : Maybe<bool> result = i::Runtime::DeleteObjectProperty(
4463 7991 : isolate, self, key_obj, i::LanguageMode::kSloppy);
4464 7991 : has_pending_exception = result.IsNothing();
4465 7991 : RETURN_ON_FAILED_EXECUTION_PRIMITIVE(bool);
4466 7991 : return result;
4467 : }
4468 : }
4469 :
4470 42 : Maybe<bool> v8::Object::DeletePrivate(Local<Context> context,
4471 : Local<Private> key) {
4472 84 : auto isolate = reinterpret_cast<i::Isolate*>(context->GetIsolate());
4473 : // In case of private symbols, i::Runtime::DeleteObjectProperty does not run
4474 : // any author script.
4475 168 : ENTER_V8_NO_SCRIPT(isolate, context, Object, Delete, Nothing<bool>(),
4476 : i::HandleScope);
4477 42 : auto self = Utils::OpenHandle(this);
4478 42 : auto key_obj = Utils::OpenHandle(*key);
4479 : Maybe<bool> result = i::Runtime::DeleteObjectProperty(
4480 42 : isolate, self, key_obj, i::LanguageMode::kSloppy);
4481 42 : has_pending_exception = result.IsNothing();
4482 42 : RETURN_ON_FAILED_EXECUTION_PRIMITIVE(bool);
4483 42 : return result;
4484 : }
4485 :
4486 9391 : Maybe<bool> v8::Object::Has(Local<Context> context, Local<Value> key) {
4487 18782 : auto isolate = reinterpret_cast<i::Isolate*>(context->GetIsolate());
4488 37564 : ENTER_V8(isolate, context, Object, Has, Nothing<bool>(), i::HandleScope);
4489 9391 : auto self = Utils::OpenHandle(this);
4490 : auto key_obj = Utils::OpenHandle(*key);
4491 : Maybe<bool> maybe = Nothing<bool>();
4492 : // Check if the given key is an array index.
4493 9391 : uint32_t index = 0;
4494 18782 : if (key_obj->ToArrayIndex(&index)) {
4495 0 : maybe = i::JSReceiver::HasElement(self, index);
4496 : } else {
4497 : // Convert the key to a name - possibly by calling back into JavaScript.
4498 : i::Handle<i::Name> name;
4499 18782 : if (i::Object::ToName(isolate, key_obj).ToHandle(&name)) {
4500 9391 : maybe = i::JSReceiver::HasProperty(self, name);
4501 : }
4502 : }
4503 9391 : has_pending_exception = maybe.IsNothing();
4504 9391 : RETURN_ON_FAILED_EXECUTION_PRIMITIVE(bool);
4505 9391 : return maybe;
4506 : }
4507 :
4508 :
4509 79235 : Maybe<bool> v8::Object::HasPrivate(Local<Context> context, Local<Private> key) {
4510 79235 : return HasOwnProperty(context, Local<Name>(reinterpret_cast<Name*>(*key)));
4511 : }
4512 :
4513 :
4514 10 : Maybe<bool> v8::Object::Delete(Local<Context> context, uint32_t index) {
4515 20 : auto isolate = reinterpret_cast<i::Isolate*>(context->GetIsolate());
4516 40 : ENTER_V8(isolate, context, Object, Delete, Nothing<bool>(), i::HandleScope);
4517 10 : auto self = Utils::OpenHandle(this);
4518 10 : Maybe<bool> result = i::JSReceiver::DeleteElement(self, index);
4519 10 : has_pending_exception = result.IsNothing();
4520 10 : RETURN_ON_FAILED_EXECUTION_PRIMITIVE(bool);
4521 5 : return result;
4522 : }
4523 :
4524 :
4525 30 : Maybe<bool> v8::Object::Has(Local<Context> context, uint32_t index) {
4526 60 : auto isolate = reinterpret_cast<i::Isolate*>(context->GetIsolate());
4527 120 : ENTER_V8(isolate, context, Object, Has, Nothing<bool>(), i::HandleScope);
4528 30 : auto self = Utils::OpenHandle(this);
4529 30 : auto maybe = i::JSReceiver::HasElement(self, index);
4530 30 : has_pending_exception = maybe.IsNothing();
4531 30 : RETURN_ON_FAILED_EXECUTION_PRIMITIVE(bool);
4532 30 : return maybe;
4533 : }
4534 :
4535 : template <typename Getter, typename Setter, typename Data>
4536 7910 : static Maybe<bool> ObjectSetAccessor(
4537 : Local<Context> context, Object* self, Local<Name> name, Getter getter,
4538 : Setter setter, Data data, AccessControl settings,
4539 : PropertyAttribute attributes, bool is_special_data_property,
4540 : bool replace_on_access, SideEffectType getter_side_effect_type,
4541 : SideEffectType setter_side_effect_type) {
4542 15820 : auto isolate = reinterpret_cast<i::Isolate*>(context->GetIsolate());
4543 31640 : ENTER_V8_NO_SCRIPT(isolate, context, Object, SetAccessor, Nothing<bool>(),
4544 : i::HandleScope);
4545 15820 : if (!Utils::OpenHandle(self)->IsJSObject()) return Just(false);
4546 : i::Handle<i::JSObject> obj =
4547 7910 : i::Handle<i::JSObject>::cast(Utils::OpenHandle(self));
4548 : v8::Local<AccessorSignature> signature;
4549 : i::Handle<i::AccessorInfo> info =
4550 : MakeAccessorInfo(isolate, name, getter, setter, data, settings, signature,
4551 7910 : is_special_data_property, replace_on_access);
4552 7910 : info->set_getter_side_effect_type(getter_side_effect_type);
4553 7910 : info->set_setter_side_effect_type(setter_side_effect_type);
4554 7910 : if (info.is_null()) return Nothing<bool>();
4555 7910 : bool fast = obj->HasFastProperties();
4556 : i::Handle<i::Object> result;
4557 :
4558 15820 : i::Handle<i::Name> accessor_name(info->name(), isolate);
4559 : i::PropertyAttributes attrs = static_cast<i::PropertyAttributes>(attributes);
4560 : has_pending_exception =
4561 : !i::JSObject::SetAccessor(obj, accessor_name, info, attrs)
4562 15820 : .ToHandle(&result);
4563 7910 : RETURN_ON_FAILED_EXECUTION_PRIMITIVE(bool);
4564 15810 : if (result->IsUndefined(isolate)) return Just(false);
4565 7880 : if (fast) {
4566 7868 : i::JSObject::MigrateSlowToFast(obj, 0, "APISetAccessor");
4567 : }
4568 : return Just(true);
4569 : }
4570 :
4571 7874 : Maybe<bool> Object::SetAccessor(Local<Context> context, Local<Name> name,
4572 : AccessorNameGetterCallback getter,
4573 : AccessorNameSetterCallback setter,
4574 : MaybeLocal<Value> data, AccessControl settings,
4575 : PropertyAttribute attribute,
4576 : SideEffectType getter_side_effect_type,
4577 : SideEffectType setter_side_effect_type) {
4578 : return ObjectSetAccessor(context, this, name, getter, setter,
4579 : data.FromMaybe(Local<Value>()), settings, attribute,
4580 : i::FLAG_disable_old_api_accessors, false,
4581 15748 : getter_side_effect_type, setter_side_effect_type);
4582 : }
4583 :
4584 :
4585 367494 : void Object::SetAccessorProperty(Local<Name> name, Local<Function> getter,
4586 : Local<Function> setter,
4587 : PropertyAttribute attribute,
4588 : AccessControl settings) {
4589 : // TODO(verwaest): Remove |settings|.
4590 : DCHECK_EQ(v8::DEFAULT, settings);
4591 : i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
4592 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(isolate);
4593 : i::HandleScope scope(isolate);
4594 : auto self = Utils::OpenHandle(this);
4595 1102485 : if (!self->IsJSObject()) return;
4596 : i::Handle<i::Object> getter_i = v8::Utils::OpenHandle(*getter);
4597 : i::Handle<i::Object> setter_i = v8::Utils::OpenHandle(*setter, true);
4598 367495 : if (setter_i.is_null()) setter_i = isolate->factory()->null_value();
4599 : i::JSObject::DefineAccessor(i::Handle<i::JSObject>::cast(self),
4600 : v8::Utils::OpenHandle(*name), getter_i, setter_i,
4601 367495 : static_cast<i::PropertyAttributes>(attribute));
4602 : }
4603 :
4604 16 : Maybe<bool> Object::SetNativeDataProperty(
4605 : v8::Local<v8::Context> context, v8::Local<Name> name,
4606 : AccessorNameGetterCallback getter, AccessorNameSetterCallback setter,
4607 : v8::Local<Value> data, PropertyAttribute attributes,
4608 : SideEffectType getter_side_effect_type,
4609 : SideEffectType setter_side_effect_type) {
4610 : return ObjectSetAccessor(context, this, name, getter, setter, data, DEFAULT,
4611 : attributes, true, false, getter_side_effect_type,
4612 16 : setter_side_effect_type);
4613 : }
4614 :
4615 20 : Maybe<bool> Object::SetLazyDataProperty(
4616 : v8::Local<v8::Context> context, v8::Local<Name> name,
4617 : AccessorNameGetterCallback getter, v8::Local<Value> data,
4618 : PropertyAttribute attributes, SideEffectType getter_side_effect_type,
4619 : SideEffectType setter_side_effect_type) {
4620 : return ObjectSetAccessor(context, this, name, getter,
4621 : static_cast<AccessorNameSetterCallback>(nullptr),
4622 : data, DEFAULT, attributes, true, true,
4623 20 : getter_side_effect_type, setter_side_effect_type);
4624 : }
4625 :
4626 86566 : Maybe<bool> v8::Object::HasOwnProperty(Local<Context> context,
4627 : Local<Name> key) {
4628 173132 : auto isolate = reinterpret_cast<i::Isolate*>(context->GetIsolate());
4629 346264 : ENTER_V8(isolate, context, Object, HasOwnProperty, Nothing<bool>(),
4630 : i::HandleScope);
4631 86566 : auto self = Utils::OpenHandle(this);
4632 86566 : auto key_val = Utils::OpenHandle(*key);
4633 86566 : auto result = i::JSReceiver::HasOwnProperty(self, key_val);
4634 86566 : has_pending_exception = result.IsNothing();
4635 86566 : RETURN_ON_FAILED_EXECUTION_PRIMITIVE(bool);
4636 86561 : return result;
4637 : }
4638 :
4639 25 : Maybe<bool> v8::Object::HasOwnProperty(Local<Context> context, uint32_t index) {
4640 50 : auto isolate = reinterpret_cast<i::Isolate*>(context->GetIsolate());
4641 100 : ENTER_V8(isolate, context, Object, HasOwnProperty, Nothing<bool>(),
4642 : i::HandleScope);
4643 25 : auto self = Utils::OpenHandle(this);
4644 25 : auto result = i::JSReceiver::HasOwnProperty(self, index);
4645 25 : has_pending_exception = result.IsNothing();
4646 25 : RETURN_ON_FAILED_EXECUTION_PRIMITIVE(bool);
4647 25 : return result;
4648 : }
4649 :
4650 12047 : Maybe<bool> v8::Object::HasRealNamedProperty(Local<Context> context,
4651 : Local<Name> key) {
4652 24094 : auto isolate = reinterpret_cast<i::Isolate*>(context->GetIsolate());
4653 48188 : ENTER_V8_NO_SCRIPT(isolate, context, Object, HasRealNamedProperty,
4654 : Nothing<bool>(), i::HandleScope);
4655 : auto self = Utils::OpenHandle(this);
4656 24094 : if (!self->IsJSObject()) return Just(false);
4657 12042 : auto key_val = Utils::OpenHandle(*key);
4658 : auto result = i::JSObject::HasRealNamedProperty(
4659 12042 : i::Handle<i::JSObject>::cast(self), key_val);
4660 12042 : has_pending_exception = result.IsNothing();
4661 12042 : RETURN_ON_FAILED_EXECUTION_PRIMITIVE(bool);
4662 12037 : return result;
4663 : }
4664 :
4665 :
4666 0 : bool v8::Object::HasRealNamedProperty(Local<String> key) {
4667 0 : auto context = ContextFromNeverReadOnlySpaceObject(Utils::OpenHandle(this));
4668 0 : return HasRealNamedProperty(context, key).FromMaybe(false);
4669 : }
4670 :
4671 :
4672 17 : Maybe<bool> v8::Object::HasRealIndexedProperty(Local<Context> context,
4673 : uint32_t index) {
4674 34 : auto isolate = reinterpret_cast<i::Isolate*>(context->GetIsolate());
4675 68 : ENTER_V8_NO_SCRIPT(isolate, context, Object, HasRealIndexedProperty,
4676 : Nothing<bool>(), i::HandleScope);
4677 : auto self = Utils::OpenHandle(this);
4678 34 : if (!self->IsJSObject()) return Just(false);
4679 : auto result = i::JSObject::HasRealElementProperty(
4680 17 : i::Handle<i::JSObject>::cast(self), index);
4681 17 : has_pending_exception = result.IsNothing();
4682 17 : RETURN_ON_FAILED_EXECUTION_PRIMITIVE(bool);
4683 12 : return result;
4684 : }
4685 :
4686 :
4687 0 : bool v8::Object::HasRealIndexedProperty(uint32_t index) {
4688 0 : auto context = ContextFromNeverReadOnlySpaceObject(Utils::OpenHandle(this));
4689 0 : return HasRealIndexedProperty(context, index).FromMaybe(false);
4690 : }
4691 :
4692 :
4693 5 : Maybe<bool> v8::Object::HasRealNamedCallbackProperty(Local<Context> context,
4694 : Local<Name> key) {
4695 10 : auto isolate = reinterpret_cast<i::Isolate*>(context->GetIsolate());
4696 20 : ENTER_V8_NO_SCRIPT(isolate, context, Object, HasRealNamedCallbackProperty,
4697 : Nothing<bool>(), i::HandleScope);
4698 : auto self = Utils::OpenHandle(this);
4699 10 : if (!self->IsJSObject()) return Just(false);
4700 5 : auto key_val = Utils::OpenHandle(*key);
4701 : auto result = i::JSObject::HasRealNamedCallbackProperty(
4702 5 : i::Handle<i::JSObject>::cast(self), key_val);
4703 5 : has_pending_exception = result.IsNothing();
4704 5 : RETURN_ON_FAILED_EXECUTION_PRIMITIVE(bool);
4705 0 : return result;
4706 : }
4707 :
4708 :
4709 0 : bool v8::Object::HasRealNamedCallbackProperty(Local<String> key) {
4710 0 : auto context = ContextFromNeverReadOnlySpaceObject(Utils::OpenHandle(this));
4711 0 : return HasRealNamedCallbackProperty(context, key).FromMaybe(false);
4712 : }
4713 :
4714 :
4715 0 : bool v8::Object::HasNamedLookupInterceptor() {
4716 : auto self = Utils::OpenHandle(this);
4717 0 : return self->IsJSObject() &&
4718 0 : i::Handle<i::JSObject>::cast(self)->HasNamedInterceptor();
4719 : }
4720 :
4721 :
4722 0 : bool v8::Object::HasIndexedLookupInterceptor() {
4723 : auto self = Utils::OpenHandle(this);
4724 0 : return self->IsJSObject() &&
4725 0 : i::Handle<i::JSObject>::cast(self)->HasIndexedInterceptor();
4726 : }
4727 :
4728 :
4729 6 : MaybeLocal<Value> v8::Object::GetRealNamedPropertyInPrototypeChain(
4730 : Local<Context> context, Local<Name> key) {
4731 24 : PREPARE_FOR_EXECUTION(context, Object, GetRealNamedPropertyInPrototypeChain,
4732 : Value);
4733 : i::Handle<i::JSReceiver> self = Utils::OpenHandle(this);
4734 12 : if (!self->IsJSObject()) return MaybeLocal<Value>();
4735 6 : i::Handle<i::Name> key_obj = Utils::OpenHandle(*key);
4736 6 : i::PrototypeIterator iter(isolate, self);
4737 6 : if (iter.IsAtEnd()) return MaybeLocal<Value>();
4738 : i::Handle<i::JSReceiver> proto =
4739 6 : i::PrototypeIterator::GetCurrent<i::JSReceiver>(iter);
4740 : i::LookupIterator it = i::LookupIterator::PropertyOrElement(
4741 : isolate, self, key_obj, proto,
4742 6 : i::LookupIterator::PROTOTYPE_CHAIN_SKIP_INTERCEPTOR);
4743 : Local<Value> result;
4744 6 : has_pending_exception = !ToLocal<Value>(i::Object::GetProperty(&it), &result);
4745 6 : RETURN_ON_FAILED_EXECUTION(Value);
4746 0 : if (!it.IsFound()) return MaybeLocal<Value>();
4747 0 : RETURN_ESCAPED(result);
4748 : }
4749 :
4750 :
4751 : Maybe<PropertyAttribute>
4752 6 : v8::Object::GetRealNamedPropertyAttributesInPrototypeChain(
4753 : Local<Context> context, Local<Name> key) {
4754 12 : auto isolate = reinterpret_cast<i::Isolate*>(context->GetIsolate());
4755 24 : ENTER_V8_NO_SCRIPT(isolate, context, Object,
4756 : GetRealNamedPropertyAttributesInPrototypeChain,
4757 : Nothing<PropertyAttribute>(), i::HandleScope);
4758 : i::Handle<i::JSReceiver> self = Utils::OpenHandle(this);
4759 12 : if (!self->IsJSObject()) return Nothing<PropertyAttribute>();
4760 6 : i::Handle<i::Name> key_obj = Utils::OpenHandle(*key);
4761 6 : i::PrototypeIterator iter(isolate, self);
4762 6 : if (iter.IsAtEnd()) return Nothing<PropertyAttribute>();
4763 : i::Handle<i::JSReceiver> proto =
4764 6 : i::PrototypeIterator::GetCurrent<i::JSReceiver>(iter);
4765 : i::LookupIterator it = i::LookupIterator::PropertyOrElement(
4766 : isolate, self, key_obj, proto,
4767 6 : i::LookupIterator::PROTOTYPE_CHAIN_SKIP_INTERCEPTOR);
4768 : Maybe<i::PropertyAttributes> result =
4769 6 : i::JSReceiver::GetPropertyAttributes(&it);
4770 : RETURN_ON_FAILED_EXECUTION_PRIMITIVE(PropertyAttribute);
4771 6 : if (!it.IsFound()) return Nothing<PropertyAttribute>();
4772 6 : if (result.FromJust() == i::ABSENT) return Just(None);
4773 : return Just(static_cast<PropertyAttribute>(result.FromJust()));
4774 : }
4775 :
4776 :
4777 833701 : MaybeLocal<Value> v8::Object::GetRealNamedProperty(Local<Context> context,
4778 : Local<Name> key) {
4779 3334804 : PREPARE_FOR_EXECUTION(context, Object, GetRealNamedProperty, Value);
4780 : auto self = Utils::OpenHandle(this);
4781 833701 : auto key_obj = Utils::OpenHandle(*key);
4782 : i::LookupIterator it = i::LookupIterator::PropertyOrElement(
4783 : isolate, self, key_obj, self,
4784 833701 : i::LookupIterator::PROTOTYPE_CHAIN_SKIP_INTERCEPTOR);
4785 : Local<Value> result;
4786 833701 : has_pending_exception = !ToLocal<Value>(i::Object::GetProperty(&it), &result);
4787 833701 : RETURN_ON_FAILED_EXECUTION(Value);
4788 833683 : if (!it.IsFound()) return MaybeLocal<Value>();
4789 253 : RETURN_ESCAPED(result);
4790 : }
4791 :
4792 :
4793 18 : Maybe<PropertyAttribute> v8::Object::GetRealNamedPropertyAttributes(
4794 : Local<Context> context, Local<Name> key) {
4795 36 : auto isolate = reinterpret_cast<i::Isolate*>(context->GetIsolate());
4796 72 : ENTER_V8_NO_SCRIPT(isolate, context, Object, GetRealNamedPropertyAttributes,
4797 : Nothing<PropertyAttribute>(), i::HandleScope);
4798 : auto self = Utils::OpenHandle(this);
4799 18 : auto key_obj = Utils::OpenHandle(*key);
4800 : i::LookupIterator it = i::LookupIterator::PropertyOrElement(
4801 : isolate, self, key_obj, self,
4802 18 : i::LookupIterator::PROTOTYPE_CHAIN_SKIP_INTERCEPTOR);
4803 18 : auto result = i::JSReceiver::GetPropertyAttributes(&it);
4804 : RETURN_ON_FAILED_EXECUTION_PRIMITIVE(PropertyAttribute);
4805 18 : if (!it.IsFound()) return Nothing<PropertyAttribute>();
4806 18 : if (result.FromJust() == i::ABSENT) {
4807 : return Just(static_cast<PropertyAttribute>(i::NONE));
4808 : }
4809 : return Just<PropertyAttribute>(
4810 : static_cast<PropertyAttribute>(result.FromJust()));
4811 : }
4812 :
4813 :
4814 16 : Local<v8::Object> v8::Object::Clone() {
4815 16 : auto self = i::Handle<i::JSObject>::cast(Utils::OpenHandle(this));
4816 : auto isolate = self->GetIsolate();
4817 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(isolate);
4818 16 : auto result = isolate->factory()->CopyJSObject(self);
4819 16 : CHECK(!result.is_null());
4820 16 : return Utils::ToLocal(result);
4821 : }
4822 :
4823 :
4824 302918 : Local<v8::Context> v8::Object::CreationContext() {
4825 : auto self = Utils::OpenHandle(this);
4826 302918 : return Utils::ToLocal(self->GetCreationContext());
4827 : }
4828 :
4829 :
4830 106 : int v8::Object::GetIdentityHash() {
4831 : i::DisallowHeapAllocation no_gc;
4832 : auto isolate = Utils::OpenHandle(this)->GetIsolate();
4833 : i::HandleScope scope(isolate);
4834 : auto self = Utils::OpenHandle(this);
4835 318 : return self->GetOrCreateIdentityHash(isolate)->value();
4836 : }
4837 :
4838 :
4839 24 : bool v8::Object::IsCallable() {
4840 : auto self = Utils::OpenHandle(this);
4841 48 : return self->IsCallable();
4842 : }
4843 :
4844 6 : bool v8::Object::IsConstructor() {
4845 : auto self = Utils::OpenHandle(this);
4846 12 : return self->IsConstructor();
4847 : }
4848 :
4849 114 : MaybeLocal<Value> Object::CallAsFunction(Local<Context> context,
4850 : Local<Value> recv, int argc,
4851 : Local<Value> argv[]) {
4852 228 : auto isolate = reinterpret_cast<i::Isolate*>(context->GetIsolate());
4853 228 : TRACE_EVENT_CALL_STATS_SCOPED(isolate, "v8", "V8.Execute");
4854 456 : ENTER_V8(isolate, context, Object, CallAsFunction, MaybeLocal<Value>(),
4855 : InternalEscapableScope);
4856 : i::TimerEventScope<i::TimerEventExecute> timer_scope(isolate);
4857 : auto self = Utils::OpenHandle(this);
4858 114 : auto recv_obj = Utils::OpenHandle(*recv);
4859 : STATIC_ASSERT(sizeof(v8::Local<v8::Value>) == sizeof(i::Handle<i::Object>));
4860 : i::Handle<i::Object>* args = reinterpret_cast<i::Handle<i::Object>*>(argv);
4861 : Local<Value> result;
4862 : has_pending_exception = !ToLocal<Value>(
4863 228 : i::Execution::Call(isolate, self, recv_obj, argc, args), &result);
4864 114 : RETURN_ON_FAILED_EXECUTION(Value);
4865 216 : RETURN_ESCAPED(result);
4866 : }
4867 :
4868 :
4869 0 : MaybeLocal<Value> Object::CallAsConstructor(Local<Context> context, int argc,
4870 : Local<Value> argv[]) {
4871 0 : auto isolate = reinterpret_cast<i::Isolate*>(context->GetIsolate());
4872 0 : TRACE_EVENT_CALL_STATS_SCOPED(isolate, "v8", "V8.Execute");
4873 0 : ENTER_V8(isolate, context, Object, CallAsConstructor, MaybeLocal<Value>(),
4874 : InternalEscapableScope);
4875 : i::TimerEventScope<i::TimerEventExecute> timer_scope(isolate);
4876 : auto self = Utils::OpenHandle(this);
4877 : STATIC_ASSERT(sizeof(v8::Local<v8::Value>) == sizeof(i::Handle<i::Object>));
4878 : i::Handle<i::Object>* args = reinterpret_cast<i::Handle<i::Object>*>(argv);
4879 : Local<Value> result;
4880 : has_pending_exception = !ToLocal<Value>(
4881 0 : i::Execution::New(isolate, self, self, argc, args), &result);
4882 0 : RETURN_ON_FAILED_EXECUTION(Value);
4883 0 : RETURN_ESCAPED(result);
4884 : }
4885 :
4886 5189 : MaybeLocal<Function> Function::New(Local<Context> context,
4887 : FunctionCallback callback, Local<Value> data,
4888 : int length, ConstructorBehavior behavior,
4889 : SideEffectType side_effect_type) {
4890 5189 : i::Isolate* isolate = Utils::OpenHandle(*context)->GetIsolate();
4891 10378 : LOG_API(isolate, Function, New);
4892 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(isolate);
4893 : auto templ =
4894 : FunctionTemplateNew(isolate, callback, data, Local<Signature>(), length,
4895 5189 : true, Local<Private>(), side_effect_type);
4896 5189 : if (behavior == ConstructorBehavior::kThrow) templ->RemovePrototype();
4897 10378 : return templ->GetFunction(context);
4898 : }
4899 :
4900 :
4901 0 : Local<Function> Function::New(Isolate* v8_isolate, FunctionCallback callback,
4902 : Local<Value> data, int length) {
4903 : return Function::New(v8_isolate->GetCurrentContext(), callback, data, length,
4904 0 : ConstructorBehavior::kAllow)
4905 0 : .FromMaybe(Local<Function>());
4906 : }
4907 :
4908 928 : MaybeLocal<Object> Function::NewInstance(Local<Context> context, int argc,
4909 : v8::Local<v8::Value> argv[]) const {
4910 : return NewInstanceWithSideEffectType(context, argc, argv,
4911 928 : SideEffectType::kHasSideEffect);
4912 : }
4913 :
4914 943 : MaybeLocal<Object> Function::NewInstanceWithSideEffectType(
4915 : Local<Context> context, int argc, v8::Local<v8::Value> argv[],
4916 : SideEffectType side_effect_type) const {
4917 1901 : auto isolate = reinterpret_cast<i::Isolate*>(context->GetIsolate());
4918 1886 : TRACE_EVENT_CALL_STATS_SCOPED(isolate, "v8", "V8.Execute");
4919 3772 : ENTER_V8(isolate, context, Function, NewInstance, MaybeLocal<Object>(),
4920 : InternalEscapableScope);
4921 : i::TimerEventScope<i::TimerEventExecute> timer_scope(isolate);
4922 : auto self = Utils::OpenHandle(this);
4923 : STATIC_ASSERT(sizeof(v8::Local<v8::Value>) == sizeof(i::Handle<i::Object>));
4924 : bool should_set_has_no_side_effect =
4925 958 : side_effect_type == SideEffectType::kHasNoSideEffect &&
4926 : isolate->debug_execution_mode() == i::DebugInfo::kSideEffects;
4927 943 : if (should_set_has_no_side_effect) {
4928 30 : CHECK(self->IsJSFunction() &&
4929 : i::JSFunction::cast(*self)->shared()->IsApiFunction());
4930 : i::Object obj =
4931 10 : i::JSFunction::cast(*self)->shared()->get_api_func_data()->call_code();
4932 10 : if (obj->IsCallHandlerInfo()) {
4933 10 : i::CallHandlerInfo handler_info = i::CallHandlerInfo::cast(obj);
4934 10 : if (!handler_info->IsSideEffectFreeCallHandlerInfo()) {
4935 10 : handler_info->SetNextCallHasNoSideEffect();
4936 : }
4937 : }
4938 : }
4939 : i::Handle<i::Object>* args = reinterpret_cast<i::Handle<i::Object>*>(argv);
4940 : Local<Object> result;
4941 : has_pending_exception = !ToLocal<Object>(
4942 1886 : i::Execution::New(isolate, self, self, argc, args), &result);
4943 943 : if (should_set_has_no_side_effect) {
4944 : i::Object obj =
4945 10 : i::JSFunction::cast(*self)->shared()->get_api_func_data()->call_code();
4946 10 : if (obj->IsCallHandlerInfo()) {
4947 10 : i::CallHandlerInfo handler_info = i::CallHandlerInfo::cast(obj);
4948 10 : if (has_pending_exception) {
4949 : // Restore the map if an exception prevented restoration.
4950 0 : handler_info->NextCallHasNoSideEffect();
4951 : } else {
4952 : DCHECK(handler_info->IsSideEffectCallHandlerInfo() ||
4953 : handler_info->IsSideEffectFreeCallHandlerInfo());
4954 : }
4955 : }
4956 : }
4957 943 : RETURN_ON_FAILED_EXECUTION(Object);
4958 1870 : RETURN_ESCAPED(result);
4959 : }
4960 :
4961 :
4962 357371 : MaybeLocal<v8::Value> Function::Call(Local<Context> context,
4963 : v8::Local<v8::Value> recv, int argc,
4964 : v8::Local<v8::Value> argv[]) {
4965 714743 : auto isolate = reinterpret_cast<i::Isolate*>(context->GetIsolate());
4966 714744 : TRACE_EVENT_CALL_STATS_SCOPED(isolate, "v8", "V8.Execute");
4967 1429485 : ENTER_V8(isolate, context, Function, Call, MaybeLocal<Value>(),
4968 : InternalEscapableScope);
4969 : i::TimerEventScope<i::TimerEventExecute> timer_scope(isolate);
4970 : auto self = Utils::OpenHandle(this);
4971 : Utils::ApiCheck(!self.is_null(), "v8::Function::Call",
4972 : "Function to be called is a null pointer");
4973 357372 : i::Handle<i::Object> recv_obj = Utils::OpenHandle(*recv);
4974 : STATIC_ASSERT(sizeof(v8::Local<v8::Value>) == sizeof(i::Handle<i::Object>));
4975 : i::Handle<i::Object>* args = reinterpret_cast<i::Handle<i::Object>*>(argv);
4976 : Local<Value> result;
4977 : has_pending_exception = !ToLocal<Value>(
4978 714739 : i::Execution::Call(isolate, self, recv_obj, argc, args), &result);
4979 357367 : RETURN_ON_FAILED_EXECUTION(Value);
4980 714416 : RETURN_ESCAPED(result);
4981 : }
4982 :
4983 :
4984 0 : Local<v8::Value> Function::Call(v8::Local<v8::Value> recv, int argc,
4985 : v8::Local<v8::Value> argv[]) {
4986 0 : auto context = ContextFromNeverReadOnlySpaceObject(Utils::OpenHandle(this));
4987 0 : RETURN_TO_LOCAL_UNCHECKED(Call(context, recv, argc, argv), Value);
4988 : }
4989 :
4990 :
4991 640 : void Function::SetName(v8::Local<v8::String> name) {
4992 : auto self = Utils::OpenHandle(this);
4993 1280 : if (!self->IsJSFunction()) return;
4994 640 : auto func = i::Handle<i::JSFunction>::cast(self);
4995 640 : func->shared()->SetName(*Utils::OpenHandle(*name));
4996 : }
4997 :
4998 :
4999 42 : Local<Value> Function::GetName() const {
5000 : auto self = Utils::OpenHandle(this);
5001 : i::Isolate* isolate = self->GetIsolate();
5002 84 : if (self->IsJSBoundFunction()) {
5003 0 : auto func = i::Handle<i::JSBoundFunction>::cast(self);
5004 : i::Handle<i::Object> name;
5005 0 : ASSIGN_RETURN_ON_EXCEPTION_VALUE(isolate, name,
5006 : i::JSBoundFunction::GetName(isolate, func),
5007 : Local<Value>());
5008 : return Utils::ToLocal(name);
5009 : }
5010 84 : if (self->IsJSFunction()) {
5011 42 : auto func = i::Handle<i::JSFunction>::cast(self);
5012 84 : return Utils::ToLocal(handle(func->shared()->Name(), isolate));
5013 : }
5014 0 : return ToApiHandle<Primitive>(isolate->factory()->undefined_value());
5015 : }
5016 :
5017 :
5018 6 : Local<Value> Function::GetInferredName() const {
5019 : auto self = Utils::OpenHandle(this);
5020 12 : if (!self->IsJSFunction()) {
5021 : return ToApiHandle<Primitive>(
5022 0 : self->GetIsolate()->factory()->undefined_value());
5023 : }
5024 6 : auto func = i::Handle<i::JSFunction>::cast(self);
5025 12 : return Utils::ToLocal(i::Handle<i::Object>(func->shared()->inferred_name(),
5026 12 : func->GetIsolate()));
5027 : }
5028 :
5029 :
5030 72 : Local<Value> Function::GetDebugName() const {
5031 : auto self = Utils::OpenHandle(this);
5032 144 : if (!self->IsJSFunction()) {
5033 : return ToApiHandle<Primitive>(
5034 0 : self->GetIsolate()->factory()->undefined_value());
5035 : }
5036 72 : auto func = i::Handle<i::JSFunction>::cast(self);
5037 72 : i::Handle<i::String> name = i::JSFunction::GetDebugName(func);
5038 : return Utils::ToLocal(i::Handle<i::Object>(*name, self->GetIsolate()));
5039 : }
5040 :
5041 :
5042 42 : Local<Value> Function::GetDisplayName() const {
5043 : i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
5044 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(isolate);
5045 : auto self = Utils::OpenHandle(this);
5046 84 : if (!self->IsJSFunction()) {
5047 0 : return ToApiHandle<Primitive>(isolate->factory()->undefined_value());
5048 : }
5049 42 : auto func = i::Handle<i::JSFunction>::cast(self);
5050 : i::Handle<i::String> property_name =
5051 42 : isolate->factory()->NewStringFromStaticChars("displayName");
5052 : i::Handle<i::Object> value =
5053 42 : i::JSReceiver::GetDataProperty(func, property_name);
5054 84 : if (value->IsString()) {
5055 18 : i::Handle<i::String> name = i::Handle<i::String>::cast(value);
5056 18 : if (name->length() > 0) return Utils::ToLocal(name);
5057 : }
5058 24 : return ToApiHandle<Primitive>(isolate->factory()->undefined_value());
5059 : }
5060 :
5061 :
5062 12 : ScriptOrigin Function::GetScriptOrigin() const {
5063 : auto self = Utils::OpenHandle(this);
5064 24 : if (!self->IsJSFunction()) {
5065 : return v8::ScriptOrigin(Local<Value>());
5066 : }
5067 12 : auto func = i::Handle<i::JSFunction>::cast(self);
5068 24 : if (func->shared()->script()->IsScript()) {
5069 24 : i::Handle<i::Script> script(i::Script::cast(func->shared()->script()),
5070 24 : func->GetIsolate());
5071 12 : return GetScriptOriginForScript(func->GetIsolate(), script);
5072 : }
5073 : return v8::ScriptOrigin(Local<Value>());
5074 : }
5075 :
5076 :
5077 : const int Function::kLineOffsetNotFound = -1;
5078 :
5079 :
5080 151317 : int Function::GetScriptLineNumber() const {
5081 : auto self = Utils::OpenHandle(this);
5082 302634 : if (!self->IsJSFunction()) {
5083 : return kLineOffsetNotFound;
5084 : }
5085 151312 : auto func = i::Handle<i::JSFunction>::cast(self);
5086 302624 : if (func->shared()->script()->IsScript()) {
5087 302624 : i::Handle<i::Script> script(i::Script::cast(func->shared()->script()),
5088 302624 : func->GetIsolate());
5089 151312 : return i::Script::GetLineNumber(script, func->shared()->StartPosition());
5090 : }
5091 : return kLineOffsetNotFound;
5092 : }
5093 :
5094 :
5095 146317 : int Function::GetScriptColumnNumber() const {
5096 : auto self = Utils::OpenHandle(this);
5097 292634 : if (!self->IsJSFunction()) {
5098 : return kLineOffsetNotFound;
5099 : }
5100 146312 : auto func = i::Handle<i::JSFunction>::cast(self);
5101 292624 : if (func->shared()->script()->IsScript()) {
5102 292624 : i::Handle<i::Script> script(i::Script::cast(func->shared()->script()),
5103 292624 : func->GetIsolate());
5104 146312 : return i::Script::GetColumnNumber(script, func->shared()->StartPosition());
5105 : }
5106 : return kLineOffsetNotFound;
5107 : }
5108 :
5109 :
5110 177205 : int Function::ScriptId() const {
5111 : auto self = Utils::OpenHandle(this);
5112 354410 : if (!self->IsJSFunction()) {
5113 : return v8::UnboundScript::kNoScriptId;
5114 : }
5115 177200 : auto func = i::Handle<i::JSFunction>::cast(self);
5116 354400 : if (!func->shared()->script()->IsScript()) {
5117 : return v8::UnboundScript::kNoScriptId;
5118 : }
5119 331610 : i::Handle<i::Script> script(i::Script::cast(func->shared()->script()),
5120 331610 : func->GetIsolate());
5121 : return script->id();
5122 : }
5123 :
5124 :
5125 252 : Local<v8::Value> Function::GetBoundFunction() const {
5126 : auto self = Utils::OpenHandle(this);
5127 504 : if (self->IsJSBoundFunction()) {
5128 102 : auto bound_function = i::Handle<i::JSBoundFunction>::cast(self);
5129 : auto bound_target_function = i::handle(
5130 204 : bound_function->bound_target_function(), bound_function->GetIsolate());
5131 102 : return Utils::CallableToLocal(bound_target_function);
5132 : }
5133 150 : return v8::Undefined(reinterpret_cast<v8::Isolate*>(self->GetIsolate()));
5134 : }
5135 :
5136 50 : int Name::GetIdentityHash() {
5137 : auto self = Utils::OpenHandle(this);
5138 50 : return static_cast<int>(self->Hash());
5139 : }
5140 :
5141 :
5142 27502985 : int String::Length() const {
5143 : i::Handle<i::String> str = Utils::OpenHandle(this);
5144 27502987 : return str->length();
5145 : }
5146 :
5147 :
5148 67 : bool String::IsOneByte() const {
5149 : i::Handle<i::String> str = Utils::OpenHandle(this);
5150 67 : return str->HasOnlyOneByteChars();
5151 : }
5152 :
5153 :
5154 : // Helpers for ContainsOnlyOneByteHelper
5155 : template<size_t size> struct OneByteMask;
5156 : template<> struct OneByteMask<4> {
5157 : static const uint32_t value = 0xFF00FF00;
5158 : };
5159 : template<> struct OneByteMask<8> {
5160 : static const uint64_t value = V8_2PART_UINT64_C(0xFF00FF00, FF00FF00);
5161 : };
5162 : static const uintptr_t kOneByteMask = OneByteMask<sizeof(uintptr_t)>::value;
5163 : static const uintptr_t kAlignmentMask = sizeof(uintptr_t) - 1;
5164 : static inline bool Unaligned(const uint16_t* chars) {
5165 45915 : return reinterpret_cast<const uintptr_t>(chars) & kAlignmentMask;
5166 : }
5167 :
5168 :
5169 : static inline const uint16_t* Align(const uint16_t* chars) {
5170 : return reinterpret_cast<uint16_t*>(
5171 17885 : reinterpret_cast<uintptr_t>(chars) & ~kAlignmentMask);
5172 : }
5173 :
5174 : class ContainsOnlyOneByteHelper {
5175 : public:
5176 17885 : ContainsOnlyOneByteHelper() : is_one_byte_(true) {}
5177 17885 : bool Check(i::String string) {
5178 17885 : i::ConsString cons_string = i::String::VisitFlat(this, string, 0);
5179 17885 : if (cons_string.is_null()) return is_one_byte_;
5180 30 : return CheckCons(cons_string);
5181 : }
5182 : void VisitOneByteString(const uint8_t* chars, int length) {
5183 : // Nothing to do.
5184 : }
5185 17885 : void VisitTwoByteString(const uint16_t* chars, int length) {
5186 : // Accumulated bits.
5187 : uintptr_t acc = 0;
5188 : // Align to uintptr_t.
5189 17885 : const uint16_t* end = chars + length;
5190 63800 : while (Unaligned(chars) && chars != end) {
5191 28030 : acc |= *chars++;
5192 : }
5193 : // Read word aligned in blocks,
5194 : // checking the return value at the end of each block.
5195 : const uint16_t* aligned_end = Align(end);
5196 : const int increment = sizeof(uintptr_t)/sizeof(uint16_t);
5197 : const int inner_loops = 16;
5198 51475 : while (chars + inner_loops*increment < aligned_end) {
5199 396400 : for (int i = 0; i < inner_loops; i++) {
5200 396400 : acc |= *reinterpret_cast<const uintptr_t*>(chars);
5201 396400 : chars += increment;
5202 : }
5203 : // Check for early return.
5204 24775 : if ((acc & kOneByteMask) != 0) {
5205 9070 : is_one_byte_ = false;
5206 26955 : return;
5207 : }
5208 : }
5209 : // Read the rest.
5210 468120 : while (chars != end) {
5211 459305 : acc |= *chars++;
5212 : }
5213 : // Check result.
5214 8815 : if ((acc & kOneByteMask) != 0) is_one_byte_ = false;
5215 : }
5216 :
5217 : private:
5218 40 : bool CheckCons(i::ConsString cons_string) {
5219 : while (true) {
5220 : // Check left side if flat.
5221 39610 : i::String left = cons_string->first();
5222 39610 : i::ConsString left_as_cons = i::String::VisitFlat(this, left, 0);
5223 39610 : if (!is_one_byte_) return false;
5224 : // Check right side if flat.
5225 39610 : i::String right = cons_string->second();
5226 39610 : i::ConsString right_as_cons = i::String::VisitFlat(this, right, 0);
5227 39610 : if (!is_one_byte_) return false;
5228 : // Standard recurse/iterate trick.
5229 39610 : if (!left_as_cons.is_null() && !right_as_cons.is_null()) {
5230 10 : if (left->length() < right->length()) {
5231 0 : CheckCons(left_as_cons);
5232 0 : cons_string = right_as_cons;
5233 : } else {
5234 10 : CheckCons(right_as_cons);
5235 10 : cons_string = left_as_cons;
5236 : }
5237 : // Check fast return.
5238 10 : if (!is_one_byte_) return false;
5239 39570 : continue;
5240 : }
5241 : // Descend left in place.
5242 39600 : if (!left_as_cons.is_null()) {
5243 19785 : cons_string = left_as_cons;
5244 19785 : continue;
5245 : }
5246 : // Descend right in place.
5247 19815 : if (!right_as_cons.is_null()) {
5248 19775 : cons_string = right_as_cons;
5249 19775 : continue;
5250 : }
5251 : // Terminate.
5252 40 : break;
5253 : }
5254 39610 : return is_one_byte_;
5255 : }
5256 : bool is_one_byte_;
5257 : DISALLOW_COPY_AND_ASSIGN(ContainsOnlyOneByteHelper);
5258 : };
5259 :
5260 :
5261 17905 : bool String::ContainsOnlyOneByte() const {
5262 : i::Handle<i::String> str = Utils::OpenHandle(this);
5263 17905 : if (str->HasOnlyOneByteChars()) return true;
5264 : ContainsOnlyOneByteHelper helper;
5265 17885 : return helper.Check(*str);
5266 : }
5267 :
5268 9076241 : int String::Utf8Length(Isolate* isolate) const {
5269 : i::Handle<i::String> str = Utils::OpenHandle(this);
5270 9076241 : str = i::String::Flatten(reinterpret_cast<i::Isolate*>(isolate), str);
5271 : int length = str->length();
5272 9076241 : if (length == 0) return 0;
5273 : i::DisallowHeapAllocation no_gc;
5274 9070090 : i::String::FlatContent flat = str->GetFlatContent(no_gc);
5275 : DCHECK(flat.IsFlat());
5276 : int utf8_length = 0;
5277 9070090 : if (flat.IsOneByte()) {
5278 272775175 : for (uint8_t c : flat.ToOneByteVector()) {
5279 263709786 : utf8_length += c >> 7;
5280 : }
5281 9065389 : utf8_length += length;
5282 : } else {
5283 : int last_character = unibrow::Utf16::kNoPreviousCharacter;
5284 1218861 : for (uint16_t c : flat.ToUC16Vector()) {
5285 2428320 : utf8_length += unibrow::Utf8::Length(c, last_character);
5286 1214160 : last_character = c;
5287 : }
5288 : }
5289 9070090 : return utf8_length;
5290 : }
5291 :
5292 : namespace {
5293 : // Writes the flat content of a string to a buffer. This is done in two phases.
5294 : // The first phase calculates a pessimistic estimate (writable_length) on how
5295 : // many code units can be safely written without exceeding the buffer capacity
5296 : // and without leaving at a lone surrogate. The estimated number of code units
5297 : // is then written out in one go, and the reported byte usage is used to
5298 : // correct the estimate. This is repeated until the estimate becomes <= 0 or
5299 : // all code units have been written out. The second phase writes out code
5300 : // units until the buffer capacity is reached, would be exceeded by the next
5301 : // unit, or all code units have been written out.
5302 : template <typename Char>
5303 9075803 : static int WriteUtf8Impl(i::Vector<const Char> string, char* write_start,
5304 : int write_capacity, int options,
5305 : int* utf16_chars_read_out) {
5306 9075803 : bool write_null = !(options & v8::String::NO_NULL_TERMINATION);
5307 9075803 : bool replace_invalid_utf8 = (options & v8::String::REPLACE_INVALID_UTF8);
5308 : char* current_write = write_start;
5309 9075803 : const Char* read_start = string.start();
5310 : int read_index = 0;
5311 9075803 : int read_length = string.length();
5312 : int prev_char = unibrow::Utf16::kNoPreviousCharacter;
5313 : // Do a fast loop where there is no exit capacity check.
5314 : // Need enough space to write everything but one character.
5315 : STATIC_ASSERT(unibrow::Utf16::kMaxExtraUtf8BytesForOneUtf16CodeUnit == 3);
5316 : static const int kMaxSizePerChar = sizeof(Char) == 1 ? 2 : 3;
5317 27372262 : while (read_index < read_length) {
5318 9233784 : int up_to = read_length;
5319 9233784 : if (write_capacity != -1) {
5320 : int remaining_capacity =
5321 177303 : write_capacity - static_cast<int>(current_write - write_start);
5322 : int writable_length =
5323 177303 : (remaining_capacity - kMaxSizePerChar) / kMaxSizePerChar;
5324 : // Need to drop into slow loop.
5325 177303 : if (writable_length <= 0) break;
5326 328350 : up_to = std::min(up_to, read_index + writable_length);
5327 : }
5328 : // Write the characters to the stream.
5329 : if (sizeof(Char) == 1) {
5330 : // Simply memcpy if we only have ASCII characters.
5331 : uint8_t char_mask = 0;
5332 9214815 : for (int i = read_index; i < up_to; i++) char_mask |= read_start[i];
5333 9214815 : if ((char_mask & 0x80) == 0) {
5334 9214616 : int copy_length = up_to - read_index;
5335 9214616 : memcpy(current_write, read_start + read_index, copy_length);
5336 9214616 : current_write += copy_length;
5337 : read_index = up_to;
5338 : } else {
5339 6378 : for (; read_index < up_to; read_index++) {
5340 6378 : current_write += unibrow::Utf8::EncodeOneByte(
5341 6378 : current_write, static_cast<uint8_t>(read_start[read_index]));
5342 : DCHECK(write_capacity == -1 ||
5343 : (current_write - write_start) <= write_capacity);
5344 : }
5345 : }
5346 : } else {
5347 136199640 : for (; read_index < up_to; read_index++) {
5348 136199640 : uint16_t character = read_start[read_index];
5349 136199640 : current_write += unibrow::Utf8::Encode(current_write, character,
5350 136199640 : prev_char, replace_invalid_utf8);
5351 136199640 : prev_char = character;
5352 : DCHECK(write_capacity == -1 ||
5353 : (current_write - write_start) <= write_capacity);
5354 : }
5355 : }
5356 : }
5357 9075803 : if (read_index < read_length) {
5358 : DCHECK_NE(-1, write_capacity);
5359 : // Aborted due to limited capacity. Check capacity on each iteration.
5360 : int remaining_capacity =
5361 13128 : write_capacity - static_cast<int>(current_write - write_start);
5362 : DCHECK_GE(remaining_capacity, 0);
5363 52490 : for (; read_index < read_length && remaining_capacity > 0; read_index++) {
5364 39411 : uint32_t character = read_start[read_index];
5365 : int written = 0;
5366 : // We can't use a local buffer here because Encode needs to modify
5367 : // previous characters in the stream. We know, however, that
5368 : // exactly one character will be advanced.
5369 78083 : if (unibrow::Utf16::IsSurrogatePair(prev_char, character)) {
5370 0 : written = unibrow::Utf8::Encode(current_write, character, prev_char,
5371 0 : replace_invalid_utf8);
5372 : DCHECK_EQ(written, 1);
5373 : } else {
5374 : // Use a scratch buffer to check the required characters.
5375 : char temp_buffer[unibrow::Utf8::kMaxEncodedSize];
5376 : // Encoding a surrogate pair to Utf8 always takes 4 bytes.
5377 : static const int kSurrogatePairEncodedSize =
5378 : static_cast<int>(unibrow::Utf8::kMaxEncodedSize);
5379 : // For REPLACE_INVALID_UTF8, catch the case where we cut off in the
5380 : // middle of a surrogate pair. Abort before encoding the pair instead.
5381 39438 : if (replace_invalid_utf8 &&
5382 : remaining_capacity < kSurrogatePairEncodedSize &&
5383 : unibrow::Utf16::IsLeadSurrogate(character) &&
5384 : read_index + 1 < read_length &&
5385 6 : unibrow::Utf16::IsTrailSurrogate(read_start[read_index + 1])) {
5386 : write_null = false;
5387 49 : break;
5388 : }
5389 : // Can't encode using prev_char as gcc has array bounds issues.
5390 39405 : written = unibrow::Utf8::Encode(temp_buffer, character,
5391 : unibrow::Utf16::kNoPreviousCharacter,
5392 39405 : replace_invalid_utf8);
5393 39405 : if (written > remaining_capacity) {
5394 : // Won't fit. Abort and do not null-terminate the result.
5395 : write_null = false;
5396 : break;
5397 : }
5398 : // Copy over the character from temp_buffer.
5399 39500 : for (int i = 0; i < written; i++) current_write[i] = temp_buffer[i];
5400 : }
5401 :
5402 39362 : current_write += written;
5403 39362 : remaining_capacity -= written;
5404 690 : prev_char = character;
5405 : }
5406 : }
5407 :
5408 : // Write out number of utf16 characters written to the stream.
5409 9075803 : if (utf16_chars_read_out != nullptr) *utf16_chars_read_out = read_index;
5410 :
5411 : // Only null-terminate if there's space.
5412 9075803 : if (write_null && (write_capacity == -1 ||
5413 : (current_write - write_start) < write_capacity)) {
5414 9062714 : *current_write++ = '\0';
5415 : }
5416 9075803 : return static_cast<int>(current_write - write_start);
5417 : }
5418 : } // anonymous namespace
5419 :
5420 9075803 : int String::WriteUtf8(Isolate* v8_isolate, char* buffer, int capacity,
5421 : int* nchars_ref, int options) const {
5422 : i::Handle<i::String> str = Utils::OpenHandle(this);
5423 9075803 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(v8_isolate);
5424 18151606 : LOG_API(isolate, String, WriteUtf8);
5425 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(isolate);
5426 9075803 : str = i::String::Flatten(isolate, str);
5427 : i::DisallowHeapAllocation no_gc;
5428 9075803 : i::String::FlatContent content = str->GetFlatContent(no_gc);
5429 9075803 : if (content.IsOneByte()) {
5430 : return WriteUtf8Impl<uint8_t>(content.ToOneByteVector(), buffer, capacity,
5431 9071436 : options, nchars_ref);
5432 : } else {
5433 : return WriteUtf8Impl<uint16_t>(content.ToUC16Vector(), buffer, capacity,
5434 4367 : options, nchars_ref);
5435 : }
5436 : }
5437 :
5438 : template <typename CharType>
5439 18521168 : static inline int WriteHelper(i::Isolate* isolate, const String* string,
5440 : CharType* buffer, int start, int length,
5441 : int options) {
5442 18521168 : LOG_API(isolate, String, Write);
5443 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(isolate);
5444 : DCHECK(start >= 0 && length >= -1);
5445 : i::Handle<i::String> str = Utils::OpenHandle(string);
5446 9260584 : str = i::String::Flatten(isolate, str);
5447 9260584 : int end = start + length;
5448 18521066 : if ((length == -1) || (length > str->length() - start) )
5449 : end = str->length();
5450 9260584 : if (end < 0) return 0;
5451 9260584 : i::String::WriteToFlat(*str, buffer, start, end);
5452 9260584 : if (!(options & String::NO_NULL_TERMINATION) &&
5453 : (length == -1 || end - start < length)) {
5454 66 : buffer[end - start] = '\0';
5455 : }
5456 9260584 : return end - start;
5457 : }
5458 :
5459 :
5460 67 : int String::WriteOneByte(Isolate* isolate, uint8_t* buffer, int start,
5461 : int length, int options) const {
5462 : return WriteHelper(reinterpret_cast<i::Isolate*>(isolate), this, buffer,
5463 67 : start, length, options);
5464 : }
5465 :
5466 :
5467 9260511 : int String::Write(Isolate* isolate, uint16_t* buffer, int start, int length,
5468 : int options) const {
5469 : return WriteHelper(reinterpret_cast<i::Isolate*>(isolate), this, buffer,
5470 9260517 : start, length, options);
5471 : }
5472 :
5473 :
5474 67 : bool v8::String::IsExternal() const {
5475 : i::Handle<i::String> str = Utils::OpenHandle(this);
5476 67 : return i::StringShape(*str).IsExternalTwoByte();
5477 : }
5478 :
5479 :
5480 27 : bool v8::String::IsExternalOneByte() const {
5481 : i::Handle<i::String> str = Utils::OpenHandle(this);
5482 27 : return i::StringShape(*str).IsExternalOneByte();
5483 : }
5484 :
5485 :
5486 0 : void v8::String::VerifyExternalStringResource(
5487 : v8::String::ExternalStringResource* value) const {
5488 : i::DisallowHeapAllocation no_allocation;
5489 0 : i::String str = *Utils::OpenHandle(this);
5490 : const v8::String::ExternalStringResource* expected;
5491 :
5492 0 : if (str->IsThinString()) {
5493 0 : str = i::ThinString::cast(str)->actual();
5494 : }
5495 :
5496 0 : if (i::StringShape(str).IsExternalTwoByte()) {
5497 : const void* resource = i::ExternalTwoByteString::cast(str)->resource();
5498 : expected = reinterpret_cast<const ExternalStringResource*>(resource);
5499 : } else {
5500 : expected = nullptr;
5501 : }
5502 0 : CHECK_EQ(expected, value);
5503 0 : }
5504 :
5505 0 : void v8::String::VerifyExternalStringResourceBase(
5506 : v8::String::ExternalStringResourceBase* value, Encoding encoding) const {
5507 : i::DisallowHeapAllocation no_allocation;
5508 0 : i::String str = *Utils::OpenHandle(this);
5509 : const v8::String::ExternalStringResourceBase* expected;
5510 : Encoding expectedEncoding;
5511 :
5512 0 : if (str->IsThinString()) {
5513 0 : str = i::ThinString::cast(str)->actual();
5514 : }
5515 :
5516 0 : if (i::StringShape(str).IsExternalOneByte()) {
5517 : const void* resource = i::ExternalOneByteString::cast(str)->resource();
5518 : expected = reinterpret_cast<const ExternalStringResourceBase*>(resource);
5519 : expectedEncoding = ONE_BYTE_ENCODING;
5520 0 : } else if (i::StringShape(str).IsExternalTwoByte()) {
5521 : const void* resource = i::ExternalTwoByteString::cast(str)->resource();
5522 : expected = reinterpret_cast<const ExternalStringResourceBase*>(resource);
5523 : expectedEncoding = TWO_BYTE_ENCODING;
5524 : } else {
5525 : expected = nullptr;
5526 : expectedEncoding =
5527 0 : str->IsOneByteRepresentation() ? ONE_BYTE_ENCODING : TWO_BYTE_ENCODING;
5528 : }
5529 0 : CHECK_EQ(expected, value);
5530 0 : CHECK_EQ(expectedEncoding, encoding);
5531 0 : }
5532 :
5533 0 : String::ExternalStringResource* String::GetExternalStringResourceSlow() const {
5534 : i::DisallowHeapAllocation no_allocation;
5535 : typedef internal::Internals I;
5536 0 : i::String str = *Utils::OpenHandle(this);
5537 :
5538 0 : if (str->IsThinString()) {
5539 0 : str = i::ThinString::cast(str)->actual();
5540 : }
5541 :
5542 0 : if (i::StringShape(str).IsExternalTwoByte()) {
5543 : void* value = I::ReadRawField<void*>(str.ptr(), I::kStringResourceOffset);
5544 0 : return reinterpret_cast<String::ExternalStringResource*>(value);
5545 : }
5546 : return nullptr;
5547 : }
5548 :
5549 6 : String::ExternalStringResourceBase* String::GetExternalStringResourceBaseSlow(
5550 : String::Encoding* encoding_out) const {
5551 : i::DisallowHeapAllocation no_allocation;
5552 : typedef internal::Internals I;
5553 : ExternalStringResourceBase* resource = nullptr;
5554 6 : i::String str = *Utils::OpenHandle(this);
5555 :
5556 6 : if (str->IsThinString()) {
5557 0 : str = i::ThinString::cast(str)->actual();
5558 : }
5559 :
5560 : internal::Address string = str.ptr();
5561 : int type = I::GetInstanceType(string) & I::kFullStringRepresentationMask;
5562 6 : *encoding_out = static_cast<Encoding>(type & I::kStringEncodingMask);
5563 12 : if (i::StringShape(str).IsExternalOneByte() ||
5564 : i::StringShape(str).IsExternalTwoByte()) {
5565 : void* value = I::ReadRawField<void*>(string, I::kStringResourceOffset);
5566 : resource = static_cast<ExternalStringResourceBase*>(value);
5567 : }
5568 6 : return resource;
5569 : }
5570 :
5571 : const v8::String::ExternalOneByteStringResource*
5572 11 : v8::String::GetExternalOneByteStringResource() const {
5573 : i::DisallowHeapAllocation no_allocation;
5574 11 : i::String str = *Utils::OpenHandle(this);
5575 11 : if (i::StringShape(str).IsExternalOneByte()) {
5576 : return i::ExternalOneByteString::cast(str)->resource();
5577 0 : } else if (str->IsThinString()) {
5578 0 : str = i::ThinString::cast(str)->actual();
5579 0 : if (i::StringShape(str).IsExternalOneByte()) {
5580 : return i::ExternalOneByteString::cast(str)->resource();
5581 : }
5582 : }
5583 : return nullptr;
5584 : }
5585 :
5586 :
5587 57836 : Local<Value> Symbol::Name() const {
5588 : i::Handle<i::Symbol> sym = Utils::OpenHandle(this);
5589 :
5590 : i::Isolate* isolate;
5591 57836 : if (!i::Isolate::FromWritableHeapObject(*sym, &isolate)) {
5592 : // If the Symbol is in RO_SPACE, then its name must be too. Since RO_SPACE
5593 : // objects are immovable we can use the Handle(Address*) constructor with
5594 : // the address of the name field in the Symbol object without needing an
5595 : // isolate.
5596 : #ifdef V8_COMPRESS_POINTERS
5597 : // Compressed fields can't serve as handle locations.
5598 : // TODO(ishell): get Isolate as a parameter.
5599 : isolate = i::Isolate::Current();
5600 : #else
5601 : i::Handle<i::HeapObject> ro_name(reinterpret_cast<i::Address*>(
5602 57565 : sym->GetFieldAddress(i::Symbol::kNameOffset)));
5603 : return Utils::ToLocal(ro_name);
5604 : #endif
5605 : }
5606 :
5607 813 : i::Handle<i::Object> name(sym->name(), isolate);
5608 :
5609 : return Utils::ToLocal(name);
5610 : }
5611 :
5612 :
5613 12 : Local<Value> Private::Name() const {
5614 12 : return reinterpret_cast<const Symbol*>(this)->Name();
5615 : }
5616 :
5617 :
5618 328838 : double Number::Value() const {
5619 : i::Handle<i::Object> obj = Utils::OpenHandle(this);
5620 328838 : return obj->Number();
5621 : }
5622 :
5623 :
5624 21790 : bool Boolean::Value() const {
5625 : i::Handle<i::Object> obj = Utils::OpenHandle(this);
5626 43580 : return obj->IsTrue();
5627 : }
5628 :
5629 :
5630 11876 : int64_t Integer::Value() const {
5631 : i::Handle<i::Object> obj = Utils::OpenHandle(this);
5632 23752 : if (obj->IsSmi()) {
5633 11840 : return i::Smi::ToInt(*obj);
5634 : } else {
5635 36 : return static_cast<int64_t>(obj->Number());
5636 : }
5637 : }
5638 :
5639 :
5640 48031 : int32_t Int32::Value() const {
5641 : i::Handle<i::Object> obj = Utils::OpenHandle(this);
5642 96062 : if (obj->IsSmi()) {
5643 48031 : return i::Smi::ToInt(*obj);
5644 : } else {
5645 0 : return static_cast<int32_t>(obj->Number());
5646 : }
5647 : }
5648 :
5649 :
5650 7263 : uint32_t Uint32::Value() const {
5651 : i::Handle<i::Object> obj = Utils::OpenHandle(this);
5652 14526 : if (obj->IsSmi()) {
5653 7245 : return i::Smi::ToInt(*obj);
5654 : } else {
5655 18 : return static_cast<uint32_t>(obj->Number());
5656 : }
5657 : }
5658 :
5659 4556 : int v8::Object::InternalFieldCount() {
5660 : i::Handle<i::JSReceiver> self = Utils::OpenHandle(this);
5661 9112 : if (!self->IsJSObject()) return 0;
5662 9112 : return i::Handle<i::JSObject>::cast(self)->GetEmbedderFieldCount();
5663 : }
5664 :
5665 2655 : static bool InternalFieldOK(i::Handle<i::JSReceiver> obj, int index,
5666 : const char* location) {
5667 : return Utils::ApiCheck(
5668 10620 : obj->IsJSObject() &&
5669 7965 : (index < i::Handle<i::JSObject>::cast(obj)->GetEmbedderFieldCount()),
5670 2655 : location, "Internal field out of bounds");
5671 : }
5672 :
5673 576 : Local<Value> v8::Object::SlowGetInternalField(int index) {
5674 : i::Handle<i::JSReceiver> obj = Utils::OpenHandle(this);
5675 : const char* location = "v8::Object::GetInternalField()";
5676 576 : if (!InternalFieldOK(obj, index, location)) return Local<Value>();
5677 : i::Handle<i::Object> value(i::JSObject::cast(*obj)->GetEmbedderField(index),
5678 1152 : obj->GetIsolate());
5679 : return Utils::ToLocal(value);
5680 : }
5681 :
5682 524 : void v8::Object::SetInternalField(int index, v8::Local<Value> value) {
5683 524 : i::Handle<i::JSReceiver> obj = Utils::OpenHandle(this);
5684 : const char* location = "v8::Object::SetInternalField()";
5685 524 : if (!InternalFieldOK(obj, index, location)) return;
5686 : i::Handle<i::Object> val = Utils::OpenHandle(*value);
5687 1048 : i::Handle<i::JSObject>::cast(obj)->SetEmbedderField(index, *val);
5688 : }
5689 :
5690 135 : void* v8::Object::SlowGetAlignedPointerFromInternalField(int index) {
5691 : i::Handle<i::JSReceiver> obj = Utils::OpenHandle(this);
5692 : const char* location = "v8::Object::GetAlignedPointerFromInternalField()";
5693 135 : if (!InternalFieldOK(obj, index, location)) return nullptr;
5694 : void* result;
5695 : Utils::ApiCheck(i::EmbedderDataSlot(i::JSObject::cast(*obj), index)
5696 135 : .ToAlignedPointer(&result),
5697 : location, "Unaligned pointer");
5698 135 : return result;
5699 : }
5700 :
5701 1420 : void v8::Object::SetAlignedPointerInInternalField(int index, void* value) {
5702 : i::Handle<i::JSReceiver> obj = Utils::OpenHandle(this);
5703 : const char* location = "v8::Object::SetAlignedPointerInInternalField()";
5704 2840 : if (!InternalFieldOK(obj, index, location)) return;
5705 : Utils::ApiCheck(i::EmbedderDataSlot(i::JSObject::cast(*obj), index)
5706 1420 : .store_aligned_pointer(value),
5707 : location, "Unaligned pointer");
5708 : DCHECK_EQ(value, GetAlignedPointerFromInternalField(index));
5709 : }
5710 :
5711 12 : void v8::Object::SetAlignedPointerInInternalFields(int argc, int indices[],
5712 : void* values[]) {
5713 : i::Handle<i::JSReceiver> obj = Utils::OpenHandle(this);
5714 : const char* location = "v8::Object::SetAlignedPointerInInternalFields()";
5715 : i::DisallowHeapAllocation no_gc;
5716 12 : i::JSObject js_obj = i::JSObject::cast(*obj);
5717 12 : int nof_embedder_fields = js_obj->GetEmbedderFieldCount();
5718 36 : for (int i = 0; i < argc; i++) {
5719 24 : int index = indices[i];
5720 24 : if (!Utils::ApiCheck(index < nof_embedder_fields, location,
5721 24 : "Internal field out of bounds")) {
5722 0 : return;
5723 : }
5724 24 : void* value = values[i];
5725 : Utils::ApiCheck(
5726 24 : i::EmbedderDataSlot(js_obj, index).store_aligned_pointer(value),
5727 : location, "Unaligned pointer");
5728 : DCHECK_EQ(value, GetAlignedPointerFromInternalField(index));
5729 : }
5730 : }
5731 :
5732 949918 : static void* ExternalValue(i::Object obj) {
5733 : // Obscure semantics for undefined, but somehow checked in our unit tests...
5734 949933 : if (obj->IsUndefined()) {
5735 : return nullptr;
5736 : }
5737 949932 : i::Object foreign = i::JSObject::cast(obj)->GetEmbedderField(0);
5738 949940 : return reinterpret_cast<void*>(i::Foreign::cast(foreign)->foreign_address());
5739 : }
5740 :
5741 : // --- E n v i r o n m e n t ---
5742 :
5743 :
5744 61281 : void v8::V8::InitializePlatform(Platform* platform) {
5745 61281 : i::V8::InitializePlatform(platform);
5746 61281 : }
5747 :
5748 :
5749 60151 : void v8::V8::ShutdownPlatform() {
5750 60151 : i::V8::ShutdownPlatform();
5751 60151 : }
5752 :
5753 :
5754 61617 : bool v8::V8::Initialize() {
5755 61617 : i::V8::Initialize();
5756 : #ifdef V8_USE_EXTERNAL_STARTUP_DATA
5757 61617 : i::ReadNatives();
5758 : #endif
5759 61617 : return true;
5760 : }
5761 :
5762 : #if V8_OS_LINUX || V8_OS_MACOSX
5763 7 : bool TryHandleWebAssemblyTrapPosix(int sig_code, siginfo_t* info,
5764 : void* context) {
5765 : #if V8_TARGET_ARCH_X64 && !V8_OS_ANDROID
5766 7 : return i::trap_handler::TryHandleSignal(sig_code, info, context);
5767 : #else
5768 : return false;
5769 : #endif
5770 : }
5771 :
5772 0 : bool V8::TryHandleSignal(int signum, void* info, void* context) {
5773 : return TryHandleWebAssemblyTrapPosix(
5774 0 : signum, reinterpret_cast<siginfo_t*>(info), context);
5775 : }
5776 : #endif
5777 :
5778 : #if V8_OS_WIN
5779 : bool TryHandleWebAssemblyTrapWindows(EXCEPTION_POINTERS* exception) {
5780 : #if V8_TARGET_ARCH_X64
5781 : return i::trap_handler::TryHandleWasmTrap(exception);
5782 : #endif
5783 : return false;
5784 : }
5785 : #endif
5786 :
5787 0 : bool V8::RegisterDefaultSignalHandler() {
5788 0 : return v8::internal::trap_handler::RegisterDefaultTrapHandler();
5789 : }
5790 :
5791 57113 : bool V8::EnableWebAssemblyTrapHandler(bool use_v8_signal_handler) {
5792 57113 : return v8::internal::trap_handler::EnableTrapHandler(use_v8_signal_handler);
5793 : }
5794 :
5795 0 : void v8::V8::SetEntropySource(EntropySource entropy_source) {
5796 0 : base::RandomNumberGenerator::SetEntropySource(entropy_source);
5797 0 : }
5798 :
5799 :
5800 0 : void v8::V8::SetReturnAddressLocationResolver(
5801 : ReturnAddressLocationResolver return_address_resolver) {
5802 0 : i::StackFrame::SetReturnAddressLocationResolver(return_address_resolver);
5803 0 : }
5804 :
5805 31878 : bool v8::V8::Dispose() {
5806 31878 : i::V8::TearDown();
5807 : #ifdef V8_USE_EXTERNAL_STARTUP_DATA
5808 31878 : i::DisposeNatives();
5809 : #endif
5810 31878 : return true;
5811 : }
5812 :
5813 31 : HeapStatistics::HeapStatistics()
5814 : : total_heap_size_(0),
5815 : total_heap_size_executable_(0),
5816 : total_physical_size_(0),
5817 : total_available_size_(0),
5818 : used_heap_size_(0),
5819 : heap_size_limit_(0),
5820 : malloced_memory_(0),
5821 : external_memory_(0),
5822 : peak_malloced_memory_(0),
5823 : does_zap_garbage_(false),
5824 : number_of_native_contexts_(0),
5825 31 : number_of_detached_contexts_(0) {}
5826 :
5827 40 : HeapSpaceStatistics::HeapSpaceStatistics()
5828 : : space_name_(nullptr),
5829 : space_size_(0),
5830 : space_used_size_(0),
5831 : space_available_size_(0),
5832 40 : physical_space_size_(0) {}
5833 :
5834 0 : HeapObjectStatistics::HeapObjectStatistics()
5835 : : object_type_(nullptr),
5836 : object_sub_type_(nullptr),
5837 : object_count_(0),
5838 0 : object_size_(0) {}
5839 :
5840 0 : HeapCodeStatistics::HeapCodeStatistics()
5841 : : code_and_metadata_size_(0),
5842 : bytecode_and_metadata_size_(0),
5843 0 : external_script_source_size_(0) {}
5844 :
5845 0 : bool v8::V8::InitializeICU(const char* icu_data_file) {
5846 0 : return i::InitializeICU(icu_data_file);
5847 : }
5848 :
5849 58263 : bool v8::V8::InitializeICUDefaultLocation(const char* exec_path,
5850 : const char* icu_data_file) {
5851 58263 : return i::InitializeICUDefaultLocation(exec_path, icu_data_file);
5852 : }
5853 :
5854 61288 : void v8::V8::InitializeExternalStartupData(const char* directory_path) {
5855 61288 : i::InitializeExternalStartupData(directory_path);
5856 61288 : }
5857 :
5858 :
5859 0 : void v8::V8::InitializeExternalStartupData(const char* natives_blob,
5860 : const char* snapshot_blob) {
5861 0 : i::InitializeExternalStartupData(natives_blob, snapshot_blob);
5862 0 : }
5863 :
5864 :
5865 33 : const char* v8::V8::GetVersion() {
5866 33 : return i::Version::GetVersion();
5867 : }
5868 :
5869 : template <typename ObjectType>
5870 : struct InvokeBootstrapper;
5871 :
5872 : template <>
5873 : struct InvokeBootstrapper<i::Context> {
5874 : i::Handle<i::Context> Invoke(
5875 91890 : i::Isolate* isolate, i::MaybeHandle<i::JSGlobalProxy> maybe_global_proxy,
5876 : v8::Local<v8::ObjectTemplate> global_proxy_template,
5877 : v8::ExtensionConfiguration* extensions, size_t context_snapshot_index,
5878 : v8::DeserializeInternalFieldsCallback embedder_fields_deserializer) {
5879 : return isolate->bootstrapper()->CreateEnvironment(
5880 : maybe_global_proxy, global_proxy_template, extensions,
5881 91890 : context_snapshot_index, embedder_fields_deserializer);
5882 : }
5883 : };
5884 :
5885 : template <>
5886 : struct InvokeBootstrapper<i::JSGlobalProxy> {
5887 : i::Handle<i::JSGlobalProxy> Invoke(
5888 18 : i::Isolate* isolate, i::MaybeHandle<i::JSGlobalProxy> maybe_global_proxy,
5889 : v8::Local<v8::ObjectTemplate> global_proxy_template,
5890 : v8::ExtensionConfiguration* extensions, size_t context_snapshot_index,
5891 : v8::DeserializeInternalFieldsCallback embedder_fields_deserializer) {
5892 : USE(extensions);
5893 : USE(context_snapshot_index);
5894 : return isolate->bootstrapper()->NewRemoteContext(maybe_global_proxy,
5895 18 : global_proxy_template);
5896 : }
5897 : };
5898 :
5899 : template <typename ObjectType>
5900 91908 : static i::Handle<ObjectType> CreateEnvironment(
5901 : i::Isolate* isolate, v8::ExtensionConfiguration* extensions,
5902 : v8::MaybeLocal<ObjectTemplate> maybe_global_template,
5903 : v8::MaybeLocal<Value> maybe_global_proxy, size_t context_snapshot_index,
5904 : v8::DeserializeInternalFieldsCallback embedder_fields_deserializer) {
5905 : i::Handle<ObjectType> result;
5906 :
5907 : {
5908 : ENTER_V8_FOR_NEW_CONTEXT(isolate);
5909 : v8::Local<ObjectTemplate> proxy_template;
5910 : i::Handle<i::FunctionTemplateInfo> proxy_constructor;
5911 : i::Handle<i::FunctionTemplateInfo> global_constructor;
5912 : i::Handle<i::Object> named_interceptor(
5913 : isolate->factory()->undefined_value());
5914 : i::Handle<i::Object> indexed_interceptor(
5915 : isolate->factory()->undefined_value());
5916 :
5917 91908 : if (!maybe_global_template.IsEmpty()) {
5918 : v8::Local<v8::ObjectTemplate> global_template =
5919 : maybe_global_template.ToLocalChecked();
5920 : // Make sure that the global_template has a constructor.
5921 54332 : global_constructor = EnsureConstructor(isolate, *global_template);
5922 :
5923 : // Create a fresh template for the global proxy object.
5924 : proxy_template = ObjectTemplate::New(
5925 : reinterpret_cast<v8::Isolate*>(isolate));
5926 54332 : proxy_constructor = EnsureConstructor(isolate, *proxy_template);
5927 :
5928 : // Set the global template to be the prototype template of
5929 : // global proxy template.
5930 54332 : i::FunctionTemplateInfo::SetPrototypeTemplate(
5931 54332 : isolate, proxy_constructor, Utils::OpenHandle(*global_template));
5932 :
5933 54332 : proxy_template->SetInternalFieldCount(
5934 54332 : global_template->InternalFieldCount());
5935 :
5936 : // Migrate security handlers from global_template to
5937 : // proxy_template. Temporarily removing access check
5938 : // information from the global template.
5939 108664 : if (!global_constructor->GetAccessCheckInfo()->IsUndefined(isolate)) {
5940 141 : i::FunctionTemplateInfo::SetAccessCheckInfo(
5941 : isolate, proxy_constructor,
5942 282 : i::handle(global_constructor->GetAccessCheckInfo(), isolate));
5943 141 : proxy_constructor->set_needs_access_check(
5944 423 : global_constructor->needs_access_check());
5945 141 : global_constructor->set_needs_access_check(false);
5946 141 : i::FunctionTemplateInfo::SetAccessCheckInfo(
5947 : isolate, global_constructor,
5948 141 : i::ReadOnlyRoots(isolate).undefined_value_handle());
5949 : }
5950 :
5951 : // Same for other interceptors. If the global constructor has
5952 : // interceptors, we need to replace them temporarily with noop
5953 : // interceptors, so the map is correctly marked as having interceptors,
5954 : // but we don't invoke any.
5955 108664 : if (!global_constructor->GetNamedPropertyHandler()->IsUndefined(
5956 108664 : isolate)) {
5957 144 : named_interceptor =
5958 288 : handle(global_constructor->GetNamedPropertyHandler(), isolate);
5959 144 : i::FunctionTemplateInfo::SetNamedPropertyHandler(
5960 : isolate, global_constructor,
5961 144 : i::ReadOnlyRoots(isolate).noop_interceptor_info_handle());
5962 : }
5963 108664 : if (!global_constructor->GetIndexedPropertyHandler()->IsUndefined(
5964 108664 : isolate)) {
5965 0 : indexed_interceptor =
5966 0 : handle(global_constructor->GetIndexedPropertyHandler(), isolate);
5967 0 : i::FunctionTemplateInfo::SetIndexedPropertyHandler(
5968 : isolate, global_constructor,
5969 0 : i::ReadOnlyRoots(isolate).noop_interceptor_info_handle());
5970 : }
5971 : }
5972 :
5973 : i::MaybeHandle<i::JSGlobalProxy> maybe_proxy;
5974 91908 : if (!maybe_global_proxy.IsEmpty()) {
5975 74 : maybe_proxy = i::Handle<i::JSGlobalProxy>::cast(
5976 : Utils::OpenHandle(*maybe_global_proxy.ToLocalChecked()));
5977 : }
5978 : // Create the environment.
5979 : InvokeBootstrapper<ObjectType> invoke;
5980 : result =
5981 : invoke.Invoke(isolate, maybe_proxy, proxy_template, extensions,
5982 : context_snapshot_index, embedder_fields_deserializer);
5983 :
5984 : // Restore the access check info and interceptors on the global template.
5985 91909 : if (!maybe_global_template.IsEmpty()) {
5986 : DCHECK(!global_constructor.is_null());
5987 : DCHECK(!proxy_constructor.is_null());
5988 54332 : i::FunctionTemplateInfo::SetAccessCheckInfo(
5989 : isolate, global_constructor,
5990 108664 : i::handle(proxy_constructor->GetAccessCheckInfo(), isolate));
5991 54332 : global_constructor->set_needs_access_check(
5992 162996 : proxy_constructor->needs_access_check());
5993 54332 : i::FunctionTemplateInfo::SetNamedPropertyHandler(
5994 : isolate, global_constructor, named_interceptor);
5995 54332 : i::FunctionTemplateInfo::SetIndexedPropertyHandler(
5996 : isolate, global_constructor, indexed_interceptor);
5997 : }
5998 : }
5999 : // Leave V8.
6000 :
6001 91909 : return result;
6002 : }
6003 :
6004 91890 : Local<Context> NewContext(
6005 : v8::Isolate* external_isolate, v8::ExtensionConfiguration* extensions,
6006 : v8::MaybeLocal<ObjectTemplate> global_template,
6007 : v8::MaybeLocal<Value> global_object, size_t context_snapshot_index,
6008 : v8::DeserializeInternalFieldsCallback embedder_fields_deserializer) {
6009 91891 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(external_isolate);
6010 : // TODO(jkummerow): This is for crbug.com/713699. Remove it if it doesn't
6011 : // fail.
6012 : // Sanity-check that the isolate is initialized and usable.
6013 183781 : CHECK(isolate->builtins()->builtin(i::Builtins::kIllegal)->IsCode());
6014 :
6015 183782 : TRACE_EVENT_CALL_STATS_SCOPED(isolate, "v8", "V8.NewContext");
6016 183782 : LOG_API(isolate, Context, New);
6017 : i::HandleScope scope(isolate);
6018 : ExtensionConfiguration no_extensions;
6019 91891 : if (extensions == nullptr) extensions = &no_extensions;
6020 : i::Handle<i::Context> env = CreateEnvironment<i::Context>(
6021 : isolate, extensions, global_template, global_object,
6022 91891 : context_snapshot_index, embedder_fields_deserializer);
6023 91891 : if (env.is_null()) {
6024 40 : if (isolate->has_pending_exception()) isolate->clear_pending_exception();
6025 40 : return Local<Context>();
6026 : }
6027 183742 : return Utils::ToLocal(scope.CloseAndEscape(env));
6028 : }
6029 :
6030 91850 : Local<Context> v8::Context::New(
6031 : v8::Isolate* external_isolate, v8::ExtensionConfiguration* extensions,
6032 : v8::MaybeLocal<ObjectTemplate> global_template,
6033 : v8::MaybeLocal<Value> global_object,
6034 : DeserializeInternalFieldsCallback internal_fields_deserializer) {
6035 : return NewContext(external_isolate, extensions, global_template,
6036 91850 : global_object, 0, internal_fields_deserializer);
6037 : }
6038 :
6039 45 : MaybeLocal<Context> v8::Context::FromSnapshot(
6040 : v8::Isolate* external_isolate, size_t context_snapshot_index,
6041 : v8::DeserializeInternalFieldsCallback embedder_fields_deserializer,
6042 : v8::ExtensionConfiguration* extensions, MaybeLocal<Value> global_object) {
6043 45 : size_t index_including_default_context = context_snapshot_index + 1;
6044 45 : if (!i::Snapshot::HasContextSnapshot(
6045 : reinterpret_cast<i::Isolate*>(external_isolate),
6046 45 : index_including_default_context)) {
6047 5 : return MaybeLocal<Context>();
6048 : }
6049 : return NewContext(external_isolate, extensions, MaybeLocal<ObjectTemplate>(),
6050 : global_object, index_including_default_context,
6051 40 : embedder_fields_deserializer);
6052 : }
6053 :
6054 18 : MaybeLocal<Object> v8::Context::NewRemoteContext(
6055 : v8::Isolate* external_isolate, v8::Local<ObjectTemplate> global_template,
6056 : v8::MaybeLocal<v8::Value> global_object) {
6057 18 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(external_isolate);
6058 36 : LOG_API(isolate, Context, NewRemoteContext);
6059 : i::HandleScope scope(isolate);
6060 : i::Handle<i::FunctionTemplateInfo> global_constructor =
6061 18 : EnsureConstructor(isolate, *global_template);
6062 36 : Utils::ApiCheck(global_constructor->needs_access_check(),
6063 : "v8::Context::NewRemoteContext",
6064 : "Global template needs to have access checks enabled.");
6065 : i::Handle<i::AccessCheckInfo> access_check_info = i::handle(
6066 : i::AccessCheckInfo::cast(global_constructor->GetAccessCheckInfo()),
6067 36 : isolate);
6068 36 : Utils::ApiCheck(access_check_info->named_interceptor() != i::Object(),
6069 : "v8::Context::NewRemoteContext",
6070 : "Global template needs to have access check handlers.");
6071 : i::Handle<i::JSGlobalProxy> global_proxy =
6072 : CreateEnvironment<i::JSGlobalProxy>(isolate, nullptr, global_template,
6073 : global_object, 0,
6074 18 : DeserializeInternalFieldsCallback());
6075 18 : if (global_proxy.is_null()) {
6076 0 : if (isolate->has_pending_exception()) isolate->clear_pending_exception();
6077 0 : return MaybeLocal<Object>();
6078 : }
6079 : return Utils::ToLocal(
6080 18 : scope.CloseAndEscape(i::Handle<i::JSObject>::cast(global_proxy)));
6081 : }
6082 :
6083 264 : void v8::Context::SetSecurityToken(Local<Value> token) {
6084 : i::Handle<i::Context> env = Utils::OpenHandle(this);
6085 : i::Handle<i::Object> token_handle = Utils::OpenHandle(*token);
6086 264 : env->set_security_token(*token_handle);
6087 264 : }
6088 :
6089 :
6090 0 : void v8::Context::UseDefaultSecurityToken() {
6091 : i::Handle<i::Context> env = Utils::OpenHandle(this);
6092 0 : env->set_security_token(env->global_object());
6093 0 : }
6094 :
6095 :
6096 18 : Local<Value> v8::Context::GetSecurityToken() {
6097 : i::Handle<i::Context> env = Utils::OpenHandle(this);
6098 : i::Isolate* isolate = env->GetIsolate();
6099 18 : i::Object security_token = env->security_token();
6100 : i::Handle<i::Object> token_handle(security_token, isolate);
6101 18 : return Utils::ToLocal(token_handle);
6102 : }
6103 :
6104 :
6105 28779450 : v8::Isolate* Context::GetIsolate() {
6106 : i::Handle<i::Context> env = Utils::OpenHandle(this);
6107 28779461 : return reinterpret_cast<Isolate*>(env->GetIsolate());
6108 : }
6109 :
6110 435460 : v8::Local<v8::Object> Context::Global() {
6111 : i::Handle<i::Context> context = Utils::OpenHandle(this);
6112 : i::Isolate* isolate = context->GetIsolate();
6113 870919 : i::Handle<i::Object> global(context->global_proxy(), isolate);
6114 : // TODO(dcarney): This should always return the global proxy
6115 : // but can't presently as calls to GetProtoype will return the wrong result.
6116 870920 : if (i::Handle<i::JSGlobalProxy>::cast(
6117 1306380 : global)->IsDetachedFrom(context->global_object())) {
6118 130 : global = i::Handle<i::Object>(context->global_object(), isolate);
6119 : }
6120 435460 : return Utils::ToLocal(i::Handle<i::JSObject>::cast(global));
6121 : }
6122 :
6123 :
6124 107 : void Context::DetachGlobal() {
6125 : i::Handle<i::Context> context = Utils::OpenHandle(this);
6126 107 : i::Isolate* isolate = context->GetIsolate();
6127 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(isolate);
6128 107 : isolate->bootstrapper()->DetachGlobal(context);
6129 107 : }
6130 :
6131 :
6132 45 : Local<v8::Object> Context::GetExtrasBindingObject() {
6133 : i::Handle<i::Context> context = Utils::OpenHandle(this);
6134 : i::Isolate* isolate = context->GetIsolate();
6135 90 : i::Handle<i::JSObject> binding(context->extras_binding_object(), isolate);
6136 45 : return Utils::ToLocal(binding);
6137 : }
6138 :
6139 :
6140 158 : void Context::AllowCodeGenerationFromStrings(bool allow) {
6141 : i::Handle<i::Context> context = Utils::OpenHandle(this);
6142 : i::Isolate* isolate = context->GetIsolate();
6143 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(isolate);
6144 : context->set_allow_code_gen_from_strings(
6145 : allow ? i::ReadOnlyRoots(isolate).true_value()
6146 474 : : i::ReadOnlyRoots(isolate).false_value());
6147 158 : }
6148 :
6149 :
6150 9327 : bool Context::IsCodeGenerationFromStringsAllowed() {
6151 : i::Handle<i::Context> context = Utils::OpenHandle(this);
6152 18654 : return !context->allow_code_gen_from_strings()->IsFalse(
6153 27981 : context->GetIsolate());
6154 : }
6155 :
6156 :
6157 5 : void Context::SetErrorMessageForCodeGenerationFromStrings(Local<String> error) {
6158 : i::Handle<i::Context> context = Utils::OpenHandle(this);
6159 : i::Handle<i::String> error_handle = Utils::OpenHandle(*error);
6160 10 : context->set_error_message_for_code_gen_from_strings(*error_handle);
6161 5 : }
6162 :
6163 : namespace {
6164 175 : i::Address* GetSerializedDataFromFixedArray(i::Isolate* isolate,
6165 : i::FixedArray list, size_t index) {
6166 175 : if (index < static_cast<size_t>(list->length())) {
6167 150 : int int_index = static_cast<int>(index);
6168 150 : i::Object object = list->get(int_index);
6169 150 : if (!object->IsTheHole(isolate)) {
6170 80 : list->set_the_hole(isolate, int_index);
6171 : // Shrink the list so that the last element is not the hole (unless it's
6172 : // the first element, because we don't want to end up with a non-canonical
6173 : // empty FixedArray).
6174 80 : int last = list->length() - 1;
6175 80 : while (last >= 0 && list->is_the_hole(isolate, last)) last--;
6176 80 : if (last != -1) list->Shrink(isolate, last + 1);
6177 : return i::Handle<i::Object>(object, isolate).location();
6178 : }
6179 : }
6180 : return nullptr;
6181 : }
6182 : } // anonymous namespace
6183 :
6184 90 : i::Address* Context::GetDataFromSnapshotOnce(size_t index) {
6185 : auto context = Utils::OpenHandle(this);
6186 : i::Isolate* i_isolate = context->GetIsolate();
6187 90 : i::FixedArray list = context->serialized_objects();
6188 90 : return GetSerializedDataFromFixedArray(i_isolate, list, index);
6189 : }
6190 :
6191 2506 : MaybeLocal<v8::Object> ObjectTemplate::NewInstance(Local<Context> context) {
6192 10024 : PREPARE_FOR_EXECUTION(context, ObjectTemplate, NewInstance, Object);
6193 2506 : auto self = Utils::OpenHandle(this);
6194 : Local<Object> result;
6195 : has_pending_exception = !ToLocal<Object>(
6196 5012 : i::ApiNatives::InstantiateObject(isolate, self), &result);
6197 2506 : RETURN_ON_FAILED_EXECUTION(Object);
6198 2506 : RETURN_ESCAPED(result);
6199 : }
6200 :
6201 :
6202 0 : Local<v8::Object> ObjectTemplate::NewInstance() {
6203 : Local<Context> context =
6204 : reinterpret_cast<v8::Isolate*>(Utils::OpenHandle(this)->GetIsolate())
6205 0 : ->GetCurrentContext();
6206 0 : RETURN_TO_LOCAL_UNCHECKED(NewInstance(context), Object);
6207 : }
6208 :
6209 0 : void v8::ObjectTemplate::CheckCast(Data* that) {
6210 : i::Handle<i::Object> obj = Utils::OpenHandle(that);
6211 0 : Utils::ApiCheck(obj->IsObjectTemplateInfo(), "v8::ObjectTemplate::Cast",
6212 : "Could not convert to object template");
6213 0 : }
6214 :
6215 0 : void v8::FunctionTemplate::CheckCast(Data* that) {
6216 : i::Handle<i::Object> obj = Utils::OpenHandle(that);
6217 0 : Utils::ApiCheck(obj->IsFunctionTemplateInfo(), "v8::FunctionTemplate::Cast",
6218 : "Could not convert to function template");
6219 0 : }
6220 :
6221 0 : void v8::Signature::CheckCast(Data* that) {
6222 : i::Handle<i::Object> obj = Utils::OpenHandle(that);
6223 0 : Utils::ApiCheck(obj->IsFunctionTemplateInfo(), "v8::Signature::Cast",
6224 : "Could not convert to signature");
6225 0 : }
6226 :
6227 0 : void v8::AccessorSignature::CheckCast(Data* that) {
6228 : i::Handle<i::Object> obj = Utils::OpenHandle(that);
6229 0 : Utils::ApiCheck(obj->IsFunctionTemplateInfo(), "v8::AccessorSignature::Cast",
6230 : "Could not convert to accessor signature");
6231 0 : }
6232 :
6233 10842 : MaybeLocal<v8::Function> FunctionTemplate::GetFunction(Local<Context> context) {
6234 43368 : PREPARE_FOR_EXECUTION(context, FunctionTemplate, GetFunction, Function);
6235 10842 : auto self = Utils::OpenHandle(this);
6236 : Local<Function> result;
6237 : has_pending_exception =
6238 21684 : !ToLocal<Function>(i::ApiNatives::InstantiateFunction(self), &result);
6239 10842 : RETURN_ON_FAILED_EXECUTION(Function);
6240 10842 : RETURN_ESCAPED(result);
6241 : }
6242 :
6243 :
6244 0 : Local<v8::Function> FunctionTemplate::GetFunction() {
6245 : Local<Context> context =
6246 : reinterpret_cast<v8::Isolate*>(Utils::OpenHandle(this)->GetIsolate())
6247 0 : ->GetCurrentContext();
6248 0 : RETURN_TO_LOCAL_UNCHECKED(GetFunction(context), Function);
6249 : }
6250 :
6251 8 : MaybeLocal<v8::Object> FunctionTemplate::NewRemoteInstance() {
6252 : auto self = Utils::OpenHandle(this);
6253 8 : i::Isolate* isolate = self->GetIsolate();
6254 16 : LOG_API(isolate, FunctionTemplate, NewRemoteInstance);
6255 : i::HandleScope scope(isolate);
6256 : i::Handle<i::FunctionTemplateInfo> constructor =
6257 16 : EnsureConstructor(isolate, *InstanceTemplate());
6258 16 : Utils::ApiCheck(constructor->needs_access_check(),
6259 : "v8::FunctionTemplate::NewRemoteInstance",
6260 : "InstanceTemplate needs to have access checks enabled.");
6261 : i::Handle<i::AccessCheckInfo> access_check_info = i::handle(
6262 16 : i::AccessCheckInfo::cast(constructor->GetAccessCheckInfo()), isolate);
6263 16 : Utils::ApiCheck(access_check_info->named_interceptor() != i::Object(),
6264 : "v8::FunctionTemplate::NewRemoteInstance",
6265 : "InstanceTemplate needs to have access check handlers.");
6266 : i::Handle<i::JSObject> object;
6267 8 : if (!i::ApiNatives::InstantiateRemoteObject(
6268 16 : Utils::OpenHandle(*InstanceTemplate()))
6269 16 : .ToHandle(&object)) {
6270 0 : if (isolate->has_pending_exception()) {
6271 0 : isolate->OptionalRescheduleException(true);
6272 : }
6273 0 : return MaybeLocal<Object>();
6274 : }
6275 8 : return Utils::ToLocal(scope.CloseAndEscape(object));
6276 : }
6277 :
6278 152 : bool FunctionTemplate::HasInstance(v8::Local<v8::Value> value) {
6279 : auto self = Utils::OpenHandle(this);
6280 : auto obj = Utils::OpenHandle(*value);
6281 456 : if (obj->IsJSObject() && self->IsTemplateFor(i::JSObject::cast(*obj))) {
6282 : return true;
6283 : }
6284 76 : if (obj->IsJSGlobalProxy()) {
6285 : // If it's a global proxy, then test with the global object. Note that the
6286 : // inner global object may not necessarily be a JSGlobalObject.
6287 : i::PrototypeIterator iter(self->GetIsolate(),
6288 2 : i::JSObject::cast(*obj)->map());
6289 : // The global proxy should always have a prototype, as it is a bug to call
6290 : // this on a detached JSGlobalProxy.
6291 : DCHECK(!iter.IsAtEnd());
6292 4 : return self->IsTemplateFor(iter.GetCurrent<i::JSObject>());
6293 : }
6294 : return false;
6295 : }
6296 :
6297 :
6298 3942 : Local<External> v8::External::New(Isolate* isolate, void* value) {
6299 : STATIC_ASSERT(sizeof(value) == sizeof(i::Address));
6300 3942 : i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
6301 7884 : LOG_API(i_isolate, External, New);
6302 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(i_isolate);
6303 3942 : i::Handle<i::JSObject> external = i_isolate->factory()->NewExternal(value);
6304 3942 : return Utils::ExternalToLocal(external);
6305 : }
6306 :
6307 :
6308 949916 : void* External::Value() const {
6309 949921 : return ExternalValue(*Utils::OpenHandle(this));
6310 : }
6311 :
6312 :
6313 : // anonymous namespace for string creation helper functions
6314 : namespace {
6315 :
6316 : inline int StringLength(const char* string) {
6317 : return i::StrLength(string);
6318 : }
6319 :
6320 :
6321 : inline int StringLength(const uint8_t* string) {
6322 : return i::StrLength(reinterpret_cast<const char*>(string));
6323 : }
6324 :
6325 :
6326 : inline int StringLength(const uint16_t* string) {
6327 : int length = 0;
6328 4053 : while (string[length] != '\0')
6329 4000 : length++;
6330 : return length;
6331 : }
6332 :
6333 : V8_WARN_UNUSED_RESULT
6334 9342129 : inline i::MaybeHandle<i::String> NewString(i::Factory* factory,
6335 : v8::NewStringType type,
6336 : i::Vector<const char> string) {
6337 9342129 : if (type == v8::NewStringType::kInternalized) {
6338 70884 : return factory->InternalizeUtf8String(string);
6339 : }
6340 9271245 : return factory->NewStringFromUtf8(string);
6341 : }
6342 :
6343 : V8_WARN_UNUSED_RESULT
6344 26 : inline i::MaybeHandle<i::String> NewString(i::Factory* factory,
6345 : v8::NewStringType type,
6346 : i::Vector<const uint8_t> string) {
6347 26 : if (type == v8::NewStringType::kInternalized) {
6348 0 : return factory->InternalizeOneByteString(string);
6349 : }
6350 26 : return factory->NewStringFromOneByte(string);
6351 : }
6352 :
6353 : V8_WARN_UNUSED_RESULT
6354 1227815 : inline i::MaybeHandle<i::String> NewString(i::Factory* factory,
6355 : v8::NewStringType type,
6356 : i::Vector<const uint16_t> string) {
6357 1227815 : if (type == v8::NewStringType::kInternalized) {
6358 0 : return factory->InternalizeTwoByteString(string);
6359 : }
6360 1227815 : return factory->NewStringFromTwoByte(string);
6361 : }
6362 :
6363 :
6364 : STATIC_ASSERT(v8::String::kMaxLength == i::String::kMaxLength);
6365 :
6366 : } // anonymous namespace
6367 :
6368 : // TODO(dcarney): throw a context free exception.
6369 : #define NEW_STRING(isolate, class_name, function_name, Char, data, type, \
6370 : length) \
6371 : MaybeLocal<String> result; \
6372 : if (length == 0) { \
6373 : result = String::Empty(isolate); \
6374 : } else if (length > i::String::kMaxLength) { \
6375 : result = MaybeLocal<String>(); \
6376 : } else { \
6377 : i::Isolate* i_isolate = reinterpret_cast<internal::Isolate*>(isolate); \
6378 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(i_isolate); \
6379 : LOG_API(i_isolate, class_name, function_name); \
6380 : if (length < 0) length = StringLength(data); \
6381 : i::Handle<i::String> handle_result = \
6382 : NewString(i_isolate->factory(), type, \
6383 : i::Vector<const Char>(data, length)) \
6384 : .ToHandleChecked(); \
6385 : result = Utils::ToLocal(handle_result); \
6386 : }
6387 :
6388 0 : Local<String> String::NewFromUtf8(Isolate* isolate,
6389 : const char* data,
6390 : NewStringType type,
6391 : int length) {
6392 0 : NEW_STRING(isolate, String, NewFromUtf8, char, data,
6393 : static_cast<v8::NewStringType>(type), length);
6394 0 : RETURN_TO_LOCAL_UNCHECKED(result, String);
6395 : }
6396 :
6397 :
6398 9342139 : MaybeLocal<String> String::NewFromUtf8(Isolate* isolate, const char* data,
6399 : v8::NewStringType type, int length) {
6400 56052789 : NEW_STRING(isolate, String, NewFromUtf8, char, data, type, length);
6401 9342139 : return result;
6402 : }
6403 :
6404 :
6405 41 : MaybeLocal<String> String::NewFromOneByte(Isolate* isolate, const uint8_t* data,
6406 : v8::NewStringType type, int length) {
6407 176 : NEW_STRING(isolate, String, NewFromOneByte, uint8_t, data, type, length);
6408 41 : return result;
6409 : }
6410 :
6411 :
6412 0 : Local<String> String::NewFromTwoByte(Isolate* isolate,
6413 : const uint16_t* data,
6414 : NewStringType type,
6415 : int length) {
6416 0 : NEW_STRING(isolate, String, NewFromTwoByte, uint16_t, data,
6417 : static_cast<v8::NewStringType>(type), length);
6418 0 : RETURN_TO_LOCAL_UNCHECKED(result, String);
6419 : }
6420 :
6421 :
6422 1231265 : MaybeLocal<String> String::NewFromTwoByte(Isolate* isolate,
6423 : const uint16_t* data,
6424 : v8::NewStringType type, int length) {
6425 7370345 : NEW_STRING(isolate, String, NewFromTwoByte, uint16_t, data, type, length);
6426 1231265 : return result;
6427 : }
6428 :
6429 140624 : Local<String> v8::String::Concat(Isolate* v8_isolate, Local<String> left,
6430 : Local<String> right) {
6431 140624 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(v8_isolate);
6432 : i::Handle<i::String> left_string = Utils::OpenHandle(*left);
6433 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(isolate);
6434 281248 : LOG_API(isolate, String, Concat);
6435 : i::Handle<i::String> right_string = Utils::OpenHandle(*right);
6436 : // If we are steering towards a range error, do not wait for the error to be
6437 : // thrown, and return the null handle instead.
6438 140624 : if (left_string->length() + right_string->length() > i::String::kMaxLength) {
6439 10 : return Local<String>();
6440 : }
6441 : i::Handle<i::String> result = isolate->factory()->NewConsString(
6442 281228 : left_string, right_string).ToHandleChecked();
6443 : return Utils::ToLocal(result);
6444 : }
6445 :
6446 18910 : MaybeLocal<String> v8::String::NewExternalTwoByte(
6447 : Isolate* isolate, v8::String::ExternalStringResource* resource) {
6448 18910 : CHECK(resource && resource->data());
6449 : // TODO(dcarney): throw a context free exception.
6450 18910 : if (resource->length() > static_cast<size_t>(i::String::kMaxLength)) {
6451 6 : return MaybeLocal<String>();
6452 : }
6453 18904 : i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
6454 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(i_isolate);
6455 37808 : LOG_API(i_isolate, String, NewExternalTwoByte);
6456 18904 : if (resource->length() > 0) {
6457 : i::Handle<i::String> string = i_isolate->factory()
6458 : ->NewExternalStringFromTwoByte(resource)
6459 37788 : .ToHandleChecked();
6460 18894 : return Utils::ToLocal(string);
6461 : } else {
6462 : // The resource isn't going to be used, free it immediately.
6463 10 : resource->Dispose();
6464 10 : return Utils::ToLocal(i_isolate->factory()->empty_string());
6465 : }
6466 : }
6467 :
6468 :
6469 1292 : MaybeLocal<String> v8::String::NewExternalOneByte(
6470 : Isolate* isolate, v8::String::ExternalOneByteStringResource* resource) {
6471 1292 : CHECK(resource && resource->data());
6472 : // TODO(dcarney): throw a context free exception.
6473 1292 : if (resource->length() > static_cast<size_t>(i::String::kMaxLength)) {
6474 6 : return MaybeLocal<String>();
6475 : }
6476 1286 : i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
6477 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(i_isolate);
6478 2572 : LOG_API(i_isolate, String, NewExternalOneByte);
6479 1286 : if (resource->length() > 0) {
6480 : i::Handle<i::String> string = i_isolate->factory()
6481 : ->NewExternalStringFromOneByte(resource)
6482 2562 : .ToHandleChecked();
6483 1281 : return Utils::ToLocal(string);
6484 : } else {
6485 : // The resource isn't going to be used, free it immediately.
6486 5 : resource->Dispose();
6487 5 : return Utils::ToLocal(i_isolate->factory()->empty_string());
6488 : }
6489 : }
6490 :
6491 :
6492 0 : Local<String> v8::String::NewExternal(
6493 : Isolate* isolate, v8::String::ExternalOneByteStringResource* resource) {
6494 0 : RETURN_TO_LOCAL_UNCHECKED(NewExternalOneByte(isolate, resource), String);
6495 : }
6496 :
6497 :
6498 295 : bool v8::String::MakeExternal(v8::String::ExternalStringResource* resource) {
6499 : i::DisallowHeapAllocation no_allocation;
6500 :
6501 295 : i::String obj = *Utils::OpenHandle(this);
6502 :
6503 295 : if (obj->IsThinString()) {
6504 0 : obj = i::ThinString::cast(obj)->actual();
6505 : }
6506 :
6507 295 : if (!obj->SupportsExternalization()) {
6508 : return false;
6509 : }
6510 :
6511 : // It is safe to call FromWritable because SupportsExternalization already
6512 : // checked that the object is writable.
6513 : i::Isolate* isolate;
6514 295 : i::Isolate::FromWritableHeapObject(obj, &isolate);
6515 295 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(isolate);
6516 :
6517 295 : CHECK(resource && resource->data());
6518 :
6519 295 : bool result = obj->MakeExternal(resource);
6520 : DCHECK(result);
6521 : DCHECK(obj->IsExternalString());
6522 : return result;
6523 : }
6524 :
6525 :
6526 295 : bool v8::String::MakeExternal(
6527 : v8::String::ExternalOneByteStringResource* resource) {
6528 : i::DisallowHeapAllocation no_allocation;
6529 :
6530 295 : i::String obj = *Utils::OpenHandle(this);
6531 :
6532 295 : if (obj->IsThinString()) {
6533 0 : obj = i::ThinString::cast(obj)->actual();
6534 : }
6535 :
6536 295 : if (!obj->SupportsExternalization()) {
6537 : return false;
6538 : }
6539 :
6540 : // It is safe to call FromWritable because SupportsExternalization already
6541 : // checked that the object is writable.
6542 : i::Isolate* isolate;
6543 295 : i::Isolate::FromWritableHeapObject(obj, &isolate);
6544 295 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(isolate);
6545 :
6546 295 : CHECK(resource && resource->data());
6547 :
6548 295 : bool result = obj->MakeExternal(resource);
6549 : DCHECK(result);
6550 : DCHECK(obj->IsExternalString());
6551 : return result;
6552 : }
6553 :
6554 :
6555 20 : bool v8::String::CanMakeExternal() {
6556 : i::DisallowHeapAllocation no_allocation;
6557 20 : i::String obj = *Utils::OpenHandle(this);
6558 :
6559 20 : if (obj->IsThinString()) {
6560 0 : obj = i::ThinString::cast(obj)->actual();
6561 : }
6562 :
6563 20 : if (!obj->SupportsExternalization()) {
6564 : return false;
6565 : }
6566 :
6567 : // Only old space strings should be externalized.
6568 20 : return !i::Heap::InNewSpace(obj);
6569 : }
6570 :
6571 19248 : bool v8::String::StringEquals(Local<String> that) {
6572 : auto self = Utils::OpenHandle(this);
6573 : auto other = Utils::OpenHandle(*that);
6574 19248 : return self->Equals(*other);
6575 : }
6576 :
6577 79949 : Isolate* v8::Object::GetIsolate() {
6578 : i::Isolate* i_isolate = Utils::OpenHandle(this)->GetIsolate();
6579 79949 : return reinterpret_cast<Isolate*>(i_isolate);
6580 : }
6581 :
6582 :
6583 107672 : Local<v8::Object> v8::Object::New(Isolate* isolate) {
6584 107672 : i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
6585 215344 : LOG_API(i_isolate, Object, New);
6586 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(i_isolate);
6587 : i::Handle<i::JSObject> obj =
6588 107672 : i_isolate->factory()->NewJSObject(i_isolate->object_function());
6589 107672 : return Utils::ToLocal(obj);
6590 : }
6591 :
6592 48 : Local<v8::Object> v8::Object::New(Isolate* isolate,
6593 : Local<Value> prototype_or_null,
6594 : Local<Name>* names, Local<Value>* values,
6595 : size_t length) {
6596 48 : i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
6597 : i::Handle<i::Object> proto = Utils::OpenHandle(*prototype_or_null);
6598 168 : if (!Utils::ApiCheck(proto->IsNull() || proto->IsJSReceiver(),
6599 : "v8::Object::New", "prototype must be null or object")) {
6600 0 : return Local<v8::Object>();
6601 : }
6602 96 : LOG_API(i_isolate, Object, New);
6603 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(i_isolate);
6604 :
6605 : // We assume that this API is mostly used to create objects with named
6606 : // properties, and so we default to creating a properties backing store
6607 : // large enough to hold all of them, while we start with no elements
6608 : // (see http://bit.ly/v8-fast-object-create-cpp for the motivation).
6609 : i::Handle<i::NameDictionary> properties =
6610 48 : i::NameDictionary::New(i_isolate, static_cast<int>(length));
6611 : i::Handle<i::FixedArrayBase> elements =
6612 : i_isolate->factory()->empty_fixed_array();
6613 61566 : for (size_t i = 0; i < length; ++i) {
6614 61518 : i::Handle<i::Name> name = Utils::OpenHandle(*names[i]);
6615 61518 : i::Handle<i::Object> value = Utils::OpenHandle(*values[i]);
6616 :
6617 : // See if the {name} is a valid array index, in which case we need to
6618 : // add the {name}/{value} pair to the {elements}, otherwise they end
6619 : // up in the {properties} backing store.
6620 : uint32_t index;
6621 61518 : if (name->AsArrayIndex(&index)) {
6622 : // If this is the first element, allocate a proper
6623 : // dictionary elements backing store for {elements}.
6624 36 : if (!elements->IsNumberDictionary()) {
6625 : elements =
6626 12 : i::NumberDictionary::New(i_isolate, static_cast<int>(length));
6627 : }
6628 : elements = i::NumberDictionary::Set(
6629 : i_isolate, i::Handle<i::NumberDictionary>::cast(elements), index,
6630 18 : value);
6631 : } else {
6632 : // Internalize the {name} first.
6633 61500 : name = i_isolate->factory()->InternalizeName(name);
6634 61500 : int const entry = properties->FindEntry(i_isolate, name);
6635 61500 : if (entry == i::NameDictionary::kNotFound) {
6636 : // Add the {name}/{value} pair as a new entry.
6637 : properties = i::NameDictionary::Add(i_isolate, properties, name, value,
6638 61488 : i::PropertyDetails::Empty());
6639 : } else {
6640 : // Overwrite the {entry} with the {value}.
6641 24 : properties->ValueAtPut(entry, *value);
6642 : }
6643 : }
6644 : }
6645 : i::Handle<i::JSObject> obj =
6646 : i_isolate->factory()->NewSlowJSObjectWithPropertiesAndElements(
6647 48 : proto, properties, elements);
6648 : return Utils::ToLocal(obj);
6649 : }
6650 :
6651 6 : Local<v8::Value> v8::NumberObject::New(Isolate* isolate, double value) {
6652 6 : i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
6653 12 : LOG_API(i_isolate, NumberObject, New);
6654 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(i_isolate);
6655 6 : i::Handle<i::Object> number = i_isolate->factory()->NewNumber(value);
6656 : i::Handle<i::Object> obj =
6657 12 : i::Object::ToObject(i_isolate, number).ToHandleChecked();
6658 6 : return Utils::ToLocal(obj);
6659 : }
6660 :
6661 :
6662 29 : double v8::NumberObject::ValueOf() const {
6663 : i::Handle<i::Object> obj = Utils::OpenHandle(this);
6664 29 : i::Handle<i::JSValue> jsvalue = i::Handle<i::JSValue>::cast(obj);
6665 29 : i::Isolate* isolate = jsvalue->GetIsolate();
6666 58 : LOG_API(isolate, NumberObject, NumberValue);
6667 58 : return jsvalue->value()->Number();
6668 : }
6669 :
6670 7 : Local<v8::Value> v8::BigIntObject::New(Isolate* isolate, int64_t value) {
6671 7 : i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
6672 14 : LOG_API(i_isolate, BigIntObject, New);
6673 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(i_isolate);
6674 7 : i::Handle<i::Object> bigint = i::BigInt::FromInt64(i_isolate, value);
6675 : i::Handle<i::Object> obj =
6676 14 : i::Object::ToObject(i_isolate, bigint).ToHandleChecked();
6677 7 : return Utils::ToLocal(obj);
6678 : }
6679 :
6680 11 : Local<v8::BigInt> v8::BigIntObject::ValueOf() const {
6681 : i::Handle<i::Object> obj = Utils::OpenHandle(this);
6682 11 : i::Handle<i::JSValue> jsvalue = i::Handle<i::JSValue>::cast(obj);
6683 11 : i::Isolate* isolate = jsvalue->GetIsolate();
6684 22 : LOG_API(isolate, BigIntObject, BigIntValue);
6685 : return Utils::ToLocal(
6686 33 : i::Handle<i::BigInt>(i::BigInt::cast(jsvalue->value()), isolate));
6687 : }
6688 :
6689 24 : Local<v8::Value> v8::BooleanObject::New(Isolate* isolate, bool value) {
6690 24 : i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
6691 48 : LOG_API(i_isolate, BooleanObject, New);
6692 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(i_isolate);
6693 : i::Handle<i::Object> boolean(value
6694 : ? i::ReadOnlyRoots(i_isolate).true_value()
6695 : : i::ReadOnlyRoots(i_isolate).false_value(),
6696 24 : i_isolate);
6697 : i::Handle<i::Object> obj =
6698 48 : i::Object::ToObject(i_isolate, boolean).ToHandleChecked();
6699 24 : return Utils::ToLocal(obj);
6700 : }
6701 :
6702 :
6703 47 : bool v8::BooleanObject::ValueOf() const {
6704 : i::Handle<i::Object> obj = Utils::OpenHandle(this);
6705 47 : i::Handle<i::JSValue> jsvalue = i::Handle<i::JSValue>::cast(obj);
6706 47 : i::Isolate* isolate = jsvalue->GetIsolate();
6707 94 : LOG_API(isolate, BooleanObject, BooleanValue);
6708 141 : return jsvalue->value()->IsTrue(isolate);
6709 : }
6710 :
6711 :
6712 12 : Local<v8::Value> v8::StringObject::New(Isolate* v8_isolate,
6713 : Local<String> value) {
6714 : i::Handle<i::String> string = Utils::OpenHandle(*value);
6715 12 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(v8_isolate);
6716 24 : LOG_API(isolate, StringObject, New);
6717 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(isolate);
6718 : i::Handle<i::Object> obj =
6719 24 : i::Object::ToObject(isolate, string).ToHandleChecked();
6720 12 : return Utils::ToLocal(obj);
6721 : }
6722 :
6723 :
6724 34 : Local<v8::String> v8::StringObject::ValueOf() const {
6725 : i::Handle<i::Object> obj = Utils::OpenHandle(this);
6726 34 : i::Handle<i::JSValue> jsvalue = i::Handle<i::JSValue>::cast(obj);
6727 34 : i::Isolate* isolate = jsvalue->GetIsolate();
6728 68 : LOG_API(isolate, StringObject, StringValue);
6729 : return Utils::ToLocal(
6730 102 : i::Handle<i::String>(i::String::cast(jsvalue->value()), isolate));
6731 : }
6732 :
6733 :
6734 6 : Local<v8::Value> v8::SymbolObject::New(Isolate* isolate, Local<Symbol> value) {
6735 6 : i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
6736 12 : LOG_API(i_isolate, SymbolObject, New);
6737 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(i_isolate);
6738 : i::Handle<i::Object> obj = i::Object::ToObject(
6739 12 : i_isolate, Utils::OpenHandle(*value)).ToHandleChecked();
6740 6 : return Utils::ToLocal(obj);
6741 : }
6742 :
6743 :
6744 11 : Local<v8::Symbol> v8::SymbolObject::ValueOf() const {
6745 : i::Handle<i::Object> obj = Utils::OpenHandle(this);
6746 11 : i::Handle<i::JSValue> jsvalue = i::Handle<i::JSValue>::cast(obj);
6747 11 : i::Isolate* isolate = jsvalue->GetIsolate();
6748 22 : LOG_API(isolate, SymbolObject, SymbolValue);
6749 : return Utils::ToLocal(
6750 33 : i::Handle<i::Symbol>(i::Symbol::cast(jsvalue->value()), isolate));
6751 : }
6752 :
6753 :
6754 132 : MaybeLocal<v8::Value> v8::Date::New(Local<Context> context, double time) {
6755 132 : if (std::isnan(time)) {
6756 : // Introduce only canonical NaN value into the VM, to avoid signaling NaNs.
6757 : time = std::numeric_limits<double>::quiet_NaN();
6758 : }
6759 528 : PREPARE_FOR_EXECUTION(context, Date, New, Value);
6760 : Local<Value> result;
6761 : has_pending_exception = !ToLocal<Value>(
6762 : i::JSDate::New(isolate->date_function(), isolate->date_function(), time),
6763 396 : &result);
6764 132 : RETURN_ON_FAILED_EXECUTION(Value);
6765 132 : RETURN_ESCAPED(result);
6766 : }
6767 :
6768 :
6769 10 : double v8::Date::ValueOf() const {
6770 : i::Handle<i::Object> obj = Utils::OpenHandle(this);
6771 10 : i::Handle<i::JSDate> jsdate = i::Handle<i::JSDate>::cast(obj);
6772 10 : i::Isolate* isolate = jsdate->GetIsolate();
6773 20 : LOG_API(isolate, Date, NumberValue);
6774 20 : return jsdate->value()->Number();
6775 : }
6776 :
6777 :
6778 0 : void v8::Date::DateTimeConfigurationChangeNotification(Isolate* isolate) {
6779 0 : i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
6780 0 : LOG_API(i_isolate, Date, DateTimeConfigurationChangeNotification);
6781 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(i_isolate);
6782 0 : i_isolate->date_cache()->ResetDateCache();
6783 : #ifdef V8_INTL_SUPPORT
6784 : i_isolate->clear_cached_icu_object(
6785 0 : i::Isolate::ICUObjectCacheType::kDefaultSimpleDateFormat);
6786 : i_isolate->clear_cached_icu_object(
6787 0 : i::Isolate::ICUObjectCacheType::kDefaultSimpleDateFormatForTime);
6788 : i_isolate->clear_cached_icu_object(
6789 0 : i::Isolate::ICUObjectCacheType::kDefaultSimpleDateFormatForDate);
6790 : #endif // V8_INTL_SUPPORT
6791 0 : }
6792 :
6793 :
6794 165 : MaybeLocal<v8::RegExp> v8::RegExp::New(Local<Context> context,
6795 : Local<String> pattern, Flags flags) {
6796 660 : PREPARE_FOR_EXECUTION(context, RegExp, New, RegExp);
6797 : Local<v8::RegExp> result;
6798 : has_pending_exception =
6799 : !ToLocal<RegExp>(i::JSRegExp::New(isolate, Utils::OpenHandle(*pattern),
6800 : static_cast<i::JSRegExp::Flags>(flags)),
6801 495 : &result);
6802 165 : RETURN_ON_FAILED_EXECUTION(RegExp);
6803 155 : RETURN_ESCAPED(result);
6804 : }
6805 :
6806 :
6807 120 : Local<v8::String> v8::RegExp::GetSource() const {
6808 : i::Handle<i::JSRegExp> obj = Utils::OpenHandle(this);
6809 : return Utils::ToLocal(
6810 240 : i::Handle<i::String>(obj->Pattern(), obj->GetIsolate()));
6811 : }
6812 :
6813 :
6814 : // Assert that the static flags cast in GetFlags is valid.
6815 : #define REGEXP_FLAG_ASSERT_EQ(flag) \
6816 : STATIC_ASSERT(static_cast<int>(v8::RegExp::flag) == \
6817 : static_cast<int>(i::JSRegExp::flag))
6818 : REGEXP_FLAG_ASSERT_EQ(kNone);
6819 : REGEXP_FLAG_ASSERT_EQ(kGlobal);
6820 : REGEXP_FLAG_ASSERT_EQ(kIgnoreCase);
6821 : REGEXP_FLAG_ASSERT_EQ(kMultiline);
6822 : REGEXP_FLAG_ASSERT_EQ(kSticky);
6823 : REGEXP_FLAG_ASSERT_EQ(kUnicode);
6824 : #undef REGEXP_FLAG_ASSERT_EQ
6825 :
6826 120 : v8::RegExp::Flags v8::RegExp::GetFlags() const {
6827 : i::Handle<i::JSRegExp> obj = Utils::OpenHandle(this);
6828 240 : return RegExp::Flags(static_cast<int>(obj->GetFlags()));
6829 : }
6830 :
6831 :
6832 67739 : Local<v8::Array> v8::Array::New(Isolate* isolate, int length) {
6833 67739 : i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
6834 135478 : LOG_API(i_isolate, Array, New);
6835 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(i_isolate);
6836 67739 : int real_length = length > 0 ? length : 0;
6837 67739 : i::Handle<i::JSArray> obj = i_isolate->factory()->NewJSArray(real_length);
6838 : i::Handle<i::Object> length_obj =
6839 67739 : i_isolate->factory()->NewNumberFromInt(real_length);
6840 67739 : obj->set_length(*length_obj);
6841 67739 : return Utils::ToLocal(obj);
6842 : }
6843 :
6844 6 : Local<v8::Array> v8::Array::New(Isolate* isolate, Local<Value>* elements,
6845 : size_t length) {
6846 6 : i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
6847 : i::Factory* factory = i_isolate->factory();
6848 12 : LOG_API(i_isolate, Array, New);
6849 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(i_isolate);
6850 6 : int len = static_cast<int>(length);
6851 :
6852 6 : i::Handle<i::FixedArray> result = factory->NewFixedArray(len);
6853 24 : for (int i = 0; i < len; i++) {
6854 18 : i::Handle<i::Object> element = Utils::OpenHandle(*elements[i]);
6855 18 : result->set(i, *element);
6856 : }
6857 :
6858 : return Utils::ToLocal(
6859 12 : factory->NewJSArrayWithElements(result, i::PACKED_ELEMENTS, len));
6860 : }
6861 :
6862 195248 : uint32_t v8::Array::Length() const {
6863 : i::Handle<i::JSArray> obj = Utils::OpenHandle(this);
6864 195248 : i::Object length = obj->length();
6865 195248 : if (length->IsSmi()) {
6866 195248 : return i::Smi::ToInt(length);
6867 : } else {
6868 0 : return static_cast<uint32_t>(length->Number());
6869 : }
6870 : }
6871 :
6872 :
6873 5 : Local<v8::Map> v8::Map::New(Isolate* isolate) {
6874 5 : i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
6875 10 : LOG_API(i_isolate, Map, New);
6876 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(i_isolate);
6877 5 : i::Handle<i::JSMap> obj = i_isolate->factory()->NewJSMap();
6878 5 : return Utils::ToLocal(obj);
6879 : }
6880 :
6881 :
6882 188 : size_t v8::Map::Size() const {
6883 : i::Handle<i::JSMap> obj = Utils::OpenHandle(this);
6884 376 : return i::OrderedHashMap::cast(obj->table())->NumberOfElements();
6885 : }
6886 :
6887 :
6888 5 : void Map::Clear() {
6889 : auto self = Utils::OpenHandle(this);
6890 5 : i::Isolate* isolate = self->GetIsolate();
6891 10 : LOG_API(isolate, Map, Clear);
6892 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(isolate);
6893 5 : i::JSMap::Clear(isolate, self);
6894 5 : }
6895 :
6896 :
6897 15 : MaybeLocal<Value> Map::Get(Local<Context> context, Local<Value> key) {
6898 60 : PREPARE_FOR_EXECUTION(context, Map, Get, Value);
6899 : auto self = Utils::OpenHandle(this);
6900 : Local<Value> result;
6901 15 : i::Handle<i::Object> argv[] = {Utils::OpenHandle(*key)};
6902 : has_pending_exception =
6903 : !ToLocal<Value>(i::Execution::Call(isolate, isolate->map_get(), self,
6904 : arraysize(argv), argv),
6905 45 : &result);
6906 15 : RETURN_ON_FAILED_EXECUTION(Value);
6907 15 : RETURN_ESCAPED(result);
6908 : }
6909 :
6910 :
6911 5 : MaybeLocal<Map> Map::Set(Local<Context> context, Local<Value> key,
6912 : Local<Value> value) {
6913 20 : PREPARE_FOR_EXECUTION(context, Map, Set, Map);
6914 : auto self = Utils::OpenHandle(this);
6915 : i::Handle<i::Object> result;
6916 : i::Handle<i::Object> argv[] = {Utils::OpenHandle(*key),
6917 10 : Utils::OpenHandle(*value)};
6918 : has_pending_exception = !i::Execution::Call(isolate, isolate->map_set(), self,
6919 10 : arraysize(argv), argv)
6920 10 : .ToHandle(&result);
6921 5 : RETURN_ON_FAILED_EXECUTION(Map);
6922 5 : RETURN_ESCAPED(Local<Map>::Cast(Utils::ToLocal(result)));
6923 : }
6924 :
6925 :
6926 30 : Maybe<bool> Map::Has(Local<Context> context, Local<Value> key) {
6927 60 : auto isolate = reinterpret_cast<i::Isolate*>(context->GetIsolate());
6928 120 : ENTER_V8(isolate, context, Map, Has, Nothing<bool>(), i::HandleScope);
6929 : auto self = Utils::OpenHandle(this);
6930 : i::Handle<i::Object> result;
6931 30 : i::Handle<i::Object> argv[] = {Utils::OpenHandle(*key)};
6932 : has_pending_exception = !i::Execution::Call(isolate, isolate->map_has(), self,
6933 60 : arraysize(argv), argv)
6934 60 : .ToHandle(&result);
6935 30 : RETURN_ON_FAILED_EXECUTION_PRIMITIVE(bool);
6936 60 : return Just(result->IsTrue(isolate));
6937 : }
6938 :
6939 :
6940 15 : Maybe<bool> Map::Delete(Local<Context> context, Local<Value> key) {
6941 30 : auto isolate = reinterpret_cast<i::Isolate*>(context->GetIsolate());
6942 60 : ENTER_V8(isolate, context, Map, Delete, Nothing<bool>(), i::HandleScope);
6943 : auto self = Utils::OpenHandle(this);
6944 : i::Handle<i::Object> result;
6945 15 : i::Handle<i::Object> argv[] = {Utils::OpenHandle(*key)};
6946 : has_pending_exception = !i::Execution::Call(isolate, isolate->map_delete(),
6947 30 : self, arraysize(argv), argv)
6948 30 : .ToHandle(&result);
6949 15 : RETURN_ON_FAILED_EXECUTION_PRIMITIVE(bool);
6950 30 : return Just(result->IsTrue(isolate));
6951 : }
6952 :
6953 : namespace {
6954 :
6955 : enum class MapAsArrayKind {
6956 : kEntries = i::JS_MAP_KEY_VALUE_ITERATOR_TYPE,
6957 : kKeys = i::JS_MAP_KEY_ITERATOR_TYPE,
6958 : kValues = i::JS_MAP_VALUE_ITERATOR_TYPE
6959 : };
6960 :
6961 230 : i::Handle<i::JSArray> MapAsArray(i::Isolate* isolate, i::Object table_obj,
6962 : int offset, MapAsArrayKind kind) {
6963 : i::Factory* factory = isolate->factory();
6964 : i::Handle<i::OrderedHashMap> table(i::OrderedHashMap::cast(table_obj),
6965 : isolate);
6966 : const bool collect_keys =
6967 230 : kind == MapAsArrayKind::kEntries || kind == MapAsArrayKind::kKeys;
6968 : const bool collect_values =
6969 230 : kind == MapAsArrayKind::kEntries || kind == MapAsArrayKind::kValues;
6970 230 : int capacity = table->UsedCapacity();
6971 : int max_length =
6972 230 : (capacity - offset) * ((collect_keys && collect_values) ? 2 : 1);
6973 230 : i::Handle<i::FixedArray> result = factory->NewFixedArray(max_length);
6974 : int result_index = 0;
6975 : {
6976 : i::DisallowHeapAllocation no_gc;
6977 : i::Oddball the_hole = i::ReadOnlyRoots(isolate).the_hole_value();
6978 5643 : for (int i = offset; i < capacity; ++i) {
6979 5413 : i::Object key = table->KeyAt(i);
6980 5418 : if (key == the_hole) continue;
6981 10673 : if (collect_keys) result->set(result_index++, key);
6982 16178 : if (collect_values) result->set(result_index++, table->ValueAt(i));
6983 : }
6984 : }
6985 : DCHECK_GE(max_length, result_index);
6986 230 : if (result_index == 0) return factory->NewJSArray(0);
6987 206 : result->Shrink(isolate, result_index);
6988 : return factory->NewJSArrayWithElements(result, i::PACKED_ELEMENTS,
6989 206 : result_index);
6990 : }
6991 :
6992 : } // namespace
6993 :
6994 123 : Local<Array> Map::AsArray() const {
6995 : i::Handle<i::JSMap> obj = Utils::OpenHandle(this);
6996 123 : i::Isolate* isolate = obj->GetIsolate();
6997 246 : LOG_API(isolate, Map, AsArray);
6998 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(isolate);
6999 : return Utils::ToLocal(
7000 246 : MapAsArray(isolate, obj->table(), 0, MapAsArrayKind::kEntries));
7001 : }
7002 :
7003 :
7004 79587 : Local<v8::Set> v8::Set::New(Isolate* isolate) {
7005 79587 : i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
7006 159174 : LOG_API(i_isolate, Set, New);
7007 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(i_isolate);
7008 79587 : i::Handle<i::JSSet> obj = i_isolate->factory()->NewJSSet();
7009 79587 : return Utils::ToLocal(obj);
7010 : }
7011 :
7012 :
7013 124 : size_t v8::Set::Size() const {
7014 : i::Handle<i::JSSet> obj = Utils::OpenHandle(this);
7015 248 : return i::OrderedHashSet::cast(obj->table())->NumberOfElements();
7016 : }
7017 :
7018 :
7019 5 : void Set::Clear() {
7020 : auto self = Utils::OpenHandle(this);
7021 5 : i::Isolate* isolate = self->GetIsolate();
7022 10 : LOG_API(isolate, Set, Clear);
7023 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(isolate);
7024 5 : i::JSSet::Clear(isolate, self);
7025 5 : }
7026 :
7027 :
7028 2986188 : MaybeLocal<Set> Set::Add(Local<Context> context, Local<Value> key) {
7029 11944752 : PREPARE_FOR_EXECUTION(context, Set, Add, Set);
7030 : auto self = Utils::OpenHandle(this);
7031 : i::Handle<i::Object> result;
7032 2986188 : i::Handle<i::Object> argv[] = {Utils::OpenHandle(*key)};
7033 : has_pending_exception = !i::Execution::Call(isolate, isolate->set_add(), self,
7034 5972376 : arraysize(argv), argv)
7035 5972376 : .ToHandle(&result);
7036 2986188 : RETURN_ON_FAILED_EXECUTION(Set);
7037 2986188 : RETURN_ESCAPED(Local<Set>::Cast(Utils::ToLocal(result)));
7038 : }
7039 :
7040 :
7041 4436401 : Maybe<bool> Set::Has(Local<Context> context, Local<Value> key) {
7042 8872802 : auto isolate = reinterpret_cast<i::Isolate*>(context->GetIsolate());
7043 17745604 : ENTER_V8(isolate, context, Set, Has, Nothing<bool>(), i::HandleScope);
7044 : auto self = Utils::OpenHandle(this);
7045 : i::Handle<i::Object> result;
7046 4436401 : i::Handle<i::Object> argv[] = {Utils::OpenHandle(*key)};
7047 : has_pending_exception = !i::Execution::Call(isolate, isolate->set_has(), self,
7048 8872802 : arraysize(argv), argv)
7049 8872802 : .ToHandle(&result);
7050 4436401 : RETURN_ON_FAILED_EXECUTION_PRIMITIVE(bool);
7051 8872802 : return Just(result->IsTrue(isolate));
7052 : }
7053 :
7054 :
7055 70 : Maybe<bool> Set::Delete(Local<Context> context, Local<Value> key) {
7056 140 : auto isolate = reinterpret_cast<i::Isolate*>(context->GetIsolate());
7057 280 : ENTER_V8(isolate, context, Set, Delete, Nothing<bool>(), i::HandleScope);
7058 : auto self = Utils::OpenHandle(this);
7059 : i::Handle<i::Object> result;
7060 70 : i::Handle<i::Object> argv[] = {Utils::OpenHandle(*key)};
7061 : has_pending_exception = !i::Execution::Call(isolate, isolate->set_delete(),
7062 140 : self, arraysize(argv), argv)
7063 140 : .ToHandle(&result);
7064 70 : RETURN_ON_FAILED_EXECUTION_PRIMITIVE(bool);
7065 140 : return Just(result->IsTrue(isolate));
7066 : }
7067 :
7068 : namespace {
7069 631 : i::Handle<i::JSArray> SetAsArray(i::Isolate* isolate, i::Object table_obj,
7070 : int offset) {
7071 : i::Factory* factory = isolate->factory();
7072 : i::Handle<i::OrderedHashSet> table(i::OrderedHashSet::cast(table_obj),
7073 : isolate);
7074 : // Elements skipped by |offset| may already be deleted.
7075 631 : int capacity = table->UsedCapacity();
7076 631 : int max_length = capacity - offset;
7077 631 : if (max_length == 0) return factory->NewJSArray(0);
7078 617 : i::Handle<i::FixedArray> result = factory->NewFixedArray(max_length);
7079 : int result_index = 0;
7080 : {
7081 : i::DisallowHeapAllocation no_gc;
7082 : i::Oddball the_hole = i::ReadOnlyRoots(isolate).the_hole_value();
7083 14056 : for (int i = offset; i < capacity; ++i) {
7084 13439 : i::Object key = table->KeyAt(i);
7085 13499 : if (key == the_hole) continue;
7086 26758 : result->set(result_index++, key);
7087 : }
7088 : }
7089 : DCHECK_GE(max_length, result_index);
7090 617 : if (result_index == 0) return factory->NewJSArray(0);
7091 617 : result->Shrink(isolate, result_index);
7092 : return factory->NewJSArrayWithElements(result, i::PACKED_ELEMENTS,
7093 617 : result_index);
7094 : }
7095 : } // namespace
7096 :
7097 529 : Local<Array> Set::AsArray() const {
7098 : i::Handle<i::JSSet> obj = Utils::OpenHandle(this);
7099 529 : i::Isolate* isolate = obj->GetIsolate();
7100 1058 : LOG_API(isolate, Set, AsArray);
7101 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(isolate);
7102 1058 : return Utils::ToLocal(SetAsArray(isolate, obj->table(), 0));
7103 : }
7104 :
7105 :
7106 9211 : MaybeLocal<Promise::Resolver> Promise::Resolver::New(Local<Context> context) {
7107 36844 : PREPARE_FOR_EXECUTION(context, Promise_Resolver, New, Resolver);
7108 : Local<Promise::Resolver> result;
7109 : has_pending_exception =
7110 9211 : !ToLocal<Promise::Resolver>(isolate->factory()->NewJSPromise(), &result);
7111 9211 : RETURN_ON_FAILED_EXECUTION(Promise::Resolver);
7112 9211 : RETURN_ESCAPED(result);
7113 : }
7114 :
7115 :
7116 9206 : Local<Promise> Promise::Resolver::GetPromise() {
7117 : i::Handle<i::JSReceiver> promise = Utils::OpenHandle(this);
7118 9206 : return Local<Promise>::Cast(Utils::ToLocal(promise));
7119 : }
7120 :
7121 :
7122 946 : Maybe<bool> Promise::Resolver::Resolve(Local<Context> context,
7123 : Local<Value> value) {
7124 1892 : auto isolate = reinterpret_cast<i::Isolate*>(context->GetIsolate());
7125 3784 : ENTER_V8(isolate, context, Promise_Resolver, Resolve, Nothing<bool>(),
7126 : i::HandleScope);
7127 946 : auto self = Utils::OpenHandle(this);
7128 946 : auto promise = i::Handle<i::JSPromise>::cast(self);
7129 :
7130 946 : if (promise->status() != Promise::kPending) {
7131 : return Just(true);
7132 : }
7133 :
7134 : has_pending_exception =
7135 1872 : i::JSPromise::Resolve(promise, Utils::OpenHandle(*value)).is_null();
7136 936 : RETURN_ON_FAILED_EXECUTION_PRIMITIVE(bool);
7137 : return Just(true);
7138 : }
7139 :
7140 :
7141 281 : Maybe<bool> Promise::Resolver::Reject(Local<Context> context,
7142 : Local<Value> value) {
7143 562 : auto isolate = reinterpret_cast<i::Isolate*>(context->GetIsolate());
7144 1124 : ENTER_V8(isolate, context, Promise_Resolver, Reject, Nothing<bool>(),
7145 : i::HandleScope);
7146 281 : auto self = Utils::OpenHandle(this);
7147 281 : auto promise = i::Handle<i::JSPromise>::cast(self);
7148 :
7149 281 : if (promise->status() != Promise::kPending) {
7150 : return Just(true);
7151 : }
7152 :
7153 : has_pending_exception =
7154 542 : i::JSPromise::Reject(promise, Utils::OpenHandle(*value)).is_null();
7155 271 : RETURN_ON_FAILED_EXECUTION_PRIMITIVE(bool);
7156 : return Just(true);
7157 : }
7158 :
7159 :
7160 655 : MaybeLocal<Promise> Promise::Catch(Local<Context> context,
7161 : Local<Function> handler) {
7162 2620 : PREPARE_FOR_EXECUTION(context, Promise, Catch, Promise);
7163 : auto self = Utils::OpenHandle(this);
7164 : i::Handle<i::Object> argv[] = { Utils::OpenHandle(*handler) };
7165 : i::Handle<i::Object> result;
7166 : has_pending_exception = !i::Execution::Call(isolate, isolate->promise_catch(),
7167 1310 : self, arraysize(argv), argv)
7168 1310 : .ToHandle(&result);
7169 655 : RETURN_ON_FAILED_EXECUTION(Promise);
7170 655 : RETURN_ESCAPED(Local<Promise>::Cast(Utils::ToLocal(result)));
7171 : }
7172 :
7173 :
7174 705 : MaybeLocal<Promise> Promise::Then(Local<Context> context,
7175 : Local<Function> handler) {
7176 2820 : PREPARE_FOR_EXECUTION(context, Promise, Then, Promise);
7177 : auto self = Utils::OpenHandle(this);
7178 : i::Handle<i::Object> argv[] = { Utils::OpenHandle(*handler) };
7179 : i::Handle<i::Object> result;
7180 : has_pending_exception = !i::Execution::Call(isolate, isolate->promise_then(),
7181 1410 : self, arraysize(argv), argv)
7182 1410 : .ToHandle(&result);
7183 705 : RETURN_ON_FAILED_EXECUTION(Promise);
7184 705 : RETURN_ESCAPED(Local<Promise>::Cast(Utils::ToLocal(result)));
7185 : }
7186 :
7187 35 : MaybeLocal<Promise> Promise::Then(Local<Context> context,
7188 : Local<Function> on_fulfilled,
7189 : Local<Function> on_rejected) {
7190 140 : PREPARE_FOR_EXECUTION(context, Promise, Then, Promise);
7191 : auto self = Utils::OpenHandle(this);
7192 : i::Handle<i::Object> argv[] = {Utils::OpenHandle(*on_fulfilled),
7193 : Utils::OpenHandle(*on_rejected)};
7194 : i::Handle<i::Object> result;
7195 : has_pending_exception = !i::Execution::Call(isolate, isolate->promise_then(),
7196 70 : self, arraysize(argv), argv)
7197 70 : .ToHandle(&result);
7198 35 : RETURN_ON_FAILED_EXECUTION(Promise);
7199 35 : RETURN_ESCAPED(Local<Promise>::Cast(Utils::ToLocal(result)));
7200 : }
7201 :
7202 345 : bool Promise::HasHandler() {
7203 : i::Handle<i::JSReceiver> promise = Utils::OpenHandle(this);
7204 345 : i::Isolate* isolate = promise->GetIsolate();
7205 690 : LOG_API(isolate, Promise, HasRejectHandler);
7206 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(isolate);
7207 690 : if (promise->IsJSPromise()) {
7208 345 : i::Handle<i::JSPromise> js_promise = i::Handle<i::JSPromise>::cast(promise);
7209 345 : return js_promise->has_handler();
7210 : }
7211 : return false;
7212 : }
7213 :
7214 50 : Local<Value> Promise::Result() {
7215 : i::Handle<i::JSReceiver> promise = Utils::OpenHandle(this);
7216 50 : i::Isolate* isolate = promise->GetIsolate();
7217 100 : LOG_API(isolate, Promise, Result);
7218 50 : i::Handle<i::JSPromise> js_promise = i::Handle<i::JSPromise>::cast(promise);
7219 100 : Utils::ApiCheck(js_promise->status() != kPending, "v8_Promise_Result",
7220 : "Promise is still pending");
7221 100 : i::Handle<i::Object> result(js_promise->result(), isolate);
7222 50 : return Utils::ToLocal(result);
7223 : }
7224 :
7225 295 : Promise::PromiseState Promise::State() {
7226 : i::Handle<i::JSReceiver> promise = Utils::OpenHandle(this);
7227 295 : i::Isolate* isolate = promise->GetIsolate();
7228 590 : LOG_API(isolate, Promise, Status);
7229 295 : i::Handle<i::JSPromise> js_promise = i::Handle<i::JSPromise>::cast(promise);
7230 590 : return static_cast<PromiseState>(js_promise->status());
7231 : }
7232 :
7233 5 : void Promise::MarkAsHandled() {
7234 : i::Handle<i::JSPromise> js_promise = Utils::OpenHandle(this);
7235 5 : js_promise->set_has_handler(true);
7236 5 : }
7237 :
7238 30 : Local<Value> Proxy::GetTarget() {
7239 : i::Handle<i::JSProxy> self = Utils::OpenHandle(this);
7240 60 : i::Handle<i::Object> target(self->target(), self->GetIsolate());
7241 30 : return Utils::ToLocal(target);
7242 : }
7243 :
7244 :
7245 10 : Local<Value> Proxy::GetHandler() {
7246 : i::Handle<i::JSProxy> self = Utils::OpenHandle(this);
7247 20 : i::Handle<i::Object> handler(self->handler(), self->GetIsolate());
7248 10 : return Utils::ToLocal(handler);
7249 : }
7250 :
7251 :
7252 10 : bool Proxy::IsRevoked() {
7253 : i::Handle<i::JSProxy> self = Utils::OpenHandle(this);
7254 20 : return self->IsRevoked();
7255 : }
7256 :
7257 :
7258 5 : void Proxy::Revoke() {
7259 5 : i::Handle<i::JSProxy> self = Utils::OpenHandle(this);
7260 5 : i::JSProxy::Revoke(self);
7261 5 : }
7262 :
7263 :
7264 17 : MaybeLocal<Proxy> Proxy::New(Local<Context> context, Local<Object> local_target,
7265 : Local<Object> local_handler) {
7266 68 : PREPARE_FOR_EXECUTION(context, Proxy, New, Proxy);
7267 : i::Handle<i::JSReceiver> target = Utils::OpenHandle(*local_target);
7268 : i::Handle<i::JSReceiver> handler = Utils::OpenHandle(*local_handler);
7269 : Local<Proxy> result;
7270 : has_pending_exception =
7271 34 : !ToLocal<Proxy>(i::JSProxy::New(isolate, target, handler), &result);
7272 17 : RETURN_ON_FAILED_EXECUTION(Proxy);
7273 17 : RETURN_ESCAPED(result);
7274 : }
7275 :
7276 57 : CompiledWasmModule::CompiledWasmModule(
7277 : std::shared_ptr<internal::wasm::NativeModule> native_module)
7278 : : native_module_(std::move(native_module)) {
7279 57 : CHECK_NOT_NULL(native_module_);
7280 57 : }
7281 :
7282 35 : OwnedBuffer CompiledWasmModule::Serialize() {
7283 35 : i::wasm::WasmSerializer wasm_serializer(native_module_.get());
7284 35 : size_t buffer_size = wasm_serializer.GetSerializedNativeModuleSize();
7285 35 : std::unique_ptr<uint8_t[]> buffer(new uint8_t[buffer_size]);
7286 35 : if (!wasm_serializer.SerializeNativeModule({buffer.get(), buffer_size}))
7287 0 : return {};
7288 : return {std::move(buffer), buffer_size};
7289 : }
7290 :
7291 52 : MemorySpan<const uint8_t> CompiledWasmModule::GetWireBytesRef() {
7292 57 : i::Vector<const uint8_t> bytes_vec = native_module_->wire_bytes();
7293 52 : return {bytes_vec.start(), bytes_vec.size()};
7294 : }
7295 :
7296 0 : WasmModuleObject::BufferReference WasmModuleObject::GetWasmWireBytesRef() {
7297 0 : return GetCompiledModule().GetWireBytesRef();
7298 : }
7299 :
7300 : WasmModuleObject::TransferrableModule
7301 115 : WasmModuleObject::GetTransferrableModule() {
7302 115 : if (i::FLAG_wasm_shared_code) {
7303 : i::Handle<i::WasmModuleObject> obj =
7304 110 : i::Handle<i::WasmModuleObject>::cast(Utils::OpenHandle(this));
7305 220 : return TransferrableModule(obj->shared_native_module());
7306 : } else {
7307 5 : CompiledWasmModule compiled_module = GetCompiledModule();
7308 5 : OwnedBuffer serialized_module = compiled_module.Serialize();
7309 : MemorySpan<const uint8_t> wire_bytes_ref =
7310 : compiled_module.GetWireBytesRef();
7311 : size_t wire_size = wire_bytes_ref.size();
7312 5 : std::unique_ptr<uint8_t[]> wire_bytes_copy(new uint8_t[wire_size]);
7313 : memcpy(wire_bytes_copy.get(), wire_bytes_ref.data(), wire_size);
7314 : return TransferrableModule(std::move(serialized_module),
7315 : {std::move(wire_bytes_copy), wire_size});
7316 : }
7317 : }
7318 :
7319 57 : CompiledWasmModule WasmModuleObject::GetCompiledModule() {
7320 : i::Handle<i::WasmModuleObject> obj =
7321 57 : i::Handle<i::WasmModuleObject>::cast(Utils::OpenHandle(this));
7322 114 : return Utils::Convert(obj->shared_native_module());
7323 : }
7324 :
7325 113 : MaybeLocal<WasmModuleObject> WasmModuleObject::FromTransferrableModule(
7326 : Isolate* isolate,
7327 : const WasmModuleObject::TransferrableModule& transferrable_module) {
7328 113 : if (i::FLAG_wasm_shared_code) {
7329 : i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
7330 : i::Handle<i::WasmModuleObject> module_object =
7331 : i_isolate->wasm_engine()->ImportNativeModule(
7332 216 : i_isolate, transferrable_module.shared_module_);
7333 : return Local<WasmModuleObject>::Cast(
7334 108 : Utils::ToLocal(i::Handle<i::JSObject>::cast(module_object)));
7335 : } else {
7336 : return Deserialize(isolate, AsReference(transferrable_module.serialized_),
7337 5 : AsReference(transferrable_module.wire_bytes_));
7338 : }
7339 : }
7340 :
7341 0 : WasmModuleObject::SerializedModule WasmModuleObject::Serialize() {
7342 : // TODO(clemensh): Deprecated; remove after M-73 branch.
7343 0 : OwnedBuffer serialized = GetCompiledModule().Serialize();
7344 0 : return {std::move(serialized.buffer), serialized.size};
7345 : }
7346 :
7347 92 : MaybeLocal<WasmModuleObject> WasmModuleObject::Deserialize(
7348 : Isolate* isolate, MemorySpan<const uint8_t> serialized_module,
7349 : MemorySpan<const uint8_t> wire_bytes) {
7350 : i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
7351 : i::MaybeHandle<i::WasmModuleObject> maybe_module_object =
7352 : i::wasm::DeserializeNativeModule(
7353 : i_isolate, {serialized_module.data(), serialized_module.size()},
7354 276 : {wire_bytes.data(), wire_bytes.size()});
7355 : i::Handle<i::WasmModuleObject> module_object;
7356 92 : if (!maybe_module_object.ToHandle(&module_object)) {
7357 82 : return MaybeLocal<WasmModuleObject>();
7358 : }
7359 : return Local<WasmModuleObject>::Cast(
7360 10 : Utils::ToLocal(i::Handle<i::JSObject>::cast(module_object)));
7361 : }
7362 :
7363 87 : MaybeLocal<WasmModuleObject> WasmModuleObject::DeserializeOrCompile(
7364 : Isolate* isolate, MemorySpan<const uint8_t> serialized_module,
7365 : MemorySpan<const uint8_t> wire_bytes) {
7366 : MaybeLocal<WasmModuleObject> ret =
7367 87 : Deserialize(isolate, serialized_module, wire_bytes);
7368 87 : if (!ret.IsEmpty()) {
7369 5 : return ret;
7370 : }
7371 82 : return Compile(isolate, wire_bytes.data(), wire_bytes.size());
7372 : }
7373 :
7374 82 : MaybeLocal<WasmModuleObject> WasmModuleObject::Compile(Isolate* isolate,
7375 : const uint8_t* start,
7376 : size_t length) {
7377 : i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
7378 : i::wasm::ErrorThrower thrower(i_isolate, "WasmModuleObject::Compile()");
7379 164 : if (!i::wasm::IsWasmCodegenAllowed(i_isolate, i_isolate->native_context())) {
7380 25 : return MaybeLocal<WasmModuleObject>();
7381 : }
7382 57 : auto enabled_features = i::wasm::WasmFeaturesFromIsolate(i_isolate);
7383 : i::MaybeHandle<i::JSObject> maybe_compiled =
7384 : i_isolate->wasm_engine()->SyncCompile(
7385 : i_isolate, enabled_features, &thrower,
7386 114 : i::wasm::ModuleWireBytes(start, start + length));
7387 57 : if (maybe_compiled.is_null()) return MaybeLocal<WasmModuleObject>();
7388 : return Local<WasmModuleObject>::Cast(
7389 52 : Utils::ToLocal(maybe_compiled.ToHandleChecked()));
7390 : }
7391 :
7392 : // Resolves the result of streaming compilation.
7393 : // TODO(ahaas): Refactor the streaming compilation API so that this class can
7394 : // move to wasm-js.cc.
7395 : class AsyncCompilationResolver : public i::wasm::CompilationResultResolver {
7396 : public:
7397 : AsyncCompilationResolver(Isolate* isolate, Local<Promise> promise)
7398 : : promise_(
7399 : reinterpret_cast<i::Isolate*>(isolate)->global_handles()->Create(
7400 : *Utils::OpenHandle(*promise))) {}
7401 :
7402 0 : ~AsyncCompilationResolver() override {
7403 0 : i::GlobalHandles::Destroy(promise_.location());
7404 0 : }
7405 :
7406 0 : void OnCompilationSucceeded(i::Handle<i::WasmModuleObject> result) override {
7407 : i::MaybeHandle<i::Object> promise_result =
7408 0 : i::JSPromise::Resolve(promise_, result);
7409 0 : CHECK_EQ(promise_result.is_null(),
7410 : promise_->GetIsolate()->has_pending_exception());
7411 0 : }
7412 :
7413 0 : void OnCompilationFailed(i::Handle<i::Object> error_reason) override {
7414 : i::MaybeHandle<i::Object> promise_result =
7415 0 : i::JSPromise::Reject(promise_, error_reason);
7416 0 : CHECK_EQ(promise_result.is_null(),
7417 : promise_->GetIsolate()->has_pending_exception());
7418 0 : }
7419 :
7420 : private:
7421 : i::Handle<i::JSPromise> promise_;
7422 : };
7423 :
7424 0 : WasmModuleObjectBuilderStreaming::WasmModuleObjectBuilderStreaming(
7425 0 : Isolate* isolate) {
7426 : USE(isolate_);
7427 0 : }
7428 :
7429 0 : Local<Promise> WasmModuleObjectBuilderStreaming::GetPromise() { return {}; }
7430 :
7431 0 : void WasmModuleObjectBuilderStreaming::OnBytesReceived(const uint8_t* bytes,
7432 : size_t size) {
7433 0 : }
7434 :
7435 0 : void WasmModuleObjectBuilderStreaming::Finish() {
7436 0 : }
7437 :
7438 0 : void WasmModuleObjectBuilderStreaming::Abort(MaybeLocal<Value> exception) {
7439 0 : }
7440 :
7441 : // static
7442 118170 : v8::ArrayBuffer::Allocator* v8::ArrayBuffer::Allocator::NewDefaultAllocator() {
7443 236340 : return new ArrayBufferAllocator();
7444 : }
7445 :
7446 89 : bool v8::ArrayBuffer::IsExternal() const {
7447 89 : return Utils::OpenHandle(this)->is_external();
7448 : }
7449 :
7450 58 : bool v8::ArrayBuffer::IsDetachable() const {
7451 58 : return Utils::OpenHandle(this)->is_detachable();
7452 : }
7453 :
7454 93 : v8::ArrayBuffer::Contents v8::ArrayBuffer::Externalize() {
7455 : i::Handle<i::JSArrayBuffer> self = Utils::OpenHandle(this);
7456 : i::Isolate* isolate = self->GetIsolate();
7457 : Utils::ApiCheck(!self->is_external(), "v8_ArrayBuffer_Externalize",
7458 : "ArrayBuffer already externalized");
7459 : self->set_is_external(true);
7460 :
7461 93 : const v8::ArrayBuffer::Contents contents = GetContents();
7462 93 : isolate->heap()->UnregisterArrayBuffer(*self);
7463 :
7464 : // A regular copy is good enough. No move semantics needed.
7465 93 : return contents;
7466 : }
7467 :
7468 0 : v8::ArrayBuffer::Contents::Contents(void* data, size_t byte_length,
7469 : void* allocation_base,
7470 : size_t allocation_length,
7471 : Allocator::AllocationMode allocation_mode,
7472 : DeleterCallback deleter, void* deleter_data)
7473 : : data_(data),
7474 : byte_length_(byte_length),
7475 : allocation_base_(allocation_base),
7476 : allocation_length_(allocation_length),
7477 : allocation_mode_(allocation_mode),
7478 : deleter_(deleter),
7479 312792 : deleter_data_(deleter_data) {
7480 : DCHECK_LE(allocation_base_, data_);
7481 : DCHECK_LE(byte_length_, allocation_length_);
7482 0 : }
7483 :
7484 54 : void WasmMemoryDeleter(void* buffer, size_t lenght, void* info) {
7485 : internal::wasm::WasmEngine* engine =
7486 : reinterpret_cast<internal::wasm::WasmEngine*>(info);
7487 54 : CHECK(engine->memory_tracker()->FreeMemoryIfIsWasmMemory(nullptr, buffer));
7488 54 : }
7489 :
7490 99 : void ArrayBufferDeleter(void* buffer, size_t length, void* info) {
7491 : v8::ArrayBuffer::Allocator* allocator =
7492 : reinterpret_cast<v8::ArrayBuffer::Allocator*>(info);
7493 99 : allocator->Free(buffer, length);
7494 99 : }
7495 :
7496 312775 : v8::ArrayBuffer::Contents v8::ArrayBuffer::GetContents() {
7497 : i::Handle<i::JSArrayBuffer> self = Utils::OpenHandle(this);
7498 : Contents contents(
7499 : self->backing_store(), self->byte_length(), self->allocation_base(),
7500 : self->allocation_length(),
7501 : self->is_wasm_memory() ? Allocator::AllocationMode::kReservation
7502 : : Allocator::AllocationMode::kNormal,
7503 : self->is_wasm_memory() ? WasmMemoryDeleter : ArrayBufferDeleter,
7504 : self->is_wasm_memory()
7505 : ? static_cast<void*>(self->GetIsolate()->wasm_engine())
7506 2189516 : : static_cast<void*>(self->GetIsolate()->array_buffer_allocator()));
7507 312792 : return contents;
7508 : }
7509 :
7510 44 : void v8::ArrayBuffer::Detach() {
7511 : i::Handle<i::JSArrayBuffer> obj = Utils::OpenHandle(this);
7512 44 : i::Isolate* isolate = obj->GetIsolate();
7513 : Utils::ApiCheck(obj->is_external(), "v8::ArrayBuffer::Detach",
7514 : "Only externalized ArrayBuffers can be detached");
7515 : Utils::ApiCheck(obj->is_detachable(), "v8::ArrayBuffer::Detach",
7516 : "Only detachable ArrayBuffers can be detached");
7517 88 : LOG_API(isolate, ArrayBuffer, Detach);
7518 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(isolate);
7519 44 : obj->Detach();
7520 44 : }
7521 :
7522 230 : size_t v8::ArrayBuffer::ByteLength() const {
7523 : i::Handle<i::JSArrayBuffer> obj = Utils::OpenHandle(this);
7524 230 : return obj->byte_length();
7525 : }
7526 :
7527 :
7528 200 : Local<ArrayBuffer> v8::ArrayBuffer::New(Isolate* isolate, size_t byte_length) {
7529 200 : i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
7530 400 : LOG_API(i_isolate, ArrayBuffer, New);
7531 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(i_isolate);
7532 : i::Handle<i::JSArrayBuffer> obj =
7533 200 : i_isolate->factory()->NewJSArrayBuffer(i::SharedFlag::kNotShared);
7534 : // TODO(jbroman): It may be useful in the future to provide a MaybeLocal
7535 : // version that throws an exception or otherwise does not crash.
7536 200 : if (!i::JSArrayBuffer::SetupAllocatingData(obj, i_isolate, byte_length)) {
7537 0 : i::FatalProcessOutOfMemory(i_isolate, "v8::ArrayBuffer::New");
7538 : }
7539 200 : return Utils::ToLocal(obj);
7540 : }
7541 :
7542 :
7543 141 : Local<ArrayBuffer> v8::ArrayBuffer::New(Isolate* isolate, void* data,
7544 : size_t byte_length,
7545 : ArrayBufferCreationMode mode) {
7546 : // Embedders must guarantee that the external backing store is valid.
7547 141 : CHECK(byte_length == 0 || data != nullptr);
7548 141 : CHECK_LE(byte_length, i::JSArrayBuffer::kMaxByteLength);
7549 141 : i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
7550 282 : LOG_API(i_isolate, ArrayBuffer, New);
7551 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(i_isolate);
7552 : i::Handle<i::JSArrayBuffer> obj =
7553 141 : i_isolate->factory()->NewJSArrayBuffer(i::SharedFlag::kNotShared);
7554 : i::JSArrayBuffer::Setup(obj, i_isolate,
7555 : mode == ArrayBufferCreationMode::kExternalized, data,
7556 141 : byte_length);
7557 141 : return Utils::ToLocal(obj);
7558 : }
7559 :
7560 :
7561 3377 : Local<ArrayBuffer> v8::ArrayBufferView::Buffer() {
7562 : i::Handle<i::JSArrayBufferView> obj = Utils::OpenHandle(this);
7563 : i::Handle<i::JSArrayBuffer> buffer;
7564 6754 : if (obj->IsJSDataView()) {
7565 : i::Handle<i::JSDataView> data_view(i::JSDataView::cast(*obj),
7566 : obj->GetIsolate());
7567 : DCHECK(data_view->buffer()->IsJSArrayBuffer());
7568 28 : buffer = i::handle(i::JSArrayBuffer::cast(data_view->buffer()),
7569 14 : data_view->GetIsolate());
7570 : } else {
7571 : DCHECK(obj->IsJSTypedArray());
7572 3363 : buffer = i::JSTypedArray::cast(*obj)->GetBuffer();
7573 : }
7574 3377 : return Utils::ToLocal(buffer);
7575 : }
7576 :
7577 :
7578 15 : size_t v8::ArrayBufferView::CopyContents(void* dest, size_t byte_length) {
7579 : i::Handle<i::JSArrayBufferView> self = Utils::OpenHandle(this);
7580 : size_t byte_offset = self->byte_offset();
7581 : size_t bytes_to_copy = i::Min(byte_length, self->byte_length());
7582 15 : if (bytes_to_copy) {
7583 : i::DisallowHeapAllocation no_gc;
7584 : i::Isolate* isolate = self->GetIsolate();
7585 : i::Handle<i::JSArrayBuffer> buffer(i::JSArrayBuffer::cast(self->buffer()),
7586 30 : isolate);
7587 : const char* source = reinterpret_cast<char*>(buffer->backing_store());
7588 15 : if (source == nullptr) {
7589 : DCHECK(self->IsJSTypedArray());
7590 : i::Handle<i::JSTypedArray> typed_array(i::JSTypedArray::cast(*self),
7591 : isolate);
7592 : i::Handle<i::FixedTypedArrayBase> fixed_array(
7593 20 : i::FixedTypedArrayBase::cast(typed_array->elements()), isolate);
7594 20 : source = reinterpret_cast<char*>(fixed_array->DataPtr());
7595 : }
7596 15 : memcpy(dest, source + byte_offset, bytes_to_copy);
7597 : }
7598 15 : return bytes_to_copy;
7599 : }
7600 :
7601 :
7602 30 : bool v8::ArrayBufferView::HasBuffer() const {
7603 : i::Handle<i::JSArrayBufferView> self = Utils::OpenHandle(this);
7604 : i::Handle<i::JSArrayBuffer> buffer(i::JSArrayBuffer::cast(self->buffer()),
7605 60 : self->GetIsolate());
7606 30 : return buffer->backing_store() != nullptr;
7607 : }
7608 :
7609 :
7610 3551 : size_t v8::ArrayBufferView::ByteOffset() {
7611 : i::Handle<i::JSArrayBufferView> obj = Utils::OpenHandle(this);
7612 7102 : return obj->WasDetached() ? 0 : obj->byte_offset();
7613 : }
7614 :
7615 :
7616 3609 : size_t v8::ArrayBufferView::ByteLength() {
7617 : i::Handle<i::JSArrayBufferView> obj = Utils::OpenHandle(this);
7618 7218 : return obj->WasDetached() ? 0 : obj->byte_length();
7619 : }
7620 :
7621 :
7622 498 : size_t v8::TypedArray::Length() {
7623 : i::Handle<i::JSTypedArray> obj = Utils::OpenHandle(this);
7624 996 : return obj->WasDetached() ? 0 : obj->length_value();
7625 : }
7626 :
7627 : static_assert(v8::TypedArray::kMaxLength == i::Smi::kMaxValue,
7628 : "v8::TypedArray::kMaxLength must match i::Smi::kMaxValue");
7629 :
7630 : #define TYPED_ARRAY_NEW(Type, type, TYPE, ctype) \
7631 : Local<Type##Array> Type##Array::New(Local<ArrayBuffer> array_buffer, \
7632 : size_t byte_offset, size_t length) { \
7633 : i::Isolate* isolate = Utils::OpenHandle(*array_buffer)->GetIsolate(); \
7634 : LOG_API(isolate, Type##Array, New); \
7635 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(isolate); \
7636 : if (!Utils::ApiCheck(length <= kMaxLength, \
7637 : "v8::" #Type \
7638 : "Array::New(Local<ArrayBuffer>, size_t, size_t)", \
7639 : "length exceeds max allowed value")) { \
7640 : return Local<Type##Array>(); \
7641 : } \
7642 : i::Handle<i::JSArrayBuffer> buffer = Utils::OpenHandle(*array_buffer); \
7643 : i::Handle<i::JSTypedArray> obj = isolate->factory()->NewJSTypedArray( \
7644 : i::kExternal##Type##Array, buffer, byte_offset, length); \
7645 : return Utils::ToLocal##Type##Array(obj); \
7646 : } \
7647 : Local<Type##Array> Type##Array::New( \
7648 : Local<SharedArrayBuffer> shared_array_buffer, size_t byte_offset, \
7649 : size_t length) { \
7650 : CHECK(i::FLAG_harmony_sharedarraybuffer); \
7651 : i::Isolate* isolate = \
7652 : Utils::OpenHandle(*shared_array_buffer)->GetIsolate(); \
7653 : LOG_API(isolate, Type##Array, New); \
7654 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(isolate); \
7655 : if (!Utils::ApiCheck( \
7656 : length <= kMaxLength, \
7657 : "v8::" #Type \
7658 : "Array::New(Local<SharedArrayBuffer>, size_t, size_t)", \
7659 : "length exceeds max allowed value")) { \
7660 : return Local<Type##Array>(); \
7661 : } \
7662 : i::Handle<i::JSArrayBuffer> buffer = \
7663 : Utils::OpenHandle(*shared_array_buffer); \
7664 : i::Handle<i::JSTypedArray> obj = isolate->factory()->NewJSTypedArray( \
7665 : i::kExternal##Type##Array, buffer, byte_offset, length); \
7666 : return Utils::ToLocal##Type##Array(obj); \
7667 : }
7668 :
7669 1120 : TYPED_ARRAYS(TYPED_ARRAY_NEW)
7670 : #undef TYPED_ARRAY_NEW
7671 :
7672 17 : Local<DataView> DataView::New(Local<ArrayBuffer> array_buffer,
7673 : size_t byte_offset, size_t byte_length) {
7674 : i::Handle<i::JSArrayBuffer> buffer = Utils::OpenHandle(*array_buffer);
7675 17 : i::Isolate* isolate = buffer->GetIsolate();
7676 34 : LOG_API(isolate, DataView, New);
7677 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(isolate);
7678 : i::Handle<i::JSDataView> obj =
7679 17 : isolate->factory()->NewJSDataView(buffer, byte_offset, byte_length);
7680 17 : return Utils::ToLocal(obj);
7681 : }
7682 :
7683 :
7684 6 : Local<DataView> DataView::New(Local<SharedArrayBuffer> shared_array_buffer,
7685 : size_t byte_offset, size_t byte_length) {
7686 6 : CHECK(i::FLAG_harmony_sharedarraybuffer);
7687 : i::Handle<i::JSArrayBuffer> buffer = Utils::OpenHandle(*shared_array_buffer);
7688 6 : i::Isolate* isolate = buffer->GetIsolate();
7689 12 : LOG_API(isolate, DataView, New);
7690 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(isolate);
7691 : i::Handle<i::JSDataView> obj =
7692 6 : isolate->factory()->NewJSDataView(buffer, byte_offset, byte_length);
7693 6 : return Utils::ToLocal(obj);
7694 : }
7695 :
7696 :
7697 408 : bool v8::SharedArrayBuffer::IsExternal() const {
7698 408 : return Utils::OpenHandle(this)->is_external();
7699 : }
7700 :
7701 162 : v8::SharedArrayBuffer::Contents v8::SharedArrayBuffer::Externalize() {
7702 : i::Handle<i::JSArrayBuffer> self = Utils::OpenHandle(this);
7703 : i::Isolate* isolate = self->GetIsolate();
7704 : Utils::ApiCheck(!self->is_external(), "v8_SharedArrayBuffer_Externalize",
7705 : "SharedArrayBuffer already externalized");
7706 : self->set_is_external(true);
7707 :
7708 162 : const v8::SharedArrayBuffer::Contents contents = GetContents();
7709 162 : isolate->heap()->UnregisterArrayBuffer(*self);
7710 :
7711 : // A regular copy is good enough. No move semantics needed.
7712 162 : return contents;
7713 : }
7714 :
7715 0 : v8::SharedArrayBuffer::Contents::Contents(
7716 : void* data, size_t byte_length, void* allocation_base,
7717 : size_t allocation_length, Allocator::AllocationMode allocation_mode,
7718 : DeleterCallback deleter, void* deleter_data)
7719 : : data_(data),
7720 : byte_length_(byte_length),
7721 : allocation_base_(allocation_base),
7722 : allocation_length_(allocation_length),
7723 : allocation_mode_(allocation_mode),
7724 : deleter_(deleter),
7725 396 : deleter_data_(deleter_data) {
7726 : DCHECK_LE(allocation_base_, data_);
7727 : DCHECK_LE(byte_length_, allocation_length_);
7728 0 : }
7729 :
7730 396 : v8::SharedArrayBuffer::Contents v8::SharedArrayBuffer::GetContents() {
7731 : i::Handle<i::JSArrayBuffer> self = Utils::OpenHandle(this);
7732 : Contents contents(
7733 : self->backing_store(), self->byte_length(), self->allocation_base(),
7734 : self->allocation_length(),
7735 : self->is_wasm_memory()
7736 : ? ArrayBuffer::Allocator::AllocationMode::kReservation
7737 : : ArrayBuffer::Allocator::AllocationMode::kNormal,
7738 : self->is_wasm_memory()
7739 : ? reinterpret_cast<Contents::DeleterCallback>(WasmMemoryDeleter)
7740 : : reinterpret_cast<Contents::DeleterCallback>(ArrayBufferDeleter),
7741 : self->is_wasm_memory()
7742 : ? static_cast<void*>(self->GetIsolate()->wasm_engine())
7743 2655 : : static_cast<void*>(self->GetIsolate()->array_buffer_allocator()));
7744 396 : return contents;
7745 : }
7746 :
7747 38 : size_t v8::SharedArrayBuffer::ByteLength() const {
7748 : i::Handle<i::JSArrayBuffer> obj = Utils::OpenHandle(this);
7749 38 : return obj->byte_length();
7750 : }
7751 :
7752 12 : Local<SharedArrayBuffer> v8::SharedArrayBuffer::New(Isolate* isolate,
7753 : size_t byte_length) {
7754 12 : CHECK(i::FLAG_harmony_sharedarraybuffer);
7755 12 : i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
7756 24 : LOG_API(i_isolate, SharedArrayBuffer, New);
7757 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(i_isolate);
7758 : i::Handle<i::JSArrayBuffer> obj =
7759 12 : i_isolate->factory()->NewJSArrayBuffer(i::SharedFlag::kShared);
7760 : // TODO(jbroman): It may be useful in the future to provide a MaybeLocal
7761 : // version that throws an exception or otherwise does not crash.
7762 12 : if (!i::JSArrayBuffer::SetupAllocatingData(obj, i_isolate, byte_length, true,
7763 12 : i::SharedFlag::kShared)) {
7764 0 : i::FatalProcessOutOfMemory(i_isolate, "v8::SharedArrayBuffer::New");
7765 : }
7766 12 : return Utils::ToLocalShared(obj);
7767 : }
7768 :
7769 :
7770 448 : Local<SharedArrayBuffer> v8::SharedArrayBuffer::New(
7771 : Isolate* isolate, void* data, size_t byte_length,
7772 : ArrayBufferCreationMode mode) {
7773 448 : CHECK(i::FLAG_harmony_sharedarraybuffer);
7774 : // Embedders must guarantee that the external backing store is valid.
7775 448 : CHECK(byte_length == 0 || data != nullptr);
7776 448 : i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
7777 896 : LOG_API(i_isolate, SharedArrayBuffer, New);
7778 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(i_isolate);
7779 : i::Handle<i::JSArrayBuffer> obj =
7780 448 : i_isolate->factory()->NewJSArrayBuffer(i::SharedFlag::kShared);
7781 : bool is_wasm_memory =
7782 448 : i_isolate->wasm_engine()->memory_tracker()->IsWasmMemory(data);
7783 : i::JSArrayBuffer::Setup(obj, i_isolate,
7784 : mode == ArrayBufferCreationMode::kExternalized, data,
7785 448 : byte_length, i::SharedFlag::kShared, is_wasm_memory);
7786 448 : return Utils::ToLocalShared(obj);
7787 : }
7788 :
7789 :
7790 131 : Local<Symbol> v8::Symbol::New(Isolate* isolate, Local<String> name) {
7791 131 : i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
7792 262 : LOG_API(i_isolate, Symbol, New);
7793 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(i_isolate);
7794 131 : i::Handle<i::Symbol> result = i_isolate->factory()->NewSymbol();
7795 225 : if (!name.IsEmpty()) result->set_name(*Utils::OpenHandle(*name));
7796 131 : return Utils::ToLocal(result);
7797 : }
7798 :
7799 :
7800 24 : Local<Symbol> v8::Symbol::For(Isolate* isolate, Local<String> name) {
7801 : i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
7802 24 : i::Handle<i::String> i_name = Utils::OpenHandle(*name);
7803 : return Utils::ToLocal(
7804 24 : i_isolate->SymbolFor(i::RootIndex::kPublicSymbolTable, i_name, false));
7805 : }
7806 :
7807 :
7808 24 : Local<Symbol> v8::Symbol::ForApi(Isolate* isolate, Local<String> name) {
7809 : i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
7810 24 : i::Handle<i::String> i_name = Utils::OpenHandle(*name);
7811 : return Utils::ToLocal(
7812 24 : i_isolate->SymbolFor(i::RootIndex::kApiSymbolTable, i_name, false));
7813 : }
7814 :
7815 : #define WELL_KNOWN_SYMBOLS(V) \
7816 : V(AsyncIterator, async_iterator) \
7817 : V(HasInstance, has_instance) \
7818 : V(IsConcatSpreadable, is_concat_spreadable) \
7819 : V(Iterator, iterator) \
7820 : V(Match, match) \
7821 : V(Replace, replace) \
7822 : V(Search, search) \
7823 : V(Split, split) \
7824 : V(ToPrimitive, to_primitive) \
7825 : V(ToStringTag, to_string_tag) \
7826 : V(Unscopables, unscopables)
7827 :
7828 : #define SYMBOL_GETTER(Name, name) \
7829 : Local<Symbol> v8::Symbol::Get##Name(Isolate* isolate) { \
7830 : i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate); \
7831 : return Utils::ToLocal(i_isolate->factory()->name##_symbol()); \
7832 : }
7833 :
7834 102754 : WELL_KNOWN_SYMBOLS(SYMBOL_GETTER)
7835 :
7836 : #undef SYMBOL_GETTER
7837 : #undef WELL_KNOWN_SYMBOLS
7838 :
7839 159 : Local<Private> v8::Private::New(Isolate* isolate, Local<String> name) {
7840 159 : i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
7841 318 : LOG_API(i_isolate, Private, New);
7842 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(i_isolate);
7843 159 : i::Handle<i::Symbol> symbol = i_isolate->factory()->NewPrivateSymbol();
7844 253 : if (!name.IsEmpty()) symbol->set_name(*Utils::OpenHandle(*name));
7845 : Local<Symbol> result = Utils::ToLocal(symbol);
7846 318 : return v8::Local<Private>(reinterpret_cast<Private*>(*result));
7847 : }
7848 :
7849 :
7850 254759 : Local<Private> v8::Private::ForApi(Isolate* isolate, Local<String> name) {
7851 : i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
7852 254759 : i::Handle<i::String> i_name = Utils::OpenHandle(*name);
7853 : Local<Symbol> result = Utils::ToLocal(
7854 254759 : i_isolate->SymbolFor(i::RootIndex::kApiPrivateSymbolTable, i_name, true));
7855 254759 : return v8::Local<Private>(reinterpret_cast<Private*>(*result));
7856 : }
7857 :
7858 :
7859 102298 : Local<Number> v8::Number::New(Isolate* isolate, double value) {
7860 : i::Isolate* internal_isolate = reinterpret_cast<i::Isolate*>(isolate);
7861 102298 : if (std::isnan(value)) {
7862 : // Introduce only canonical NaN value into the VM, to avoid signaling NaNs.
7863 : value = std::numeric_limits<double>::quiet_NaN();
7864 : }
7865 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(internal_isolate);
7866 102298 : i::Handle<i::Object> result = internal_isolate->factory()->NewNumber(value);
7867 102298 : return Utils::NumberToLocal(result);
7868 : }
7869 :
7870 :
7871 726929 : Local<Integer> v8::Integer::New(Isolate* isolate, int32_t value) {
7872 : i::Isolate* internal_isolate = reinterpret_cast<i::Isolate*>(isolate);
7873 : if (i::Smi::IsValid(value)) {
7874 : return Utils::IntegerToLocal(i::Handle<i::Object>(i::Smi::FromInt(value),
7875 : internal_isolate));
7876 : }
7877 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(internal_isolate);
7878 : i::Handle<i::Object> result = internal_isolate->factory()->NewNumber(value);
7879 : return Utils::IntegerToLocal(result);
7880 : }
7881 :
7882 :
7883 122 : Local<Integer> v8::Integer::NewFromUnsigned(Isolate* isolate, uint32_t value) {
7884 : i::Isolate* internal_isolate = reinterpret_cast<i::Isolate*>(isolate);
7885 122 : bool fits_into_int32_t = (value & (1 << 31)) == 0;
7886 122 : if (fits_into_int32_t) {
7887 29 : return Integer::New(isolate, static_cast<int32_t>(value));
7888 : }
7889 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(internal_isolate);
7890 93 : i::Handle<i::Object> result = internal_isolate->factory()->NewNumber(value);
7891 : return Utils::IntegerToLocal(result);
7892 : }
7893 :
7894 90 : Local<BigInt> v8::BigInt::New(Isolate* isolate, int64_t value) {
7895 : i::Isolate* internal_isolate = reinterpret_cast<i::Isolate*>(isolate);
7896 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(internal_isolate);
7897 90 : i::Handle<i::BigInt> result = i::BigInt::FromInt64(internal_isolate, value);
7898 90 : return Utils::ToLocal(result);
7899 : }
7900 :
7901 0 : Local<BigInt> v8::BigInt::NewFromUnsigned(Isolate* isolate, uint64_t value) {
7902 : i::Isolate* internal_isolate = reinterpret_cast<i::Isolate*>(isolate);
7903 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(internal_isolate);
7904 0 : i::Handle<i::BigInt> result = i::BigInt::FromUint64(internal_isolate, value);
7905 0 : return Utils::ToLocal(result);
7906 : }
7907 :
7908 30 : MaybeLocal<BigInt> v8::BigInt::NewFromWords(Local<Context> context,
7909 : int sign_bit, int word_count,
7910 : const uint64_t* words) {
7911 60 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(context->GetIsolate());
7912 120 : ENTER_V8_NO_SCRIPT(isolate, context, BigInt, NewFromWords,
7913 : MaybeLocal<BigInt>(), InternalEscapableScope);
7914 : i::MaybeHandle<i::BigInt> result =
7915 30 : i::BigInt::FromWords64(isolate, sign_bit, word_count, words);
7916 : has_pending_exception = result.is_null();
7917 30 : RETURN_ON_FAILED_EXECUTION(BigInt);
7918 15 : RETURN_ESCAPED(Utils::ToLocal(result.ToHandleChecked()));
7919 : }
7920 :
7921 55 : uint64_t v8::BigInt::Uint64Value(bool* lossless) const {
7922 : i::Handle<i::BigInt> handle = Utils::OpenHandle(this);
7923 55 : return handle->AsUint64(lossless);
7924 : }
7925 :
7926 95 : int64_t v8::BigInt::Int64Value(bool* lossless) const {
7927 : i::Handle<i::BigInt> handle = Utils::OpenHandle(this);
7928 95 : return handle->AsInt64(lossless);
7929 : }
7930 :
7931 15 : int BigInt::WordCount() const {
7932 : i::Handle<i::BigInt> handle = Utils::OpenHandle(this);
7933 15 : return handle->Words64Count();
7934 : }
7935 :
7936 10 : void BigInt::ToWordsArray(int* sign_bit, int* word_count,
7937 : uint64_t* words) const {
7938 : i::Handle<i::BigInt> handle = Utils::OpenHandle(this);
7939 10 : return handle->ToWordsArray64(sign_bit, word_count, words);
7940 : }
7941 :
7942 1200536 : void Isolate::ReportExternalAllocationLimitReached() {
7943 1200536 : i::Heap* heap = reinterpret_cast<i::Isolate*>(this)->heap();
7944 2401072 : if (heap->gc_state() != i::Heap::NOT_IN_GC) return;
7945 1200536 : heap->ReportExternalMemoryPressure();
7946 : }
7947 :
7948 2121124 : void Isolate::CheckMemoryPressure() {
7949 2121124 : i::Heap* heap = reinterpret_cast<i::Isolate*>(this)->heap();
7950 4242248 : if (heap->gc_state() != i::Heap::NOT_IN_GC) return;
7951 2121124 : heap->CheckMemoryPressure();
7952 : }
7953 :
7954 8123 : HeapProfiler* Isolate::GetHeapProfiler() {
7955 : i::HeapProfiler* heap_profiler =
7956 : reinterpret_cast<i::Isolate*>(this)->heap_profiler();
7957 8123 : return reinterpret_cast<HeapProfiler*>(heap_profiler);
7958 : }
7959 :
7960 0 : void Isolate::SetIdle(bool is_idle) {
7961 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
7962 0 : isolate->SetIdle(is_idle);
7963 0 : }
7964 :
7965 88361 : bool Isolate::InContext() {
7966 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
7967 88361 : return !isolate->context().is_null();
7968 : }
7969 :
7970 :
7971 13141318 : v8::Local<v8::Context> Isolate::GetCurrentContext() {
7972 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
7973 13141318 : i::Context context = isolate->context();
7974 13141318 : if (context.is_null()) return Local<Context>();
7975 13141280 : i::Context native_context = context->native_context();
7976 13141286 : if (native_context.is_null()) return Local<Context>();
7977 : return Utils::ToLocal(i::Handle<i::Context>(native_context, isolate));
7978 : }
7979 :
7980 :
7981 0 : v8::Local<v8::Context> Isolate::GetEnteredContext() {
7982 0 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
7983 : i::Handle<i::Object> last =
7984 0 : isolate->handle_scope_implementer()->LastEnteredContext();
7985 0 : if (last.is_null()) return Local<Context>();
7986 0 : return Utils::ToLocal(i::Handle<i::Context>::cast(last));
7987 : }
7988 :
7989 51622 : v8::Local<v8::Context> Isolate::GetEnteredOrMicrotaskContext() {
7990 51622 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
7991 : i::Handle<i::Object> last =
7992 51622 : isolate->handle_scope_implementer()->LastEnteredOrMicrotaskContext();
7993 51622 : if (last.is_null()) return Local<Context>();
7994 : DCHECK(last->IsNativeContext());
7995 51622 : return Utils::ToLocal(i::Handle<i::Context>::cast(last));
7996 : }
7997 :
7998 4 : v8::Local<v8::Context> Isolate::GetIncumbentContext() {
7999 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
8000 4 : i::Handle<i::Context> context = isolate->GetIncumbentContext();
8001 4 : return Utils::ToLocal(context);
8002 : }
8003 :
8004 5428 : v8::Local<Value> Isolate::ThrowException(v8::Local<v8::Value> value) {
8005 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
8006 : ENTER_V8_DO_NOT_USE(isolate);
8007 : // If we're passed an empty handle, we throw an undefined exception
8008 : // to deal more gracefully with out of memory situations.
8009 5428 : if (value.IsEmpty()) {
8010 24 : isolate->ScheduleThrow(i::ReadOnlyRoots(isolate).undefined_value());
8011 : } else {
8012 5404 : isolate->ScheduleThrow(*Utils::OpenHandle(*value));
8013 : }
8014 10856 : return v8::Undefined(reinterpret_cast<v8::Isolate*>(isolate));
8015 : }
8016 :
8017 10 : void Isolate::AddGCPrologueCallback(GCCallbackWithData callback, void* data,
8018 : GCType gc_type) {
8019 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
8020 40 : isolate->heap()->AddGCPrologueCallback(callback, gc_type, data);
8021 10 : }
8022 :
8023 10 : void Isolate::RemoveGCPrologueCallback(GCCallbackWithData callback,
8024 : void* data) {
8025 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
8026 35 : isolate->heap()->RemoveGCPrologueCallback(callback, data);
8027 10 : }
8028 :
8029 10 : void Isolate::AddGCEpilogueCallback(GCCallbackWithData callback, void* data,
8030 : GCType gc_type) {
8031 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
8032 35 : isolate->heap()->AddGCEpilogueCallback(callback, gc_type, data);
8033 10 : }
8034 :
8035 10 : void Isolate::RemoveGCEpilogueCallback(GCCallbackWithData callback,
8036 : void* data) {
8037 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
8038 35 : isolate->heap()->RemoveGCEpilogueCallback(callback, data);
8039 10 : }
8040 :
8041 92 : static void CallGCCallbackWithoutData(Isolate* isolate, GCType type,
8042 : GCCallbackFlags flags, void* data) {
8043 92 : reinterpret_cast<Isolate::GCCallback>(data)(isolate, type, flags);
8044 92 : }
8045 :
8046 30 : void Isolate::AddGCPrologueCallback(GCCallback callback, GCType gc_type) {
8047 : void* data = reinterpret_cast<void*>(callback);
8048 : AddGCPrologueCallback(CallGCCallbackWithoutData, data, gc_type);
8049 30 : }
8050 :
8051 25 : void Isolate::RemoveGCPrologueCallback(GCCallback callback) {
8052 : void* data = reinterpret_cast<void*>(callback);
8053 : RemoveGCPrologueCallback(CallGCCallbackWithoutData, data);
8054 25 : }
8055 :
8056 25 : void Isolate::AddGCEpilogueCallback(GCCallback callback, GCType gc_type) {
8057 : void* data = reinterpret_cast<void*>(callback);
8058 : AddGCEpilogueCallback(CallGCCallbackWithoutData, data, gc_type);
8059 25 : }
8060 :
8061 25 : void Isolate::RemoveGCEpilogueCallback(GCCallback callback) {
8062 : void* data = reinterpret_cast<void*>(callback);
8063 : RemoveGCEpilogueCallback(CallGCCallbackWithoutData, data);
8064 25 : }
8065 :
8066 70 : void Isolate::SetEmbedderHeapTracer(EmbedderHeapTracer* tracer) {
8067 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
8068 70 : isolate->heap()->SetEmbedderHeapTracer(tracer);
8069 70 : }
8070 :
8071 0 : EmbedderHeapTracer* Isolate::GetEmbedderHeapTracer() {
8072 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
8073 0 : return isolate->heap()->GetEmbedderHeapTracer();
8074 : }
8075 :
8076 0 : void Isolate::SetGetExternallyAllocatedMemoryInBytesCallback(
8077 : GetExternallyAllocatedMemoryInBytesCallback callback) {
8078 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
8079 : isolate->heap()->SetGetExternallyAllocatedMemoryInBytesCallback(callback);
8080 0 : }
8081 :
8082 1254 : void Isolate::TerminateExecution() {
8083 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
8084 1254 : isolate->stack_guard()->RequestTerminateExecution();
8085 1254 : }
8086 :
8087 :
8088 1500 : bool Isolate::IsExecutionTerminating() {
8089 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
8090 1500 : return IsExecutionTerminatingCheck(isolate);
8091 : }
8092 :
8093 :
8094 85 : void Isolate::CancelTerminateExecution() {
8095 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
8096 85 : isolate->stack_guard()->ClearTerminateExecution();
8097 85 : isolate->CancelTerminateExecution();
8098 85 : }
8099 :
8100 :
8101 60020 : void Isolate::RequestInterrupt(InterruptCallback callback, void* data) {
8102 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
8103 60020 : isolate->RequestInterrupt(callback, data);
8104 60020 : }
8105 :
8106 :
8107 20821 : void Isolate::RequestGarbageCollectionForTesting(GarbageCollectionType type) {
8108 20821 : CHECK(i::FLAG_expose_gc);
8109 20821 : if (type == kMinorGarbageCollection) {
8110 : reinterpret_cast<i::Isolate*>(this)->heap()->CollectGarbage(
8111 : i::NEW_SPACE, i::GarbageCollectionReason::kTesting,
8112 108 : kGCCallbackFlagForced);
8113 : } else {
8114 : DCHECK_EQ(kFullGarbageCollection, type);
8115 : reinterpret_cast<i::Isolate*>(this)->heap()->PreciseCollectAllGarbage(
8116 : i::Heap::kNoGCFlags, i::GarbageCollectionReason::kTesting,
8117 20713 : kGCCallbackFlagForced);
8118 : }
8119 20821 : }
8120 :
8121 :
8122 923661 : Isolate* Isolate::GetCurrent() {
8123 : i::Isolate* isolate = i::Isolate::Current();
8124 923661 : return reinterpret_cast<Isolate*>(isolate);
8125 : }
8126 :
8127 : // static
8128 106 : Isolate* Isolate::Allocate() {
8129 62621 : return reinterpret_cast<Isolate*>(i::Isolate::New());
8130 : }
8131 :
8132 : // static
8133 : // This is separate so that tests can provide a different |isolate|.
8134 62586 : void Isolate::Initialize(Isolate* isolate,
8135 : const v8::Isolate::CreateParams& params) {
8136 0 : i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
8137 62586 : CHECK_NOT_NULL(params.array_buffer_allocator);
8138 : i_isolate->set_array_buffer_allocator(params.array_buffer_allocator);
8139 62586 : if (params.snapshot_blob != nullptr) {
8140 : i_isolate->set_snapshot_blob(params.snapshot_blob);
8141 : } else {
8142 62416 : i_isolate->set_snapshot_blob(i::Snapshot::DefaultSnapshotBlob());
8143 : }
8144 62587 : auto code_event_handler = params.code_event_handler;
8145 : #ifdef ENABLE_GDB_JIT_INTERFACE
8146 62587 : if (code_event_handler == nullptr && i::FLAG_gdbjit) {
8147 : code_event_handler = i::GDBJITInterface::EventHandler;
8148 : }
8149 : #endif // ENABLE_GDB_JIT_INTERFACE
8150 62587 : if (code_event_handler) {
8151 0 : i_isolate->InitializeLoggingAndCounters();
8152 : i_isolate->logger()->SetCodeEventHandler(kJitCodeEventDefault,
8153 0 : code_event_handler);
8154 : }
8155 62587 : if (params.counter_lookup_callback) {
8156 : isolate->SetCounterFunction(params.counter_lookup_callback);
8157 : }
8158 :
8159 62587 : if (params.create_histogram_callback) {
8160 : isolate->SetCreateHistogramFunction(params.create_histogram_callback);
8161 : }
8162 :
8163 62587 : if (params.add_histogram_sample_callback) {
8164 : isolate->SetAddHistogramSampleFunction(
8165 : params.add_histogram_sample_callback);
8166 : }
8167 :
8168 62587 : i_isolate->set_api_external_references(params.external_references);
8169 62587 : i_isolate->set_allow_atomics_wait(params.allow_atomics_wait);
8170 :
8171 62587 : SetResourceConstraints(i_isolate, params.constraints);
8172 : // TODO(jochen): Once we got rid of Isolate::Current(), we can remove this.
8173 : Isolate::Scope isolate_scope(isolate);
8174 62587 : if (!i::Snapshot::Initialize(i_isolate)) {
8175 : // If snapshot data was provided and we failed to deserialize it must
8176 : // have been corrupted.
8177 0 : if (i_isolate->snapshot_blob() != nullptr) {
8178 : FATAL(
8179 : "Failed to deserialize the V8 snapshot blob. This can mean that the "
8180 0 : "snapshot blob file is corrupted or missing.");
8181 : }
8182 : base::ElapsedTimer timer;
8183 0 : if (i::FLAG_profile_deserialization) timer.Start();
8184 0 : i_isolate->Init(nullptr);
8185 0 : if (i::FLAG_profile_deserialization) {
8186 0 : double ms = timer.Elapsed().InMillisecondsF();
8187 0 : i::PrintF("[Initializing isolate from scratch took %0.3f ms]\n", ms);
8188 : }
8189 : }
8190 : i_isolate->set_only_terminate_in_safe_scope(
8191 62585 : params.only_terminate_in_safe_scope);
8192 62585 : }
8193 :
8194 62330 : Isolate* Isolate::New(const Isolate::CreateParams& params) {
8195 : Isolate* isolate = Allocate();
8196 62330 : Initialize(isolate, params);
8197 62329 : return isolate;
8198 : }
8199 :
8200 62873 : void Isolate::Dispose() {
8201 62873 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
8202 125746 : if (!Utils::ApiCheck(!isolate->IsInUse(),
8203 : "v8::Isolate::Dispose()",
8204 : "Disposing the isolate that is entered by a thread.")) {
8205 62872 : return;
8206 : }
8207 62868 : i::Isolate::Delete(isolate);
8208 : }
8209 :
8210 0 : void Isolate::DumpAndResetStats() {
8211 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
8212 0 : isolate->DumpAndResetStats();
8213 0 : }
8214 :
8215 1499 : void Isolate::DiscardThreadSpecificMetadata() {
8216 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
8217 1499 : isolate->DiscardPerThreadDataForThisThread();
8218 1497 : }
8219 :
8220 :
8221 195612 : void Isolate::Enter() {
8222 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
8223 258385 : isolate->Enter();
8224 195607 : }
8225 :
8226 :
8227 195601 : void Isolate::Exit() {
8228 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
8229 258372 : isolate->Exit();
8230 195597 : }
8231 :
8232 :
8233 5 : void Isolate::SetAbortOnUncaughtExceptionCallback(
8234 : AbortOnUncaughtExceptionCallback callback) {
8235 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
8236 5 : isolate->SetAbortOnUncaughtExceptionCallback(callback);
8237 5 : }
8238 :
8239 29447 : void Isolate::SetHostImportModuleDynamicallyCallback(
8240 : HostImportModuleDynamicallyCallback callback) {
8241 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
8242 29447 : isolate->SetHostImportModuleDynamicallyCallback(callback);
8243 29447 : }
8244 :
8245 29447 : void Isolate::SetHostInitializeImportMetaObjectCallback(
8246 : HostInitializeImportMetaObjectCallback callback) {
8247 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
8248 29447 : isolate->SetHostInitializeImportMetaObjectCallback(callback);
8249 29447 : }
8250 :
8251 12 : void Isolate::SetPrepareStackTraceCallback(PrepareStackTraceCallback callback) {
8252 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
8253 12 : isolate->SetPrepareStackTraceCallback(callback);
8254 12 : }
8255 :
8256 28904 : Isolate::DisallowJavascriptExecutionScope::DisallowJavascriptExecutionScope(
8257 : Isolate* isolate,
8258 : Isolate::DisallowJavascriptExecutionScope::OnFailure on_failure)
8259 28904 : : on_failure_(on_failure) {
8260 : i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
8261 28904 : switch (on_failure_) {
8262 : case CRASH_ON_FAILURE:
8263 : internal_ = reinterpret_cast<void*>(
8264 5 : new i::DisallowJavascriptExecution(i_isolate));
8265 5 : break;
8266 : case THROW_ON_FAILURE:
8267 : DCHECK_EQ(THROW_ON_FAILURE, on_failure);
8268 : internal_ =
8269 28894 : reinterpret_cast<void*>(new i::ThrowOnJavascriptExecution(i_isolate));
8270 28894 : break;
8271 : case DUMP_ON_FAILURE:
8272 : internal_ =
8273 5 : reinterpret_cast<void*>(new i::DumpOnJavascriptExecution(i_isolate));
8274 5 : break;
8275 : default:
8276 0 : UNREACHABLE();
8277 : break;
8278 : }
8279 28904 : }
8280 :
8281 :
8282 28904 : Isolate::DisallowJavascriptExecutionScope::~DisallowJavascriptExecutionScope() {
8283 28904 : switch (on_failure_) {
8284 : case CRASH_ON_FAILURE:
8285 5 : delete reinterpret_cast<i::DisallowJavascriptExecution*>(internal_);
8286 : break;
8287 : case THROW_ON_FAILURE:
8288 28894 : delete reinterpret_cast<i::ThrowOnJavascriptExecution*>(internal_);
8289 : break;
8290 : case DUMP_ON_FAILURE:
8291 5 : delete reinterpret_cast<i::DumpOnJavascriptExecution*>(internal_);
8292 : break;
8293 : default:
8294 0 : UNREACHABLE();
8295 : break;
8296 : }
8297 28904 : }
8298 :
8299 :
8300 271285 : Isolate::AllowJavascriptExecutionScope::AllowJavascriptExecutionScope(
8301 : Isolate* isolate) {
8302 : i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
8303 : internal_assert_ = reinterpret_cast<void*>(
8304 271285 : new i::AllowJavascriptExecution(i_isolate));
8305 : internal_throws_ = reinterpret_cast<void*>(
8306 271285 : new i::NoThrowOnJavascriptExecution(i_isolate));
8307 : internal_dump_ =
8308 271285 : reinterpret_cast<void*>(new i::NoDumpOnJavascriptExecution(i_isolate));
8309 271285 : }
8310 :
8311 :
8312 271285 : Isolate::AllowJavascriptExecutionScope::~AllowJavascriptExecutionScope() {
8313 271285 : delete reinterpret_cast<i::AllowJavascriptExecution*>(internal_assert_);
8314 271285 : delete reinterpret_cast<i::NoThrowOnJavascriptExecution*>(internal_throws_);
8315 271285 : delete reinterpret_cast<i::NoDumpOnJavascriptExecution*>(internal_dump_);
8316 271285 : }
8317 :
8318 :
8319 52475 : Isolate::SuppressMicrotaskExecutionScope::SuppressMicrotaskExecutionScope(
8320 : Isolate* isolate)
8321 52475 : : isolate_(reinterpret_cast<i::Isolate*>(isolate)) {
8322 104950 : isolate_->handle_scope_implementer()->IncrementCallDepth();
8323 52475 : isolate_->default_microtask_queue()->IncrementMicrotasksSuppressions();
8324 52475 : }
8325 :
8326 :
8327 52475 : Isolate::SuppressMicrotaskExecutionScope::~SuppressMicrotaskExecutionScope() {
8328 104950 : isolate_->default_microtask_queue()->DecrementMicrotasksSuppressions();
8329 52475 : isolate_->handle_scope_implementer()->DecrementCallDepth();
8330 52475 : }
8331 :
8332 23606 : Isolate::SafeForTerminationScope::SafeForTerminationScope(v8::Isolate* isolate)
8333 : : isolate_(reinterpret_cast<i::Isolate*>(isolate)),
8334 23606 : prev_value_(isolate_->next_v8_call_is_safe_for_termination()) {
8335 : isolate_->set_next_v8_call_is_safe_for_termination(true);
8336 23606 : }
8337 :
8338 23606 : Isolate::SafeForTerminationScope::~SafeForTerminationScope() {
8339 23606 : isolate_->set_next_v8_call_is_safe_for_termination(prev_value_);
8340 23606 : }
8341 :
8342 85 : i::Address* Isolate::GetDataFromSnapshotOnce(size_t index) {
8343 : i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(this);
8344 85 : i::FixedArray list = i_isolate->heap()->serialized_objects();
8345 85 : return GetSerializedDataFromFixedArray(i_isolate, list, index);
8346 : }
8347 :
8348 246 : void Isolate::GetHeapStatistics(HeapStatistics* heap_statistics) {
8349 492 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
8350 246 : i::Heap* heap = isolate->heap();
8351 246 : heap_statistics->total_heap_size_ = heap->CommittedMemory();
8352 : heap_statistics->total_heap_size_executable_ =
8353 246 : heap->CommittedMemoryExecutable();
8354 246 : heap_statistics->total_physical_size_ = heap->CommittedPhysicalMemory();
8355 246 : heap_statistics->total_available_size_ = heap->Available();
8356 246 : heap_statistics->used_heap_size_ = heap->SizeOfObjects();
8357 246 : heap_statistics->heap_size_limit_ = heap->MaxReserved();
8358 : // TODO(7424): There is no public API for the {WasmEngine} yet. Once such an
8359 : // API becomes available we should report the malloced memory separately. For
8360 : // now we just add the values, thereby over-approximating the peak slightly.
8361 : heap_statistics->malloced_memory_ =
8362 492 : isolate->allocator()->GetCurrentMemoryUsage() +
8363 492 : isolate->wasm_engine()->allocator()->GetCurrentMemoryUsage();
8364 246 : heap_statistics->external_memory_ = isolate->heap()->external_memory();
8365 : heap_statistics->peak_malloced_memory_ =
8366 492 : isolate->allocator()->GetMaxMemoryUsage() +
8367 492 : isolate->wasm_engine()->allocator()->GetMaxMemoryUsage();
8368 246 : heap_statistics->number_of_native_contexts_ = heap->NumberOfNativeContexts();
8369 : heap_statistics->number_of_detached_contexts_ =
8370 246 : heap->NumberOfDetachedContexts();
8371 246 : heap_statistics->does_zap_garbage_ = heap->ShouldZapGarbage();
8372 246 : }
8373 :
8374 :
8375 45 : size_t Isolate::NumberOfHeapSpaces() {
8376 45 : return i::LAST_SPACE - i::FIRST_SPACE + 1;
8377 : }
8378 :
8379 :
8380 40 : bool Isolate::GetHeapSpaceStatistics(HeapSpaceStatistics* space_statistics,
8381 : size_t index) {
8382 40 : if (!space_statistics) return false;
8383 40 : if (!i::Heap::IsValidAllocationSpace(static_cast<i::AllocationSpace>(index)))
8384 : return false;
8385 :
8386 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
8387 40 : i::Heap* heap = isolate->heap();
8388 40 : i::Space* space = heap->space(static_cast<int>(index));
8389 :
8390 40 : space_statistics->space_name_ = heap->GetSpaceName(static_cast<int>(index));
8391 40 : space_statistics->space_size_ = space->CommittedMemory();
8392 40 : space_statistics->space_used_size_ = space->SizeOfObjects();
8393 40 : space_statistics->space_available_size_ = space->Available();
8394 40 : space_statistics->physical_space_size_ = space->CommittedPhysicalMemory();
8395 40 : return true;
8396 : }
8397 :
8398 :
8399 0 : size_t Isolate::NumberOfTrackedHeapObjectTypes() {
8400 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
8401 0 : i::Heap* heap = isolate->heap();
8402 0 : return heap->NumberOfTrackedHeapObjectTypes();
8403 : }
8404 :
8405 :
8406 0 : bool Isolate::GetHeapObjectStatisticsAtLastGC(
8407 : HeapObjectStatistics* object_statistics, size_t type_index) {
8408 0 : if (!object_statistics) return false;
8409 0 : if (V8_LIKELY(!i::FLAG_gc_stats)) return false;
8410 :
8411 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
8412 0 : i::Heap* heap = isolate->heap();
8413 0 : if (type_index >= heap->NumberOfTrackedHeapObjectTypes()) return false;
8414 :
8415 : const char* object_type;
8416 : const char* object_sub_type;
8417 0 : size_t object_count = heap->ObjectCountAtLastGC(type_index);
8418 0 : size_t object_size = heap->ObjectSizeAtLastGC(type_index);
8419 0 : if (!heap->GetObjectTypeName(type_index, &object_type, &object_sub_type)) {
8420 : // There should be no objects counted when the type is unknown.
8421 : DCHECK_EQ(object_count, 0U);
8422 : DCHECK_EQ(object_size, 0U);
8423 : return false;
8424 : }
8425 :
8426 0 : object_statistics->object_type_ = object_type;
8427 0 : object_statistics->object_sub_type_ = object_sub_type;
8428 0 : object_statistics->object_count_ = object_count;
8429 0 : object_statistics->object_size_ = object_size;
8430 0 : return true;
8431 : }
8432 :
8433 0 : bool Isolate::GetHeapCodeAndMetadataStatistics(
8434 : HeapCodeStatistics* code_statistics) {
8435 0 : if (!code_statistics) return false;
8436 :
8437 0 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
8438 0 : isolate->heap()->CollectCodeStatistics();
8439 :
8440 0 : code_statistics->code_and_metadata_size_ = isolate->code_and_metadata_size();
8441 : code_statistics->bytecode_and_metadata_size_ =
8442 0 : isolate->bytecode_and_metadata_size();
8443 : code_statistics->external_script_source_size_ =
8444 0 : isolate->external_script_source_size();
8445 0 : return true;
8446 : }
8447 :
8448 15 : void Isolate::GetStackSample(const RegisterState& state, void** frames,
8449 : size_t frames_limit, SampleInfo* sample_info) {
8450 15 : RegisterState regs = state;
8451 15 : if (TickSample::GetStackSample(this, ®s, TickSample::kSkipCEntryFrame,
8452 : frames, frames_limit, sample_info)) {
8453 15 : return;
8454 : }
8455 0 : sample_info->frames_count = 0;
8456 0 : sample_info->vm_state = OTHER;
8457 0 : sample_info->external_callback_entry = nullptr;
8458 : }
8459 :
8460 15 : size_t Isolate::NumberOfPhantomHandleResetsSinceLastCall() {
8461 15 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
8462 15 : size_t result = isolate->global_handles()->NumberOfPhantomHandleResets();
8463 : isolate->global_handles()->ResetNumberOfPhantomHandleResets();
8464 15 : return result;
8465 : }
8466 :
8467 5 : void Isolate::SetEventLogger(LogEventCallback that) {
8468 : // Do not overwrite the event logger if we want to log explicitly.
8469 10 : if (i::FLAG_log_internal_timer_events) return;
8470 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
8471 : isolate->set_event_logger(that);
8472 : }
8473 :
8474 :
8475 15 : void Isolate::AddBeforeCallEnteredCallback(BeforeCallEnteredCallback callback) {
8476 30 : if (callback == nullptr) return;
8477 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
8478 15 : isolate->AddBeforeCallEnteredCallback(callback);
8479 : }
8480 :
8481 :
8482 5 : void Isolate::RemoveBeforeCallEnteredCallback(
8483 : BeforeCallEnteredCallback callback) {
8484 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
8485 5 : isolate->RemoveBeforeCallEnteredCallback(callback);
8486 5 : }
8487 :
8488 :
8489 65 : void Isolate::AddCallCompletedCallback(CallCompletedCallback callback) {
8490 130 : if (callback == nullptr) return;
8491 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
8492 65 : isolate->AddCallCompletedCallback(callback);
8493 : }
8494 :
8495 :
8496 3684 : void Isolate::RemoveCallCompletedCallback(CallCompletedCallback callback) {
8497 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
8498 3684 : isolate->RemoveCallCompletedCallback(callback);
8499 3684 : }
8500 :
8501 20 : void Isolate::AtomicsWaitWakeHandle::Wake() {
8502 20 : reinterpret_cast<i::AtomicsWaitWakeHandle*>(this)->Wake();
8503 20 : }
8504 :
8505 5 : void Isolate::SetAtomicsWaitCallback(AtomicsWaitCallback callback, void* data) {
8506 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
8507 5 : isolate->SetAtomicsWaitCallback(callback, data);
8508 5 : }
8509 :
8510 110 : void Isolate::SetPromiseHook(PromiseHook hook) {
8511 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
8512 110 : isolate->SetPromiseHook(hook);
8513 110 : }
8514 :
8515 1135 : void Isolate::SetPromiseRejectCallback(PromiseRejectCallback callback) {
8516 2270 : if (callback == nullptr) return;
8517 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
8518 1135 : isolate->SetPromiseRejectCallback(callback);
8519 : }
8520 :
8521 :
8522 126 : void Isolate::RunMicrotasks() {
8523 : DCHECK_NE(MicrotasksPolicy::kScoped, GetMicrotasksPolicy());
8524 126 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
8525 126 : isolate->default_microtask_queue()->RunMicrotasks(isolate);
8526 126 : }
8527 :
8528 120 : void Isolate::EnqueueMicrotask(Local<Function> function) {
8529 120 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
8530 : i::Handle<i::CallableTask> microtask = isolate->factory()->NewCallableTask(
8531 240 : Utils::OpenHandle(*function), isolate->native_context());
8532 240 : isolate->default_microtask_queue()->EnqueueMicrotask(*microtask);
8533 120 : }
8534 :
8535 441 : void Isolate::EnqueueMicrotask(MicrotaskCallback callback, void* data) {
8536 441 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
8537 : i::HandleScope scope(isolate);
8538 : i::Handle<i::CallbackTask> microtask = isolate->factory()->NewCallbackTask(
8539 : isolate->factory()->NewForeign(reinterpret_cast<i::Address>(callback)),
8540 441 : isolate->factory()->NewForeign(reinterpret_cast<i::Address>(data)));
8541 882 : isolate->default_microtask_queue()->EnqueueMicrotask(*microtask);
8542 441 : }
8543 :
8544 :
8545 2290 : void Isolate::SetMicrotasksPolicy(MicrotasksPolicy policy) {
8546 2290 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
8547 : isolate->handle_scope_implementer()->set_microtasks_policy(policy);
8548 2290 : }
8549 :
8550 :
8551 0 : MicrotasksPolicy Isolate::GetMicrotasksPolicy() const {
8552 0 : i::Isolate* isolate =
8553 : reinterpret_cast<i::Isolate*>(const_cast<Isolate*>(this));
8554 0 : return isolate->handle_scope_implementer()->microtasks_policy();
8555 : }
8556 :
8557 :
8558 45 : void Isolate::AddMicrotasksCompletedCallback(
8559 : MicrotasksCompletedCallback callback) {
8560 : DCHECK(callback);
8561 45 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
8562 45 : isolate->default_microtask_queue()->AddMicrotasksCompletedCallback(callback);
8563 45 : }
8564 :
8565 :
8566 3684 : void Isolate::RemoveMicrotasksCompletedCallback(
8567 : MicrotasksCompletedCallback callback) {
8568 3684 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
8569 : isolate->default_microtask_queue()->RemoveMicrotasksCompletedCallback(
8570 3684 : callback);
8571 3684 : }
8572 :
8573 :
8574 55 : void Isolate::SetUseCounterCallback(UseCounterCallback callback) {
8575 55 : reinterpret_cast<i::Isolate*>(this)->SetUseCounterCallback(callback);
8576 55 : }
8577 :
8578 :
8579 5 : void Isolate::SetCounterFunction(CounterLookupCallback callback) {
8580 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
8581 5 : isolate->counters()->ResetCounterFunction(callback);
8582 5 : }
8583 :
8584 :
8585 8 : void Isolate::SetCreateHistogramFunction(CreateHistogramCallback callback) {
8586 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
8587 8 : isolate->counters()->ResetCreateHistogramFunction(callback);
8588 8 : }
8589 :
8590 :
8591 8 : void Isolate::SetAddHistogramSampleFunction(
8592 : AddHistogramSampleCallback callback) {
8593 : reinterpret_cast<i::Isolate*>(this)
8594 : ->counters()
8595 : ->SetAddHistogramSampleFunction(callback);
8596 8 : }
8597 :
8598 :
8599 460 : bool Isolate::IdleNotificationDeadline(double deadline_in_seconds) {
8600 : // Returning true tells the caller that it need not
8601 : // continue to call IdleNotification.
8602 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
8603 460 : if (!i::FLAG_use_idle_notification) return true;
8604 460 : return isolate->heap()->IdleNotification(deadline_in_seconds);
8605 : }
8606 :
8607 590 : void Isolate::LowMemoryNotification() {
8608 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
8609 : {
8610 : i::HistogramTimerScope idle_notification_scope(
8611 590 : isolate->counters()->gc_low_memory_notification());
8612 1770 : TRACE_EVENT0("v8", "V8.GCLowMemoryNotification");
8613 : isolate->heap()->CollectAllAvailableGarbage(
8614 590 : i::GarbageCollectionReason::kLowMemoryNotification);
8615 : }
8616 : {
8617 590 : i::HeapIterator iterator(isolate->heap());
8618 7533700 : for (i::HeapObject obj = iterator.next(); !obj.is_null();
8619 : obj = iterator.next()) {
8620 3766260 : if (obj->IsAbstractCode()) {
8621 892156 : i::AbstractCode::cast(obj)->DropStackFrameCache();
8622 : }
8623 590 : }
8624 : }
8625 590 : }
8626 :
8627 :
8628 599 : int Isolate::ContextDisposedNotification(bool dependant_context) {
8629 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
8630 599 : if (!dependant_context) {
8631 : // We left the current context, we can abort all WebAssembly compilations on
8632 : // that isolate.
8633 10 : isolate->wasm_engine()->DeleteCompileJobsOnIsolate(isolate);
8634 : }
8635 : // TODO(ahaas): move other non-heap activity out of the heap call.
8636 599 : return isolate->heap()->NotifyContextDisposed(dependant_context);
8637 : }
8638 :
8639 :
8640 0 : void Isolate::IsolateInForegroundNotification() {
8641 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
8642 0 : return isolate->IsolateInForegroundNotification();
8643 : }
8644 :
8645 :
8646 0 : void Isolate::IsolateInBackgroundNotification() {
8647 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
8648 0 : return isolate->IsolateInBackgroundNotification();
8649 : }
8650 :
8651 90 : void Isolate::MemoryPressureNotification(MemoryPressureLevel level) {
8652 110 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
8653 : bool on_isolate_thread =
8654 90 : v8::Locker::IsActive()
8655 : ? isolate->thread_manager()->IsLockedByCurrentThread()
8656 200 : : i::ThreadId::Current().Equals(isolate->thread_id());
8657 90 : isolate->heap()->MemoryPressureNotification(level, on_isolate_thread);
8658 90 : isolate->allocator()->MemoryPressureNotification(level);
8659 90 : }
8660 :
8661 5 : void Isolate::EnableMemorySavingsMode() {
8662 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
8663 : isolate->EnableMemorySavingsMode();
8664 5 : }
8665 :
8666 5 : void Isolate::DisableMemorySavingsMode() {
8667 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
8668 : isolate->DisableMemorySavingsMode();
8669 5 : }
8670 :
8671 0 : void Isolate::SetRAILMode(RAILMode rail_mode) {
8672 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
8673 0 : return isolate->SetRAILMode(rail_mode);
8674 : }
8675 :
8676 0 : void Isolate::IncreaseHeapLimitForDebugging() {
8677 : // No-op.
8678 0 : }
8679 :
8680 5 : void Isolate::RestoreOriginalHeapLimit() {
8681 : // No-op.
8682 5 : }
8683 :
8684 0 : bool Isolate::IsHeapLimitIncreasedForDebugging() { return false; }
8685 :
8686 60 : void Isolate::SetJitCodeEventHandler(JitCodeEventOptions options,
8687 : JitCodeEventHandler event_handler) {
8688 60 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
8689 : // Ensure that logging is initialized for our isolate.
8690 60 : isolate->InitializeLoggingAndCounters();
8691 60 : isolate->logger()->SetCodeEventHandler(options, event_handler);
8692 60 : }
8693 :
8694 :
8695 10 : void Isolate::SetStackLimit(uintptr_t stack_limit) {
8696 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
8697 10 : CHECK(stack_limit);
8698 10 : isolate->stack_guard()->SetStackLimit(stack_limit);
8699 10 : }
8700 :
8701 0 : void Isolate::GetCodeRange(void** start, size_t* length_in_bytes) {
8702 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
8703 140 : const base::AddressRegion& code_range =
8704 70 : isolate->heap()->memory_allocator()->code_range();
8705 70 : *start = reinterpret_cast<void*>(code_range.begin());
8706 70 : *length_in_bytes = code_range.size();
8707 0 : }
8708 :
8709 70 : UnwindState Isolate::GetUnwindState() {
8710 70 : UnwindState unwind_state;
8711 : void* code_range_start;
8712 : GetCodeRange(&code_range_start, &unwind_state.code_range.length_in_bytes);
8713 70 : unwind_state.code_range.start = code_range_start;
8714 :
8715 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
8716 : unwind_state.embedded_code_range.start =
8717 70 : reinterpret_cast<const void*>(isolate->embedded_blob());
8718 : unwind_state.embedded_code_range.length_in_bytes =
8719 70 : isolate->embedded_blob_size();
8720 :
8721 70 : i::Code js_entry = isolate->heap()->builtin(i::Builtins::kJSEntry);
8722 : unwind_state.js_entry_stub.code.start =
8723 70 : reinterpret_cast<const void*>(js_entry->InstructionStart());
8724 70 : unwind_state.js_entry_stub.code.length_in_bytes = js_entry->InstructionSize();
8725 :
8726 70 : return unwind_state;
8727 : }
8728 :
8729 : #define CALLBACK_SETTER(ExternalName, Type, InternalName) \
8730 : void Isolate::Set##ExternalName(Type callback) { \
8731 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this); \
8732 : isolate->set_##InternalName(callback); \
8733 : }
8734 :
8735 80 : CALLBACK_SETTER(FatalErrorHandler, FatalErrorCallback, exception_behavior)
8736 0 : CALLBACK_SETTER(OOMErrorHandler, OOMErrorCallback, oom_behavior)
8737 378 : CALLBACK_SETTER(AllowCodeGenerationFromStringsCallback,
8738 : AllowCodeGenerationFromStringsCallback, allow_code_gen_callback)
8739 306 : CALLBACK_SETTER(AllowWasmCodeGenerationCallback,
8740 : AllowWasmCodeGenerationCallback, allow_wasm_code_gen_callback)
8741 :
8742 108 : CALLBACK_SETTER(WasmModuleCallback, ExtensionCallback, wasm_module_callback)
8743 18 : CALLBACK_SETTER(WasmInstanceCallback, ExtensionCallback, wasm_instance_callback)
8744 :
8745 0 : CALLBACK_SETTER(WasmCompileStreamingCallback, ApiImplementationCallback,
8746 : wasm_compile_streaming_callback)
8747 :
8748 60 : CALLBACK_SETTER(WasmStreamingCallback, WasmStreamingCallback,
8749 : wasm_streaming_callback)
8750 :
8751 64 : CALLBACK_SETTER(WasmThreadsEnabledCallback, WasmThreadsEnabledCallback,
8752 : wasm_threads_enabled_callback)
8753 :
8754 3396 : void Isolate::AddNearHeapLimitCallback(v8::NearHeapLimitCallback callback,
8755 : void* data) {
8756 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
8757 3396 : isolate->heap()->AddNearHeapLimitCallback(callback, data);
8758 3396 : }
8759 :
8760 3396 : void Isolate::RemoveNearHeapLimitCallback(v8::NearHeapLimitCallback callback,
8761 : size_t heap_limit) {
8762 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
8763 3396 : isolate->heap()->RemoveNearHeapLimitCallback(callback, heap_limit);
8764 3396 : }
8765 :
8766 0 : void Isolate::AutomaticallyRestoreInitialHeapLimit(double threshold_percent) {
8767 : DCHECK_GT(threshold_percent, 0.0);
8768 : DCHECK_LT(threshold_percent, 1.0);
8769 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
8770 0 : isolate->heap()->AutomaticallyRestoreInitialHeapLimit(threshold_percent);
8771 0 : }
8772 :
8773 0 : bool Isolate::IsDead() {
8774 0 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
8775 0 : return isolate->IsDead();
8776 : }
8777 :
8778 1256 : bool Isolate::AddMessageListener(MessageCallback that, Local<Value> data) {
8779 1256 : return AddMessageListenerWithErrorLevel(that, kMessageError, data);
8780 : }
8781 :
8782 30087 : bool Isolate::AddMessageListenerWithErrorLevel(MessageCallback that,
8783 : int message_levels,
8784 : Local<Value> data) {
8785 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
8786 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(isolate);
8787 : i::HandleScope scope(isolate);
8788 : i::Handle<i::TemplateList> list = isolate->factory()->message_listeners();
8789 30087 : i::Handle<i::FixedArray> listener = isolate->factory()->NewFixedArray(3);
8790 : i::Handle<i::Foreign> foreign =
8791 30087 : isolate->factory()->NewForeign(FUNCTION_ADDR(that));
8792 60174 : listener->set(0, *foreign);
8793 : listener->set(1, data.IsEmpty() ? i::ReadOnlyRoots(isolate).undefined_value()
8794 90245 : : *Utils::OpenHandle(*data));
8795 : listener->set(2, i::Smi::FromInt(message_levels));
8796 30087 : list = i::TemplateList::Add(isolate, list, listener);
8797 30087 : isolate->heap()->SetMessageListeners(*list);
8798 30087 : return true;
8799 : }
8800 :
8801 :
8802 151 : void Isolate::RemoveMessageListeners(MessageCallback that) {
8803 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
8804 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(isolate);
8805 : i::HandleScope scope(isolate);
8806 : i::DisallowHeapAllocation no_gc;
8807 151 : i::TemplateList listeners = isolate->heap()->message_listeners();
8808 343 : for (int i = 0; i < listeners->length(); i++) {
8809 384 : if (listeners->get(i)->IsUndefined(isolate)) continue; // skip deleted ones
8810 151 : i::FixedArray listener = i::FixedArray::cast(listeners->get(i));
8811 : i::Foreign callback_obj = i::Foreign::cast(listener->get(0));
8812 151 : if (callback_obj->foreign_address() == FUNCTION_ADDR(that)) {
8813 151 : listeners->set(i, i::ReadOnlyRoots(isolate).undefined_value());
8814 : }
8815 : }
8816 151 : }
8817 :
8818 :
8819 26 : void Isolate::SetFailedAccessCheckCallbackFunction(
8820 : FailedAccessCheckCallback callback) {
8821 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
8822 26 : isolate->SetFailedAccessCheckCallback(callback);
8823 26 : }
8824 :
8825 :
8826 1337 : void Isolate::SetCaptureStackTraceForUncaughtExceptions(
8827 : bool capture, int frame_limit, StackTrace::StackTraceOptions options) {
8828 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
8829 : isolate->SetCaptureStackTraceForUncaughtExceptions(capture, frame_limit,
8830 1337 : options);
8831 1337 : }
8832 :
8833 :
8834 5 : void Isolate::VisitExternalResources(ExternalResourceVisitor* visitor) {
8835 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
8836 5 : isolate->heap()->VisitExternalResources(visitor);
8837 5 : }
8838 :
8839 :
8840 27011 : bool Isolate::IsInUse() {
8841 27011 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
8842 27011 : return isolate->IsInUse();
8843 : }
8844 :
8845 :
8846 10 : void Isolate::VisitHandlesWithClassIds(PersistentHandleVisitor* visitor) {
8847 10 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
8848 : i::DisallowHeapAllocation no_allocation;
8849 10 : isolate->global_handles()->IterateAllRootsWithClassIds(visitor);
8850 10 : }
8851 :
8852 :
8853 5 : void Isolate::VisitHandlesForPartialDependence(
8854 : PersistentHandleVisitor* visitor) {
8855 5 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
8856 : i::DisallowHeapAllocation no_allocation;
8857 5 : isolate->global_handles()->IterateAllRootsInNewSpaceWithClassIds(visitor);
8858 5 : }
8859 :
8860 :
8861 0 : void Isolate::VisitWeakHandles(PersistentHandleVisitor* visitor) {
8862 0 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
8863 : i::DisallowHeapAllocation no_allocation;
8864 0 : isolate->global_handles()->IterateWeakRootsInNewSpaceWithClassIds(visitor);
8865 0 : }
8866 :
8867 5 : void Isolate::SetAllowAtomicsWait(bool allow) {
8868 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
8869 : isolate->set_allow_atomics_wait(allow);
8870 5 : }
8871 :
8872 1230063 : MicrotasksScope::MicrotasksScope(Isolate* isolate, MicrotasksScope::Type type)
8873 : : isolate_(reinterpret_cast<i::Isolate*>(isolate)),
8874 1230063 : run_(type == MicrotasksScope::kRunMicrotasks) {
8875 1230063 : auto* microtask_queue = isolate_->default_microtask_queue();
8876 1230063 : if (run_) microtask_queue->IncrementMicrotasksScopeDepth();
8877 : #ifdef DEBUG
8878 : if (!run_) microtask_queue->IncrementDebugMicrotasksScopeDepth();
8879 : #endif
8880 1230063 : }
8881 :
8882 :
8883 1230063 : MicrotasksScope::~MicrotasksScope() {
8884 1391796 : auto handle_scope_implementer = isolate_->handle_scope_implementer();
8885 : auto* microtask_queue = isolate_->default_microtask_queue();
8886 1230063 : if (run_) {
8887 : microtask_queue->DecrementMicrotasksScopeDepth();
8888 161733 : if (MicrotasksPolicy::kScoped ==
8889 : handle_scope_implementer->microtasks_policy()) {
8890 93933 : PerformCheckpoint(reinterpret_cast<Isolate*>(isolate_));
8891 : }
8892 : }
8893 : #ifdef DEBUG
8894 : if (!run_) microtask_queue->DecrementDebugMicrotasksScopeDepth();
8895 : #endif
8896 1230062 : }
8897 :
8898 :
8899 169781 : void MicrotasksScope::PerformCheckpoint(Isolate* v8Isolate) {
8900 169781 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(v8Isolate);
8901 337752 : auto* microtask_queue = isolate->default_microtask_queue();
8902 337752 : if (!microtask_queue->GetMicrotasksScopeDepth() &&
8903 : !microtask_queue->HasMicrotasksSuppressions()) {
8904 167887 : microtask_queue->RunMicrotasks(isolate);
8905 : }
8906 169780 : }
8907 :
8908 :
8909 0 : int MicrotasksScope::GetCurrentDepth(Isolate* v8Isolate) {
8910 0 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(v8Isolate);
8911 0 : return isolate->default_microtask_queue()->GetMicrotasksScopeDepth();
8912 : }
8913 :
8914 100 : bool MicrotasksScope::IsRunningMicrotasks(Isolate* v8Isolate) {
8915 100 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(v8Isolate);
8916 100 : return isolate->default_microtask_queue()->IsRunningMicrotasks();
8917 : }
8918 :
8919 9062509 : String::Utf8Value::Utf8Value(v8::Isolate* isolate, v8::Local<v8::Value> obj)
8920 9062509 : : str_(nullptr), length_(0) {
8921 9062552 : if (obj.IsEmpty()) return;
8922 : i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
8923 : ENTER_V8_DO_NOT_USE(i_isolate);
8924 : i::HandleScope scope(i_isolate);
8925 9062474 : Local<Context> context = isolate->GetCurrentContext();
8926 18124940 : TryCatch try_catch(isolate);
8927 : Local<String> str;
8928 18124956 : if (!obj->ToString(context).ToLocal(&str)) return;
8929 9062466 : length_ = str->Utf8Length(isolate);
8930 9062466 : str_ = i::NewArray<char>(length_ + 1);
8931 9062466 : str->WriteUtf8(isolate, str_);
8932 : }
8933 :
8934 9062509 : String::Utf8Value::~Utf8Value() {
8935 9062509 : i::DeleteArray(str_);
8936 9062509 : }
8937 :
8938 6 : String::Value::Value(v8::Isolate* isolate, v8::Local<v8::Value> obj)
8939 6 : : str_(nullptr), length_(0) {
8940 6 : if (obj.IsEmpty()) return;
8941 : i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
8942 : ENTER_V8_DO_NOT_USE(i_isolate);
8943 : i::HandleScope scope(i_isolate);
8944 6 : Local<Context> context = isolate->GetCurrentContext();
8945 12 : TryCatch try_catch(isolate);
8946 : Local<String> str;
8947 12 : if (!obj->ToString(context).ToLocal(&str)) return;
8948 6 : length_ = str->Length();
8949 6 : str_ = i::NewArray<uint16_t>(length_ + 1);
8950 : str->Write(isolate, str_);
8951 : }
8952 :
8953 6 : String::Value::~Value() {
8954 6 : i::DeleteArray(str_);
8955 6 : }
8956 :
8957 : #define DEFINE_ERROR(NAME, name) \
8958 : Local<Value> Exception::NAME(v8::Local<v8::String> raw_message) { \
8959 : i::Isolate* isolate = i::Isolate::Current(); \
8960 : LOG_API(isolate, NAME, New); \
8961 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(isolate); \
8962 : i::Object error; \
8963 : { \
8964 : i::HandleScope scope(isolate); \
8965 : i::Handle<i::String> message = Utils::OpenHandle(*raw_message); \
8966 : i::Handle<i::JSFunction> constructor = isolate->name##_function(); \
8967 : error = *isolate->factory()->NewError(constructor, message); \
8968 : } \
8969 : i::Handle<i::Object> result(error, isolate); \
8970 : return Utils::ToLocal(result); \
8971 : }
8972 :
8973 120 : DEFINE_ERROR(RangeError, range_error)
8974 30 : DEFINE_ERROR(ReferenceError, reference_error)
8975 30 : DEFINE_ERROR(SyntaxError, syntax_error)
8976 30 : DEFINE_ERROR(TypeError, type_error)
8977 1220 : DEFINE_ERROR(Error, error)
8978 :
8979 : #undef DEFINE_ERROR
8980 :
8981 :
8982 328 : Local<Message> Exception::CreateMessage(Isolate* isolate,
8983 : Local<Value> exception) {
8984 328 : i::Handle<i::Object> obj = Utils::OpenHandle(*exception);
8985 : i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
8986 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(i_isolate);
8987 : i::HandleScope scope(i_isolate);
8988 : return Utils::MessageToLocal(
8989 656 : scope.CloseAndEscape(i_isolate->CreateMessage(obj, nullptr)));
8990 : }
8991 :
8992 :
8993 107 : Local<StackTrace> Exception::GetStackTrace(Local<Value> exception) {
8994 : i::Handle<i::Object> obj = Utils::OpenHandle(*exception);
8995 214 : if (!obj->IsJSObject()) return Local<StackTrace>();
8996 107 : i::Handle<i::JSObject> js_obj = i::Handle<i::JSObject>::cast(obj);
8997 : i::Isolate* isolate = js_obj->GetIsolate();
8998 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(isolate);
8999 107 : return Utils::StackTraceToLocal(isolate->GetDetailedStackTrace(js_obj));
9000 : }
9001 :
9002 :
9003 : // --- D e b u g S u p p o r t ---
9004 :
9005 3739 : void debug::SetContextId(Local<Context> context, int id) {
9006 7478 : Utils::OpenHandle(*context)->set_debug_context_id(i::Smi::FromInt(id));
9007 3739 : }
9008 :
9009 3863587 : int debug::GetContextId(Local<Context> context) {
9010 3863587 : i::Object value = Utils::OpenHandle(*context)->debug_context_id();
9011 3863587 : return (value->IsSmi()) ? i::Smi::ToInt(value) : 0;
9012 : }
9013 :
9014 7278 : void debug::SetInspector(Isolate* isolate,
9015 : v8_inspector::V8Inspector* inspector) {
9016 : i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
9017 : i_isolate->set_inspector(inspector);
9018 7278 : }
9019 :
9020 7156662 : v8_inspector::V8Inspector* debug::GetInspector(Isolate* isolate) {
9021 7156662 : return reinterpret_cast<i::Isolate*>(isolate)->inspector();
9022 : }
9023 :
9024 6615 : void debug::SetBreakOnNextFunctionCall(Isolate* isolate) {
9025 6615 : reinterpret_cast<i::Isolate*>(isolate)->debug()->SetBreakOnNextFunctionCall();
9026 6615 : }
9027 :
9028 30 : void debug::ClearBreakOnNextFunctionCall(Isolate* isolate) {
9029 : reinterpret_cast<i::Isolate*>(isolate)
9030 : ->debug()
9031 30 : ->ClearBreakOnNextFunctionCall();
9032 30 : }
9033 :
9034 79152 : MaybeLocal<Array> debug::GetInternalProperties(Isolate* v8_isolate,
9035 : Local<Value> value) {
9036 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(v8_isolate);
9037 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(isolate);
9038 79152 : i::Handle<i::Object> val = Utils::OpenHandle(*value);
9039 : i::Handle<i::JSArray> result;
9040 158304 : if (!i::Runtime::GetInternalProperties(isolate, val).ToHandle(&result))
9041 0 : return MaybeLocal<Array>();
9042 79152 : return Utils::ToLocal(result);
9043 : }
9044 :
9045 7472 : void debug::ChangeBreakOnException(Isolate* isolate, ExceptionBreakState type) {
9046 14944 : i::Isolate* internal_isolate = reinterpret_cast<i::Isolate*>(isolate);
9047 : internal_isolate->debug()->ChangeBreakOnException(
9048 14944 : i::BreakException, type == BreakOnAnyException);
9049 : internal_isolate->debug()->ChangeBreakOnException(i::BreakUncaughtException,
9050 14944 : type != NoBreakOnException);
9051 7472 : }
9052 :
9053 7034 : void debug::SetBreakPointsActive(Isolate* v8_isolate, bool is_active) {
9054 7034 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(v8_isolate);
9055 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(isolate);
9056 : isolate->debug()->set_break_points_active(is_active);
9057 7034 : }
9058 :
9059 44519 : void debug::PrepareStep(Isolate* v8_isolate, StepAction action) {
9060 89038 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(v8_isolate);
9061 : ENTER_V8_DO_NOT_USE(isolate);
9062 44519 : CHECK(isolate->debug()->CheckExecutionState());
9063 : // Clear all current stepping setup.
9064 44519 : isolate->debug()->ClearStepping();
9065 : // Prepare step.
9066 89038 : isolate->debug()->PrepareStep(static_cast<i::StepAction>(action));
9067 44519 : }
9068 :
9069 0 : void debug::ClearStepping(Isolate* v8_isolate) {
9070 0 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(v8_isolate);
9071 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(isolate);
9072 : // Clear all current stepping setup.
9073 0 : isolate->debug()->ClearStepping();
9074 0 : }
9075 :
9076 60221 : void debug::BreakRightNow(Isolate* v8_isolate) {
9077 60221 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(v8_isolate);
9078 : ENTER_V8_DO_NOT_USE(isolate);
9079 60221 : isolate->debug()->HandleDebugBreak(i::kIgnoreIfAllFramesBlackboxed);
9080 60221 : }
9081 :
9082 370 : bool debug::AllFramesOnStackAreBlackboxed(Isolate* v8_isolate) {
9083 370 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(v8_isolate);
9084 : ENTER_V8_DO_NOT_USE(isolate);
9085 740 : return isolate->debug()->AllFramesOnStackAreBlackboxed();
9086 : }
9087 :
9088 0 : v8::Isolate* debug::Script::GetIsolate() const {
9089 0 : return reinterpret_cast<v8::Isolate*>(Utils::OpenHandle(this)->GetIsolate());
9090 : }
9091 :
9092 0 : ScriptOriginOptions debug::Script::OriginOptions() const {
9093 0 : return Utils::OpenHandle(this)->origin_options();
9094 : }
9095 :
9096 33662 : bool debug::Script::WasCompiled() const {
9097 67324 : return Utils::OpenHandle(this)->compilation_state() ==
9098 67324 : i::Script::COMPILATION_STATE_COMPILED;
9099 : }
9100 :
9101 33559 : bool debug::Script::IsEmbedded() const {
9102 : i::Handle<i::Script> script = Utils::OpenHandle(this);
9103 67118 : return script->context_data() ==
9104 67118 : script->GetReadOnlyRoots().uninitialized_symbol();
9105 : }
9106 :
9107 731784 : int debug::Script::Id() const { return Utils::OpenHandle(this)->id(); }
9108 :
9109 60366 : int debug::Script::LineOffset() const {
9110 60366 : return Utils::OpenHandle(this)->line_offset();
9111 : }
9112 :
9113 60366 : int debug::Script::ColumnOffset() const {
9114 60366 : return Utils::OpenHandle(this)->column_offset();
9115 : }
9116 :
9117 60366 : std::vector<int> debug::Script::LineEnds() const {
9118 : i::Handle<i::Script> script = Utils::OpenHandle(this);
9119 120737 : if (script->type() == i::Script::TYPE_WASM &&
9120 60371 : this->SourceMappingURL().IsEmpty()) {
9121 : return std::vector<int>();
9122 : }
9123 : i::Isolate* isolate = script->GetIsolate();
9124 : i::HandleScope scope(isolate);
9125 60366 : i::Script::InitLineEnds(script);
9126 120732 : CHECK(script->line_ends()->IsFixedArray());
9127 : i::Handle<i::FixedArray> line_ends(i::FixedArray::cast(script->line_ends()),
9128 120732 : isolate);
9129 60366 : std::vector<int> result(line_ends->length());
9130 17327308 : for (int i = 0; i < line_ends->length(); ++i) {
9131 : i::Smi line_end = i::Smi::cast(line_ends->get(i));
9132 17206576 : result[i] = line_end->value();
9133 : }
9134 : return result;
9135 : }
9136 :
9137 58454 : MaybeLocal<String> debug::Script::Name() const {
9138 : i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
9139 : i::HandleScope handle_scope(isolate);
9140 : i::Handle<i::Script> script = Utils::OpenHandle(this);
9141 116908 : i::Handle<i::Object> value(script->name(), isolate);
9142 116908 : if (!value->IsString()) return MaybeLocal<String>();
9143 : return Utils::ToLocal(
9144 22900 : handle_scope.CloseAndEscape(i::Handle<i::String>::cast(value)));
9145 : }
9146 :
9147 120947 : MaybeLocal<String> debug::Script::SourceURL() const {
9148 : i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
9149 : i::HandleScope handle_scope(isolate);
9150 : i::Handle<i::Script> script = Utils::OpenHandle(this);
9151 241894 : i::Handle<i::Object> value(script->source_url(), isolate);
9152 241894 : if (!value->IsString()) return MaybeLocal<String>();
9153 : return Utils::ToLocal(
9154 4438 : handle_scope.CloseAndEscape(i::Handle<i::String>::cast(value)));
9155 : }
9156 :
9157 60569 : MaybeLocal<String> debug::Script::SourceMappingURL() const {
9158 : i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
9159 : i::HandleScope handle_scope(isolate);
9160 : i::Handle<i::Script> script = Utils::OpenHandle(this);
9161 121138 : i::Handle<i::Object> value(script->source_mapping_url(), isolate);
9162 121138 : if (!value->IsString()) return MaybeLocal<String>();
9163 : return Utils::ToLocal(
9164 600 : handle_scope.CloseAndEscape(i::Handle<i::String>::cast(value)));
9165 : }
9166 :
9167 132069 : Maybe<int> debug::Script::ContextId() const {
9168 : i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
9169 : i::HandleScope handle_scope(isolate);
9170 : i::Handle<i::Script> script = Utils::OpenHandle(this);
9171 132069 : i::Object value = script->context_data();
9172 132069 : if (value->IsSmi()) return Just(i::Smi::ToInt(value));
9173 : return Nothing<int>();
9174 : }
9175 :
9176 141703 : MaybeLocal<String> debug::Script::Source() const {
9177 : i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
9178 : i::HandleScope handle_scope(isolate);
9179 : i::Handle<i::Script> script = Utils::OpenHandle(this);
9180 283406 : i::Handle<i::Object> value(script->source(), isolate);
9181 283406 : if (!value->IsString()) return MaybeLocal<String>();
9182 : return Utils::ToLocal(
9183 141703 : handle_scope.CloseAndEscape(i::Handle<i::String>::cast(value)));
9184 : }
9185 :
9186 27003 : bool debug::Script::IsWasm() const {
9187 27003 : return Utils::OpenHandle(this)->type() == i::Script::TYPE_WASM;
9188 : }
9189 :
9190 60366 : bool debug::Script::IsModule() const {
9191 120732 : return Utils::OpenHandle(this)->origin_options().IsModule();
9192 : }
9193 :
9194 : namespace {
9195 18310 : int GetSmiValue(i::Handle<i::FixedArray> array, int index) {
9196 18310 : return i::Smi::ToInt(array->get(index));
9197 : }
9198 :
9199 42855 : bool CompareBreakLocation(const i::BreakLocation& loc1,
9200 42855 : const i::BreakLocation& loc2) {
9201 42855 : return loc1.position() < loc2.position();
9202 : }
9203 : } // namespace
9204 :
9205 260 : bool debug::Script::GetPossibleBreakpoints(
9206 : const debug::Location& start, const debug::Location& end,
9207 : bool restrict_to_function,
9208 : std::vector<debug::BreakLocation>* locations) const {
9209 260 : CHECK(!start.IsEmpty());
9210 : i::Handle<i::Script> script = Utils::OpenHandle(this);
9211 550 : if (script->type() == i::Script::TYPE_WASM &&
9212 290 : this->SourceMappingURL().IsEmpty()) {
9213 : i::WasmModuleObject module_object =
9214 60 : i::WasmModuleObject::cast(script->wasm_module_object());
9215 30 : return module_object->GetPossibleBreakpoints(start, end, locations);
9216 : }
9217 :
9218 230 : i::Script::InitLineEnds(script);
9219 460 : CHECK(script->line_ends()->IsFixedArray());
9220 205 : i::Isolate* isolate = script->GetIsolate();
9221 : i::Handle<i::FixedArray> line_ends =
9222 460 : i::Handle<i::FixedArray>::cast(i::handle(script->line_ends(), isolate));
9223 230 : CHECK(line_ends->length());
9224 :
9225 230 : int start_offset = GetSourceOffset(start);
9226 : int end_offset = end.IsEmpty()
9227 145 : ? GetSmiValue(line_ends, line_ends->length() - 1) + 1
9228 375 : : GetSourceOffset(end);
9229 230 : if (start_offset >= end_offset) return true;
9230 :
9231 : std::vector<i::BreakLocation> v8_locations;
9232 205 : if (!isolate->debug()->GetPossibleBreakpoints(
9233 : script, start_offset, end_offset, restrict_to_function,
9234 410 : &v8_locations)) {
9235 : return false;
9236 : }
9237 :
9238 200 : std::sort(v8_locations.begin(), v8_locations.end(), CompareBreakLocation);
9239 : int current_line_end_index = 0;
9240 5450 : for (const auto& v8_location : v8_locations) {
9241 : int offset = v8_location.position();
9242 13255 : while (offset > GetSmiValue(line_ends, current_line_end_index)) {
9243 3155 : ++current_line_end_index;
9244 3155 : CHECK(current_line_end_index < line_ends->length());
9245 : }
9246 : int line_offset = 0;
9247 :
9248 5050 : if (current_line_end_index > 0) {
9249 4630 : line_offset = GetSmiValue(line_ends, current_line_end_index - 1) + 1;
9250 : }
9251 : locations->emplace_back(
9252 5050 : current_line_end_index + script->line_offset(),
9253 10100 : offset - line_offset +
9254 : (current_line_end_index == 0 ? script->column_offset() : 0),
9255 15150 : v8_location.type());
9256 : }
9257 : return true;
9258 : }
9259 :
9260 2965 : int debug::Script::GetSourceOffset(const debug::Location& location) const {
9261 : i::Handle<i::Script> script = Utils::OpenHandle(this);
9262 2965 : if (script->type() == i::Script::TYPE_WASM) {
9263 200 : if (this->SourceMappingURL().IsEmpty()) {
9264 190 : return i::WasmModuleObject::cast(script->wasm_module_object())
9265 285 : ->GetFunctionOffset(location.GetLineNumber()) +
9266 95 : location.GetColumnNumber();
9267 : }
9268 : DCHECK_EQ(0, location.GetLineNumber());
9269 5 : return location.GetColumnNumber();
9270 : }
9271 :
9272 8595 : int line = std::max(location.GetLineNumber() - script->line_offset(), 0);
9273 2865 : int column = location.GetColumnNumber();
9274 2865 : if (line == 0) {
9275 770 : column = std::max(0, column - script->column_offset());
9276 : }
9277 :
9278 2865 : i::Script::InitLineEnds(script);
9279 5730 : CHECK(script->line_ends()->IsFixedArray());
9280 : i::Handle<i::FixedArray> line_ends = i::Handle<i::FixedArray>::cast(
9281 5730 : i::handle(script->line_ends(), script->GetIsolate()));
9282 2865 : CHECK(line_ends->length());
9283 2865 : if (line >= line_ends->length())
9284 15 : return GetSmiValue(line_ends, line_ends->length() - 1);
9285 2850 : int line_offset = GetSmiValue(line_ends, line);
9286 3235 : if (line == 0) return std::min(column, line_offset);
9287 2465 : int prev_line_offset = GetSmiValue(line_ends, line - 1);
9288 4930 : return std::min(prev_line_offset + column + 1, line_offset);
9289 : }
9290 :
9291 459430 : v8::debug::Location debug::Script::GetSourceLocation(int offset) const {
9292 459430 : i::Handle<i::Script> script = Utils::OpenHandle(this);
9293 : i::Script::PositionInfo info;
9294 459430 : i::Script::GetPositionInfo(script, offset, &info, i::Script::WITH_OFFSET);
9295 459430 : return debug::Location(info.line, info.column);
9296 : }
9297 :
9298 50 : bool debug::Script::SetScriptSource(v8::Local<v8::String> newSource,
9299 : bool preview,
9300 : debug::LiveEditResult* result) const {
9301 : i::Handle<i::Script> script = Utils::OpenHandle(this);
9302 50 : i::Isolate* isolate = script->GetIsolate();
9303 : return isolate->debug()->SetScriptSource(
9304 100 : script, Utils::OpenHandle(*newSource), preview, result);
9305 : }
9306 :
9307 2190 : bool debug::Script::SetBreakpoint(v8::Local<v8::String> condition,
9308 : debug::Location* location,
9309 : debug::BreakpointId* id) const {
9310 : i::Handle<i::Script> script = Utils::OpenHandle(this);
9311 2190 : i::Isolate* isolate = script->GetIsolate();
9312 2190 : int offset = GetSourceOffset(*location);
9313 2190 : if (!isolate->debug()->SetBreakPointForScript(
9314 2190 : script, Utils::OpenHandle(*condition), &offset, id)) {
9315 : return false;
9316 : }
9317 2160 : *location = GetSourceLocation(offset);
9318 2160 : return true;
9319 : }
9320 :
9321 2250 : void debug::RemoveBreakpoint(Isolate* v8_isolate, BreakpointId id) {
9322 2250 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(v8_isolate);
9323 : i::HandleScope handle_scope(isolate);
9324 2250 : isolate->debug()->RemoveBreakpoint(id);
9325 2250 : }
9326 :
9327 30 : v8::Platform* debug::GetCurrentPlatform() {
9328 30 : return i::V8::GetCurrentPlatform();
9329 : }
9330 :
9331 63 : debug::WasmScript* debug::WasmScript::Cast(debug::Script* script) {
9332 63 : CHECK(script->IsWasm());
9333 63 : return static_cast<WasmScript*>(script);
9334 : }
9335 :
9336 165 : int debug::WasmScript::NumFunctions() const {
9337 : i::DisallowHeapAllocation no_gc;
9338 : i::Handle<i::Script> script = Utils::OpenHandle(this);
9339 : DCHECK_EQ(i::Script::TYPE_WASM, script->type());
9340 : i::WasmModuleObject module_object =
9341 330 : i::WasmModuleObject::cast(script->wasm_module_object());
9342 : const i::wasm::WasmModule* module = module_object->module();
9343 : DCHECK_GE(i::kMaxInt, module->functions.size());
9344 330 : return static_cast<int>(module->functions.size());
9345 : }
9346 :
9347 165 : int debug::WasmScript::NumImportedFunctions() const {
9348 : i::DisallowHeapAllocation no_gc;
9349 : i::Handle<i::Script> script = Utils::OpenHandle(this);
9350 : DCHECK_EQ(i::Script::TYPE_WASM, script->type());
9351 : i::WasmModuleObject module_object =
9352 330 : i::WasmModuleObject::cast(script->wasm_module_object());
9353 : const i::wasm::WasmModule* module = module_object->module();
9354 : DCHECK_GE(i::kMaxInt, module->num_imported_functions);
9355 165 : return static_cast<int>(module->num_imported_functions);
9356 : }
9357 :
9358 5 : std::pair<int, int> debug::WasmScript::GetFunctionRange(
9359 : int function_index) const {
9360 : i::DisallowHeapAllocation no_gc;
9361 : i::Handle<i::Script> script = Utils::OpenHandle(this);
9362 : DCHECK_EQ(i::Script::TYPE_WASM, script->type());
9363 : i::WasmModuleObject module_object =
9364 10 : i::WasmModuleObject::cast(script->wasm_module_object());
9365 : const i::wasm::WasmModule* module = module_object->module();
9366 : DCHECK_LE(0, function_index);
9367 : DCHECK_GT(module->functions.size(), function_index);
9368 5 : const i::wasm::WasmFunction& func = module->functions[function_index];
9369 : DCHECK_GE(i::kMaxInt, func.code.offset());
9370 : DCHECK_GE(i::kMaxInt, func.code.end_offset());
9371 : return std::make_pair(static_cast<int>(func.code.offset()),
9372 5 : static_cast<int>(func.code.end_offset()));
9373 : }
9374 :
9375 102 : uint32_t debug::WasmScript::GetFunctionHash(int function_index) {
9376 : i::DisallowHeapAllocation no_gc;
9377 : i::Handle<i::Script> script = Utils::OpenHandle(this);
9378 : DCHECK_EQ(i::Script::TYPE_WASM, script->type());
9379 : i::WasmModuleObject module_object =
9380 204 : i::WasmModuleObject::cast(script->wasm_module_object());
9381 : const i::wasm::WasmModule* module = module_object->module();
9382 : DCHECK_LE(0, function_index);
9383 : DCHECK_GT(module->functions.size(), function_index);
9384 102 : const i::wasm::WasmFunction& func = module->functions[function_index];
9385 : i::wasm::ModuleWireBytes wire_bytes(
9386 102 : module_object->native_module()->wire_bytes());
9387 : i::Vector<const i::byte> function_bytes = wire_bytes.GetFunctionBytes(&func);
9388 : // TODO(herhut): Maybe also take module, name and signature into account.
9389 : return i::StringHasher::HashSequentialString(function_bytes.start(),
9390 102 : function_bytes.length(), 0);
9391 : }
9392 :
9393 92 : debug::WasmDisassembly debug::WasmScript::DisassembleFunction(
9394 : int function_index) const {
9395 : i::DisallowHeapAllocation no_gc;
9396 : i::Handle<i::Script> script = Utils::OpenHandle(this);
9397 : DCHECK_EQ(i::Script::TYPE_WASM, script->type());
9398 : i::WasmModuleObject module_object =
9399 184 : i::WasmModuleObject::cast(script->wasm_module_object());
9400 92 : return module_object->DisassembleFunction(function_index);
9401 : }
9402 :
9403 28569 : debug::Location::Location(int line_number, int column_number)
9404 : : line_number_(line_number),
9405 : column_number_(column_number),
9406 33619 : is_empty_(false) {}
9407 :
9408 270 : debug::Location::Location()
9409 : : line_number_(v8::Function::kLineOffsetNotFound),
9410 : column_number_(v8::Function::kLineOffsetNotFound),
9411 270 : is_empty_(true) {}
9412 :
9413 475015 : int debug::Location::GetLineNumber() const {
9414 : DCHECK(!IsEmpty());
9415 477975 : return line_number_;
9416 : }
9417 :
9418 471160 : int debug::Location::GetColumnNumber() const {
9419 : DCHECK(!IsEmpty());
9420 474125 : return column_number_;
9421 : }
9422 :
9423 1010 : bool debug::Location::IsEmpty() const { return is_empty_; }
9424 :
9425 3471 : void debug::GetLoadedScripts(v8::Isolate* v8_isolate,
9426 : PersistentValueVector<debug::Script>& scripts) {
9427 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(v8_isolate);
9428 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(isolate);
9429 : {
9430 : i::DisallowHeapAllocation no_gc;
9431 3471 : i::Script::Iterator iterator(isolate);
9432 95316 : for (i::Script script = iterator.Next(); !script.is_null();
9433 : script = iterator.Next()) {
9434 44187 : if (!script->IsUserJavaScript()) continue;
9435 33667 : if (script->HasValidSource()) {
9436 : i::HandleScope handle_scope(isolate);
9437 : i::Handle<i::Script> script_handle(script, isolate);
9438 33667 : scripts.Append(ToApiHandle<Script>(script_handle));
9439 : }
9440 : }
9441 : }
9442 3471 : }
9443 :
9444 266 : MaybeLocal<UnboundScript> debug::CompileInspectorScript(Isolate* v8_isolate,
9445 : Local<String> source) {
9446 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(v8_isolate);
9447 798 : PREPARE_FOR_DEBUG_INTERFACE_EXECUTION_WITH_ISOLATE(isolate, UnboundScript);
9448 266 : i::Handle<i::String> str = Utils::OpenHandle(*source);
9449 : i::Handle<i::SharedFunctionInfo> result;
9450 : {
9451 : ScriptOriginOptions origin_options;
9452 : i::ScriptData* script_data = nullptr;
9453 : i::MaybeHandle<i::SharedFunctionInfo> maybe_function_info =
9454 : i::Compiler::GetSharedFunctionInfoForScript(
9455 : isolate, str, i::Compiler::ScriptDetails(), origin_options, nullptr,
9456 : script_data, ScriptCompiler::kNoCompileOptions,
9457 : ScriptCompiler::kNoCacheBecauseInspector,
9458 : i::FLAG_expose_inspector_scripts ? i::NOT_NATIVES_CODE
9459 532 : : i::INSPECTOR_CODE);
9460 : has_pending_exception = !maybe_function_info.ToHandle(&result);
9461 266 : RETURN_ON_FAILED_EXECUTION(UnboundScript);
9462 : }
9463 266 : RETURN_ESCAPED(ToApiHandle<UnboundScript>(result));
9464 : }
9465 :
9466 7847 : void debug::SetDebugDelegate(Isolate* v8_isolate,
9467 : debug::DebugDelegate* delegate) {
9468 7847 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(v8_isolate);
9469 7847 : isolate->debug()->SetDebugDelegate(delegate);
9470 7847 : }
9471 :
9472 460 : void debug::SetAsyncEventDelegate(Isolate* v8_isolate,
9473 : debug::AsyncEventDelegate* delegate) {
9474 : reinterpret_cast<i::Isolate*>(v8_isolate)->set_async_event_delegate(delegate);
9475 460 : }
9476 :
9477 120527 : void debug::ResetBlackboxedStateCache(Isolate* v8_isolate,
9478 : v8::Local<debug::Script> script) {
9479 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(v8_isolate);
9480 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(isolate);
9481 : i::DisallowHeapAllocation no_gc;
9482 : i::SharedFunctionInfo::ScriptIterator iter(isolate,
9483 120527 : *Utils::OpenHandle(*script));
9484 2494804 : for (i::SharedFunctionInfo info = iter.Next(); !info.is_null();
9485 : info = iter.Next()) {
9486 1126875 : if (info->HasDebugInfo()) {
9487 18363 : info->GetDebugInfo()->set_computed_debug_is_blackboxed(false);
9488 : }
9489 : }
9490 120527 : }
9491 :
9492 7020 : int debug::EstimatedValueSize(Isolate* v8_isolate, v8::Local<v8::Value> value) {
9493 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(v8_isolate);
9494 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(isolate);
9495 : i::Handle<i::Object> object = Utils::OpenHandle(*value);
9496 14040 : if (object->IsSmi()) return i::kTaggedSize;
9497 12560 : CHECK(object->IsHeapObject());
9498 12560 : return i::Handle<i::HeapObject>::cast(object)->Size();
9499 : }
9500 :
9501 90067 : v8::MaybeLocal<v8::Array> v8::Object::PreviewEntries(bool* is_key_value) {
9502 90067 : if (IsMap()) {
9503 108 : *is_key_value = true;
9504 108 : return Map::Cast(this)->AsArray();
9505 : }
9506 89959 : if (IsSet()) {
9507 89 : *is_key_value = false;
9508 89 : return Set::Cast(this)->AsArray();
9509 : }
9510 :
9511 : i::Handle<i::JSReceiver> object = Utils::OpenHandle(this);
9512 : i::Isolate* isolate = object->GetIsolate();
9513 : Isolate* v8_isolate = reinterpret_cast<Isolate*>(isolate);
9514 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(isolate);
9515 179740 : if (object->IsJSWeakCollection()) {
9516 196 : *is_key_value = object->IsJSWeakMap();
9517 : return Utils::ToLocal(i::JSWeakCollection::GetEntries(
9518 98 : i::Handle<i::JSWeakCollection>::cast(object), 0));
9519 : }
9520 179544 : if (object->IsJSMapIterator()) {
9521 : i::Handle<i::JSMapIterator> iterator =
9522 137 : i::Handle<i::JSMapIterator>::cast(object);
9523 : MapAsArrayKind const kind =
9524 137 : static_cast<MapAsArrayKind>(iterator->map()->instance_type());
9525 137 : *is_key_value = kind == MapAsArrayKind::kEntries;
9526 137 : if (!iterator->HasMore()) return v8::Array::New(v8_isolate);
9527 214 : return Utils::ToLocal(MapAsArray(isolate, iterator->table(),
9528 321 : i::Smi::ToInt(iterator->index()), kind));
9529 : }
9530 179270 : if (object->IsJSSetIterator()) {
9531 112 : i::Handle<i::JSSetIterator> it = i::Handle<i::JSSetIterator>::cast(object);
9532 112 : *is_key_value = false;
9533 112 : if (!it->HasMore()) return v8::Array::New(v8_isolate);
9534 : return Utils::ToLocal(
9535 204 : SetAsArray(isolate, it->table(), i::Smi::ToInt(it->index())));
9536 : }
9537 89523 : return v8::MaybeLocal<v8::Array>();
9538 : }
9539 :
9540 0 : Local<Function> debug::GetBuiltin(Isolate* v8_isolate, Builtin builtin) {
9541 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(v8_isolate);
9542 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(isolate);
9543 : i::HandleScope handle_scope(isolate);
9544 : i::Builtins::Name builtin_id;
9545 0 : switch (builtin) {
9546 : case kStringToLowerCase:
9547 : builtin_id = i::Builtins::kStringPrototypeToLocaleLowerCase;
9548 : break;
9549 : default:
9550 0 : UNREACHABLE();
9551 : }
9552 :
9553 0 : i::Handle<i::String> name = isolate->factory()->empty_string();
9554 : i::NewFunctionArgs args = i::NewFunctionArgs::ForBuiltinWithoutPrototype(
9555 0 : name, builtin_id, i::LanguageMode::kStrict);
9556 0 : i::Handle<i::JSFunction> fun = isolate->factory()->NewFunction(args);
9557 :
9558 0 : fun->shared()->set_internal_formal_parameter_count(0);
9559 0 : fun->shared()->set_length(0);
9560 0 : return Utils::ToLocal(handle_scope.CloseAndEscape(fun));
9561 : }
9562 :
9563 36725 : void debug::SetConsoleDelegate(Isolate* v8_isolate, ConsoleDelegate* delegate) {
9564 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(v8_isolate);
9565 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(isolate);
9566 : isolate->set_console_delegate(delegate);
9567 36725 : }
9568 :
9569 425 : debug::ConsoleCallArguments::ConsoleCallArguments(
9570 : const v8::FunctionCallbackInfo<v8::Value>& info)
9571 425 : : v8::FunctionCallbackInfo<v8::Value>(nullptr, info.values_, info.length_) {
9572 425 : }
9573 :
9574 12779 : debug::ConsoleCallArguments::ConsoleCallArguments(
9575 : internal::BuiltinArguments& args)
9576 : : v8::FunctionCallbackInfo<v8::Value>(
9577 : nullptr,
9578 : // Drop the first argument (receiver, i.e. the "console" object).
9579 : args.address_of_arg_at(args.length() > 1 ? 1 : 0),
9580 25558 : args.length() - 1) {}
9581 :
9582 6025 : int debug::GetStackFrameId(v8::Local<v8::StackFrame> frame) {
9583 6025 : return Utils::OpenHandle(*frame)->id();
9584 : }
9585 :
9586 15 : v8::Local<v8::StackTrace> debug::GetDetailedStackTrace(
9587 : Isolate* v8_isolate, v8::Local<v8::Object> v8_error) {
9588 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(v8_isolate);
9589 : i::Handle<i::JSReceiver> error = Utils::OpenHandle(*v8_error);
9590 30 : if (!error->IsJSObject()) {
9591 0 : return v8::Local<v8::StackTrace>();
9592 : }
9593 : i::Handle<i::FixedArray> stack_trace =
9594 15 : isolate->GetDetailedStackTrace(i::Handle<i::JSObject>::cast(error));
9595 : return Utils::StackTraceToLocal(stack_trace);
9596 : }
9597 :
9598 40 : MaybeLocal<debug::Script> debug::GeneratorObject::Script() {
9599 : i::Handle<i::JSGeneratorObject> obj = Utils::OpenHandle(this);
9600 40 : i::Object maybe_script = obj->function()->shared()->script();
9601 40 : if (!maybe_script->IsScript()) return MaybeLocal<debug::Script>();
9602 : i::Handle<i::Script> script(i::Script::cast(maybe_script), obj->GetIsolate());
9603 40 : return ToApiHandle<debug::Script>(script);
9604 : }
9605 :
9606 10 : Local<Function> debug::GeneratorObject::Function() {
9607 : i::Handle<i::JSGeneratorObject> obj = Utils::OpenHandle(this);
9608 20 : return Utils::ToLocal(handle(obj->function(), obj->GetIsolate()));
9609 : }
9610 :
9611 40 : debug::Location debug::GeneratorObject::SuspendedLocation() {
9612 : i::Handle<i::JSGeneratorObject> obj = Utils::OpenHandle(this);
9613 40 : CHECK(obj->is_suspended());
9614 40 : i::Object maybe_script = obj->function()->shared()->script();
9615 40 : if (!maybe_script->IsScript()) return debug::Location();
9616 : i::Handle<i::Script> script(i::Script::cast(maybe_script), obj->GetIsolate());
9617 : i::Script::PositionInfo info;
9618 : i::Script::GetPositionInfo(script, obj->source_position(), &info,
9619 40 : i::Script::WITH_OFFSET);
9620 40 : return debug::Location(info.line, info.column);
9621 : }
9622 :
9623 100 : bool debug::GeneratorObject::IsSuspended() {
9624 100 : return Utils::OpenHandle(this)->is_suspended();
9625 : }
9626 :
9627 100 : v8::Local<debug::GeneratorObject> debug::GeneratorObject::Cast(
9628 : v8::Local<v8::Value> value) {
9629 100 : CHECK(value->IsGeneratorObject());
9630 100 : return ToApiHandle<debug::GeneratorObject>(Utils::OpenHandle(*value));
9631 : }
9632 :
9633 8878 : MaybeLocal<v8::Value> debug::EvaluateGlobal(v8::Isolate* isolate,
9634 : v8::Local<v8::String> source,
9635 : bool throw_on_side_effect) {
9636 : i::Isolate* internal_isolate = reinterpret_cast<i::Isolate*>(isolate);
9637 26634 : PREPARE_FOR_DEBUG_INTERFACE_EXECUTION_WITH_ISOLATE(internal_isolate, Value);
9638 : Local<Value> result;
9639 : has_pending_exception = !ToLocal<Value>(
9640 : i::DebugEvaluate::Global(internal_isolate, Utils::OpenHandle(*source),
9641 : throw_on_side_effect),
9642 17756 : &result);
9643 8878 : RETURN_ON_FAILED_EXECUTION(Value);
9644 8333 : RETURN_ESCAPED(result);
9645 : }
9646 :
9647 85 : void debug::QueryObjects(v8::Local<v8::Context> v8_context,
9648 : QueryObjectPredicate* predicate,
9649 : PersistentValueVector<v8::Object>* objects) {
9650 170 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(v8_context->GetIsolate());
9651 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(isolate);
9652 : isolate->heap_profiler()->QueryObjects(Utils::OpenHandle(*v8_context),
9653 85 : predicate, objects);
9654 85 : }
9655 :
9656 35 : void debug::GlobalLexicalScopeNames(
9657 : v8::Local<v8::Context> v8_context,
9658 : v8::PersistentValueVector<v8::String>* names) {
9659 : i::Handle<i::Context> context = Utils::OpenHandle(*v8_context);
9660 : i::Isolate* isolate = context->GetIsolate();
9661 : i::Handle<i::ScriptContextTable> table(
9662 70 : context->global_object()->native_context()->script_context_table(),
9663 70 : isolate);
9664 340 : for (int i = 0; i < table->used(); i++) {
9665 : i::Handle<i::Context> context =
9666 135 : i::ScriptContextTable::GetContext(isolate, table, i);
9667 : DCHECK(context->IsScriptContext());
9668 270 : i::Handle<i::ScopeInfo> scope_info(context->scope_info(), isolate);
9669 135 : int local_count = scope_info->ContextLocalCount();
9670 285 : for (int j = 0; j < local_count; ++j) {
9671 150 : i::String name = scope_info->ContextLocalName(j);
9672 185 : if (i::ScopeInfo::VariableIsSynthetic(name)) continue;
9673 115 : names->Append(Utils::ToLocal(handle(name, isolate)));
9674 : }
9675 : }
9676 35 : }
9677 :
9678 10 : void debug::SetReturnValue(v8::Isolate* v8_isolate,
9679 : v8::Local<v8::Value> value) {
9680 10 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(v8_isolate);
9681 : isolate->debug()->set_return_value(*Utils::OpenHandle(*value));
9682 10 : }
9683 :
9684 10193 : int64_t debug::GetNextRandomInt64(v8::Isolate* v8_isolate) {
9685 : return reinterpret_cast<i::Isolate*>(v8_isolate)
9686 : ->random_number_generator()
9687 10193 : ->NextInt64();
9688 : }
9689 :
9690 170 : int debug::GetDebuggingId(v8::Local<v8::Function> function) {
9691 : i::Handle<i::JSReceiver> callable = v8::Utils::OpenHandle(*function);
9692 340 : if (!callable->IsJSFunction()) return i::DebugInfo::kNoDebuggingId;
9693 170 : i::Handle<i::JSFunction> func = i::Handle<i::JSFunction>::cast(callable);
9694 170 : int id = func->GetIsolate()->debug()->GetFunctionDebuggingId(func);
9695 : DCHECK_NE(i::DebugInfo::kNoDebuggingId, id);
9696 170 : return id;
9697 : }
9698 :
9699 95 : bool debug::SetFunctionBreakpoint(v8::Local<v8::Function> function,
9700 : v8::Local<v8::String> condition,
9701 : BreakpointId* id) {
9702 : i::Handle<i::JSReceiver> callable = Utils::OpenHandle(*function);
9703 190 : if (!callable->IsJSFunction()) return false;
9704 : i::Handle<i::JSFunction> jsfunction =
9705 95 : i::Handle<i::JSFunction>::cast(callable);
9706 95 : i::Isolate* isolate = jsfunction->GetIsolate();
9707 : i::Handle<i::String> condition_string =
9708 : condition.IsEmpty() ? isolate->factory()->empty_string()
9709 140 : : Utils::OpenHandle(*condition);
9710 : return isolate->debug()->SetBreakpointForFunction(jsfunction,
9711 95 : condition_string, id);
9712 : }
9713 :
9714 0 : debug::PostponeInterruptsScope::PostponeInterruptsScope(v8::Isolate* isolate)
9715 : : scope_(
9716 : new i::PostponeInterruptsScope(reinterpret_cast<i::Isolate*>(isolate),
9717 0 : i::StackGuard::API_INTERRUPT)) {}
9718 :
9719 : debug::PostponeInterruptsScope::~PostponeInterruptsScope() = default;
9720 :
9721 361 : Local<String> CpuProfileNode::GetFunctionName() const {
9722 722 : const i::ProfileNode* node = reinterpret_cast<const i::ProfileNode*>(this);
9723 : i::Isolate* isolate = node->isolate();
9724 361 : const i::CodeEntry* entry = node->entry();
9725 : i::Handle<i::String> name =
9726 361 : isolate->factory()->InternalizeUtf8String(entry->name());
9727 361 : return ToApiHandle<String>(name);
9728 : }
9729 :
9730 40 : int debug::Coverage::BlockData::StartOffset() const { return block_->start; }
9731 40 : int debug::Coverage::BlockData::EndOffset() const { return block_->end; }
9732 40 : uint32_t debug::Coverage::BlockData::Count() const { return block_->count; }
9733 :
9734 515 : int debug::Coverage::FunctionData::StartOffset() const {
9735 515 : return function_->start;
9736 : }
9737 515 : int debug::Coverage::FunctionData::EndOffset() const { return function_->end; }
9738 505 : uint32_t debug::Coverage::FunctionData::Count() const {
9739 505 : return function_->count;
9740 : }
9741 :
9742 475 : MaybeLocal<String> debug::Coverage::FunctionData::Name() const {
9743 475 : return ToApiHandle<String>(function_->name);
9744 : }
9745 :
9746 515 : size_t debug::Coverage::FunctionData::BlockCount() const {
9747 1030 : return function_->blocks.size();
9748 : }
9749 :
9750 475 : bool debug::Coverage::FunctionData::HasBlockCoverage() const {
9751 475 : return function_->has_block_coverage;
9752 : }
9753 :
9754 40 : debug::Coverage::BlockData debug::Coverage::FunctionData::GetBlockData(
9755 : size_t i) const {
9756 80 : return BlockData(&function_->blocks.at(i), coverage_);
9757 : }
9758 :
9759 210 : Local<debug::Script> debug::Coverage::ScriptData::GetScript() const {
9760 210 : return ToApiHandle<debug::Script>(script_->script);
9761 : }
9762 :
9763 690 : size_t debug::Coverage::ScriptData::FunctionCount() const {
9764 1380 : return script_->functions.size();
9765 : }
9766 :
9767 500 : debug::Coverage::FunctionData debug::Coverage::ScriptData::GetFunctionData(
9768 : size_t i) const {
9769 1000 : return FunctionData(&script_->functions.at(i), coverage_);
9770 : }
9771 :
9772 215 : debug::Coverage::ScriptData::ScriptData(size_t index,
9773 : std::shared_ptr<i::Coverage> coverage)
9774 645 : : script_(&coverage->at(index)), coverage_(std::move(coverage)) {}
9775 :
9776 770 : size_t debug::Coverage::ScriptCount() const { return coverage_->size(); }
9777 :
9778 215 : debug::Coverage::ScriptData debug::Coverage::GetScriptData(size_t i) const {
9779 430 : return ScriptData(i, coverage_);
9780 : }
9781 :
9782 125 : debug::Coverage debug::Coverage::CollectPrecise(Isolate* isolate) {
9783 : return Coverage(
9784 375 : i::Coverage::CollectPrecise(reinterpret_cast<i::Isolate*>(isolate)));
9785 : }
9786 :
9787 60 : debug::Coverage debug::Coverage::CollectBestEffort(Isolate* isolate) {
9788 : return Coverage(
9789 180 : i::Coverage::CollectBestEffort(reinterpret_cast<i::Isolate*>(isolate)));
9790 : }
9791 :
9792 370 : void debug::Coverage::SelectMode(Isolate* isolate, debug::Coverage::Mode mode) {
9793 370 : i::Coverage::SelectMode(reinterpret_cast<i::Isolate*>(isolate), mode);
9794 370 : }
9795 :
9796 185 : int debug::TypeProfile::Entry::SourcePosition() const {
9797 185 : return entry_->position;
9798 : }
9799 :
9800 185 : std::vector<MaybeLocal<String>> debug::TypeProfile::Entry::Types() const {
9801 : std::vector<MaybeLocal<String>> result;
9802 615 : for (const internal::Handle<internal::String>& type : entry_->types) {
9803 245 : result.emplace_back(ToApiHandle<String>(type));
9804 : }
9805 185 : return result;
9806 : }
9807 :
9808 40 : debug::TypeProfile::ScriptData::ScriptData(
9809 : size_t index, std::shared_ptr<i::TypeProfile> type_profile)
9810 40 : : script_(&type_profile->at(index)),
9811 80 : type_profile_(std::move(type_profile)) {}
9812 :
9813 40 : Local<debug::Script> debug::TypeProfile::ScriptData::GetScript() const {
9814 40 : return ToApiHandle<debug::Script>(script_->script);
9815 : }
9816 :
9817 40 : std::vector<debug::TypeProfile::Entry> debug::TypeProfile::ScriptData::Entries()
9818 : const {
9819 : std::vector<debug::TypeProfile::Entry> result;
9820 305 : for (const internal::TypeProfileEntry& entry : script_->entries) {
9821 185 : result.push_back(debug::TypeProfile::Entry(&entry, type_profile_));
9822 : }
9823 40 : return result;
9824 : }
9825 :
9826 50 : debug::TypeProfile debug::TypeProfile::Collect(Isolate* isolate) {
9827 : return TypeProfile(
9828 150 : i::TypeProfile::Collect(reinterpret_cast<i::Isolate*>(isolate)));
9829 : }
9830 :
9831 125 : void debug::TypeProfile::SelectMode(Isolate* isolate,
9832 : debug::TypeProfile::Mode mode) {
9833 125 : i::TypeProfile::SelectMode(reinterpret_cast<i::Isolate*>(isolate), mode);
9834 125 : }
9835 :
9836 180 : size_t debug::TypeProfile::ScriptCount() const { return type_profile_->size(); }
9837 :
9838 40 : debug::TypeProfile::ScriptData debug::TypeProfile::GetScriptData(
9839 : size_t i) const {
9840 80 : return ScriptData(i, type_profile_);
9841 : }
9842 :
9843 2412 : v8::MaybeLocal<v8::Value> debug::WeakMap::Get(v8::Local<v8::Context> context,
9844 : v8::Local<v8::Value> key) {
9845 9648 : PREPARE_FOR_EXECUTION(context, WeakMap, Get, Value);
9846 : auto self = Utils::OpenHandle(this);
9847 : Local<Value> result;
9848 2412 : i::Handle<i::Object> argv[] = {Utils::OpenHandle(*key)};
9849 : has_pending_exception =
9850 : !ToLocal<Value>(i::Execution::Call(isolate, isolate->weakmap_get(), self,
9851 : arraysize(argv), argv),
9852 7236 : &result);
9853 2412 : RETURN_ON_FAILED_EXECUTION(Value);
9854 2412 : RETURN_ESCAPED(result);
9855 : }
9856 :
9857 10827 : v8::MaybeLocal<debug::WeakMap> debug::WeakMap::Set(
9858 : v8::Local<v8::Context> context, v8::Local<v8::Value> key,
9859 : v8::Local<v8::Value> value) {
9860 43308 : PREPARE_FOR_EXECUTION(context, WeakMap, Set, WeakMap);
9861 : auto self = Utils::OpenHandle(this);
9862 : i::Handle<i::Object> result;
9863 : i::Handle<i::Object> argv[] = {Utils::OpenHandle(*key),
9864 21654 : Utils::OpenHandle(*value)};
9865 : has_pending_exception = !i::Execution::Call(isolate, isolate->weakmap_set(),
9866 21654 : self, arraysize(argv), argv)
9867 21654 : .ToHandle(&result);
9868 10827 : RETURN_ON_FAILED_EXECUTION(WeakMap);
9869 10827 : RETURN_ESCAPED(Local<WeakMap>::Cast(Utils::ToLocal(result)));
9870 : }
9871 :
9872 64 : Local<debug::WeakMap> debug::WeakMap::New(v8::Isolate* isolate) {
9873 64 : i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
9874 128 : LOG_API(i_isolate, WeakMap, New);
9875 : ENTER_V8_NO_SCRIPT_NO_EXCEPTION(i_isolate);
9876 64 : i::Handle<i::JSWeakMap> obj = i_isolate->factory()->NewJSWeakMap();
9877 64 : return ToApiHandle<debug::WeakMap>(obj);
9878 : }
9879 :
9880 : debug::WeakMap* debug::WeakMap::Cast(v8::Value* value) {
9881 : return static_cast<debug::WeakMap*>(value);
9882 : }
9883 :
9884 163 : const char* CpuProfileNode::GetFunctionNameStr() const {
9885 163 : const i::ProfileNode* node = reinterpret_cast<const i::ProfileNode*>(this);
9886 163 : return node->entry()->name();
9887 : }
9888 :
9889 142 : int CpuProfileNode::GetScriptId() const {
9890 142 : const i::ProfileNode* node = reinterpret_cast<const i::ProfileNode*>(this);
9891 142 : const i::CodeEntry* entry = node->entry();
9892 142 : return entry->script_id();
9893 : }
9894 :
9895 132 : Local<String> CpuProfileNode::GetScriptResourceName() const {
9896 264 : const i::ProfileNode* node = reinterpret_cast<const i::ProfileNode*>(this);
9897 : i::Isolate* isolate = node->isolate();
9898 : return ToApiHandle<String>(isolate->factory()->InternalizeUtf8String(
9899 132 : node->entry()->resource_name()));
9900 : }
9901 :
9902 25 : const char* CpuProfileNode::GetScriptResourceNameStr() const {
9903 25 : const i::ProfileNode* node = reinterpret_cast<const i::ProfileNode*>(this);
9904 25 : return node->entry()->resource_name();
9905 : }
9906 :
9907 262 : int CpuProfileNode::GetLineNumber() const {
9908 262 : return reinterpret_cast<const i::ProfileNode*>(this)->line_number();
9909 : }
9910 :
9911 :
9912 132 : int CpuProfileNode::GetColumnNumber() const {
9913 : return reinterpret_cast<const i::ProfileNode*>(this)->
9914 132 : entry()->column_number();
9915 : }
9916 :
9917 :
9918 107 : unsigned int CpuProfileNode::GetHitLineCount() const {
9919 : const i::ProfileNode* node = reinterpret_cast<const i::ProfileNode*>(this);
9920 107 : return node->GetHitLineCount();
9921 : }
9922 :
9923 :
9924 2 : bool CpuProfileNode::GetLineTicks(LineTick* entries,
9925 : unsigned int length) const {
9926 : const i::ProfileNode* node = reinterpret_cast<const i::ProfileNode*>(this);
9927 2 : return node->GetLineTicks(entries, length);
9928 : }
9929 :
9930 :
9931 112 : const char* CpuProfileNode::GetBailoutReason() const {
9932 112 : const i::ProfileNode* node = reinterpret_cast<const i::ProfileNode*>(this);
9933 112 : return node->entry()->bailout_reason();
9934 : }
9935 :
9936 :
9937 117 : unsigned CpuProfileNode::GetHitCount() const {
9938 117 : return reinterpret_cast<const i::ProfileNode*>(this)->self_ticks();
9939 : }
9940 :
9941 :
9942 0 : unsigned CpuProfileNode::GetCallUid() const {
9943 0 : return reinterpret_cast<const i::ProfileNode*>(this)->function_id();
9944 : }
9945 :
9946 :
9947 213 : unsigned CpuProfileNode::GetNodeId() const {
9948 213 : return reinterpret_cast<const i::ProfileNode*>(this)->id();
9949 : }
9950 :
9951 :
9952 506 : int CpuProfileNode::GetChildrenCount() const {
9953 : return static_cast<int>(
9954 1012 : reinterpret_cast<const i::ProfileNode*>(this)->children()->size());
9955 : }
9956 :
9957 :
9958 491 : const CpuProfileNode* CpuProfileNode::GetChild(int index) const {
9959 : const i::ProfileNode* child =
9960 982 : reinterpret_cast<const i::ProfileNode*>(this)->children()->at(index);
9961 491 : return reinterpret_cast<const CpuProfileNode*>(child);
9962 : }
9963 :
9964 :
9965 0 : const std::vector<CpuProfileDeoptInfo>& CpuProfileNode::GetDeoptInfos() const {
9966 : const i::ProfileNode* node = reinterpret_cast<const i::ProfileNode*>(this);
9967 0 : return node->deopt_infos();
9968 : }
9969 :
9970 :
9971 630 : void CpuProfile::Delete() {
9972 630 : i::CpuProfile* profile = reinterpret_cast<i::CpuProfile*>(this);
9973 : i::CpuProfiler* profiler = profile->cpu_profiler();
9974 : DCHECK_NOT_NULL(profiler);
9975 630 : profiler->DeleteProfile(profile);
9976 630 : }
9977 :
9978 :
9979 0 : Local<String> CpuProfile::GetTitle() const {
9980 0 : const i::CpuProfile* profile = reinterpret_cast<const i::CpuProfile*>(this);
9981 0 : i::Isolate* isolate = profile->top_down()->isolate();
9982 : return ToApiHandle<String>(isolate->factory()->InternalizeUtf8String(
9983 0 : profile->title()));
9984 : }
9985 :
9986 :
9987 116 : const CpuProfileNode* CpuProfile::GetTopDownRoot() const {
9988 : const i::CpuProfile* profile = reinterpret_cast<const i::CpuProfile*>(this);
9989 116 : return reinterpret_cast<const CpuProfileNode*>(profile->top_down()->root());
9990 : }
9991 :
9992 :
9993 44 : const CpuProfileNode* CpuProfile::GetSample(int index) const {
9994 : const i::CpuProfile* profile = reinterpret_cast<const i::CpuProfile*>(this);
9995 44 : return reinterpret_cast<const CpuProfileNode*>(profile->sample(index));
9996 : }
9997 :
9998 :
9999 44 : int64_t CpuProfile::GetSampleTimestamp(int index) const {
10000 : const i::CpuProfile* profile = reinterpret_cast<const i::CpuProfile*>(this);
10001 132 : return (profile->sample_timestamp(index) - base::TimeTicks())
10002 88 : .InMicroseconds();
10003 : }
10004 :
10005 :
10006 95 : int64_t CpuProfile::GetStartTime() const {
10007 : const i::CpuProfile* profile = reinterpret_cast<const i::CpuProfile*>(this);
10008 95 : return (profile->start_time() - base::TimeTicks()).InMicroseconds();
10009 : }
10010 :
10011 :
10012 50 : int64_t CpuProfile::GetEndTime() const {
10013 : const i::CpuProfile* profile = reinterpret_cast<const i::CpuProfile*>(this);
10014 50 : return (profile->end_time() - base::TimeTicks()).InMicroseconds();
10015 : }
10016 :
10017 :
10018 90 : int CpuProfile::GetSamplesCount() const {
10019 90 : return reinterpret_cast<const i::CpuProfile*>(this)->samples_count();
10020 : }
10021 :
10022 198 : CpuProfiler* CpuProfiler::New(Isolate* isolate) {
10023 : return reinterpret_cast<CpuProfiler*>(
10024 198 : new i::CpuProfiler(reinterpret_cast<i::Isolate*>(isolate)));
10025 : }
10026 :
10027 198 : void CpuProfiler::Dispose() { delete reinterpret_cast<i::CpuProfiler*>(this); }
10028 :
10029 : // static
10030 5 : void CpuProfiler::CollectSample(Isolate* isolate) {
10031 5 : i::CpuProfiler::CollectSample(reinterpret_cast<i::Isolate*>(isolate));
10032 5 : }
10033 :
10034 530 : void CpuProfiler::SetSamplingInterval(int us) {
10035 : DCHECK_GE(us, 0);
10036 : return reinterpret_cast<i::CpuProfiler*>(this)->set_sampling_interval(
10037 530 : base::TimeDelta::FromMicroseconds(us));
10038 : }
10039 :
10040 0 : void CpuProfiler::CollectSample() {
10041 0 : reinterpret_cast<i::CpuProfiler*>(this)->CollectSample();
10042 0 : }
10043 :
10044 208 : void CpuProfiler::StartProfiling(Local<String> title, bool record_samples) {
10045 : reinterpret_cast<i::CpuProfiler*>(this)->StartProfiling(
10046 416 : *Utils::OpenHandle(*title), record_samples, kLeafNodeLineNumbers);
10047 208 : }
10048 :
10049 535 : void CpuProfiler::StartProfiling(Local<String> title, CpuProfilingMode mode,
10050 : bool record_samples) {
10051 : reinterpret_cast<i::CpuProfiler*>(this)->StartProfiling(
10052 1070 : *Utils::OpenHandle(*title), record_samples, mode);
10053 535 : }
10054 :
10055 663 : CpuProfile* CpuProfiler::StopProfiling(Local<String> title) {
10056 : return reinterpret_cast<CpuProfile*>(
10057 : reinterpret_cast<i::CpuProfiler*>(this)->StopProfiling(
10058 663 : *Utils::OpenHandle(*title)));
10059 : }
10060 :
10061 :
10062 0 : void CpuProfiler::SetIdle(bool is_idle) {
10063 0 : i::CpuProfiler* profiler = reinterpret_cast<i::CpuProfiler*>(this);
10064 : i::Isolate* isolate = profiler->isolate();
10065 0 : isolate->SetIdle(is_idle);
10066 0 : }
10067 :
10068 5 : void CpuProfiler::UseDetailedSourcePositionsForProfiling(Isolate* isolate) {
10069 : reinterpret_cast<i::Isolate*>(isolate)
10070 : ->set_detailed_source_positions_for_profiling(true);
10071 5 : }
10072 :
10073 0 : uintptr_t CodeEvent::GetCodeStartAddress() {
10074 0 : return reinterpret_cast<i::CodeEvent*>(this)->code_start_address;
10075 : }
10076 :
10077 0 : size_t CodeEvent::GetCodeSize() {
10078 0 : return reinterpret_cast<i::CodeEvent*>(this)->code_size;
10079 : }
10080 :
10081 120 : Local<String> CodeEvent::GetFunctionName() {
10082 : return ToApiHandle<String>(
10083 120 : reinterpret_cast<i::CodeEvent*>(this)->function_name);
10084 : }
10085 :
10086 0 : Local<String> CodeEvent::GetScriptName() {
10087 : return ToApiHandle<String>(
10088 0 : reinterpret_cast<i::CodeEvent*>(this)->script_name);
10089 : }
10090 :
10091 0 : int CodeEvent::GetScriptLine() {
10092 0 : return reinterpret_cast<i::CodeEvent*>(this)->script_line;
10093 : }
10094 :
10095 0 : int CodeEvent::GetScriptColumn() {
10096 0 : return reinterpret_cast<i::CodeEvent*>(this)->script_column;
10097 : }
10098 :
10099 20826 : CodeEventType CodeEvent::GetCodeType() {
10100 20826 : return reinterpret_cast<i::CodeEvent*>(this)->code_type;
10101 : }
10102 :
10103 20826 : const char* CodeEvent::GetComment() {
10104 20826 : return reinterpret_cast<i::CodeEvent*>(this)->comment;
10105 : }
10106 :
10107 20826 : const char* CodeEvent::GetCodeEventTypeName(CodeEventType code_event_type) {
10108 20826 : switch (code_event_type) {
10109 : case kUnknownType:
10110 : return "Unknown";
10111 : #define V(Name) \
10112 : case k##Name##Type: \
10113 : return #Name;
10114 20700 : CODE_EVENTS_LIST(V)
10115 : #undef V
10116 : }
10117 : // The execution should never pass here
10118 0 : UNREACHABLE();
10119 : // NOTE(mmarchini): Workaround to fix a compiler failure on GCC 4.9
10120 : return "Unknown";
10121 : }
10122 :
10123 20 : CodeEventHandler::CodeEventHandler(Isolate* isolate) {
10124 : internal_listener_ =
10125 20 : new i::ExternalCodeEventListener(reinterpret_cast<i::Isolate*>(isolate));
10126 20 : }
10127 :
10128 20 : CodeEventHandler::~CodeEventHandler() {
10129 20 : delete reinterpret_cast<i::ExternalCodeEventListener*>(internal_listener_);
10130 20 : }
10131 :
10132 20 : void CodeEventHandler::Enable() {
10133 : reinterpret_cast<i::ExternalCodeEventListener*>(internal_listener_)
10134 20 : ->StartListening(this);
10135 20 : }
10136 :
10137 0 : void CodeEventHandler::Disable() {
10138 : reinterpret_cast<i::ExternalCodeEventListener*>(internal_listener_)
10139 0 : ->StopListening();
10140 0 : }
10141 :
10142 : static i::HeapGraphEdge* ToInternal(const HeapGraphEdge* edge) {
10143 : return const_cast<i::HeapGraphEdge*>(
10144 : reinterpret_cast<const i::HeapGraphEdge*>(edge));
10145 : }
10146 :
10147 :
10148 6056935 : HeapGraphEdge::Type HeapGraphEdge::GetType() const {
10149 12113870 : return static_cast<HeapGraphEdge::Type>(ToInternal(this)->type());
10150 : }
10151 :
10152 :
10153 6007900 : Local<Value> HeapGraphEdge::GetName() const {
10154 18023700 : i::HeapGraphEdge* edge = ToInternal(this);
10155 : i::Isolate* isolate = edge->isolate();
10156 6007900 : switch (edge->type()) {
10157 : case i::HeapGraphEdge::kContextVariable:
10158 : case i::HeapGraphEdge::kInternal:
10159 : case i::HeapGraphEdge::kProperty:
10160 : case i::HeapGraphEdge::kShortcut:
10161 : case i::HeapGraphEdge::kWeak:
10162 : return ToApiHandle<String>(
10163 6007651 : isolate->factory()->InternalizeUtf8String(edge->name()));
10164 : case i::HeapGraphEdge::kElement:
10165 : case i::HeapGraphEdge::kHidden:
10166 : return ToApiHandle<Number>(
10167 249 : isolate->factory()->NewNumberFromInt(edge->index()));
10168 0 : default: UNREACHABLE();
10169 : }
10170 : return v8::Undefined(reinterpret_cast<v8::Isolate*>(isolate));
10171 : }
10172 :
10173 :
10174 340 : const HeapGraphNode* HeapGraphEdge::GetFromNode() const {
10175 : const i::HeapEntry* from = ToInternal(this)->from();
10176 340 : return reinterpret_cast<const HeapGraphNode*>(from);
10177 : }
10178 :
10179 :
10180 654423 : const HeapGraphNode* HeapGraphEdge::GetToNode() const {
10181 654423 : const i::HeapEntry* to = ToInternal(this)->to();
10182 654423 : return reinterpret_cast<const HeapGraphNode*>(to);
10183 : }
10184 :
10185 :
10186 : static i::HeapEntry* ToInternal(const HeapGraphNode* entry) {
10187 : return const_cast<i::HeapEntry*>(
10188 : reinterpret_cast<const i::HeapEntry*>(entry));
10189 : }
10190 :
10191 :
10192 370 : HeapGraphNode::Type HeapGraphNode::GetType() const {
10193 370 : return static_cast<HeapGraphNode::Type>(ToInternal(this)->type());
10194 : }
10195 :
10196 :
10197 1115 : Local<String> HeapGraphNode::GetName() const {
10198 1115 : i::Isolate* isolate = ToInternal(this)->isolate();
10199 : return ToApiHandle<String>(
10200 1115 : isolate->factory()->InternalizeUtf8String(ToInternal(this)->name()));
10201 : }
10202 :
10203 :
10204 549855 : SnapshotObjectId HeapGraphNode::GetId() const {
10205 549855 : return ToInternal(this)->id();
10206 : }
10207 :
10208 :
10209 20 : size_t HeapGraphNode::GetShallowSize() const {
10210 20 : return ToInternal(this)->self_size();
10211 : }
10212 :
10213 :
10214 60434 : int HeapGraphNode::GetChildrenCount() const {
10215 60434 : return ToInternal(this)->children_count();
10216 : }
10217 :
10218 :
10219 6341405 : const HeapGraphEdge* HeapGraphNode::GetChild(int index) const {
10220 6341405 : return reinterpret_cast<const HeapGraphEdge*>(ToInternal(this)->child(index));
10221 : }
10222 :
10223 :
10224 : static i::HeapSnapshot* ToInternal(const HeapSnapshot* snapshot) {
10225 : return const_cast<i::HeapSnapshot*>(
10226 : reinterpret_cast<const i::HeapSnapshot*>(snapshot));
10227 : }
10228 :
10229 :
10230 40 : void HeapSnapshot::Delete() {
10231 75 : i::Isolate* isolate = ToInternal(this)->profiler()->isolate();
10232 40 : if (isolate->heap_profiler()->GetSnapshotsCount() > 1) {
10233 5 : ToInternal(this)->Delete();
10234 : } else {
10235 : // If this is the last snapshot, clean up all accessory data as well.
10236 35 : isolate->heap_profiler()->DeleteAllSnapshots();
10237 : }
10238 40 : }
10239 :
10240 :
10241 299 : const HeapGraphNode* HeapSnapshot::GetRoot() const {
10242 299 : return reinterpret_cast<const HeapGraphNode*>(ToInternal(this)->root());
10243 : }
10244 :
10245 :
10246 181975 : const HeapGraphNode* HeapSnapshot::GetNodeById(SnapshotObjectId id) const {
10247 : return reinterpret_cast<const HeapGraphNode*>(
10248 181975 : ToInternal(this)->GetEntryById(id));
10249 : }
10250 :
10251 :
10252 10 : int HeapSnapshot::GetNodesCount() const {
10253 10 : return static_cast<int>(ToInternal(this)->entries().size());
10254 : }
10255 :
10256 :
10257 66757 : const HeapGraphNode* HeapSnapshot::GetNode(int index) const {
10258 : return reinterpret_cast<const HeapGraphNode*>(
10259 66757 : &ToInternal(this)->entries().at(index));
10260 : }
10261 :
10262 :
10263 20 : SnapshotObjectId HeapSnapshot::GetMaxSnapshotJSObjectId() const {
10264 20 : return ToInternal(this)->max_snapshot_js_object_id();
10265 : }
10266 :
10267 :
10268 35 : void HeapSnapshot::Serialize(OutputStream* stream,
10269 : HeapSnapshot::SerializationFormat format) const {
10270 : Utils::ApiCheck(format == kJSON,
10271 : "v8::HeapSnapshot::Serialize",
10272 : "Unknown serialization format");
10273 35 : Utils::ApiCheck(stream->GetChunkSize() > 0,
10274 : "v8::HeapSnapshot::Serialize",
10275 : "Invalid stream chunk size");
10276 : i::HeapSnapshotJSONSerializer serializer(ToInternal(this));
10277 35 : serializer.Serialize(stream);
10278 35 : }
10279 :
10280 :
10281 : // static
10282 : STATIC_CONST_MEMBER_DEFINITION const SnapshotObjectId
10283 : HeapProfiler::kUnknownObjectId;
10284 :
10285 :
10286 115 : int HeapProfiler::GetSnapshotCount() {
10287 115 : return reinterpret_cast<i::HeapProfiler*>(this)->GetSnapshotsCount();
10288 : }
10289 :
10290 :
10291 30 : const HeapSnapshot* HeapProfiler::GetHeapSnapshot(int index) {
10292 : return reinterpret_cast<const HeapSnapshot*>(
10293 30 : reinterpret_cast<i::HeapProfiler*>(this)->GetSnapshot(index));
10294 : }
10295 :
10296 :
10297 155 : SnapshotObjectId HeapProfiler::GetObjectId(Local<Value> value) {
10298 155 : i::Handle<i::Object> obj = Utils::OpenHandle(*value);
10299 155 : return reinterpret_cast<i::HeapProfiler*>(this)->GetSnapshotObjectId(obj);
10300 : }
10301 :
10302 :
10303 140 : Local<Value> HeapProfiler::FindObjectById(SnapshotObjectId id) {
10304 : i::Handle<i::Object> obj =
10305 140 : reinterpret_cast<i::HeapProfiler*>(this)->FindHeapObjectById(id);
10306 140 : if (obj.is_null()) return Local<Value>();
10307 : return Utils::ToLocal(obj);
10308 : }
10309 :
10310 :
10311 3839 : void HeapProfiler::ClearObjectIds() {
10312 3839 : reinterpret_cast<i::HeapProfiler*>(this)->ClearHeapObjectMap();
10313 3839 : }
10314 :
10315 :
10316 399 : const HeapSnapshot* HeapProfiler::TakeHeapSnapshot(
10317 : ActivityControl* control, ObjectNameResolver* resolver) {
10318 : return reinterpret_cast<const HeapSnapshot*>(
10319 : reinterpret_cast<i::HeapProfiler*>(this)
10320 399 : ->TakeSnapshot(control, resolver));
10321 : }
10322 :
10323 :
10324 45 : void HeapProfiler::StartTrackingHeapObjects(bool track_allocations) {
10325 : reinterpret_cast<i::HeapProfiler*>(this)->StartHeapObjectsTracking(
10326 45 : track_allocations);
10327 45 : }
10328 :
10329 :
10330 3879 : void HeapProfiler::StopTrackingHeapObjects() {
10331 3879 : reinterpret_cast<i::HeapProfiler*>(this)->StopHeapObjectsTracking();
10332 3879 : }
10333 :
10334 :
10335 55 : SnapshotObjectId HeapProfiler::GetHeapStats(OutputStream* stream,
10336 : int64_t* timestamp_us) {
10337 : i::HeapProfiler* heap_profiler = reinterpret_cast<i::HeapProfiler*>(this);
10338 55 : return heap_profiler->PushHeapObjectsStats(stream, timestamp_us);
10339 : }
10340 :
10341 59 : bool HeapProfiler::StartSamplingHeapProfiler(uint64_t sample_interval,
10342 : int stack_depth,
10343 : SamplingFlags flags) {
10344 : return reinterpret_cast<i::HeapProfiler*>(this)->StartSamplingHeapProfiler(
10345 59 : sample_interval, stack_depth, flags);
10346 : }
10347 :
10348 :
10349 59 : void HeapProfiler::StopSamplingHeapProfiler() {
10350 59 : reinterpret_cast<i::HeapProfiler*>(this)->StopSamplingHeapProfiler();
10351 59 : }
10352 :
10353 :
10354 77 : AllocationProfile* HeapProfiler::GetAllocationProfile() {
10355 77 : return reinterpret_cast<i::HeapProfiler*>(this)->GetAllocationProfile();
10356 : }
10357 :
10358 35 : void HeapProfiler::DeleteAllHeapSnapshots() {
10359 35 : reinterpret_cast<i::HeapProfiler*>(this)->DeleteAllSnapshots();
10360 35 : }
10361 :
10362 :
10363 0 : void HeapProfiler::SetWrapperClassInfoProvider(uint16_t class_id,
10364 : WrapperInfoCallback callback) {
10365 : reinterpret_cast<i::HeapProfiler*>(this)->DefineWrapperClass(class_id,
10366 0 : callback);
10367 0 : }
10368 :
10369 0 : void HeapProfiler::SetGetRetainerInfosCallback(
10370 : GetRetainerInfosCallback callback) {
10371 : reinterpret_cast<i::HeapProfiler*>(this)->SetGetRetainerInfosCallback(
10372 0 : callback);
10373 0 : }
10374 :
10375 0 : void HeapProfiler::SetBuildEmbedderGraphCallback(
10376 : LegacyBuildEmbedderGraphCallback callback) {
10377 : reinterpret_cast<i::HeapProfiler*>(this)->AddBuildEmbedderGraphCallback(
10378 0 : [](v8::Isolate* isolate, v8::EmbedderGraph* graph, void* data) {
10379 : reinterpret_cast<LegacyBuildEmbedderGraphCallback>(data)(isolate,
10380 0 : graph);
10381 0 : },
10382 0 : reinterpret_cast<void*>(callback));
10383 0 : }
10384 :
10385 35 : void HeapProfiler::AddBuildEmbedderGraphCallback(
10386 : BuildEmbedderGraphCallback callback, void* data) {
10387 : reinterpret_cast<i::HeapProfiler*>(this)->AddBuildEmbedderGraphCallback(
10388 35 : callback, data);
10389 35 : }
10390 :
10391 5 : void HeapProfiler::RemoveBuildEmbedderGraphCallback(
10392 : BuildEmbedderGraphCallback callback, void* data) {
10393 : reinterpret_cast<i::HeapProfiler*>(this)->RemoveBuildEmbedderGraphCallback(
10394 5 : callback, data);
10395 5 : }
10396 :
10397 : v8::Testing::StressType internal::Testing::stress_type_ =
10398 : v8::Testing::kStressTypeOpt;
10399 :
10400 :
10401 5265 : void Testing::SetStressRunType(Testing::StressType type) {
10402 : internal::Testing::set_stress_type(type);
10403 5265 : }
10404 :
10405 :
10406 5265 : int Testing::GetStressRuns() {
10407 52642 : if (internal::FLAG_stress_runs != 0) return internal::FLAG_stress_runs;
10408 : #ifdef DEBUG
10409 : // In debug mode the code runs much slower so stressing will only make two
10410 : // runs.
10411 : return 2;
10412 : #else
10413 : return 5;
10414 : #endif
10415 : }
10416 :
10417 :
10418 21057 : static void SetFlagsFromString(const char* flags) {
10419 : V8::SetFlagsFromString(flags, i::StrLength(flags));
10420 21057 : }
10421 :
10422 :
10423 26321 : void Testing::PrepareStressRun(int run) {
10424 : static const char* kLazyOptimizations =
10425 : "--prepare-always-opt "
10426 : "--max-inlined-bytecode-size=999999 "
10427 : "--max-inlined-bytecode-size-cumulative=999999 "
10428 : "--noalways-opt";
10429 : static const char* kForcedOptimizations = "--always-opt";
10430 :
10431 : // If deoptimization stressed turn on frequent deoptimization. If no value
10432 : // is spefified through --deopt-every-n-times use a default default value.
10433 : static const char* kDeoptEvery13Times = "--deopt-every-n-times=13";
10434 26321 : if (internal::Testing::stress_type() == Testing::kStressTypeDeopt &&
10435 0 : internal::FLAG_deopt_every_n_times == 0) {
10436 0 : SetFlagsFromString(kDeoptEvery13Times);
10437 : }
10438 :
10439 : #ifdef DEBUG
10440 : // As stressing in debug mode only make two runs skip the deopt stressing
10441 : // here.
10442 : if (run == GetStressRuns() - 1) {
10443 : SetFlagsFromString(kForcedOptimizations);
10444 : } else {
10445 : SetFlagsFromString(kLazyOptimizations);
10446 : }
10447 : #else
10448 26321 : if (run == GetStressRuns() - 1) {
10449 5265 : SetFlagsFromString(kForcedOptimizations);
10450 21056 : } else if (run != GetStressRuns() - 2) {
10451 15792 : SetFlagsFromString(kLazyOptimizations);
10452 : }
10453 : #endif
10454 26321 : }
10455 :
10456 :
10457 5265 : void Testing::DeoptimizeAll(Isolate* isolate) {
10458 : i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
10459 : i::HandleScope scope(i_isolate);
10460 5265 : i::Deoptimizer::DeoptimizeAll(i_isolate);
10461 5265 : }
10462 :
10463 10 : void EmbedderHeapTracer::FinalizeTracing() {
10464 10 : if (isolate_) {
10465 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(isolate_);
10466 20 : if (isolate->heap()->incremental_marking()->IsMarking()) {
10467 : isolate->heap()->FinalizeIncrementalMarkingAtomically(
10468 5 : i::GarbageCollectionReason::kExternalFinalize);
10469 : }
10470 : }
10471 10 : }
10472 :
10473 5 : void EmbedderHeapTracer::GarbageCollectionForTesting(
10474 : EmbedderStackState stack_state) {
10475 5 : CHECK(isolate_);
10476 5 : CHECK(i::FLAG_expose_gc);
10477 5 : i::Heap* const heap = reinterpret_cast<i::Isolate*>(isolate_)->heap();
10478 5 : heap->SetEmbedderStackStateForNextFinalizaton(stack_state);
10479 : heap->PreciseCollectAllGarbage(i::Heap::kNoGCFlags,
10480 : i::GarbageCollectionReason::kTesting,
10481 5 : kGCCallbackFlagForced);
10482 5 : }
10483 :
10484 : namespace internal {
10485 :
10486 : const size_t HandleScopeImplementer::kEnteredContextsOffset =
10487 : offsetof(HandleScopeImplementer, entered_contexts_);
10488 : const size_t HandleScopeImplementer::kIsMicrotaskContextOffset =
10489 : offsetof(HandleScopeImplementer, is_microtask_context_);
10490 :
10491 5918 : void HandleScopeImplementer::FreeThreadResources() {
10492 5918 : Free();
10493 5918 : }
10494 :
10495 :
10496 24256 : char* HandleScopeImplementer::ArchiveThread(char* storage) {
10497 24256 : HandleScopeData* current = isolate_->handle_scope_data();
10498 24256 : handle_scope_data_ = *current;
10499 : MemCopy(storage, this, sizeof(*this));
10500 :
10501 : ResetAfterArchive();
10502 : current->Initialize();
10503 :
10504 24256 : return storage + ArchiveSpacePerThread();
10505 : }
10506 :
10507 :
10508 53752 : int HandleScopeImplementer::ArchiveSpacePerThread() {
10509 53752 : return sizeof(HandleScopeImplementer);
10510 : }
10511 :
10512 :
10513 24256 : char* HandleScopeImplementer::RestoreThread(char* storage) {
10514 : MemCopy(this, storage, sizeof(*this));
10515 24256 : *isolate_->handle_scope_data() = handle_scope_data_;
10516 24256 : return storage + ArchiveSpacePerThread();
10517 : }
10518 :
10519 620849 : void HandleScopeImplementer::IterateThis(RootVisitor* v) {
10520 : #ifdef DEBUG
10521 : bool found_block_before_deferred = false;
10522 : #endif
10523 : // Iterate over all handles in the blocks except for the last.
10524 751854 : for (int i = static_cast<int>(blocks()->size()) - 2; i >= 0; --i) {
10525 262010 : Address* block = blocks()->at(i);
10526 : // Cast possibly-unrelated pointers to plain Address before comparing them
10527 : // to avoid undefined behavior.
10528 131022 : if (last_handle_before_deferred_block_ != nullptr &&
10529 34 : (reinterpret_cast<Address>(last_handle_before_deferred_block_) <=
10530 34 : reinterpret_cast<Address>(&block[kHandleBlockSize])) &&
10531 17 : (reinterpret_cast<Address>(last_handle_before_deferred_block_) >=
10532 : reinterpret_cast<Address>(block))) {
10533 : v->VisitRootPointers(Root::kHandleScope, nullptr, FullObjectSlot(block),
10534 34 : FullObjectSlot(last_handle_before_deferred_block_));
10535 17 : DCHECK(!found_block_before_deferred);
10536 : #ifdef DEBUG
10537 : found_block_before_deferred = true;
10538 : #endif
10539 : } else {
10540 : v->VisitRootPointers(Root::kHandleScope, nullptr, FullObjectSlot(block),
10541 261976 : FullObjectSlot(&block[kHandleBlockSize]));
10542 : }
10543 : }
10544 :
10545 : DCHECK(last_handle_before_deferred_block_ == nullptr ||
10546 : found_block_before_deferred);
10547 :
10548 : // Iterate over live handles in the last block (if any).
10549 620849 : if (!blocks()->empty()) {
10550 : v->VisitRootPointers(Root::kHandleScope, nullptr,
10551 : FullObjectSlot(blocks()->back()),
10552 1275669 : FullObjectSlot(handle_scope_data_.next));
10553 : }
10554 :
10555 : DetachableVector<Context>* context_lists[2] = {&saved_contexts_,
10556 620849 : &entered_contexts_};
10557 1862547 : for (unsigned i = 0; i < arraysize(context_lists); i++) {
10558 1241698 : context_lists[i]->shrink_to_fit();
10559 1241698 : if (context_lists[i]->empty()) continue;
10560 : FullObjectSlot start(&context_lists[i]->front());
10561 : v->VisitRootPointers(Root::kHandleScope, nullptr, start,
10562 1800388 : start + static_cast<int>(context_lists[i]->size()));
10563 : }
10564 620849 : }
10565 :
10566 614505 : void HandleScopeImplementer::Iterate(RootVisitor* v) {
10567 614505 : HandleScopeData* current = isolate_->handle_scope_data();
10568 614505 : handle_scope_data_ = *current;
10569 614505 : IterateThis(v);
10570 614505 : }
10571 :
10572 6344 : char* HandleScopeImplementer::Iterate(RootVisitor* v, char* storage) {
10573 : HandleScopeImplementer* scope_implementer =
10574 : reinterpret_cast<HandleScopeImplementer*>(storage);
10575 6344 : scope_implementer->IterateThis(v);
10576 6344 : return storage + ArchiveSpacePerThread();
10577 : }
10578 :
10579 12327 : DeferredHandles* HandleScopeImplementer::Detach(Address* prev_limit) {
10580 : DeferredHandles* deferred =
10581 12327 : new DeferredHandles(isolate()->handle_scope_data()->next, isolate());
10582 :
10583 36981 : while (!blocks_.empty()) {
10584 24654 : Address* block_start = blocks_.back();
10585 24654 : Address* block_limit = &block_start[kHandleBlockSize];
10586 : // We should not need to check for SealHandleScope here. Assert this.
10587 : DCHECK(prev_limit == block_limit ||
10588 : !(block_start <= prev_limit && prev_limit <= block_limit));
10589 24654 : if (prev_limit == block_limit) break;
10590 12327 : deferred->blocks_.push_back(blocks_.back());
10591 24654 : blocks_.pop_back();
10592 : }
10593 :
10594 : // deferred->blocks_ now contains the blocks installed on the
10595 : // HandleScope stack since BeginDeferredScope was called, but in
10596 : // reverse order.
10597 :
10598 : DCHECK(prev_limit == nullptr || !blocks_.empty());
10599 :
10600 : DCHECK(!blocks_.empty() && prev_limit != nullptr);
10601 : DCHECK_NOT_NULL(last_handle_before_deferred_block_);
10602 12327 : last_handle_before_deferred_block_ = nullptr;
10603 12327 : return deferred;
10604 : }
10605 :
10606 :
10607 12327 : void HandleScopeImplementer::BeginDeferredScope() {
10608 : DCHECK_NULL(last_handle_before_deferred_block_);
10609 12327 : last_handle_before_deferred_block_ = isolate()->handle_scope_data()->next;
10610 12327 : }
10611 :
10612 :
10613 12327 : DeferredHandles::~DeferredHandles() {
10614 24654 : isolate_->UnlinkDeferredHandles(this);
10615 :
10616 49308 : for (size_t i = 0; i < blocks_.size(); i++) {
10617 : #ifdef ENABLE_HANDLE_ZAPPING
10618 36981 : HandleScope::ZapRange(blocks_[i], &blocks_[i][kHandleBlockSize]);
10619 : #endif
10620 12327 : isolate_->handle_scope_implementer()->ReturnBlock(blocks_[i]);
10621 : }
10622 12327 : }
10623 :
10624 3688 : void DeferredHandles::Iterate(RootVisitor* v) {
10625 : DCHECK(!blocks_.empty());
10626 :
10627 : // Comparing pointers that do not point into the same array is undefined
10628 : // behavior, which means if we didn't cast everything to plain Address
10629 : // before comparing, the compiler would be allowed to assume that all
10630 : // comparisons evaluate to true and drop the entire check.
10631 : DCHECK((reinterpret_cast<Address>(first_block_limit_) >=
10632 : reinterpret_cast<Address>(blocks_.front())) &&
10633 : (reinterpret_cast<Address>(first_block_limit_) <=
10634 : reinterpret_cast<Address>(&(blocks_.front())[kHandleBlockSize])));
10635 :
10636 : v->VisitRootPointers(Root::kHandleScope, nullptr,
10637 3688 : FullObjectSlot(blocks_.front()),
10638 11064 : FullObjectSlot(first_block_limit_));
10639 :
10640 7376 : for (size_t i = 1; i < blocks_.size(); i++) {
10641 : v->VisitRootPointers(Root::kHandleScope, nullptr,
10642 : FullObjectSlot(blocks_[i]),
10643 0 : FullObjectSlot(&blocks_[i][kHandleBlockSize]));
10644 : }
10645 3688 : }
10646 :
10647 :
10648 645 : void InvokeAccessorGetterCallback(
10649 : v8::Local<v8::Name> property,
10650 : const v8::PropertyCallbackInfo<v8::Value>& info,
10651 : v8::AccessorNameGetterCallback getter) {
10652 : // Leaving JavaScript.
10653 : Isolate* isolate = reinterpret_cast<Isolate*>(info.GetIsolate());
10654 : RuntimeCallTimerScope timer(isolate,
10655 645 : RuntimeCallCounterId::kAccessorGetterCallback);
10656 645 : Address getter_address = reinterpret_cast<Address>(getter);
10657 1290 : VMState<EXTERNAL> state(isolate);
10658 1290 : ExternalCallbackScope call_scope(isolate, getter_address);
10659 645 : getter(property, info);
10660 645 : }
10661 :
10662 :
10663 2 : void InvokeFunctionCallback(const v8::FunctionCallbackInfo<v8::Value>& info,
10664 : v8::FunctionCallback callback) {
10665 : Isolate* isolate = reinterpret_cast<Isolate*>(info.GetIsolate());
10666 : RuntimeCallTimerScope timer(isolate,
10667 2 : RuntimeCallCounterId::kInvokeFunctionCallback);
10668 2 : Address callback_address = reinterpret_cast<Address>(callback);
10669 4 : VMState<EXTERNAL> state(isolate);
10670 4 : ExternalCallbackScope call_scope(isolate, callback_address);
10671 2 : callback(info);
10672 2 : }
10673 :
10674 : // Undefine macros for jumbo build.
10675 : #undef LOG_API
10676 : #undef ENTER_V8_DO_NOT_USE
10677 : #undef ENTER_V8_HELPER_DO_NOT_USE
10678 : #undef PREPARE_FOR_DEBUG_INTERFACE_EXECUTION_WITH_ISOLATE
10679 : #undef PREPARE_FOR_EXECUTION_WITH_CONTEXT
10680 : #undef PREPARE_FOR_EXECUTION
10681 : #undef ENTER_V8
10682 : #undef ENTER_V8_NO_SCRIPT
10683 : #undef ENTER_V8_NO_SCRIPT_NO_EXCEPTION
10684 : #undef ENTER_V8_FOR_NEW_CONTEXT
10685 : #undef EXCEPTION_BAILOUT_CHECK_SCOPED_DO_NOT_USE
10686 : #undef RETURN_ON_FAILED_EXECUTION
10687 : #undef RETURN_ON_FAILED_EXECUTION_PRIMITIVE
10688 : #undef RETURN_TO_LOCAL_UNCHECKED
10689 : #undef RETURN_ESCAPED
10690 : #undef SET_FIELD_WRAPPED
10691 : #undef NEW_STRING
10692 : #undef CALLBACK_SETTER
10693 :
10694 : } // namespace internal
10695 183867 : } // namespace v8
|