chromium/v8/test/cctest/cctest.h

// Copyright 2008 the V8 project authors. All rights reserved.
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
//     * Redistributions of source code must retain the above copyright
//       notice, this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above
//       copyright notice, this list of conditions and the following
//       disclaimer in the documentation and/or other materials provided
//       with the distribution.
//     * Neither the name of Google Inc. nor the names of its
//       contributors may be used to endorse or promote products derived
//       from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#ifndef CCTEST_H_
#define CCTEST_H_

#include <memory>

#include "include/libplatform/libplatform.h"
#include "include/v8-platform.h"
#include "src/base/enum-set.h"
#include "src/codegen/register-configuration.h"
#include "src/debug/debug-interface.h"
#include "src/execution/isolate-inl.h"
#include "src/execution/simulator.h"
#include "src/heap/factory.h"
#include "src/objects/js-function.h"
#include "src/objects/objects.h"
#include "src/zone/accounting-allocator.h"

namespace v8 {
namespace base {

class RandomNumberGenerator;

}  // namespace base

namespace internal {

const auto GetRegConfig =;

class HandleScope;
class ManualGCScope;
class Zone;

namespace compiler {

class JSHeapBroker;

}  // namespace compiler

}  // namespace internal

}  // namespace v8

#ifndef TEST
#define TEST(Name)
#endif

#ifndef UNINITIALIZED_TEST
#define UNINITIALIZED_TEST(Name)
#endif

#ifndef TEST_WITH_PLATFORM
#define TEST_WITH_PLATFORM(Name, PlatformClass)
#endif

#ifndef DISABLED_TEST
#define DISABLED_TEST(Name)
#endif

// Similar to TEST, but used when test definitions appear as members of a
// (probably parameterized) class. This allows re-using the given tests multiple
// times. For this to work, the following conditions must hold:
//   1. The class has a template parameter named kTestFileName of type  char
//      const*, which is instantiated with __FILE__ at the *use site*, in order
//      to correctly associate the tests with the test suite using them.
//   2. To actually execute the tests, create an instance of the class
//      containing the MEMBER_TESTs.
#define MEMBER_TEST(Name)

#define EXTENSION_LIST(V)

#define DEFINE_EXTENSION_ID
enum CcTestExtensionId {};
#undef DEFINE_EXTENSION_ID

using CcTestExtensionFlags = v8::base::EnumSet<CcTestExtensionId>;

#define DEFINE_EXTENSION_NAME
static constexpr const char* kExtensionName[kMaxExtensions] =;
#undef DEFINE_EXTENSION_NAME

class CcTest;
class TestPlatform;

using CcTestMapType = std::map<std::string, CcTest*>;

class CcTest {};

// Switches between all the Api tests using the threading support.
// In order to get a surprising but repeatable pattern of thread
// switching it has extra semaphores to control the order in which
// the tests alternate, not relying solely on the big V8 lock.
//
// A test is augmented with calls to ApiTestFuzzer::Fuzz() in its
// callbacks.  This will have no effect when we are not running the
// thread fuzzing test.  In the thread fuzzing test it will
// pseudorandomly select a successor thread and switch execution
// to that thread, suspending the current test.
class ApiTestFuzzer: public v8::base::Thread {};

// In threaded cctests, control flow alternates between different threads, each
// of which runs a single test. All threaded cctests share the same isolate and
// a heap. With conservative stack scanning (CSS), whenever a thread invokes a
// GC for the common heap, the stacks of all threads are scanned. In this
// setting, it is not possible to disable CSS without losing correctness.
// Therefore, tests defined with THREADED_TEST:
//
// 1.  must not explicitly disable CSS, using the scope
//     internal::DisableConservativeStackScanningScopeForTesting, and
// 2.  cannot rely on the assumption that garbage collection will reclaim all
//     non-live objects.

#define THREADED_TEST(Name)

class RegisterThreadedTest {};

// A LocalContext holds a reference to a v8::Context.
class LocalContext {};


static inline uint16_t* AsciiToTwoByteString(const char* source) {}

static inline uint16_t* AsciiToTwoByteString(const char16_t* source,
                                             size_t* length_out = nullptr) {}

template <typename T>
static inline i::Handle<T> GetGlobal(const char* name) {}

