v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
api-macros.h
Go to the documentation of this file.
1// Copyright 2021 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// Note 1: Any file that includes this one should include api-macros-undef.h
6// at the bottom.
7
8// Note 2: This file is deliberately missing the include guards (the undeffing
9// approach wouldn't work otherwise).
10//
11// PRESUBMIT_INTENTIONALLY_MISSING_INCLUDE_GUARD
12
13/*
14 * Most API methods should use one of the three macros:
15 *
16 * ENTER_V8, ENTER_V8_NO_SCRIPT, ENTER_V8_NO_SCRIPT_NO_EXCEPTION.
17 *
18 * The latter two assume that no script is executed, and no exceptions are
19 * scheduled in addition (respectively). Creating an exception and
20 * removing it before returning is ok.
21 *
22 * Exceptions should be handled either by invoking one of the
23 * RETURN_ON_FAILED_EXECUTION* macros.
24 *
25 * API methods that are part of the debug interface should use
26 *
27 * PREPARE_FOR_DEBUG_INTERFACE_EXECUTION_WITH_ISOLATE
28 *
29 * in a similar fashion to ENTER_V8.
30 */
31
32#define API_RCS_SCOPE(i_isolate, class_name, function_name) \
33 RCS_SCOPE(i_isolate, \
34 i::RuntimeCallCounterId::kAPI_##class_name##_##function_name);
35
36#define ENTER_V8_BASIC(i_isolate) \
37 /* Embedders should never enter V8 after terminating it */ \
38 DCHECK_IMPLIES(i::v8_flags.strict_termination_checks, \
39 !i_isolate->is_execution_terminating()); \
40 i::VMState<v8::OTHER> __state__((i_isolate))
41
42#define ENTER_V8_HELPER_INTERNAL(i_isolate, context, class_name, \
43 function_name, HandleScopeClass, do_callback) \
44 DCHECK(!i_isolate->is_execution_terminating()); \
45 HandleScopeClass handle_scope(i_isolate); \
46 CallDepthScope<do_callback> call_depth_scope(i_isolate, context); \
47 API_RCS_SCOPE(i_isolate, class_name, function_name); \
48 i::VMState<v8::OTHER> __state__((i_isolate)); \
49 bool has_exception = false
50
51#define PREPARE_FOR_DEBUG_INTERFACE_EXECUTION_WITH_ISOLATE(i_isolate, context, \
52 T) \
53 DCHECK(!i_isolate->is_execution_terminating()); \
54 InternalEscapableScope handle_scope(i_isolate); \
55 CallDepthScope<false> call_depth_scope(i_isolate, context); \
56 i::VMState<v8::OTHER> __state__((i_isolate)); \
57 bool has_exception = false
58
59#define PREPARE_FOR_EXECUTION(context, class_name, function_name) \
60 auto i_isolate = reinterpret_cast<i::Isolate*>(context->GetIsolate()); \
61 i_isolate->clear_internal_exception(); \
62 ENTER_V8_HELPER_INTERNAL(i_isolate, context, class_name, function_name, \
63 InternalEscapableScope, false);
64
65#define ENTER_V8(i_isolate, context, class_name, function_name, \
66 HandleScopeClass) \
67 ENTER_V8_HELPER_INTERNAL(i_isolate, context, class_name, function_name, \
68 HandleScopeClass, true)
69
70#ifdef DEBUG
71#define ENTER_V8_NO_SCRIPT(i_isolate, context, class_name, function_name, \
72 HandleScopeClass) \
73 ENTER_V8_HELPER_INTERNAL(i_isolate, context, class_name, function_name, \
74 HandleScopeClass, false); \
75 i::DisallowJavascriptExecutionDebugOnly __no_script__((i_isolate))
76
77// Lightweight version for APIs that don't require an active context.
78#define DCHECK_NO_SCRIPT_NO_EXCEPTION(i_isolate) \
79 i::DisallowJavascriptExecutionDebugOnly __no_script__((i_isolate)); \
80 i::DisallowExceptions __no_exceptions__((i_isolate))
81
82#define ENTER_V8_NO_SCRIPT_NO_EXCEPTION(i_isolate) \
83 i::VMState<v8::OTHER> __state__((i_isolate)); \
84 DCHECK_NO_SCRIPT_NO_EXCEPTION(i_isolate)
85
86#define ENTER_V8_FOR_NEW_CONTEXT(i_isolate) \
87 DCHECK_IMPLIES(i::v8_flags.strict_termination_checks, \
88 !(i_isolate)->is_execution_terminating()); \
89 i::VMState<v8::OTHER> __state__((i_isolate)); \
90 i::DisallowExceptions __no_exceptions__((i_isolate))
91#else // DEBUG
92#define ENTER_V8_NO_SCRIPT(i_isolate, context, class_name, function_name, \
93 HandleScopeClass) \
94 ENTER_V8_HELPER_INTERNAL(i_isolate, context, class_name, function_name, \
95 HandleScopeClass, false)
96
97#define DCHECK_NO_SCRIPT_NO_EXCEPTION(i_isolate)
98
99#define ENTER_V8_NO_SCRIPT_NO_EXCEPTION(i_isolate) \
100 i::VMState<v8::OTHER> __state__((i_isolate));
101
102#define ENTER_V8_FOR_NEW_CONTEXT(i_isolate) \
103 i::VMState<v8::OTHER> __state__((i_isolate));
104#endif // DEBUG
105
106#define RETURN_ON_FAILED_EXECUTION(T) \
107 if (has_exception) return MaybeLocal<T>();
108
109#define RETURN_ON_FAILED_EXECUTION_PRIMITIVE(T) \
110 if (has_exception) return Nothing<T>();
111
112#define RETURN_ESCAPED(value) return handle_scope.Escape(value);