create uniform string api
R=yangguo@chromium.org BUG= Review URL: https://chromiumcodereview.appspot.com/12426015 Patch from Dan Carney <dcarney@chromium.org>. git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@14379 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
This commit is contained in:
parent
4d1362720f
commit
555c78c513
68
include/v8.h
68
include/v8.h
@ -1326,22 +1326,48 @@ class V8EXPORT String : public Primitive {
|
||||
|
||||
V8_INLINE(static String* Cast(v8::Value* obj));
|
||||
|
||||
// TODO(dcarney): deprecate
|
||||
/**
|
||||
* Allocates a new string from either UTF-8 encoded or ASCII data.
|
||||
* The second parameter 'length' gives the buffer length. If omitted,
|
||||
* the function calls 'strlen' to determine the buffer length.
|
||||
*/
|
||||
static Local<String> New(const char* data, int length = -1);
|
||||
V8_INLINE(static Local<String> New(const char* data, int length = -1));
|
||||
|
||||
// TODO(dcarney): deprecate
|
||||
/** Allocates a new string from 16-bit character codes.*/
|
||||
static Local<String> New(const uint16_t* data, int length = -1);
|
||||
V8_INLINE(static Local<String> New(const uint16_t* data, int length = -1));
|
||||
|
||||
// TODO(dcarney): deprecate
|
||||
/**
|
||||
* Creates an internalized string (historically called a "symbol",
|
||||
* not to be confused with ES6 symbols). Returns one if it exists already.
|
||||
* TODO(rossberg): Deprecate me when the new string API is here.
|
||||
*/
|
||||
static Local<String> NewSymbol(const char* data, int length = -1);
|
||||
V8_INLINE(static Local<String> NewSymbol(const char* data, int length = -1));
|
||||
|
||||
enum NewStringType {
|
||||
kNormalString, kInternalizedString, kUndetectableString
|
||||
};
|
||||
|
||||
/** Allocates a new string from UTF-8 data.*/
|
||||
static Local<String> NewFromUtf8(Isolate* isolate,
|
||||
const char* data,
|
||||
NewStringType type = kNormalString,
|
||||
int length = -1);
|
||||
|
||||
/** Allocates a new string from Latin-1 data.*/
|
||||
static Local<String> NewFromOneByte(
|
||||
Isolate* isolate,
|
||||
const uint8_t* data,
|
||||
NewStringType type = kNormalString,
|
||||
int length = -1);
|
||||
|
||||
/** Allocates a new string from UTF-16 data.*/
|
||||
static Local<String> NewFromTwoByte(
|
||||
Isolate* isolate,
|
||||
const uint16_t* data,
|
||||
NewStringType type = kNormalString,
|
||||
int length = -1);
|
||||
|
||||
/**
|
||||
* Creates a new string by concatenating the left and the right strings
|
||||
@ -1396,11 +1422,15 @@ class V8EXPORT String : public Primitive {
|
||||
*/
|
||||
bool CanMakeExternal();
|
||||
|
||||
// TODO(dcarney): deprecate
|
||||
/** Creates an undetectable string from the supplied ASCII or UTF-8 data.*/
|
||||
static Local<String> NewUndetectable(const char* data, int length = -1);
|
||||
V8_INLINE(
|
||||
static Local<String> NewUndetectable(const char* data, int length = -1));
|
||||
|
||||
// TODO(dcarney): deprecate
|
||||
/** Creates an undetectable string from the supplied 16-bit character codes.*/
|
||||
static Local<String> NewUndetectable(const uint16_t* data, int length = -1);
|
||||
V8_INLINE(static Local<String> NewUndetectable(
|
||||
const uint16_t* data, int length = -1));
|
||||
|
||||
/**
|
||||
* Converts an object to a UTF-8-encoded character array. Useful if
|
||||
@ -4867,6 +4897,32 @@ Local<String> String::Empty(Isolate* isolate) {
|
||||
}
|
||||
|
||||
|
||||
Local<String> String::New(const char* data, int length) {
|
||||
return NewFromUtf8(Isolate::GetCurrent(), data, kNormalString, length);
|
||||
}
|
||||
|
||||
|
||||
Local<String> String::New(const uint16_t* data, int length) {
|
||||
return NewFromTwoByte(Isolate::GetCurrent(), data, kNormalString, length);
|
||||
}
|
||||
|
||||
|
||||
Local<String> String::NewSymbol(const char* data, int length) {
|
||||
return NewFromUtf8(Isolate::GetCurrent(), data, kInternalizedString, length);
|
||||
}
|
||||
|
||||
|
||||
Local<String> String::NewUndetectable(const char* data, int length) {
|
||||
return NewFromUtf8(Isolate::GetCurrent(), data, kUndetectableString, length);
|
||||
}
|
||||
|
||||
|
||||
Local<String> String::NewUndetectable(const uint16_t* data, int length) {
|
||||
return NewFromTwoByte(
|
||||
Isolate::GetCurrent(), data, kUndetectableString, length);
|
||||
}
|
||||
|
||||
|
||||
String::ExternalStringResource* String::GetExternalStringResource() const {
|
||||
typedef internal::Object O;
|
||||
typedef internal::Internals I;
|
||||
|
181
src/api.cc
181
src/api.cc
@ -5289,19 +5289,121 @@ 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();
|
||||
// anonymous namespace for string creation helper functions
|
||||
namespace {
|
||||
|
||||
inline int StringLength(const char* string) {
|
||||
return i::StrLength(string);
|
||||
}
|
||||
|
||||
|
||||
inline int StringLength(const uint8_t* string) {
|
||||
return i::StrLength(reinterpret_cast<const char*>(string));
|
||||
}
|
||||
|
||||
|
||||
inline int StringLength(const uint16_t* string) {
|
||||
int length = 0;
|
||||
while (string[length] != '\0')
|
||||
length++;
|
||||
return length;
|
||||
}
|
||||
|
||||
|
||||
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 = i::StrLength(data);
|
||||
i::Handle<i::String> result =
|
||||
isolate->factory()->NewStringFromUtf8(
|
||||
i::Vector<const char>(data, length));
|
||||
if (length == -1) length = StringLength(data);
|
||||
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> 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);
|
||||
@ -5316,55 +5418,6 @@ Local<String> v8::String::Concat(Handle<String> left, Handle<String> right) {
|
||||
}
|
||||
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
static int TwoByteStringLength(const uint16_t* data) {
|
||||
int length = 0;
|
||||
while (data[length] != '\0') length++;
|
||||
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();
|
||||
ENTER_V8(isolate);
|
||||
if (length == -1) length = TwoByteStringLength(data);
|
||||
i::Handle<i::String> result =
|
||||
isolate->factory()->NewStringFromTwoByte(
|
||||
i::Vector<const uint16_t>(data, length));
|
||||
return Utils::ToLocal(result);
|
||||
}
|
||||
|
||||
|
||||
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_)");
|
||||
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();
|
||||
return Utils::ToLocal(result);
|
||||
}
|
||||
|
||||
|
||||
i::Handle<i::String> NewExternalStringHandle(i::Isolate* isolate,
|
||||
v8::String::ExternalStringResource* resource) {
|
||||
i::Handle<i::String> result =
|
||||
@ -5739,18 +5792,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<Symbol> v8::Symbol::New(Isolate* isolate) {
|
||||
i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
|
||||
EnsureInitializedForIsolate(i_isolate, "v8::Symbol::New()");
|
||||
|
Loading…
Reference in New Issue
Block a user