Skip to content

Commit

Permalink
Introduce NanNewOneByteString
Browse files Browse the repository at this point in the history
  • Loading branch information
kkoopa committed May 26, 2015
1 parent bb3a553 commit ecd549e
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 53 deletions.
37 changes: 37 additions & 0 deletions nan.h
Original file line number Diff line number Diff line change
Expand Up @@ -639,6 +639,12 @@ class NanEscapableScope {

#if defined(V8_MAJOR_VERSION) && (V8_MAJOR_VERSION > 4 || \
(V8_MAJOR_VERSION == 4 && defined(V8_MINOR_VERSION) && V8_MINOR_VERSION >= 3))
NAN_INLINE NanMaybeLocal<v8::String>
NanNewOneByteString(const uint8_t * value, int length = -1) {
return v8::String::NewFromOneByte(v8::Isolate::GetCurrent(), value,
v8::NewStringType::kNormal, length);
}

NAN_INLINE NanMaybeLocal<NanBoundScript> NanCompileScript(
v8::Local<v8::String> s
, const v8::ScriptOrigin& origin
Expand Down Expand Up @@ -666,6 +672,15 @@ class NanEscapableScope {
return script->Run(NanGetCurrentContext());
}
#else
NAN_INLINE NanMaybeLocal<v8::String>
NanNewOneByteString(const uint8_t * value, int length = -1) {
return NanMaybeLocal<v8::String>(
v8::String::NewFromOneByte(
v8::Isolate::GetCurrent()
, value
, v8::String::kNormalString, length));
}

NAN_INLINE NanMaybeLocal<NanBoundScript> NanCompileScript(
v8::Local<v8::String> s
, const v8::ScriptOrigin& origin
Expand Down Expand Up @@ -1005,6 +1020,28 @@ class NanEscapableScope {
return function_template->HasInstance(value);
}

namespace Nan { namespace imp {
NAN_INLINE void
widenString(std::vector<uint16_t> *ws, const uint8_t *s, int l) {
size_t len = static_cast<size_t>(l);
if (l < 0) {
len = strlen(reinterpret_cast<const char*>(s));
}
assert(len <= INT_MAX && "string too long");
ws->resize(len);
std::copy(s, s + len, ws->begin());
}
} // end of namespace imp
} // end of namespace Nan

NAN_INLINE NanMaybeLocal<v8::String>
NanNewOneByteString(const uint8_t * value, int length = -1) {
std::vector<uint16_t> wideString;
Nan::imp::widenString(&wideString, value, length);
return Factory<v8::String>::return_t(v8::String::New(&wideString.front()
, static_cast<int>(wideString.size())));
}

NAN_INLINE NanMaybeLocal<NanBoundScript> NanCompileScript(
v8::Local<v8::String> s
, const v8::ScriptOrigin& origin
Expand Down
15 changes: 0 additions & 15 deletions nan_implementation_12_inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -294,12 +294,6 @@ Factory<v8::String>::New(std::string const& value) {
value.data(), v8::NewStringType::kNormal, static_cast<int>(value.size()));
}

Factory<v8::String>::return_t
Factory<v8::String>::New(const uint8_t * value, int length) {
return v8::String::NewFromOneByte(v8::Isolate::GetCurrent(), value,
v8::NewStringType::kNormal, length);
}

Factory<v8::String>::return_t
Factory<v8::String>::New(const uint16_t * value, int length) {
return v8::String::NewFromTwoByte(v8::Isolate::GetCurrent(), value,
Expand Down Expand Up @@ -337,15 +331,6 @@ Factory<v8::String>::New(std::string const& value) {
, static_cast<int>(value.size())));
}

Factory<v8::String>::return_t
Factory<v8::String>::New(const uint8_t * value, int length) {
return Factory<v8::String>::return_t(
v8::String::NewFromOneByte(
v8::Isolate::GetCurrent()
, value
, v8::String::kNormalString, length));
}

Factory<v8::String>::return_t
Factory<v8::String>::New(const uint16_t * value, int length) {
return Factory<v8::String>::return_t(
Expand Down
24 changes: 0 additions & 24 deletions nan_implementation_pre_12_inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -226,35 +226,11 @@ Factory<v8::String>::New(std::string const& value) {
v8::String::New( value.data(), static_cast<int>(value.size())));
}

inline
void
widenString(std::vector<uint16_t> *ws, const uint8_t *s, int l = -1) {
size_t len = static_cast<size_t>(l);
if (l < 0) {
len = strlen(reinterpret_cast<const char*>(s));
}
assert(len <= INT_MAX && "string too long");
ws->resize(len);
std::copy(s, s + len, ws->begin());
}

Factory<v8::String>::return_t
Factory<v8::String>::New(const uint16_t * value, int length) {
return Factory<v8::String>::return_t(v8::String::New(value, length));
}

Factory<v8::String>::return_t
Factory<v8::String>::New(const uint8_t * value, int length) {
std::vector<uint16_t> wideString;
widenString(&wideString, value, length);
if (wideString.size() == 0) {
return Factory<v8::String>::return_t(v8::String::Empty());
} else {
return Factory<v8::String>::return_t(v8::String::New(&wideString.front()
, static_cast<int>(wideString.size())));
}
}

Factory<v8::String>::return_t
Factory<v8::String>::New(v8::String::ExternalStringResource * value) {
return Factory<v8::String>::return_t(v8::String::NewExternal(value));
Expand Down
9 changes: 0 additions & 9 deletions nan_new.h
Original file line number Diff line number Diff line change
Expand Up @@ -173,9 +173,6 @@ struct Factory<v8::String> : MaybeFactoryBase<v8::String> {

static inline return_t New(v8::String::ExternalStringResource * value);
static inline return_t New(NanExternalOneByteStringResource * value);

// TODO(agnat): Deprecate.
static inline return_t New(const uint8_t * value, int length = -1);
};

template <>
Expand Down Expand Up @@ -316,12 +313,6 @@ NanNew(const char * value) {
return NanNew<v8::String>(value);
}

inline
Nan::imp::Factory<v8::String>::return_t
NanNew(const uint8_t * value) {
return NanNew<v8::String>(value);
}

inline
Nan::imp::Factory<v8::String>::return_t
NanNew(const uint16_t * value) {
Expand Down
2 changes: 1 addition & 1 deletion test/cpp/morenews.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ NAN_METHOD(NewUtf8String) {

NAN_METHOD(NewLatin1String) {
const uint8_t s[] = "str\xefng";
info.GetReturnValue().Set(NanNew(s).ToLocalChecked());
info.GetReturnValue().Set(NanNewOneByteString(s).ToLocalChecked());
}

NAN_METHOD(NewUcs2String) {
Expand Down
5 changes: 2 additions & 3 deletions test/cpp/nannew.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -301,12 +301,11 @@ NAN_METHOD(testString) {
t.ok(_( stringMatches( NanNew<String>().ToLocalChecked(), "")));
t.ok(_( assertType<String>( NanNew<String>().ToLocalChecked())));

// These should be deprecated
const uint8_t *ustring = reinterpret_cast<const uint8_t *>("unsigned chars");
t.ok(_( stringMatches(
NanNew<String>(ustring).ToLocalChecked(), "unsigned chars")));
NanNewOneByteString(ustring).ToLocalChecked(), "unsigned chars")));
t.ok(_( stringMatches(
NanNew<String>(ustring, 8).ToLocalChecked(), "unsigned")));
NanNewOneByteString(ustring, 8).ToLocalChecked(), "unsigned")));

// === Convenience

Expand Down
2 changes: 1 addition & 1 deletion test/cpp/news.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ NAN_METHOD(NewUtf8String) {

NAN_METHOD(NewLatin1String) {
const uint8_t s[] = "str\xefng";
info.GetReturnValue().Set(NanNew(s).ToLocalChecked());
info.GetReturnValue().Set(NanNewOneByteString(s).ToLocalChecked());
}

NAN_METHOD(NewUcs2String) {
Expand Down

0 comments on commit ecd549e

Please sign in to comment.