// 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-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()); v8::V8::InitializePlatform(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_; } private: std::unique_ptr array_buffer_allocator_; std::unique_ptr counter_map_; v8::Isolate* isolate_; }; // // 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(); } private: v8::IsolateWrapper isolate_wrapper_; }; template class WithIsolateScopeMixin : public TMixin { public: WithIsolateScopeMixin() : isolate_scope_(this->v8_isolate()), handle_scope_(this->v8_isolate()) {} WithIsolateScopeMixin(const WithIsolateScopeMixin&) = delete; WithIsolateScopeMixin& operator=(const WithIsolateScopeMixin&) = delete; v8::Isolate* isolate() const { return this->v8_isolate(); } v8::internal::Isolate* i_isolate() const { return reinterpret_cast(this->v8_isolate()); } i::Handle MakeName(const char* str, int suffix) { v8::base::EmbeddedVector buffer; v8::base::SNPrintF(buffer, "%s%d", str, suffix); return MakeString(buffer.begin()); } i::Handle MakeString(const char* str) { i::Factory* factory = i_isolate()->factory(); return factory->InternalizeUtf8String(str); } Local RunJS(const char* source) { return RunJS( v8::String::NewFromUtf8(this->v8_isolate(), source).ToLocalChecked()); } MaybeLocal TryRunJS(const char* source) { return TryRunJS( v8::String::NewFromUtf8(this->v8_isolate(), source).ToLocalChecked()); } static MaybeLocal TryRunJS(Isolate* isolate, Local source) { auto context = isolate->GetCurrentContext(); v8::Local result; Local