Add an overloaded PreCompile method that takes a Handle<String>.
authorager@chromium.org <ager@chromium.org@ce2b1a6d-e550-0410-aec6-3dcde31c8c00>
Tue, 8 Jun 2010 06:20:49 +0000 (06:20 +0000)
committerager@chromium.org <ager@chromium.org@ce2b1a6d-e550-0410-aec6-3dcde31c8c00>
Tue, 8 Jun 2010 06:20:49 +0000 (06:20 +0000)
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
src/api.cc
test/cctest/test-api.cc

index fe01e30..eb6d401 100644 (file)
@@ -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<String> source);
+
+  /**
    * Load previous pre-compilation data.
    *
    * \param data Pointer to data returned by a call to Data() of a previous
index dfdd7bd..38b7032 100644 (file)
@@ -1120,6 +1120,12 @@ ScriptData* ScriptData::PreCompile(const char* input, int length) {
 }
 
 
+ScriptData* ScriptData::PreCompile(v8::Handle<String> source) {
+  i::Handle<i::String> 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) {
index b520e56..2676394 100644 (file)
@@ -8497,6 +8497,30 @@ TEST(PreCompileDeserializationError) {
 }
 
 
+// Verifies that the Handle<String> 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