static inline v8::Local<v8::Boolean> v8_bool(bool val) {}

static inline v8::Local<v8::Number> v8_num(double x) {}

static inline v8::Local<v8::Integer> v8_int(int32_t x) {}

static inline v8::Local<v8::Integer> v8_uint(uint32_t x) {}

static inline v8::Local<v8::BigInt> v8_bigint(int64_t x) {}

static inline v8::Local<v8::String> v8_str(const char* x) {}


static inline v8::Local<v8::String> v8_str(v8::Isolate* isolate,
                                           const char* x) {}


static inline v8::Local<v8::Symbol> v8_symbol(const char* name) {}


static inline v8::Local<v8::Script> v8_compile(v8::Local<v8::String> x) {}

static inline v8::Local<v8::Script> v8_compile(const char* x) {}

static inline v8::MaybeLocal<v8::Script> v8_try_compile(
    v8::Local<v8::String> x) {}

static inline v8::MaybeLocal<v8::Script> v8_try_compile(const char* x) {}

static inline int32_t v8_run_int32value(v8::Local<v8::Script> script) {}

static inline v8::Local<v8::Script> CompileWithOrigin(
    v8::Local<v8::String> source, v8::Local<v8::String> origin_url,
    bool is_shared_cross_origin) {}

static inline v8::Local<v8::Script> CompileWithOrigin(
    v8::Local<v8::String> source, const char* origin_url,
    bool is_shared_cross_origin) {}

static inline v8::Local<v8::Script> CompileWithOrigin(
    const char* source, const char* origin_url, bool is_shared_cross_origin) {}

// Helper functions that compile and run the source.
static inline v8::MaybeLocal<v8::Value> CompileRun(
    v8::Local<v8::Context> context, const char* source) {}


static inline v8::Local<v8::Value> CompileRunChecked(v8::Isolate* isolate,
                                                     const char* source) {}


static inline v8::Local<v8::Value> CompileRun(v8::Local<v8::String> source) {}


// Helper functions that compile and run the source.
static inline v8::Local<v8::Value> CompileRun(const char* source) {}


static inline v8::Local<v8::Value> CompileRun(
    v8::Local<v8::Context> context, v8::ScriptCompiler::Source* script_source,
    v8::ScriptCompiler::CompileOptions options) {}


// Helper functions that compile and run the source with given origin.
static inline v8::Local<v8::Value> CompileRunWithOrigin(const char* source,
                                                        const char* origin_url,
                                                        int line_number,
                                                        int column_number) {}


static inline v8::Local<v8::Value> CompileRunWithOrigin(
    v8::Local<v8::String> source, const char* origin_url) {}


static inline v8::Local<v8::Value> CompileRunWithOrigin(
    const char* source, const char* origin_url) {}

// Run a ScriptStreamingTask in a separate thread.
class StreamerThread : public v8::base::Thread {};

// Takes a JSFunction and runs it through the test version of the optimizing
// pipeline, allocating the temporary compilation artifacts in a given Zone.
// For possible {flags} values, look at OptimizedCompilationInfo::Flag.
i::Handle<i::JSFunction> Optimize(i::Handle<i::JSFunction> function,
                                  i::Zone* zone, i::Isolate* isolate,
                                  uint32_t flags);

static inline void ExpectString(const char* code, const char* expected) {}

static inline void ExpectInt32(const char* code, int expected) {}


static inline void ExpectBoolean(const char* code, bool expected) {}


static inline void ExpectTrue(const char* code) {}


static inline void ExpectFalse(const char* code) {}


static inline void ExpectObject(const char* code,
                                v8::Local<v8::Value> expected) {}


static inline void ExpectUndefined(const char* code) {}


static inline void ExpectNull(const char* code) {}


static inline void CheckDoubleEquals(double expected, double actual) {}

static v8::debug::DebugDelegate dummy_delegate;

static inline void EnableDebugger(v8::Isolate* isolate) {}


static inline void DisableDebugger(v8::Isolate* isolate) {}


static inline void EmptyMessageQueues(v8::Isolate* isolate) {}

class InitializedHandleScopeImpl;

class V8_NODISCARD InitializedHandleScope {};

class V8_NODISCARD HandleAndZoneScope : public InitializedHandleScope {};

class StaticOneByteResource : public v8::String::ExternalOneByteStringResource {};

