2008-09-09 20:08:45 +00:00
|
|
|
// Copyright 2008 the V8 project authors. All rights reserved.
|
2008-08-22 13:33:59 +00:00
|
|
|
// 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_
|
|
|
|
|
2014-06-03 08:12:43 +00:00
|
|
|
#include "src/v8.h"
|
2009-11-04 08:51:48 +00:00
|
|
|
|
2014-07-30 13:54:45 +00:00
|
|
|
#include "src/isolate-inl.h"
|
|
|
|
|
2008-08-22 13:33:59 +00:00
|
|
|
#ifndef TEST
|
2013-09-19 07:33:45 +00:00
|
|
|
#define TEST(Name) \
|
|
|
|
static void Test##Name(); \
|
|
|
|
CcTest register_test_##Name(Test##Name, __FILE__, #Name, NULL, true, true); \
|
|
|
|
static void Test##Name()
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef UNINITIALIZED_TEST
|
|
|
|
#define UNINITIALIZED_TEST(Name) \
|
|
|
|
static void Test##Name(); \
|
|
|
|
CcTest register_test_##Name(Test##Name, __FILE__, #Name, NULL, true, false); \
|
2009-02-03 08:35:03 +00:00
|
|
|
static void Test##Name()
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef DEPENDENT_TEST
|
2013-09-19 07:33:45 +00:00
|
|
|
#define DEPENDENT_TEST(Name, Dep) \
|
|
|
|
static void Test##Name(); \
|
|
|
|
CcTest register_test_##Name(Test##Name, __FILE__, #Name, #Dep, true, true); \
|
2008-09-05 00:04:37 +00:00
|
|
|
static void Test##Name()
|
|
|
|
#endif
|
|
|
|
|
2014-09-19 08:01:35 +00:00
|
|
|
#ifndef UNINITIALIZED_DEPENDENT_TEST
|
|
|
|
#define UNINITIALIZED_DEPENDENT_TEST(Name, Dep) \
|
|
|
|
static void Test##Name(); \
|
|
|
|
CcTest register_test_##Name(Test##Name, __FILE__, #Name, #Dep, true, false); \
|
|
|
|
static void Test##Name()
|
|
|
|
#endif
|
|
|
|
|
2008-09-05 00:04:37 +00:00
|
|
|
#ifndef DISABLED_TEST
|
2013-09-19 07:33:45 +00:00
|
|
|
#define DISABLED_TEST(Name) \
|
|
|
|
static void Test##Name(); \
|
|
|
|
CcTest register_test_##Name(Test##Name, __FILE__, #Name, NULL, false, true); \
|
2008-08-22 13:33:59 +00:00
|
|
|
static void Test##Name()
|
|
|
|
#endif
|
|
|
|
|
2014-01-17 10:52:00 +00:00
|
|
|
#define EXTENSION_LIST(V) \
|
|
|
|
V(GC_EXTENSION, "v8/gc") \
|
|
|
|
V(PRINT_EXTENSION, "v8/print") \
|
|
|
|
V(PROFILER_EXTENSION, "v8/profiler") \
|
|
|
|
V(TRACE_EXTENSION, "v8/trace")
|
2013-04-10 08:29:39 +00:00
|
|
|
|
|
|
|
#define DEFINE_EXTENSION_ID(Name, Ident) Name##_ID,
|
|
|
|
enum CcTestExtensionIds {
|
|
|
|
EXTENSION_LIST(DEFINE_EXTENSION_ID)
|
|
|
|
kMaxExtensions
|
|
|
|
};
|
|
|
|
#undef DEFINE_EXTENSION_ID
|
|
|
|
|
|
|
|
typedef v8::internal::EnumSet<CcTestExtensionIds> CcTestExtensionFlags;
|
|
|
|
#define DEFINE_EXTENSION_FLAG(Name, Ident) \
|
|
|
|
static const CcTestExtensionFlags Name(1 << Name##_ID);
|
|
|
|
static const CcTestExtensionFlags NO_EXTENSIONS(0);
|
|
|
|
static const CcTestExtensionFlags ALL_EXTENSIONS((1 << kMaxExtensions) - 1);
|
|
|
|
EXTENSION_LIST(DEFINE_EXTENSION_FLAG)
|
|
|
|
#undef DEFINE_EXTENSION_FLAG
|
|
|
|
|
2013-09-11 07:14:41 +00:00
|
|
|
|
2014-04-29 13:58:55 +00:00
|
|
|
// Use this to expose protected methods in i::Heap.
|
|
|
|
class TestHeap : public i::Heap {
|
|
|
|
public:
|
2014-04-30 06:26:50 +00:00
|
|
|
using i::Heap::AllocateByteArray;
|
|
|
|
using i::Heap::AllocateFixedArray;
|
2014-04-29 13:58:55 +00:00
|
|
|
using i::Heap::AllocateHeapNumber;
|
|
|
|
using i::Heap::AllocateJSObject;
|
|
|
|
using i::Heap::AllocateJSObjectFromMap;
|
2014-04-30 06:26:50 +00:00
|
|
|
using i::Heap::AllocateMap;
|
2014-04-29 13:58:55 +00:00
|
|
|
using i::Heap::CopyCode;
|
2015-01-15 11:22:04 +00:00
|
|
|
using i::Heap::kInitialNumberStringCacheSize;
|
2014-04-29 13:58:55 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2008-08-22 13:33:59 +00:00
|
|
|
class CcTest {
|
|
|
|
public:
|
|
|
|
typedef void (TestFunction)();
|
2008-09-05 00:04:37 +00:00
|
|
|
CcTest(TestFunction* callback, const char* file, const char* name,
|
2013-09-19 07:33:45 +00:00
|
|
|
const char* dependency, bool enabled, bool initialize);
|
|
|
|
void Run();
|
2008-08-27 10:11:39 +00:00
|
|
|
static CcTest* last() { return last_; }
|
2008-08-22 13:33:59 +00:00
|
|
|
CcTest* prev() { return prev_; }
|
|
|
|
const char* file() { return file_; }
|
|
|
|
const char* name() { return name_; }
|
2009-02-03 08:35:03 +00:00
|
|
|
const char* dependency() { return dependency_; }
|
2008-09-05 00:04:37 +00:00
|
|
|
bool enabled() { return enabled_; }
|
2013-05-06 13:01:03 +00:00
|
|
|
|
2013-09-19 07:33:45 +00:00
|
|
|
static v8::Isolate* isolate() {
|
2013-09-20 10:52:20 +00:00
|
|
|
CHECK(isolate_ != NULL);
|
2014-10-08 09:34:40 +00:00
|
|
|
v8::base::NoBarrier_Store(&isolate_used_, 1);
|
2013-09-19 13:30:47 +00:00
|
|
|
return isolate_;
|
2013-09-19 07:33:45 +00:00
|
|
|
}
|
2013-04-10 08:29:39 +00:00
|
|
|
|
2014-07-30 13:54:45 +00:00
|
|
|
static i::Isolate* InitIsolateOnce() {
|
|
|
|
if (!initialize_called_) InitializeVM();
|
|
|
|
return i_isolate();
|
|
|
|
}
|
|
|
|
|
2013-09-05 08:48:34 +00:00
|
|
|
static i::Isolate* i_isolate() {
|
2013-09-19 07:33:45 +00:00
|
|
|
return reinterpret_cast<i::Isolate*>(isolate());
|
2013-09-05 08:48:34 +00:00
|
|
|
}
|
|
|
|
|
2013-09-19 09:46:15 +00:00
|
|
|
static i::Heap* heap() {
|
|
|
|
return i_isolate()->heap();
|
|
|
|
}
|
|
|
|
|
2014-04-29 13:58:55 +00:00
|
|
|
static TestHeap* test_heap() {
|
|
|
|
return reinterpret_cast<TestHeap*>(i_isolate()->heap());
|
|
|
|
}
|
|
|
|
|
2014-07-30 13:54:45 +00:00
|
|
|
static v8::base::RandomNumberGenerator* random_number_generator() {
|
|
|
|
return InitIsolateOnce()->random_number_generator();
|
|
|
|
}
|
|
|
|
|
2013-09-23 11:25:52 +00:00
|
|
|
static v8::Local<v8::Object> global() {
|
|
|
|
return isolate()->GetCurrentContext()->Global();
|
|
|
|
}
|
|
|
|
|
2013-09-19 13:30:47 +00:00
|
|
|
// TODO(dcarney): Remove.
|
|
|
|
// This must be called first in a test.
|
|
|
|
static void InitializeVM() {
|
2014-10-08 09:34:40 +00:00
|
|
|
CHECK(!v8::base::NoBarrier_Load(&isolate_used_));
|
2013-09-19 13:30:47 +00:00
|
|
|
CHECK(!initialize_called_);
|
|
|
|
initialize_called_ = true;
|
|
|
|
v8::HandleScope handle_scope(CcTest::isolate());
|
|
|
|
v8::Context::New(CcTest::isolate())->Enter();
|
|
|
|
}
|
|
|
|
|
2013-09-20 10:52:20 +00:00
|
|
|
// Only for UNINITIALIZED_TESTs
|
|
|
|
static void DisableAutomaticDispose();
|
|
|
|
|
2013-09-19 13:30:47 +00:00
|
|
|
// Helper function to configure a context.
|
|
|
|
// Must be in a HandleScope.
|
|
|
|
static v8::Local<v8::Context> NewContext(
|
|
|
|
CcTestExtensionFlags extensions,
|
|
|
|
v8::Isolate* isolate = CcTest::isolate());
|
2013-01-18 07:20:17 +00:00
|
|
|
|
2014-01-31 07:29:25 +00:00
|
|
|
static void TearDown() {
|
2014-02-04 12:23:30 +00:00
|
|
|
if (isolate_ != NULL) isolate_->Dispose();
|
2014-01-31 07:29:25 +00:00
|
|
|
}
|
|
|
|
|
2008-08-22 13:33:59 +00:00
|
|
|
private:
|
2013-05-06 13:01:03 +00:00
|
|
|
friend int main(int argc, char** argv);
|
2008-08-22 13:33:59 +00:00
|
|
|
TestFunction* callback_;
|
|
|
|
const char* file_;
|
|
|
|
const char* name_;
|
2009-02-03 08:35:03 +00:00
|
|
|
const char* dependency_;
|
2008-09-05 00:04:37 +00:00
|
|
|
bool enabled_;
|
2013-09-19 07:33:45 +00:00
|
|
|
bool initialize_;
|
2008-08-22 13:33:59 +00:00
|
|
|
CcTest* prev_;
|
2013-04-10 08:29:39 +00:00
|
|
|
static CcTest* last_;
|
2013-09-19 13:30:47 +00:00
|
|
|
static v8::Isolate* isolate_;
|
|
|
|
static bool initialize_called_;
|
2014-10-08 09:34:40 +00:00
|
|
|
static v8::base::Atomic32 isolate_used_;
|
2008-08-22 13:33:59 +00:00
|
|
|
};
|
|
|
|
|
2009-11-04 08:51:48 +00:00
|
|
|
// 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.
|
2014-06-30 13:25:46 +00:00
|
|
|
class ApiTestFuzzer: public v8::base::Thread {
|
2009-11-04 08:51:48 +00:00
|
|
|
public:
|
|
|
|
void CallTest();
|
|
|
|
|
|
|
|
// The ApiTestFuzzer is also a Thread, so it has a Run method.
|
|
|
|
virtual void Run();
|
|
|
|
|
2011-05-06 09:22:54 +00:00
|
|
|
enum PartOfTest { FIRST_PART,
|
|
|
|
SECOND_PART,
|
|
|
|
THIRD_PART,
|
|
|
|
FOURTH_PART,
|
|
|
|
LAST_PART = FOURTH_PART };
|
2009-11-04 08:51:48 +00:00
|
|
|
|
2012-01-13 13:09:52 +00:00
|
|
|
static void SetUp(PartOfTest part);
|
2009-11-04 08:51:48 +00:00
|
|
|
static void RunAllTests();
|
|
|
|
static void TearDown();
|
|
|
|
// This method switches threads if we are running the Threading test.
|
|
|
|
// Otherwise it does nothing.
|
|
|
|
static void Fuzz();
|
2011-09-08 19:57:14 +00:00
|
|
|
|
2009-11-04 08:51:48 +00:00
|
|
|
private:
|
2013-01-18 07:20:17 +00:00
|
|
|
explicit ApiTestFuzzer(int num)
|
2014-08-12 13:33:35 +00:00
|
|
|
: Thread(Options("ApiTestFuzzer")),
|
2013-01-18 07:20:17 +00:00
|
|
|
test_number_(num),
|
2013-09-02 12:26:06 +00:00
|
|
|
gate_(0),
|
2014-08-12 13:33:35 +00:00
|
|
|
active_(true) {}
|
2013-09-02 12:26:06 +00:00
|
|
|
~ApiTestFuzzer() {}
|
2013-01-18 07:20:17 +00:00
|
|
|
|
2009-11-04 08:51:48 +00:00
|
|
|
static bool fuzzing_;
|
|
|
|
static int tests_being_run_;
|
|
|
|
static int current_;
|
|
|
|
static int active_tests_;
|
|
|
|
static bool NextThread();
|
|
|
|
int test_number_;
|
2014-06-30 13:25:46 +00:00
|
|
|
v8::base::Semaphore gate_;
|
2009-11-04 08:51:48 +00:00
|
|
|
bool active_;
|
|
|
|
void ContextSwitch();
|
|
|
|
static int GetNextTestNumber();
|
2014-06-30 13:25:46 +00:00
|
|
|
static v8::base::Semaphore all_tests_done_;
|
2009-11-04 08:51:48 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
#define THREADED_TEST(Name) \
|
|
|
|
static void Test##Name(); \
|
|
|
|
RegisterThreadedTest register_##Name(Test##Name, #Name); \
|
|
|
|
/* */ TEST(Name)
|
|
|
|
|
|
|
|
|
|
|
|
class RegisterThreadedTest {
|
|
|
|
public:
|
|
|
|
explicit RegisterThreadedTest(CcTest::TestFunction* callback,
|
|
|
|
const char* name)
|
|
|
|
: fuzzer_(NULL), callback_(callback), name_(name) {
|
|
|
|
prev_ = first_;
|
|
|
|
first_ = this;
|
|
|
|
count_++;
|
|
|
|
}
|
|
|
|
static int count() { return count_; }
|
|
|
|
static RegisterThreadedTest* nth(int i) {
|
|
|
|
CHECK(i < count());
|
|
|
|
RegisterThreadedTest* current = first_;
|
|
|
|
while (i > 0) {
|
|
|
|
i--;
|
|
|
|
current = current->prev_;
|
|
|
|
}
|
|
|
|
return current;
|
|
|
|
}
|
|
|
|
CcTest::TestFunction* callback() { return callback_; }
|
|
|
|
ApiTestFuzzer* fuzzer_;
|
|
|
|
const char* name() { return name_; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
static RegisterThreadedTest* first_;
|
|
|
|
static int count_;
|
|
|
|
CcTest::TestFunction* callback_;
|
|
|
|
RegisterThreadedTest* prev_;
|
|
|
|
const char* name_;
|
|
|
|
};
|
|
|
|
|
|
|
|
// A LocalContext holds a reference to a v8::Context.
|
|
|
|
class LocalContext {
|
|
|
|
public:
|
2013-09-19 10:31:04 +00:00
|
|
|
LocalContext(v8::Isolate* isolate,
|
|
|
|
v8::ExtensionConfiguration* extensions = 0,
|
|
|
|
v8::Handle<v8::ObjectTemplate> global_template =
|
|
|
|
v8::Handle<v8::ObjectTemplate>(),
|
|
|
|
v8::Handle<v8::Value> global_object = v8::Handle<v8::Value>()) {
|
|
|
|
Initialize(isolate, extensions, global_template, global_object);
|
|
|
|
}
|
|
|
|
|
2009-11-04 08:51:48 +00:00
|
|
|
LocalContext(v8::ExtensionConfiguration* extensions = 0,
|
|
|
|
v8::Handle<v8::ObjectTemplate> global_template =
|
|
|
|
v8::Handle<v8::ObjectTemplate>(),
|
2013-05-08 07:45:16 +00:00
|
|
|
v8::Handle<v8::Value> global_object = v8::Handle<v8::Value>()) {
|
2013-09-19 10:31:04 +00:00
|
|
|
Initialize(CcTest::isolate(), extensions, global_template, global_object);
|
2009-11-04 08:51:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual ~LocalContext() {
|
2013-06-13 09:27:09 +00:00
|
|
|
v8::HandleScope scope(isolate_);
|
|
|
|
v8::Local<v8::Context>::New(isolate_, context_)->Exit();
|
2013-11-22 12:43:17 +00:00
|
|
|
context_.Reset();
|
2009-11-04 08:51:48 +00:00
|
|
|
}
|
|
|
|
|
2013-06-13 09:27:09 +00:00
|
|
|
v8::Context* operator->() {
|
|
|
|
return *reinterpret_cast<v8::Context**>(&context_);
|
|
|
|
}
|
|
|
|
v8::Context* operator*() { return operator->(); }
|
2009-11-04 08:51:48 +00:00
|
|
|
bool IsReady() { return !context_.IsEmpty(); }
|
|
|
|
|
|
|
|
v8::Local<v8::Context> local() {
|
2013-05-02 20:18:42 +00:00
|
|
|
return v8::Local<v8::Context>::New(isolate_, context_);
|
2009-11-04 08:51:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2013-09-19 10:31:04 +00:00
|
|
|
void Initialize(v8::Isolate* isolate,
|
|
|
|
v8::ExtensionConfiguration* extensions,
|
|
|
|
v8::Handle<v8::ObjectTemplate> global_template,
|
|
|
|
v8::Handle<v8::Value> global_object) {
|
|
|
|
v8::HandleScope scope(isolate);
|
|
|
|
v8::Local<v8::Context> context = v8::Context::New(isolate,
|
|
|
|
extensions,
|
|
|
|
global_template,
|
|
|
|
global_object);
|
|
|
|
context_.Reset(isolate, context);
|
|
|
|
context->Enter();
|
|
|
|
// We can't do this later perhaps because of a fatal error.
|
|
|
|
isolate_ = isolate;
|
|
|
|
}
|
|
|
|
|
2009-11-04 08:51:48 +00:00
|
|
|
v8::Persistent<v8::Context> context_;
|
2013-01-25 08:31:46 +00:00
|
|
|
v8::Isolate* isolate_;
|
2009-11-04 08:51:48 +00:00
|
|
|
};
|
|
|
|
|
2014-06-03 15:02:16 +00:00
|
|
|
|
|
|
|
static inline uint16_t* AsciiToTwoByteString(const char* source) {
|
|
|
|
int array_length = i::StrLength(source) + 1;
|
|
|
|
uint16_t* converted = i::NewArray<uint16_t>(array_length);
|
|
|
|
for (int i = 0; i < array_length; i++) converted[i] = source[i];
|
|
|
|
return converted;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-11-04 08:51:48 +00:00
|
|
|
static inline v8::Local<v8::Value> v8_num(double x) {
|
2014-01-03 14:31:17 +00:00
|
|
|
return v8::Number::New(v8::Isolate::GetCurrent(), x);
|
2009-11-04 08:51:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static inline v8::Local<v8::String> v8_str(const char* x) {
|
2013-11-22 12:43:17 +00:00
|
|
|
return v8::String::NewFromUtf8(v8::Isolate::GetCurrent(), x);
|
2009-11-04 08:51:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-02-03 21:05:04 +00:00
|
|
|
static inline v8::Local<v8::Symbol> v8_symbol(const char* name) {
|
|
|
|
return v8::Symbol::New(v8::Isolate::GetCurrent(), v8_str(name));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-11-04 08:51:48 +00:00
|
|
|
static inline v8::Local<v8::Script> v8_compile(const char* x) {
|
|
|
|
return v8::Script::Compile(v8_str(x));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-03-10 11:58:56 +00:00
|
|
|
static inline v8::Local<v8::Script> v8_compile(v8::Local<v8::String> x) {
|
|
|
|
return v8::Script::Compile(x);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-03-14 10:20:33 +00:00
|
|
|
static inline v8::Local<v8::Script> CompileWithOrigin(
|
|
|
|
v8::Local<v8::String> source, v8::Local<v8::String> origin_url) {
|
|
|
|
v8::ScriptOrigin origin(origin_url);
|
2014-03-19 13:24:13 +00:00
|
|
|
v8::ScriptCompiler::Source script_source(source, origin);
|
2014-03-14 10:20:33 +00:00
|
|
|
return v8::ScriptCompiler::Compile(
|
2014-03-19 13:24:13 +00:00
|
|
|
v8::Isolate::GetCurrent(), &script_source);
|
2014-03-10 11:58:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static inline v8::Local<v8::Script> CompileWithOrigin(
|
|
|
|
v8::Local<v8::String> source, const char* origin_url) {
|
2014-03-14 10:20:33 +00:00
|
|
|
return CompileWithOrigin(source, v8_str(origin_url));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static inline v8::Local<v8::Script> CompileWithOrigin(const char* source,
|
|
|
|
const char* origin_url) {
|
|
|
|
return CompileWithOrigin(v8_str(source), v8_str(origin_url));
|
2014-03-10 11:58:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Helper functions that compile and run the source.
|
2009-11-04 08:51:48 +00:00
|
|
|
static inline v8::Local<v8::Value> CompileRun(const char* source) {
|
2014-03-10 11:58:56 +00:00
|
|
|
return v8::Script::Compile(v8_str(source))->Run();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-02-06 17:52:20 +00:00
|
|
|
// Compiles source as an ES6 module.
|
|
|
|
static inline v8::Local<v8::Value> CompileRunModule(const char* source) {
|
|
|
|
v8::ScriptCompiler::Source script_source(v8_str(source));
|
|
|
|
return v8::ScriptCompiler::CompileModule(v8::Isolate::GetCurrent(),
|
|
|
|
&script_source)->Run();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-03-10 11:58:56 +00:00
|
|
|
static inline v8::Local<v8::Value> CompileRun(v8::Local<v8::String> source) {
|
|
|
|
return v8::Script::Compile(source)->Run();
|
2009-11-04 08:51:48 +00:00
|
|
|
}
|
|
|
|
|
2012-10-01 09:41:18 +00:00
|
|
|
|
Change ScriptCompiler::CompileOptions to allow for two 'cache' modes
(parser or code) and to be explicit about cache consumption or production
(rather than making presence of cached_data imply one or the other.)
Also add a --cache flag to d8, to allow testing the functionality.
-----------------------------
API change
Reason: Currently, V8 supports a 'parser cache' for repeatedly executing the same script. We'd like to add a 2nd mode that would cache code, and would like to let the embedder decide which mode they chose (if any).
Note: Previously, the 'use cached data' property was implied by the presence of the cached data itself. (That is, kNoCompileOptions and source->cached_data != NULL.) That is no longer sufficient, since the presence of data is no longer sufficient to determine /which kind/ of data is present.
Changes from old behaviour:
- If you previously didn't use caching, nothing changes.
Example:
v8::CompileUnbound(isolate, source, kNoCompileOptions);
- If you previously used caching, it worked like this:
- 1st run:
v8::CompileUnbound(isolate, source, kProduceToCache);
Then, source->cached_data would contain the
data-to-be cached. This remains the same, except you
need to tell V8 which type of data you want.
v8::CompileUnbound(isolate, source, kProduceParserCache);
- 2nd run:
v8::CompileUnbound(isolate, source, kNoCompileOptions);
with source->cached_data set to the data you received in
the first run. This will now ignore the cached data, and
you need to explicitly tell V8 to use it:
v8::CompileUnbound(isolate, source, kConsumeParserCache);
-----------------------------
BUG=
R=marja@chromium.org, yangguo@chromium.org
Review URL: https://codereview.chromium.org/389573006
git-svn-id: https://v8.googlecode.com/svn/branches/bleeding_edge@22431 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
2014-07-16 12:18:33 +00:00
|
|
|
static inline v8::Local<v8::Value> ParserCacheCompileRun(const char* source) {
|
2014-04-04 12:36:23 +00:00
|
|
|
// Compile once just to get the preparse data, then compile the second time
|
|
|
|
// using the data.
|
2014-03-14 10:20:33 +00:00
|
|
|
v8::Isolate* isolate = v8::Isolate::GetCurrent();
|
2014-04-04 12:36:23 +00:00
|
|
|
v8::ScriptCompiler::Source script_source(v8_str(source));
|
|
|
|
v8::ScriptCompiler::Compile(isolate, &script_source,
|
Change ScriptCompiler::CompileOptions to allow for two 'cache' modes
(parser or code) and to be explicit about cache consumption or production
(rather than making presence of cached_data imply one or the other.)
Also add a --cache flag to d8, to allow testing the functionality.
-----------------------------
API change
Reason: Currently, V8 supports a 'parser cache' for repeatedly executing the same script. We'd like to add a 2nd mode that would cache code, and would like to let the embedder decide which mode they chose (if any).
Note: Previously, the 'use cached data' property was implied by the presence of the cached data itself. (That is, kNoCompileOptions and source->cached_data != NULL.) That is no longer sufficient, since the presence of data is no longer sufficient to determine /which kind/ of data is present.
Changes from old behaviour:
- If you previously didn't use caching, nothing changes.
Example:
v8::CompileUnbound(isolate, source, kNoCompileOptions);
- If you previously used caching, it worked like this:
- 1st run:
v8::CompileUnbound(isolate, source, kProduceToCache);
Then, source->cached_data would contain the
data-to-be cached. This remains the same, except you
need to tell V8 which type of data you want.
v8::CompileUnbound(isolate, source, kProduceParserCache);
- 2nd run:
v8::CompileUnbound(isolate, source, kNoCompileOptions);
with source->cached_data set to the data you received in
the first run. This will now ignore the cached data, and
you need to explicitly tell V8 to use it:
v8::CompileUnbound(isolate, source, kConsumeParserCache);
-----------------------------
BUG=
R=marja@chromium.org, yangguo@chromium.org
Review URL: https://codereview.chromium.org/389573006
git-svn-id: https://v8.googlecode.com/svn/branches/bleeding_edge@22431 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
2014-07-16 12:18:33 +00:00
|
|
|
v8::ScriptCompiler::kProduceParserCache);
|
|
|
|
|
|
|
|
// Check whether we received cached data, and if so use it.
|
|
|
|
v8::ScriptCompiler::CompileOptions options =
|
|
|
|
script_source.GetCachedData() ? v8::ScriptCompiler::kConsumeParserCache
|
|
|
|
: v8::ScriptCompiler::kNoCompileOptions;
|
|
|
|
|
|
|
|
return v8::ScriptCompiler::Compile(isolate, &script_source, options)->Run();
|
2014-02-20 11:35:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-03-10 11:58:56 +00:00
|
|
|
// Helper functions that compile and run the source with given origin.
|
2012-09-21 08:09:34 +00:00
|
|
|
static inline v8::Local<v8::Value> CompileRunWithOrigin(const char* source,
|
|
|
|
const char* origin_url,
|
|
|
|
int line_number,
|
|
|
|
int column_number) {
|
2013-11-22 12:43:17 +00:00
|
|
|
v8::Isolate* isolate = v8::Isolate::GetCurrent();
|
2014-03-10 11:58:56 +00:00
|
|
|
v8::ScriptOrigin origin(v8_str(origin_url),
|
2014-01-03 14:31:17 +00:00
|
|
|
v8::Integer::New(isolate, line_number),
|
|
|
|
v8::Integer::New(isolate, column_number));
|
2014-03-19 13:24:13 +00:00
|
|
|
v8::ScriptCompiler::Source script_source(v8_str(source), origin);
|
|
|
|
return v8::ScriptCompiler::Compile(isolate, &script_source)->Run();
|
2014-03-13 08:54:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static inline v8::Local<v8::Value> CompileRunWithOrigin(
|
2014-03-14 10:20:33 +00:00
|
|
|
v8::Local<v8::String> source, const char* origin_url) {
|
2014-03-19 13:24:13 +00:00
|
|
|
v8::ScriptCompiler::Source script_source(
|
|
|
|
source, v8::ScriptOrigin(v8_str(origin_url)));
|
|
|
|
return v8::ScriptCompiler::Compile(v8::Isolate::GetCurrent(), &script_source)
|
|
|
|
->Run();
|
2014-03-14 10:20:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static inline v8::Local<v8::Value> CompileRunWithOrigin(
|
|
|
|
const char* source, const char* origin_url) {
|
|
|
|
return CompileRunWithOrigin(v8_str(source), origin_url);
|
2012-09-21 08:09:34 +00:00
|
|
|
}
|
|
|
|
|
2009-11-04 08:51:48 +00:00
|
|
|
|
2014-06-03 15:02:16 +00:00
|
|
|
|
|
|
|
static inline void ExpectString(const char* code, const char* expected) {
|
|
|
|
v8::Local<v8::Value> result = CompileRun(code);
|
|
|
|
CHECK(result->IsString());
|
|
|
|
v8::String::Utf8Value utf8(result);
|
2015-01-30 09:29:25 +00:00
|
|
|
CHECK_EQ(0, strcmp(expected, *utf8));
|
2014-06-03 15:02:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static inline void ExpectInt32(const char* code, int expected) {
|
|
|
|
v8::Local<v8::Value> result = CompileRun(code);
|
|
|
|
CHECK(result->IsInt32());
|
|
|
|
CHECK_EQ(expected, result->Int32Value());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static inline void ExpectBoolean(const char* code, bool expected) {
|
|
|
|
v8::Local<v8::Value> result = CompileRun(code);
|
|
|
|
CHECK(result->IsBoolean());
|
|
|
|
CHECK_EQ(expected, result->BooleanValue());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static inline void ExpectTrue(const char* code) {
|
|
|
|
ExpectBoolean(code, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static inline void ExpectFalse(const char* code) {
|
|
|
|
ExpectBoolean(code, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static inline void ExpectObject(const char* code,
|
|
|
|
v8::Local<v8::Value> expected) {
|
|
|
|
v8::Local<v8::Value> result = CompileRun(code);
|
|
|
|
CHECK(result->SameValue(expected));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static inline void ExpectUndefined(const char* code) {
|
|
|
|
v8::Local<v8::Value> result = CompileRun(code);
|
|
|
|
CHECK(result->IsUndefined());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-02-06 13:21:28 +00:00
|
|
|
// Helper function that simulates a full new-space in the heap.
|
2014-10-22 06:58:38 +00:00
|
|
|
static inline bool FillUpOnePage(v8::internal::NewSpace* space) {
|
2014-04-30 12:25:18 +00:00
|
|
|
v8::internal::AllocationResult allocation =
|
2014-10-22 06:58:38 +00:00
|
|
|
space->AllocateRaw(v8::internal::Page::kMaxRegularHeapObjectSize);
|
|
|
|
if (allocation.IsRetry()) return false;
|
2015-01-27 14:56:51 +00:00
|
|
|
v8::internal::HeapObject* free_space = NULL;
|
|
|
|
CHECK(allocation.To(&free_space));
|
|
|
|
space->heap()->CreateFillerObjectAt(
|
|
|
|
free_space->address(), v8::internal::Page::kMaxRegularHeapObjectSize);
|
2014-10-22 06:58:38 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-01-27 14:56:51 +00:00
|
|
|
// Helper function that simulates a fill new-space in the heap.
|
|
|
|
static inline void AllocateAllButNBytes(v8::internal::NewSpace* space,
|
|
|
|
int extra_bytes) {
|
|
|
|
int space_remaining = static_cast<int>(*space->allocation_limit_address() -
|
2015-01-27 09:41:10 +00:00
|
|
|
*space->allocation_top_address());
|
2015-01-27 14:56:51 +00:00
|
|
|
CHECK(space_remaining >= extra_bytes);
|
|
|
|
int new_linear_size = space_remaining - extra_bytes;
|
|
|
|
if (new_linear_size == 0) return;
|
|
|
|
v8::internal::AllocationResult allocation =
|
|
|
|
space->AllocateRaw(new_linear_size);
|
|
|
|
v8::internal::HeapObject* free_space = NULL;
|
|
|
|
CHECK(allocation.To(&free_space));
|
|
|
|
space->heap()->CreateFillerObjectAt(free_space->address(), new_linear_size);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static inline void FillCurrentPage(v8::internal::NewSpace* space) {
|
|
|
|
AllocateAllButNBytes(space, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static inline void SimulateFullSpace(v8::internal::NewSpace* space) {
|
|
|
|
FillCurrentPage(space);
|
|
|
|
while (FillUpOnePage(space)) {
|
2014-10-22 06:58:38 +00:00
|
|
|
}
|
2012-11-20 10:47:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Helper function that simulates a full old-space in the heap.
|
|
|
|
static inline void SimulateFullSpace(v8::internal::PagedSpace* space) {
|
2013-11-14 15:14:37 +00:00
|
|
|
space->EmptyAllocationInfo();
|
2012-11-20 10:47:31 +00:00
|
|
|
space->ResetFreeList();
|
|
|
|
space->ClearStats();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-08-05 07:29:13 +00:00
|
|
|
// Helper function that simulates many incremental marking steps until
|
|
|
|
// marking is completed.
|
|
|
|
static inline void SimulateIncrementalMarking(i::Heap* heap) {
|
|
|
|
i::MarkCompactCollector* collector = heap->mark_compact_collector();
|
|
|
|
i::IncrementalMarking* marking = heap->incremental_marking();
|
|
|
|
if (collector->sweeping_in_progress()) {
|
|
|
|
collector->EnsureSweepingCompleted();
|
|
|
|
}
|
|
|
|
CHECK(marking->IsMarking() || marking->IsStopped());
|
|
|
|
if (marking->IsStopped()) {
|
|
|
|
marking->Start();
|
|
|
|
}
|
|
|
|
CHECK(marking->IsMarking());
|
|
|
|
while (!marking->IsComplete()) {
|
|
|
|
marking->Step(i::MB, i::IncrementalMarking::NO_GC_VIA_STACK_GUARD);
|
|
|
|
}
|
|
|
|
CHECK(marking->IsComplete());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-10-14 12:41:28 +00:00
|
|
|
// Helper class for new allocations tracking and checking.
|
|
|
|
// To use checking of JS allocations tracking in a test,
|
|
|
|
// just create an instance of this class.
|
|
|
|
class HeapObjectsTracker {
|
|
|
|
public:
|
|
|
|
HeapObjectsTracker() {
|
|
|
|
heap_profiler_ = i::Isolate::Current()->heap_profiler();
|
2015-01-30 09:29:25 +00:00
|
|
|
CHECK_NOT_NULL(heap_profiler_);
|
2013-12-02 14:27:24 +00:00
|
|
|
heap_profiler_->StartHeapObjectsTracking(true);
|
2013-10-14 12:41:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
~HeapObjectsTracker() {
|
|
|
|
i::Isolate::Current()->heap()->CollectAllAvailableGarbage();
|
2013-12-03 09:48:30 +00:00
|
|
|
CHECK_EQ(0, heap_profiler_->heap_object_map()->FindUntrackedObjects());
|
2013-12-02 14:27:24 +00:00
|
|
|
heap_profiler_->StopHeapObjectsTracking();
|
2013-10-14 12:41:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
i::HeapProfiler* heap_profiler_;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2014-07-30 13:54:45 +00:00
|
|
|
class InitializedHandleScope {
|
|
|
|
public:
|
|
|
|
InitializedHandleScope()
|
|
|
|
: main_isolate_(CcTest::InitIsolateOnce()),
|
|
|
|
handle_scope_(main_isolate_) {}
|
|
|
|
|
|
|
|
// Prefixing the below with main_ reduces a lot of naming clashes.
|
|
|
|
i::Isolate* main_isolate() { return main_isolate_; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
i::Isolate* main_isolate_;
|
|
|
|
i::HandleScope handle_scope_;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class HandleAndZoneScope : public InitializedHandleScope {
|
|
|
|
public:
|
2015-01-23 15:19:34 +00:00
|
|
|
HandleAndZoneScope() {}
|
2014-07-30 13:54:45 +00:00
|
|
|
|
|
|
|
// Prefixing the below with main_ reduces a lot of naming clashes.
|
|
|
|
i::Zone* main_zone() { return &main_zone_; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
i::Zone main_zone_;
|
|
|
|
};
|
|
|
|
|
2008-08-22 13:33:59 +00:00
|
|
|
#endif // ifndef CCTEST_H_
|