11cb0ef41Sopenharmony_ci// Copyright 2021 the V8 project authors. All rights reserved.
21cb0ef41Sopenharmony_ci// Use of this source code is governed by a BSD-style license that can be
31cb0ef41Sopenharmony_ci// found in the LICENSE file.
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_ci// Note 1: Any file that includes this one should include api-macros-undef.h
61cb0ef41Sopenharmony_ci// at the bottom.
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_ci// Note 2: This file is deliberately missing the include guards (the undeffing
91cb0ef41Sopenharmony_ci// approach wouldn't work otherwise).
101cb0ef41Sopenharmony_ci//
111cb0ef41Sopenharmony_ci// PRESUBMIT_INTENTIONALLY_MISSING_INCLUDE_GUARD
121cb0ef41Sopenharmony_ci
131cb0ef41Sopenharmony_ci/*
141cb0ef41Sopenharmony_ci * Most API methods should use one of the three macros:
151cb0ef41Sopenharmony_ci *
161cb0ef41Sopenharmony_ci * ENTER_V8, ENTER_V8_NO_SCRIPT, ENTER_V8_NO_SCRIPT_NO_EXCEPTION.
171cb0ef41Sopenharmony_ci *
181cb0ef41Sopenharmony_ci * The latter two assume that no script is executed, and no exceptions are
191cb0ef41Sopenharmony_ci * scheduled in addition (respectively). Creating a pending exception and
201cb0ef41Sopenharmony_ci * removing it before returning is ok.
211cb0ef41Sopenharmony_ci *
221cb0ef41Sopenharmony_ci * Exceptions should be handled either by invoking one of the
231cb0ef41Sopenharmony_ci * RETURN_ON_FAILED_EXECUTION* macros.
241cb0ef41Sopenharmony_ci *
251cb0ef41Sopenharmony_ci * API methods that are part of the debug interface should use
261cb0ef41Sopenharmony_ci *
271cb0ef41Sopenharmony_ci * PREPARE_FOR_DEBUG_INTERFACE_EXECUTION_WITH_ISOLATE
281cb0ef41Sopenharmony_ci *
291cb0ef41Sopenharmony_ci * in a similar fashion to ENTER_V8.
301cb0ef41Sopenharmony_ci *
311cb0ef41Sopenharmony_ci * Don't use macros with DO_NOT_USE in their name.
321cb0ef41Sopenharmony_ci *
331cb0ef41Sopenharmony_ci * TODO(cbruni): Document LOG_API and other RuntimeCallStats macros.
341cb0ef41Sopenharmony_ci * TODO(verwaest): All API methods should invoke one of the ENTER_V8* macros.
351cb0ef41Sopenharmony_ci * TODO(verwaest): Remove calls form API methods to DO_NOT_USE macros.
361cb0ef41Sopenharmony_ci */
371cb0ef41Sopenharmony_ci
381cb0ef41Sopenharmony_ci#define API_RCS_SCOPE(isolate, class_name, function_name) \
391cb0ef41Sopenharmony_ci  RCS_SCOPE(isolate,                                      \
401cb0ef41Sopenharmony_ci            i::RuntimeCallCounterId::kAPI_##class_name##_##function_name);
411cb0ef41Sopenharmony_ci
421cb0ef41Sopenharmony_ci#define ENTER_V8_DO_NOT_USE(isolate) i::VMState<v8::OTHER> __state__((isolate))
431cb0ef41Sopenharmony_ci
441cb0ef41Sopenharmony_ci#define ENTER_V8_HELPER_DO_NOT_USE(isolate, context, class_name,  \
451cb0ef41Sopenharmony_ci                                   function_name, bailout_value,  \
461cb0ef41Sopenharmony_ci                                   HandleScopeClass, do_callback) \
471cb0ef41Sopenharmony_ci  if (IsExecutionTerminatingCheck(isolate)) {                     \
481cb0ef41Sopenharmony_ci    return bailout_value;                                         \
491cb0ef41Sopenharmony_ci  }                                                               \
501cb0ef41Sopenharmony_ci  HandleScopeClass handle_scope(isolate);                         \
511cb0ef41Sopenharmony_ci  CallDepthScope<do_callback> call_depth_scope(isolate, context); \
521cb0ef41Sopenharmony_ci  API_RCS_SCOPE(isolate, class_name, function_name);              \
531cb0ef41Sopenharmony_ci  i::VMState<v8::OTHER> __state__((isolate));                     \
541cb0ef41Sopenharmony_ci  bool has_pending_exception = false
551cb0ef41Sopenharmony_ci
561cb0ef41Sopenharmony_ci#define PREPARE_FOR_DEBUG_INTERFACE_EXECUTION_WITH_ISOLATE(isolate, T)       \
571cb0ef41Sopenharmony_ci  if (IsExecutionTerminatingCheck(isolate)) {                                \
581cb0ef41Sopenharmony_ci    return MaybeLocal<T>();                                                  \
591cb0ef41Sopenharmony_ci  }                                                                          \
601cb0ef41Sopenharmony_ci  InternalEscapableScope handle_scope(isolate);                              \
611cb0ef41Sopenharmony_ci  CallDepthScope<false> call_depth_scope(isolate, v8::Local<v8::Context>()); \
621cb0ef41Sopenharmony_ci  i::VMState<v8::OTHER> __state__((isolate));                                \
631cb0ef41Sopenharmony_ci  bool has_pending_exception = false
641cb0ef41Sopenharmony_ci
651cb0ef41Sopenharmony_ci#define PREPARE_FOR_EXECUTION_WITH_CONTEXT(context, class_name, function_name, \
661cb0ef41Sopenharmony_ci                                           bailout_value, HandleScopeClass,    \
671cb0ef41Sopenharmony_ci                                           do_callback)                        \
681cb0ef41Sopenharmony_ci  auto isolate = context.IsEmpty()                                             \
691cb0ef41Sopenharmony_ci                     ? i::Isolate::Current()                                   \
701cb0ef41Sopenharmony_ci                     : reinterpret_cast<i::Isolate*>(context->GetIsolate());   \
711cb0ef41Sopenharmony_ci  ENTER_V8_HELPER_DO_NOT_USE(isolate, context, class_name, function_name,      \
721cb0ef41Sopenharmony_ci                             bailout_value, HandleScopeClass, do_callback);
731cb0ef41Sopenharmony_ci
741cb0ef41Sopenharmony_ci#define PREPARE_FOR_EXECUTION(context, class_name, function_name, T)          \
751cb0ef41Sopenharmony_ci  PREPARE_FOR_EXECUTION_WITH_CONTEXT(context, class_name, function_name,      \
761cb0ef41Sopenharmony_ci                                     MaybeLocal<T>(), InternalEscapableScope, \
771cb0ef41Sopenharmony_ci                                     false)
781cb0ef41Sopenharmony_ci
791cb0ef41Sopenharmony_ci#define ENTER_V8(isolate, context, class_name, function_name, bailout_value, \
801cb0ef41Sopenharmony_ci                 HandleScopeClass)                                           \
811cb0ef41Sopenharmony_ci  ENTER_V8_HELPER_DO_NOT_USE(isolate, context, class_name, function_name,    \
821cb0ef41Sopenharmony_ci                             bailout_value, HandleScopeClass, true)
831cb0ef41Sopenharmony_ci
841cb0ef41Sopenharmony_ci#ifdef DEBUG
851cb0ef41Sopenharmony_ci#define ENTER_V8_NO_SCRIPT(isolate, context, class_name, function_name,   \
861cb0ef41Sopenharmony_ci                           bailout_value, HandleScopeClass)               \
871cb0ef41Sopenharmony_ci  ENTER_V8_HELPER_DO_NOT_USE(isolate, context, class_name, function_name, \
881cb0ef41Sopenharmony_ci                             bailout_value, HandleScopeClass, false);     \
891cb0ef41Sopenharmony_ci  i::DisallowJavascriptExecutionDebugOnly __no_script__((isolate))
901cb0ef41Sopenharmony_ci
911cb0ef41Sopenharmony_ci// Lightweight version for APIs that don't require an active context.
921cb0ef41Sopenharmony_ci#define ASSERT_NO_SCRIPT_NO_EXCEPTION(isolate)                      \
931cb0ef41Sopenharmony_ci  i::DisallowJavascriptExecutionDebugOnly __no_script__((isolate)); \
941cb0ef41Sopenharmony_ci  i::DisallowExceptions __no_exceptions__((isolate))
951cb0ef41Sopenharmony_ci
961cb0ef41Sopenharmony_ci#define ENTER_V8_NO_SCRIPT_NO_EXCEPTION(isolate) \
971cb0ef41Sopenharmony_ci  i::VMState<v8::OTHER> __state__((isolate));    \
981cb0ef41Sopenharmony_ci  ASSERT_NO_SCRIPT_NO_EXCEPTION(isolate)
991cb0ef41Sopenharmony_ci
1001cb0ef41Sopenharmony_ci#define ENTER_V8_FOR_NEW_CONTEXT(isolate)     \
1011cb0ef41Sopenharmony_ci  i::VMState<v8::OTHER> __state__((isolate)); \
1021cb0ef41Sopenharmony_ci  i::DisallowExceptions __no_exceptions__((isolate))
1031cb0ef41Sopenharmony_ci#else
1041cb0ef41Sopenharmony_ci#define ENTER_V8_NO_SCRIPT(isolate, context, class_name, function_name,   \
1051cb0ef41Sopenharmony_ci                           bailout_value, HandleScopeClass)               \
1061cb0ef41Sopenharmony_ci  ENTER_V8_HELPER_DO_NOT_USE(isolate, context, class_name, function_name, \
1071cb0ef41Sopenharmony_ci                             bailout_value, HandleScopeClass, false)
1081cb0ef41Sopenharmony_ci
1091cb0ef41Sopenharmony_ci#define ASSERT_NO_SCRIPT_NO_EXCEPTION(isolate)
1101cb0ef41Sopenharmony_ci
1111cb0ef41Sopenharmony_ci#define ENTER_V8_NO_SCRIPT_NO_EXCEPTION(isolate) \
1121cb0ef41Sopenharmony_ci  i::VMState<v8::OTHER> __state__((isolate));
1131cb0ef41Sopenharmony_ci
1141cb0ef41Sopenharmony_ci#define ENTER_V8_FOR_NEW_CONTEXT(isolate) \
1151cb0ef41Sopenharmony_ci  i::VMState<v8::OTHER> __state__((isolate));
1161cb0ef41Sopenharmony_ci#endif  // DEBUG
1171cb0ef41Sopenharmony_ci
1181cb0ef41Sopenharmony_ci#define EXCEPTION_BAILOUT_CHECK_SCOPED_DO_NOT_USE(isolate, value) \
1191cb0ef41Sopenharmony_ci  do {                                                            \
1201cb0ef41Sopenharmony_ci    if (has_pending_exception) {                                  \
1211cb0ef41Sopenharmony_ci      call_depth_scope.Escape();                                  \
1221cb0ef41Sopenharmony_ci      return value;                                               \
1231cb0ef41Sopenharmony_ci    }                                                             \
1241cb0ef41Sopenharmony_ci  } while (false)
1251cb0ef41Sopenharmony_ci
1261cb0ef41Sopenharmony_ci#define RETURN_ON_FAILED_EXECUTION(T) \
1271cb0ef41Sopenharmony_ci  EXCEPTION_BAILOUT_CHECK_SCOPED_DO_NOT_USE(isolate, MaybeLocal<T>())
1281cb0ef41Sopenharmony_ci
1291cb0ef41Sopenharmony_ci#define RETURN_ON_FAILED_EXECUTION_PRIMITIVE(T) \
1301cb0ef41Sopenharmony_ci  EXCEPTION_BAILOUT_CHECK_SCOPED_DO_NOT_USE(isolate, Nothing<T>())
1311cb0ef41Sopenharmony_ci
1321cb0ef41Sopenharmony_ci#define RETURN_ESCAPED(value) return handle_scope.Escape(value);
133