From e0b8ab8815f51f8c37a27301a50de0c10d709827 Mon Sep 17 00:00:00 2001 From: "iposva@chromium.org" Date: Wed, 14 Oct 2009 15:26:38 +0000 Subject: [PATCH] - Add String::Concat(Handle left, Handle 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. Review URL: http://codereview.chromium.org/271085 git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@3066 ce2b1a6d-e550-0410-aec6-3dcde31c8c00 --- include/v8.h | 6 ++++++ src/api.cc | 12 ++++++++++++ test/cctest/test-api.cc | 38 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 56 insertions(+) diff --git a/include/v8.h b/include/v8.h index 680c38711..d2f3f5eed 100644 --- a/include/v8.h +++ b/include/v8.h @@ -918,6 +918,12 @@ class V8EXPORT String : public Primitive { /** Creates a symbol. Returns one if it exists already.*/ static Local 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 Concat(Handle left, Handleright); + /** * Creates a new external string using the data defined in the given * resource. The resource is deleted when the external string is no diff --git a/src/api.cc b/src/api.cc index 68d097903..630fa8f4d 100644 --- a/src/api.cc +++ b/src/api.cc @@ -2926,6 +2926,18 @@ Local v8::String::New(const char* data, int length) { } +Local v8::String::Concat(Handle left, Handle right) { + EnsureInitialized("v8::String::New()"); + LOG_API("String::New(char)"); + ENTER_V8; + i::Handle left_string = Utils::OpenHandle(*left); + i::Handle right_string = Utils::OpenHandle(*right); + i::Handle result = i::Factory::NewConsString(left_string, + right_string); + return Utils::ToLocal(result); +} + + Local v8::String::NewUndetectable(const char* data, int length) { EnsureInitialized("v8::String::NewUndetectable()"); LOG_API("String::NewUndetectable(char)"); diff --git a/test/cctest/test-api.cc b/test/cctest/test-api.cc index a1f9e72bb..c63ba3165 100644 --- a/test/cctest/test-api.cc +++ b/test/cctest/test-api.cc @@ -572,6 +572,44 @@ THREADED_TEST(UsingExternalAsciiString) { } +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 left = v8_str(one_byte_string_1); + Local right = String::New(AsciiToTwoByteString(two_byte_string_1)); + Local 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