// This is a base class that can be overridden to implement a test platform. It
// delegates all operations to the default platform.
class TestPlatform : public v8::Platform {};

#if defined(USE_SIMULATOR)
class SimulatorHelper {
 public:
  inline bool Init(v8::Isolate* isolate) {
    simulator_ = reinterpret_cast<v8::internal::Isolate*>(isolate)
                     ->thread_local_top()
                     ->simulator_;
    // Check if there is active simulator.
    return simulator_ != nullptr;
  }

  inline void FillRegisters(v8::RegisterState* state) {
#if V8_TARGET_ARCH_ARM
    state->pc = reinterpret_cast<void*>(simulator_->get_pc());
    state->sp = reinterpret_cast<void*>(
        simulator_->get_register(v8::internal::Simulator::sp));
    state->fp = reinterpret_cast<void*>(
        simulator_->get_register(v8::internal::Simulator::r11));
    state->lr = reinterpret_cast<void*>(
        simulator_->get_register(v8::internal::Simulator::lr));
#elif V8_TARGET_ARCH_ARM64
    if (simulator_->sp() == 0 || simulator_->fp() == 0) {
      // It's possible that the simulator is interrupted while it is updating
      // the sp or fp register. ARM64 simulator does this in two steps:
      // first setting it to zero and then setting it to a new value.
      // Bailout if sp/fp doesn't contain the new value.
      return;
    }
    state->pc = reinterpret_cast<void*>(simulator_->pc());
    state->sp = reinterpret_cast<void*>(simulator_->sp());
    state->fp = reinterpret_cast<void*>(simulator_->fp());
    state->lr = reinterpret_cast<void*>(simulator_->lr());
#elif V8_TARGET_ARCH_MIPS64 || V8_TARGET_ARCH_LOONG64
    state->pc = reinterpret_cast<void*>(simulator_->get_pc());
    state->sp = reinterpret_cast<void*>(
        simulator_->get_register(v8::internal::Simulator::sp));
    state->fp = reinterpret_cast<void*>(
        simulator_->get_register(v8::internal::Simulator::fp));
#elif V8_TARGET_ARCH_RISCV64 || V8_TARGET_ARCH_RISCV32
    state->pc = reinterpret_cast<void*>(simulator_->get_pc());
    state->sp = reinterpret_cast<void*>(
        simulator_->get_register(v8::internal::Simulator::sp));
    state->fp = reinterpret_cast<void*>(
        simulator_->get_register(v8::internal::Simulator::fp));
    state->lr = reinterpret_cast<void*>(
        simulator_->get_register(v8::internal::Simulator::ra));
#elif V8_TARGET_ARCH_PPC64
    state->pc = reinterpret_cast<void*>(simulator_->get_pc());
    state->sp = reinterpret_cast<void*>(
        simulator_->get_register(v8::internal::Simulator::sp));
    state->fp = reinterpret_cast<void*>(
        simulator_->get_register(v8::internal::Simulator::fp));
    state->lr = reinterpret_cast<void*>(simulator_->get_lr());
#elif V8_TARGET_ARCH_S390 || V8_TARGET_ARCH_S390X
    state->pc = reinterpret_cast<void*>(simulator_->get_pc());
    state->sp = reinterpret_cast<void*>(
        simulator_->get_register(v8::internal::Simulator::sp));
    state->fp = reinterpret_cast<void*>(
        simulator_->get_register(v8::internal::Simulator::fp));
    state->lr = reinterpret_cast<void*>(
        simulator_->get_register(v8::internal::Simulator::ra));
#endif
  }

 private:
  v8::internal::Simulator* simulator_;
};
#endif  // USE_SIMULATOR

// The following should correspond to Chromium's kV8DOMWrapperTypeIndex and
// kV8DOMWrapperObjectIndex.
static const int kV8WrapperTypeIndex =;
static const int kV8WrapperObjectIndex =;

enum class ApiCheckerResult : uint8_t {};
using ApiCheckerResultFlags = v8::base::Flags<ApiCheckerResult>;
DEFINE_OPERATORS_FOR_FLAGS()

bool IsValidUnwrapObject(v8::Object* object);

template <typename T>
T* GetInternalField(v8::Object* wrapper) {}

#endif  // ifndef CCTEST_H_