// Copyright 2014 the V8 project authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. #ifndef V8_UNITTESTS_TEST_UTILS_H_ #define V8_UNITTESTS_TEST_UTILS_H_ #include #include #include "include/libplatform/libplatform.h" #include "include/v8-array-buffer.h" #include "include/v8-context.h" #include "include/v8-extension.h" #include "include/v8-local-handle.h" #include "include/v8-object.h" #include "include/v8-primitive.h" #include "include/v8-template.h" #include "src/api/api-inl.h" #include "src/base/macros.h" #include "src/base/utils/random-number-generator.h" #include "src/handles/handles.h" #include "src/heap/parked-scope.h" #include "src/objects/objects-inl.h" #include "src/objects/objects.h" #include "src/zone/accounting-allocator.h" #include "src/zone/zone.h" #include "testing/gtest-support.h" namespace v8 { class ArrayBufferAllocator; template class WithDefaultPlatformMixin : public TMixin { public: WithDefaultPlatformMixin() { platform_ = v8::platform::NewDefaultPlatform( 0, v8::platform::IdleTaskSupport::kEnabled); CHECK_NOT_NULL(platform_.get()); i::V8::InitializePlatformForTesting(platform_.get()); // Allow changing flags in unit tests. // TODO(12887): Fix tests to avoid changing flag values after // initialization. i::v8_flags.freeze_flags_after_init = false; v8::V8::Initialize(); } virtual ~WithDefaultPlatformMixin() { CHECK_NOT_NULL(platform_.get()); v8::V8::Dispose(); v8::V8::DisposePlatform(); } v8::Platform* platform() const { return platform_.get(); } private: std::unique_ptr platform_; }; template class WithJSSharedMemoryFeatureFlagsMixin : public TMixin { public: WithJSSharedMemoryFeatureFlagsMixin() { i::v8_flags.harmony_struct = true; } }; using CounterMap = std::map; enum CountersMode { kNoCounters, kEnableCounters }; // RAII-like Isolate instance wrapper. // // It is the caller's responsibility to ensure that the shared Isolate outlives // all client Isolates. class IsolateWrapper final { public: explicit IsolateWrapper(CountersMode counters_mode); ~IsolateWrapper(); IsolateWrapper(const IsolateWrapper&) = delete; IsolateWrapper& operator=(const IsolateWrapper&) = delete; v8::Isolate* isolate() const { return isolate_; } i::Isolate* i_isolate() const { return reinterpret_cast(isolate_); } private: std::unique_ptr array_buffer_allocator_; std::unique_ptr counter_map_; v8::Isolate* isolate_; }; class IsolateWithContextWrapper final { public: IsolateWithContextWrapper() : isolate_wrapper_(kNoCounters), isolate_scope_(isolate_wrapper_.isolate()), handle_scope_(isolate_wrapper_.isolate()), context_(v8::Context::New(isolate_wrapper_.isolate())), context_scope_(context_) {} v8::Isolate* v8_isolate() const { return isolate_wrapper_.isolate(); } i::Isolate* isolate() const { return reinterpret_cast(v8_isolate()); } private: IsolateWrapper isolate_wrapper_; v8::Isolate::Scope isolate_scope_; v8::HandleScope handle_scope_; v8::Local context_; v8::Context::Scope context_scope_; }; // // A set of mixins from which the test fixtures will be constructed. // template class WithIsolateMixin : public TMixin { public: WithIsolateMixin() : isolate_wrapper_(kCountersMode) {} v8::Isolate* v8_isolate() const { return isolate_wrapper_.isolate(); } Local RunJS(const char* source, Local context) { return RunJS( v8::String::NewFromUtf8(this->v8_isolate(), source).ToLocalChecked(), context); } Local RunJS(Local source, Local context) { Local