1 // Copyright 2008 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are
6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided
11 // with the distribution.
12 // * Neither the name of Google Inc. nor the names of its
13 // contributors may be used to endorse or promote products derived
14 // from this software without specific prior written permission.
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 static void Test##Name(); \
36 CcTest register_test_##Name(Test##Name, __FILE__, #Name, NULL, true, true); \
37 static void Test##Name()
40 #ifndef UNINITIALIZED_TEST
41 #define UNINITIALIZED_TEST(Name) \
42 static void Test##Name(); \
43 CcTest register_test_##Name(Test##Name, __FILE__, #Name, NULL, true, false); \
44 static void Test##Name()
47 #ifndef DEPENDENT_TEST
48 #define DEPENDENT_TEST(Name, Dep) \
49 static void Test##Name(); \
50 CcTest register_test_##Name(Test##Name, __FILE__, #Name, #Dep, true, true); \
51 static void Test##Name()
55 #define DISABLED_TEST(Name) \
56 static void Test##Name(); \
57 CcTest register_test_##Name(Test##Name, __FILE__, #Name, NULL, false, true); \
58 static void Test##Name()
61 #define EXTENSION_LIST(V) \
62 V(GC_EXTENSION, "v8/gc") \
63 V(PRINT_EXTENSION, "v8/print") \
64 V(PROFILER_EXTENSION, "v8/profiler") \
65 V(TRACE_EXTENSION, "v8/trace")
67 #define DEFINE_EXTENSION_ID(Name, Ident) Name##_ID,
68 enum CcTestExtensionIds {
69 EXTENSION_LIST(DEFINE_EXTENSION_ID)
72 #undef DEFINE_EXTENSION_ID
74 typedef v8::internal::EnumSet<CcTestExtensionIds> CcTestExtensionFlags;
75 #define DEFINE_EXTENSION_FLAG(Name, Ident) \
76 static const CcTestExtensionFlags Name(1 << Name##_ID);
77 static const CcTestExtensionFlags NO_EXTENSIONS(0);
78 static const CcTestExtensionFlags ALL_EXTENSIONS((1 << kMaxExtensions) - 1);
79 EXTENSION_LIST(DEFINE_EXTENSION_FLAG)
80 #undef DEFINE_EXTENSION_FLAG
83 // Use this to expose protected methods in i::Heap.
84 class TestHeap : public i::Heap {
86 using i::Heap::AllocateArgumentsObject;
87 using i::Heap::AllocateByteArray;
88 using i::Heap::AllocateFixedArray;
89 using i::Heap::AllocateHeapNumber;
90 using i::Heap::AllocateJSObject;
91 using i::Heap::AllocateJSObjectFromMap;
92 using i::Heap::AllocateMap;
93 using i::Heap::CopyCode;
99 typedef void (TestFunction)();
100 CcTest(TestFunction* callback, const char* file, const char* name,
101 const char* dependency, bool enabled, bool initialize);
103 static CcTest* last() { return last_; }
104 CcTest* prev() { return prev_; }
105 const char* file() { return file_; }
106 const char* name() { return name_; }
107 const char* dependency() { return dependency_; }
108 bool enabled() { return enabled_; }
110 static v8::Isolate* isolate() {
111 CHECK(isolate_ != NULL);
112 isolate_used_ = true;
116 static i::Isolate* i_isolate() {
117 return reinterpret_cast<i::Isolate*>(isolate());
120 static i::Heap* heap() {
121 return i_isolate()->heap();
124 static TestHeap* test_heap() {
125 return reinterpret_cast<TestHeap*>(i_isolate()->heap());
128 static v8::Local<v8::Object> global() {
129 return isolate()->GetCurrentContext()->Global();
132 // TODO(dcarney): Remove.
133 // This must be called first in a test.
134 static void InitializeVM() {
135 CHECK(!isolate_used_);
136 CHECK(!initialize_called_);
137 initialize_called_ = true;
138 v8::HandleScope handle_scope(CcTest::isolate());
139 v8::Context::New(CcTest::isolate())->Enter();
142 // Only for UNINITIALIZED_TESTs
143 static void DisableAutomaticDispose();
145 // Helper function to configure a context.
146 // Must be in a HandleScope.
147 static v8::Local<v8::Context> NewContext(
148 CcTestExtensionFlags extensions,
149 v8::Isolate* isolate = CcTest::isolate());
151 static void TearDown() {
152 if (isolate_ != NULL) isolate_->Dispose();
156 friend int main(int argc, char** argv);
157 TestFunction* callback_;
160 const char* dependency_;
164 static CcTest* last_;
165 static v8::Isolate* isolate_;
166 static bool initialize_called_;
167 static bool isolate_used_;
170 // Switches between all the Api tests using the threading support.
171 // In order to get a surprising but repeatable pattern of thread
172 // switching it has extra semaphores to control the order in which
173 // the tests alternate, not relying solely on the big V8 lock.
175 // A test is augmented with calls to ApiTestFuzzer::Fuzz() in its
176 // callbacks. This will have no effect when we are not running the
177 // thread fuzzing test. In the thread fuzzing test it will
178 // pseudorandomly select a successor thread and switch execution
179 // to that thread, suspending the current test.
180 class ApiTestFuzzer: public v8::internal::Thread {
184 // The ApiTestFuzzer is also a Thread, so it has a Run method.
187 enum PartOfTest { FIRST_PART,
191 LAST_PART = FOURTH_PART };
193 static void SetUp(PartOfTest part);
194 static void RunAllTests();
195 static void TearDown();
196 // This method switches threads if we are running the Threading test.
197 // Otherwise it does nothing.
201 explicit ApiTestFuzzer(int num)
202 : Thread("ApiTestFuzzer"),
209 static bool fuzzing_;
210 static int tests_being_run_;
212 static int active_tests_;
213 static bool NextThread();
215 v8::internal::Semaphore gate_;
217 void ContextSwitch();
218 static int GetNextTestNumber();
219 static v8::internal::Semaphore all_tests_done_;
223 #define THREADED_TEST(Name) \
224 static void Test##Name(); \
225 RegisterThreadedTest register_##Name(Test##Name, #Name); \
229 class RegisterThreadedTest {
231 explicit RegisterThreadedTest(CcTest::TestFunction* callback,
233 : fuzzer_(NULL), callback_(callback), name_(name) {
238 static int count() { return count_; }
239 static RegisterThreadedTest* nth(int i) {
241 RegisterThreadedTest* current = first_;
244 current = current->prev_;
248 CcTest::TestFunction* callback() { return callback_; }
249 ApiTestFuzzer* fuzzer_;
250 const char* name() { return name_; }
253 static RegisterThreadedTest* first_;
255 CcTest::TestFunction* callback_;
256 RegisterThreadedTest* prev_;
260 // A LocalContext holds a reference to a v8::Context.
263 LocalContext(v8::Isolate* isolate,
264 v8::ExtensionConfiguration* extensions = 0,
265 v8::Handle<v8::ObjectTemplate> global_template =
266 v8::Handle<v8::ObjectTemplate>(),
267 v8::Handle<v8::Value> global_object = v8::Handle<v8::Value>()) {
268 Initialize(isolate, extensions, global_template, global_object);
271 LocalContext(v8::ExtensionConfiguration* extensions = 0,
272 v8::Handle<v8::ObjectTemplate> global_template =
273 v8::Handle<v8::ObjectTemplate>(),
274 v8::Handle<v8::Value> global_object = v8::Handle<v8::Value>()) {
275 Initialize(CcTest::isolate(), extensions, global_template, global_object);
278 virtual ~LocalContext() {
279 v8::HandleScope scope(isolate_);
280 v8::Local<v8::Context>::New(isolate_, context_)->Exit();
284 v8::Context* operator->() {
285 return *reinterpret_cast<v8::Context**>(&context_);
287 v8::Context* operator*() { return operator->(); }
288 bool IsReady() { return !context_.IsEmpty(); }
290 v8::Local<v8::Context> local() {
291 return v8::Local<v8::Context>::New(isolate_, context_);
295 void Initialize(v8::Isolate* isolate,
296 v8::ExtensionConfiguration* extensions,
297 v8::Handle<v8::ObjectTemplate> global_template,
298 v8::Handle<v8::Value> global_object) {
299 v8::HandleScope scope(isolate);
300 v8::Local<v8::Context> context = v8::Context::New(isolate,
304 context_.Reset(isolate, context);
306 // We can't do this later perhaps because of a fatal error.
310 v8::Persistent<v8::Context> context_;
311 v8::Isolate* isolate_;
314 static inline v8::Local<v8::Value> v8_num(double x) {
315 return v8::Number::New(v8::Isolate::GetCurrent(), x);
319 static inline v8::Local<v8::String> v8_str(const char* x) {
320 return v8::String::NewFromUtf8(v8::Isolate::GetCurrent(), x);
324 static inline v8::Local<v8::Script> v8_compile(const char* x) {
325 return v8::Script::Compile(v8_str(x));
329 static inline v8::Local<v8::Script> v8_compile(v8::Local<v8::String> x) {
330 return v8::Script::Compile(x);
334 static inline v8::Local<v8::Script> CompileWithOrigin(
335 v8::Local<v8::String> source, v8::Local<v8::String> origin_url) {
336 v8::ScriptOrigin origin(origin_url);
337 v8::ScriptCompiler::Source script_source(source, origin);
338 return v8::ScriptCompiler::Compile(
339 v8::Isolate::GetCurrent(), &script_source);
343 static inline v8::Local<v8::Script> CompileWithOrigin(
344 v8::Local<v8::String> source, const char* origin_url) {
345 return CompileWithOrigin(source, v8_str(origin_url));
349 static inline v8::Local<v8::Script> CompileWithOrigin(const char* source,
350 const char* origin_url) {
351 return CompileWithOrigin(v8_str(source), v8_str(origin_url));
355 // Helper functions that compile and run the source.
356 static inline v8::Local<v8::Value> CompileRun(const char* source) {
357 return v8::Script::Compile(v8_str(source))->Run();
361 static inline v8::Local<v8::Value> CompileRun(v8::Local<v8::String> source) {
362 return v8::Script::Compile(source)->Run();
366 static inline v8::Local<v8::Value> PreCompileCompileRun(const char* source) {
367 // Compile once just to get the preparse data, then compile the second time
369 v8::Isolate* isolate = v8::Isolate::GetCurrent();
370 v8::ScriptCompiler::Source script_source(v8_str(source));
371 v8::ScriptCompiler::Compile(isolate, &script_source,
372 v8::ScriptCompiler::kProduceDataToCache);
373 return v8::ScriptCompiler::Compile(isolate, &script_source)->Run();
377 // Helper functions that compile and run the source with given origin.
378 static inline v8::Local<v8::Value> CompileRunWithOrigin(const char* source,
379 const char* origin_url,
382 v8::Isolate* isolate = v8::Isolate::GetCurrent();
383 v8::ScriptOrigin origin(v8_str(origin_url),
384 v8::Integer::New(isolate, line_number),
385 v8::Integer::New(isolate, column_number));
386 v8::ScriptCompiler::Source script_source(v8_str(source), origin);
387 return v8::ScriptCompiler::Compile(isolate, &script_source)->Run();
391 static inline v8::Local<v8::Value> CompileRunWithOrigin(
392 v8::Local<v8::String> source, const char* origin_url) {
393 v8::ScriptCompiler::Source script_source(
394 source, v8::ScriptOrigin(v8_str(origin_url)));
395 return v8::ScriptCompiler::Compile(v8::Isolate::GetCurrent(), &script_source)
400 static inline v8::Local<v8::Value> CompileRunWithOrigin(
401 const char* source, const char* origin_url) {
402 return CompileRunWithOrigin(v8_str(source), origin_url);
406 // Pick a slightly different port to allow tests to be run in parallel.
407 static inline int FlagDependentPortOffset() {
408 return ::v8::internal::FLAG_crankshaft == false ? 100 :
409 ::v8::internal::FLAG_always_opt ? 200 : 0;
413 // Helper function that simulates a full new-space in the heap.
414 static inline void SimulateFullSpace(v8::internal::NewSpace* space) {
415 int new_linear_size = static_cast<int>(
416 *space->allocation_limit_address() - *space->allocation_top_address());
417 if (new_linear_size == 0) return;
418 v8::internal::AllocationResult allocation =
419 space->AllocateRaw(new_linear_size);
420 v8::internal::FreeListNode* node =
421 v8::internal::FreeListNode::cast(allocation.ToObjectChecked());
422 node->set_size(space->heap(), new_linear_size);
426 // Helper function that simulates a full old-space in the heap.
427 static inline void SimulateFullSpace(v8::internal::PagedSpace* space) {
428 space->EmptyAllocationInfo();
429 space->ResetFreeList();
434 // Helper class for new allocations tracking and checking.
435 // To use checking of JS allocations tracking in a test,
436 // just create an instance of this class.
437 class HeapObjectsTracker {
439 HeapObjectsTracker() {
440 heap_profiler_ = i::Isolate::Current()->heap_profiler();
441 CHECK_NE(NULL, heap_profiler_);
442 heap_profiler_->StartHeapObjectsTracking(true);
445 ~HeapObjectsTracker() {
446 i::Isolate::Current()->heap()->CollectAllAvailableGarbage();
447 CHECK_EQ(0, heap_profiler_->heap_object_map()->FindUntrackedObjects());
448 heap_profiler_->StopHeapObjectsTracking();
452 i::HeapProfiler* heap_profiler_;
456 #endif // ifndef CCTEST_H_