Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(623)

Unified Diff: src/api.cc

Issue 12426015: create uniform string api (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Created 7 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « include/v8.h ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: src/api.cc
diff --git a/src/api.cc b/src/api.cc
index faba6de0a10f797faefa059ecfcf9fe51c3e4ff2..a7008cd4f29da37a66c5326e1c18b19d503e0935 100644
--- a/src/api.cc
+++ b/src/api.cc
@@ -5170,78 +5170,131 @@ Local<String> v8::String::Empty() {
}
-Local<String> v8::String::New(const char* data, int length) {
- i::Isolate* isolate = i::Isolate::Current();
- EnsureInitializedForIsolate(isolate, "v8::String::New()");
- LOG_API(isolate, "String::New(char)");
- if (length == 0) return Empty();
- ENTER_V8(isolate);
- if (length == -1) length = i::StrLength(data);
- i::Handle<i::String> result =
- isolate->factory()->NewStringFromUtf8(
- i::Vector<const char>(data, length));
- return Utils::ToLocal(result);
-}
-
+// anonymous namespace for string creation helper functions
+namespace {
-Local<String> v8::String::Concat(Handle<String> left, Handle<String> right) {
- i::Handle<i::String> left_string = Utils::OpenHandle(*left);
- i::Isolate* isolate = left_string->GetIsolate();
- EnsureInitializedForIsolate(isolate, "v8::String::New()");
- LOG_API(isolate, "String::New(char)");
- ENTER_V8(isolate);
- i::Handle<i::String> right_string = Utils::OpenHandle(*right);
- i::Handle<i::String> result = isolate->factory()->NewConsString(left_string,
- right_string);
- return Utils::ToLocal(result);
+inline int StringLength(const char* string) {
Yang 2013/03/22 12:46:05 use the INLINE macro if you *really* want to inlin
+ return i::StrLength(string);
}
-Local<String> v8::String::NewUndetectable(const char* data, int length) {
- i::Isolate* isolate = i::Isolate::Current();
- EnsureInitializedForIsolate(isolate, "v8::String::NewUndetectable()");
- LOG_API(isolate, "String::NewUndetectable(char)");
- ENTER_V8(isolate);
- if (length == -1) length = i::StrLength(data);
- i::Handle<i::String> result =
- isolate->factory()->NewStringFromUtf8(
- i::Vector<const char>(data, length));
- result->MarkAsUndetectable();
- return Utils::ToLocal(result);
+inline int StringLength(const uint8_t* string) {
+ return i::StrLength(reinterpret_cast<const char*>(string));
}
-static int TwoByteStringLength(const uint16_t* data) {
+inline int StringLength(const uint16_t* string) {
int length = 0;
- while (data[length] != '\0') length++;
+ while (string[length] != '\0')
+ length++;
Yang 2013/03/22 12:46:05 either brackets or no line break.
return length;
}
-Local<String> v8::String::New(const uint16_t* data, int length) {
- i::Isolate* isolate = i::Isolate::Current();
- EnsureInitializedForIsolate(isolate, "v8::String::New()");
- LOG_API(isolate, "String::New(uint16_)");
- if (length == 0) return Empty();
+inline i::Handle<i::String> NewString(i::Factory* factory,
+ String::NewStringType type,
+ i::Vector<const char> string) {
+ if (type ==String::kInternalizedString) {
+ return factory->InternalizeUtf8String(string);
+ }
+ return factory->NewStringFromUtf8(string);
+}
+
+
+inline i::Handle<i::String> NewString(i::Factory* factory,
+ String::NewStringType type,
+ i::Vector<const uint8_t> string) {
+ if (type == String::kInternalizedString) {
+ return factory->InternalizeOneByteString(string);
+ }
+ return factory->NewStringFromOneByte(string);
+}
+
+
+inline i::Handle<i::String> NewString(i::Factory* factory,
+ String::NewStringType type,
+ i::Vector<const uint16_t> string) {
+ if (type == String::kInternalizedString) {
+ return factory->InternalizeTwoByteString(string);
+ }
+ return factory->NewStringFromTwoByte(string);
+}
+
+
+template<typename Char>
+inline Local<String> NewString(Isolate* v8_isolate,
+ const char* location,
+ const char* env,
+ const Char* data,
+ String::NewStringType type,
+ int length) {
+ i::Isolate* isolate = reinterpret_cast<internal::Isolate*>(v8_isolate);
+ EnsureInitializedForIsolate(isolate, location);
+ LOG_API(isolate, env);
+ if (length == 0 && type != String::kUndetectableString) {
+ return String::Empty();
+ }
ENTER_V8(isolate);
- if (length == -1) length = TwoByteStringLength(data);
- i::Handle<i::String> result =
- isolate->factory()->NewStringFromTwoByte(
- i::Vector<const uint16_t>(data, length));
+ if (length == -1) length = StringLength(data);
Yang 2013/03/22 12:46:05 I don't have a strong opinion on this, but maybe w
+ i::Handle<i::String> result = NewString(
+ isolate->factory(), type, i::Vector<const Char>(data, length));
+ if (type == String::kUndetectableString) {
+ result->MarkAsUndetectable();
+ }
return Utils::ToLocal(result);
}
+} // anonymous namespace
-Local<String> v8::String::NewUndetectable(const uint16_t* data, int length) {
- i::Isolate* isolate = i::Isolate::Current();
- EnsureInitializedForIsolate(isolate, "v8::String::NewUndetectable()");
- LOG_API(isolate, "String::NewUndetectable(uint16_)");
+
+Local<String> String::NewFromUtf8(Isolate* isolate,
+ const char* data,
+ NewStringType type,
+ int length) {
+ return NewString(isolate,
+ "v8::String::NewFromUtf8()",
+ "String::NewFromUtf8",
+ data,
+ type,
+ length);
+}
+
+
+Local<String> String::NewFromOneByte(Isolate* isolate,
+ const uint8_t* data,
+ NewStringType type,
+ int length) {
+ return NewString(isolate,
+ "v8::String::NewFromOneByte()",
+ "String::NewFromOneByte",
+ data,
+ type,
+ length);
+}
+
+
+Local<String> String::NewFromTwoByte(Isolate* isolate,
+ const uint16_t* data,
+ NewStringType type,
+ int length) {
+ return NewString(isolate,
+ "v8::String::NewFromTwoByte()",
+ "String::NewFromTwoByte",
+ data,
+ type,
+ length);
+}
+
+
+Local<String> v8::String::Concat(Handle<String> left, Handle<String> right) {
+ i::Handle<i::String> left_string = Utils::OpenHandle(*left);
+ i::Isolate* isolate = left_string->GetIsolate();
+ EnsureInitializedForIsolate(isolate, "v8::String::New()");
+ LOG_API(isolate, "String::New(char)");
ENTER_V8(isolate);
- if (length == -1) length = TwoByteStringLength(data);
- i::Handle<i::String> result =
- isolate->factory()->NewStringFromTwoByte(
- i::Vector<const uint16_t>(data, length));
- result->MarkAsUndetectable();
+ i::Handle<i::String> right_string = Utils::OpenHandle(*right);
+ i::Handle<i::String> result = isolate->factory()->NewConsString(left_string,
+ right_string);
return Utils::ToLocal(result);
}
@@ -5597,18 +5650,6 @@ Local<Object> Array::CloneElementAt(uint32_t index) {
}
-Local<String> v8::String::NewSymbol(const char* data, int length) {
- i::Isolate* isolate = i::Isolate::Current();
- EnsureInitializedForIsolate(isolate, "v8::String::NewSymbol()");
- LOG_API(isolate, "String::NewSymbol(char)");
- ENTER_V8(isolate);
- if (length == -1) length = i::StrLength(data);
- i::Handle<i::String> result = isolate->factory()->InternalizeUtf8String(
- i::Vector<const char>(data, length));
- return Utils::ToLocal(result);
-}
-
-
Local<Number> v8::Number::New(double value) {
i::Isolate* isolate = i::Isolate::Current();
EnsureInitializedForIsolate(isolate, "v8::Number::New()");
« no previous file with comments | « include/v8.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698