Reviewers: Christian Plesner Hansen,

Description:
- Add String::Concat(Handle<String> left, Handle<String> right) to the V8  
API.

This is the first step to address http://crbug.com/23131 by creating
a series of V8 ConsStrings as more data arrives from the server.


Please review this at http://codereview.chromium.org/271085

SVN Base: http://v8.googlecode.com/svn/branches/bleeding_edge/

Affected files:
   M     include/v8.h
   M     src/api.cc
   M     test/cctest/test-api.cc


Index: test/cctest/test-api.cc
===================================================================
--- test/cctest/test-api.cc     (revision 3058)
+++ test/cctest/test-api.cc     (working copy)
@@ -572,6 +572,44 @@
  }


+THREADED_TEST(StringConcat) {
+  {
+    v8::HandleScope scope;
+    LocalContext env;
+    const char* one_byte_string_1 = "function a_times_t";
+    const char* two_byte_string_1 = "wo_plus_b(a, b) {return ";
+    const char* one_byte_extern_1 = "a * 2 + b;} a_times_two_plus_b(4, 8)  
+ ";
+    const char* two_byte_extern_1 = "a_times_two_plus_b(4, 8) + ";
+    const char* one_byte_string_2 = "a_times_two_plus_b(4, 8) + ";
+    const char* two_byte_string_2 = "a_times_two_plus_b(4, 8) + ";
+    const char* two_byte_extern_2 = "a_times_two_plus_b(1, 2);";
+    Local<String> left = v8_str(one_byte_string_1);
+    Local<String> right =  
String::New(AsciiToTwoByteString(two_byte_string_1));
+    Local<String> source = String::Concat(left, right);
+    right = String::NewExternal(
+        new TestAsciiResource(i::StrDup(one_byte_extern_1)));
+    source = String::Concat(source, right);
+    right = String::NewExternal(
+        new TestResource(AsciiToTwoByteString(two_byte_extern_1)));
+    source = String::Concat(source, right);
+    right = v8_str(one_byte_string_2);
+    source = String::Concat(source, right);
+    right = String::New(AsciiToTwoByteString(two_byte_string_2));
+    source = String::Concat(source, right);
+    right = String::NewExternal(
+        new TestResource(AsciiToTwoByteString(two_byte_extern_2)));
+    source = String::Concat(source, right);
+    Local<Script> script = Script::Compile(source);
+    Local<Value> value = script->Run();
+    CHECK(value->IsNumber());
+    CHECK_EQ(68, value->Int32Value());
+  }
+  v8::internal::CompilationCache::Clear();
+  i::Heap::CollectAllGarbage(false);
+  i::Heap::CollectAllGarbage(false);
+}
+
+
  THREADED_TEST(GlobalProperties) {
    v8::HandleScope scope;
    LocalContext env;
Index: include/v8.h
===================================================================
--- include/v8.h        (revision 3058)
+++ include/v8.h        (working copy)
@@ -919,6 +919,12 @@
    static Local<String> NewSymbol(const char* data, int length = -1);

    /**
+   * Creates a new string by concatenating the left and the right strings
+   * passed in as parameters.
+   */
+  static Local<String> Concat(Handle<String> left, Handle<String>right);
+
+  /**
     * Creates a new external string using the data defined in the given
     * resource. The resource is deleted when the external string is no
     * longer live on V8's heap. The caller of this function should not
Index: src/api.cc
===================================================================
--- src/api.cc  (revision 3058)
+++ src/api.cc  (working copy)
@@ -2914,6 +2914,18 @@
  }


+Local<String> v8::String::Concat(Handle<String> left, Handle<String>  
right) {
+  EnsureInitialized("v8::String::New()");
+  LOG_API("String::New(char)");
+  ENTER_V8;
+  i::Handle<i::String> left_string = Utils::OpenHandle(*left);
+  i::Handle<i::String> right_string = Utils::OpenHandle(*right);
+  i::Handle<i::String> result = i::Factory::NewConsString(left_string,
+                                                          right_string);
+  return Utils::ToLocal(result);
+}
+
+
  Local<String> v8::String::NewUndetectable(const char* data, int length) {
    EnsureInitialized("v8::String::NewUndetectable()");
    LOG_API("String::NewUndetectable(char)");



--~--~---------~--~----~------------~-------~--~----~
v8-dev mailing list
[email protected]
http://groups.google.com/group/v8-dev
-~----------~----~----~----~------~----~------~--~---

Reply via email to