From 6dc72b690cae1a1b8a2a3b684051951a9faffa1f Mon Sep 17 00:00:00 2001 From: "ager@chromium.org" Date: Tue, 8 Jun 2010 06:20:49 +0000 Subject: [PATCH] Add an overloaded PreCompile method that takes a Handle. Code review URL: http://codereview.chromium.org/2633004/show git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@4815 ce2b1a6d-e550-0410-aec6-3dcde31c8c00 --- include/v8.h | 11 +++++++++++ src/api.cc | 6 ++++++ test/cctest/test-api.cc | 24 ++++++++++++++++++++++++ 3 files changed, 41 insertions(+) diff --git a/include/v8.h b/include/v8.h index fe01e30..eb6d401 100644 --- a/include/v8.h +++ b/include/v8.h @@ -513,6 +513,7 @@ class V8EXPORT Data { class V8EXPORT ScriptData { // NOLINT public: virtual ~ScriptData() { } + /** * Pre-compiles the specified script (context-independent). * @@ -522,6 +523,16 @@ class V8EXPORT ScriptData { // NOLINT static ScriptData* PreCompile(const char* input, int length); /** + * Pre-compiles the specified script (context-independent). + * + * NOTE: Pre-compilation using this method cannot happen on another thread + * without using Lockers. + * + * \param source Script source code. + */ + static ScriptData* PreCompile(Handle source); + + /** * Load previous pre-compilation data. * * \param data Pointer to data returned by a call to Data() of a previous diff --git a/src/api.cc b/src/api.cc index dfdd7bd..38b7032 100644 --- a/src/api.cc +++ b/src/api.cc @@ -1120,6 +1120,12 @@ ScriptData* ScriptData::PreCompile(const char* input, int length) { } +ScriptData* ScriptData::PreCompile(v8::Handle source) { + i::Handle str = Utils::OpenHandle(*source); + return i::PreParse(str, NULL, NULL); +} + + ScriptData* ScriptData::New(const char* data, int length) { // Return an empty ScriptData if the length is obviously invalid. if (length % sizeof(unsigned) != 0) { diff --git a/test/cctest/test-api.cc b/test/cctest/test-api.cc index b520e56..2676394 100644 --- a/test/cctest/test-api.cc +++ b/test/cctest/test-api.cc @@ -8497,6 +8497,30 @@ TEST(PreCompileDeserializationError) { } +// Verifies that the Handle and const char* versions of the API produce +// the same results (at least for one trivial case). +TEST(PreCompileAPIVariationsAreSame) { + v8::V8::Initialize(); + v8::HandleScope scope; + + const char* cstring = "function foo(a) { return a+1; }"; + v8::ScriptData* sd_from_cstring = + v8::ScriptData::PreCompile(cstring, i::StrLength(cstring)); + + TestAsciiResource* resource = new TestAsciiResource(cstring); + v8::ScriptData* sd_from_istring = v8::ScriptData::PreCompile( + v8::String::NewExternal(resource)); + + CHECK_EQ(sd_from_cstring->Length(), sd_from_istring->Length()); + CHECK_EQ(0, memcmp(sd_from_cstring->Data(), + sd_from_istring->Data(), + sd_from_cstring->Length())); + + delete sd_from_cstring; + delete sd_from_istring; +} + + // This tests that we do not allow dictionary load/call inline caches // to use functions that have not yet been compiled. The potential // problem of loading a function that has not yet been compiled can -- 2.7.4