From 8bd37cb7c3f67b6f50242a78488f2faa8aa978f0 Mon Sep 17 00:00:00 2001 From: "marja@chromium.org" Date: Mon, 10 Mar 2014 11:58:56 +0000 Subject: [PATCH] Refactor script compilation / running & use of helper funcs in test-api.cc. The tests were using different kind of constructs for achieving the same thing. This makes refactoring the compilation API more difficult than it should be. cctest.h already contained helpers for compiling and running scripts, but they were not used consistently. For example, all these were used for running scripts: v8::Script::Compile(v8_str("foo"))->Run(); v8::Script::Compile(v8::String::NewFromUtf8(isolate, "foo))->Run(); CompileRun(v8_str("foo")); CompileRun(v8::String::NewFromUtf8(some_way_to_get_isolate(), "foo")); v8::Local script = any_of_the_above; script->Run(); Most of the tests just want to run a script (which is in const char*) and don't care about how the v8::String is constructed or passed to the compiler API. Using the helpers makes the test more readable and reduces boilerplate code which is unrelated to what the test is testing. R=dcarney@chromium.org BUG= Review URL: https://codereview.chromium.org/190503002 git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@19753 ce2b1a6d-e550-0410-aec6-3dcde31c8c00 --- test/cctest/cctest.h | 43 ++++- test/cctest/test-api.cc | 483 +++++++++++++++++++++++------------------------- 2 files changed, 264 insertions(+), 262 deletions(-) diff --git a/test/cctest/cctest.h b/test/cctest/cctest.h index a4991a5..c4066d0 100644 --- a/test/cctest/cctest.h +++ b/test/cctest/cctest.h @@ -308,10 +308,33 @@ static inline v8::Local v8_compile(const char* x) { } -// Helper function that compiles and runs the source. +static inline v8::Local v8_compile(v8::Local x) { + return v8::Script::Compile(x); +} + + +static inline v8::Local CompileWithOrigin(const char* source, + const char* origin_url) { + v8::ScriptOrigin origin(v8_str(origin_url)); + return v8::Script::Compile(v8_str(source), &origin); +} + + +static inline v8::Local CompileWithOrigin( + v8::Local source, const char* origin_url) { + v8::ScriptOrigin origin(v8_str(origin_url)); + return v8::Script::Compile(source, &origin); +} + + +// Helper functions that compile and run the source. static inline v8::Local CompileRun(const char* source) { - return v8::Script::Compile( - v8::String::NewFromUtf8(v8::Isolate::GetCurrent(), source))->Run(); + return v8::Script::Compile(v8_str(source))->Run(); +} + + +static inline v8::Local CompileRun(v8::Local source) { + return v8::Script::Compile(source)->Run(); } @@ -327,17 +350,23 @@ static inline v8::Local PreCompileCompileRun(const char* source) { } -// Helper function that compiles and runs the source with given origin. +// Helper functions that compile and run the source with given origin. static inline v8::Local CompileRunWithOrigin(const char* source, const char* origin_url, int line_number, int column_number) { v8::Isolate* isolate = v8::Isolate::GetCurrent(); - v8::ScriptOrigin origin(v8::String::NewFromUtf8(isolate, origin_url), + v8::ScriptOrigin origin(v8_str(origin_url), v8::Integer::New(isolate, line_number), v8::Integer::New(isolate, column_number)); - return v8::Script::Compile(v8::String::NewFromUtf8(isolate, source), &origin) - ->Run(); + return v8::Script::Compile(v8_str(source), &origin)->Run(); +} + + +static inline v8::Local CompileRunWithOrigin( + const char* source, const char* origin_url) { + v8::ScriptOrigin origin(v8_str(origin_url)); + return v8::Script::Compile(v8_str(source), &origin)->Run(); } diff --git a/test/cctest/test-api.cc b/test/cctest/test-api.cc index 35f2d91..fbb6e56 100644 --- a/test/cctest/test-api.cc +++ b/test/cctest/test-api.cc @@ -204,9 +204,8 @@ THREADED_TEST(Handles) { CHECK(!undef.IsEmpty()); CHECK(undef->IsUndefined()); - const char* c_source = "1 + 2 + 3"; - Local source = String::NewFromUtf8(CcTest::isolate(), c_source); - Local