Skip to content

Commit

Permalink
napi: String API improvements (nodejs#83)
Browse files Browse the repository at this point in the history
 - Make string API names consistent
 - Make string API coercion behavior consistent
 - Switch from number-remaining to number-copied results
 - Add UTF-16 string APIs
 - Add coercion APIs for number and bool
 - Update tests for API changes
  • Loading branch information
jasongin authored Feb 3, 2017
1 parent 6e70e77 commit 8c0d361
Show file tree
Hide file tree
Showing 18 changed files with 249 additions and 243 deletions.
24 changes: 10 additions & 14 deletions src/node_api_helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,28 +87,24 @@ namespace Napi {
public:
inline explicit Utf8String(napi_value from) :
length_(0), str_(str_st_) {
if (from != NULL) {
if (from != nullptr) {
napi_env env;
napi_get_current_env(&env);
napi_value string;
napi_value string = nullptr;
napi_coerce_to_string(env, from, &string);
if (string != NULL) {
int len;
napi_get_string_length(env, string, &len);
size_t utf8len = 3 * len + 1;
assert(utf8len <= INT_MAX);
if (utf8len > sizeof(str_st_)) {
str_ = new char[utf8len];
if (string != nullptr) {
napi_get_value_string_utf8_length(env, string, &length_);
int bufsize = length_ + 1;
if (static_cast<size_t>(bufsize) > sizeof(str_st_)) {
str_ = new char[bufsize];
assert(str_ != 0);
}
napi_get_string_utf8(env, string, str_,
static_cast<int>(utf8len), &length_);
str_[length_] = '\0';
napi_get_value_string_utf8(env, string, str_, bufsize, nullptr);
}
}
}

inline int length() const {
inline size_t length() const {
return length_;
}

Expand Down Expand Up @@ -431,7 +427,7 @@ namespace Napi {
napi_get_current_env(&env);

napi_value s;
napi_create_string(env, ErrorMessage(), &s);
napi_create_string_utf8(env, ErrorMessage(), -1, &s);

napi_value argv[1];
napi_create_error(env, s, argv);
Expand Down
Loading

0 comments on commit 8c0d361

Please sign in to comment.