v8  3.14.5(node0.10.28)
V8 is Google's open source JavaScript engine
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
cctest.h
Go to the documentation of this file.
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
4 // met:
5 //
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.
15 //
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.
27 
28 #ifndef CCTEST_H_
29 #define CCTEST_H_
30 
31 #include "v8.h"
32 
33 #ifndef TEST
34 #define TEST(Name) \
35  static void Test##Name(); \
36  CcTest register_test_##Name(Test##Name, __FILE__, #Name, NULL, true); \
37  static void Test##Name()
38 #endif
39 
40 #ifndef DEPENDENT_TEST
41 #define DEPENDENT_TEST(Name, Dep) \
42  static void Test##Name(); \
43  CcTest register_test_##Name(Test##Name, __FILE__, #Name, #Dep, true); \
44  static void Test##Name()
45 #endif
46 
47 #ifndef DISABLED_TEST
48 #define DISABLED_TEST(Name) \
49  static void Test##Name(); \
50  CcTest register_test_##Name(Test##Name, __FILE__, #Name, NULL, false); \
51  static void Test##Name()
52 #endif
53 
54 class CcTest {
55  public:
56  typedef void (TestFunction)();
57  CcTest(TestFunction* callback, const char* file, const char* name,
58  const char* dependency, bool enabled);
59  void Run() { callback_(); }
60  static int test_count();
61  static CcTest* last() { return last_; }
62  CcTest* prev() { return prev_; }
63  const char* file() { return file_; }
64  const char* name() { return name_; }
65  const char* dependency() { return dependency_; }
66  bool enabled() { return enabled_; }
67  private:
68  TestFunction* callback_;
69  const char* file_;
70  const char* name_;
71  const char* dependency_;
72  bool enabled_;
73  static CcTest* last_;
74  CcTest* prev_;
75 };
76 
77 // Switches between all the Api tests using the threading support.
78 // In order to get a surprising but repeatable pattern of thread
79 // switching it has extra semaphores to control the order in which
80 // the tests alternate, not relying solely on the big V8 lock.
81 //
82 // A test is augmented with calls to ApiTestFuzzer::Fuzz() in its
83 // callbacks. This will have no effect when we are not running the
84 // thread fuzzing test. In the thread fuzzing test it will
85 // pseudorandomly select a successor thread and switch execution
86 // to that thread, suspending the current test.
88  public:
89  void CallTest();
90  explicit ApiTestFuzzer(int num)
91  : Thread("ApiTestFuzzer"),
92  test_number_(num),
93  gate_(v8::internal::OS::CreateSemaphore(0)),
94  active_(true) {
95  }
96  ~ApiTestFuzzer() { delete gate_; }
97 
98  // The ApiTestFuzzer is also a Thread, so it has a Run method.
99  virtual void Run();
100 
106 
107  static void SetUp(PartOfTest part);
108  static void RunAllTests();
109  static void TearDown();
110  // This method switches threads if we are running the Threading test.
111  // Otherwise it does nothing.
112  static void Fuzz();
113 
114  private:
115  static bool fuzzing_;
116  static int tests_being_run_;
117  static int current_;
118  static int active_tests_;
119  static bool NextThread();
120  int test_number_;
122  bool active_;
123  void ContextSwitch();
124  static int GetNextTestNumber();
125  static v8::internal::Semaphore* all_tests_done_;
126 };
127 
128 
129 #define THREADED_TEST(Name) \
130  static void Test##Name(); \
131  RegisterThreadedTest register_##Name(Test##Name, #Name); \
132  /* */ TEST(Name)
133 
134 
136  public:
138  const char* name)
139  : fuzzer_(NULL), callback_(callback), name_(name) {
140  prev_ = first_;
141  first_ = this;
142  count_++;
143  }
144  static int count() { return count_; }
145  static RegisterThreadedTest* nth(int i) {
146  CHECK(i < count());
147  RegisterThreadedTest* current = first_;
148  while (i > 0) {
149  i--;
150  current = current->prev_;
151  }
152  return current;
153  }
154  CcTest::TestFunction* callback() { return callback_; }
156  const char* name() { return name_; }
157 
158  private:
159  static RegisterThreadedTest* first_;
160  static int count_;
161  CcTest::TestFunction* callback_;
162  RegisterThreadedTest* prev_;
163  const char* name_;
164 };
165 
166 
167 // A LocalContext holds a reference to a v8::Context.
169  public:
171  v8::Handle<v8::ObjectTemplate> global_template =
174  : context_(v8::Context::New(extensions, global_template, global_object)) {
175  context_->Enter();
176  }
177 
178  virtual ~LocalContext() {
179  context_->Exit();
180  context_.Dispose();
181  }
182 
183  v8::Context* operator->() { return *context_; }
184  v8::Context* operator*() { return *context_; }
185  bool IsReady() { return !context_.IsEmpty(); }
186 
188  return v8::Local<v8::Context>::New(context_);
189  }
190 
191  private:
193 };
194 
195 
196 static inline v8::Local<v8::Value> v8_num(double x) {
197  return v8::Number::New(x);
198 }
199 
200 
201 static inline v8::Local<v8::String> v8_str(const char* x) {
202  return v8::String::New(x);
203 }
204 
205 
206 static inline v8::Local<v8::Script> v8_compile(const char* x) {
207  return v8::Script::Compile(v8_str(x));
208 }
209 
210 
211 // Helper function that compiles and runs the source.
212 static inline v8::Local<v8::Value> CompileRun(const char* source) {
213  return v8::Script::Compile(v8::String::New(source))->Run();
214 }
215 
216 
217 // Helper function that compiles and runs the source with given origin.
218 static inline v8::Local<v8::Value> CompileRunWithOrigin(const char* source,
219  const char* origin_url,
220  int line_number,
221  int column_number) {
222  v8::ScriptOrigin origin(v8::String::New(origin_url),
223  v8::Integer::New(line_number),
224  v8::Integer::New(column_number));
225  return v8::Script::Compile(v8::String::New(source), &origin)->Run();
226 }
227 
228 
229 // Pick a slightly different port to allow tests to be run in parallel.
230 static inline int FlagDependentPortOffset() {
231  return ::v8::internal::FLAG_crankshaft == false ? 100 :
232  ::v8::internal::FLAG_always_opt ? 200 : 0;
233 }
234 
235 
236 #endif // ifndef CCTEST_H_
static void RunAllTests()
Definition: test-api.cc:10693
virtual ~LocalContext()
Definition: cctest.h:178
static Local< Script > Compile(Handle< String > source, ScriptOrigin *origin=NULL, ScriptData *pre_data=NULL, Handle< String > script_data=Handle< String >())
Definition: api.cc:1568
static void TearDown()
Definition: test-api.cc:10725
void Dispose()
Definition: v8.h:4235
Thread(const Options &options)
static int test_count()
LocalContext(v8::ExtensionConfiguration *extensions=0, v8::Handle< v8::ObjectTemplate > global_template=v8::Handle< v8::ObjectTemplate >(), v8::Handle< v8::Value > global_object=v8::Handle< v8::Value >())
Definition: cctest.h:170
static V8EXPORT Local< String > New(const char *data, int length=-1)
Definition: api.cc:4779
const char * file()
Definition: cctest.h:63
v8::Context * operator*()
Definition: cctest.h:184
ApiTestFuzzer * fuzzer_
Definition: cctest.h:155
void( TestFunction)()
Definition: cctest.h:56
const char * dependency()
Definition: cctest.h:65
#define CHECK(condition)
Definition: checks.h:56
bool IsReady()
Definition: cctest.h:185
const char * name()
Definition: cctest.h:156
~ApiTestFuzzer()
Definition: cctest.h:96
static void Fuzz()
Definition: test-api.cc:10620
void Run()
Definition: cctest.h:59
RegisterThreadedTest(CcTest::TestFunction *callback, const char *name)
Definition: cctest.h:137
static Local< T > New(Handle< T > that)
Definition: v8.h:4193
virtual void Run()
Definition: test-api.cc:10648
Definition: v8.h:105
bool enabled()
Definition: cctest.h:66
const char * name()
Definition: cctest.h:64
static CcTest * last()
Definition: cctest.h:61
static RegisterThreadedTest * nth(int i)
Definition: cctest.h:145
CcTest * prev()
Definition: cctest.h:62
ApiTestFuzzer(int num)
Definition: cctest.h:90
static V8EXPORT Local< Integer > New(int32_t value)
Definition: api.cc:5228
activate correct semantics for inheriting readonliness enable harmony semantics for typeof enable harmony enable harmony proxies enable all harmony harmony_scoping harmony_proxies harmony_scoping tracks arrays with only smi values automatically unbox arrays of doubles use crankshaft use hydrogen range analysis use hydrogen global value numbering use function inlining maximum number of AST nodes considered for a single inlining loop invariant code motion print statistics for hydrogen trace generated IR for specified phases trace register allocator trace range analysis trace representation types environment for every instruction put a break point before deoptimizing polymorphic inlining perform array bounds checks elimination use dead code elimination trace on stack replacement optimize closures cache optimized code for closures functions with arguments object loop weight for representation inference allow uint32 values on optimize frames if they are used only in safe operations track parallel recompilation enable all profiler experiments number of stack frames inspected by the profiler call recompile stub directly when self optimizing trigger profiler ticks based on counting instead of timing weight back edges by jump distance for interrupt triggering percentage of ICs that must have type info to allow optimization watch_ic_patching retry_self_opt interrupt_at_exit extra verbose compilation tracing generate extra emit comments in code disassembly enable use of SSE3 instructions if available enable use of CMOV instruction if available enable use of SAHF instruction if enable use of VFP3 instructions if available this implies enabling ARMv7 and VFP2 enable use of VFP2 instructions if available enable use of SDIV and UDIV instructions if enable loading bit constant by means of movw movt instruction enable unaligned accesses for enable use of MIPS FPU instructions if NULL
static V8EXPORT Local< Number > New(double value)
Definition: api.cc:5215
bool IsEmpty() const
Definition: v8.h:209
CcTest::TestFunction * callback()
Definition: cctest.h:154
void CallTest()
Definition: test-api.cc:10759
v8::Local< v8::Context > local()
Definition: cctest.h:187
static void SetUp(PartOfTest part)
Definition: test-api.cc:10672
v8::Context * operator->()
Definition: cctest.h:183
Definition: v8.h:106
Definition: cctest.h:54
CcTest(TestFunction *callback, const char *file, const char *name, const char *dependency, bool enabled)
Definition: cctest.cc:36
static int count()
Definition: cctest.h:144