node/src/util.cc

32 lines
737 B
C++
Raw Normal View History

#include "util.h"
2010-12-11 16:49:38 +08:00
#include "string_bytes.h"
2010-12-11 16:49:38 +08:00
namespace node {
Utf8Value::Utf8Value(v8::Isolate* isolate, v8::Handle<v8::Value> value)
: length_(0), str_(nullptr) {
if (value.IsEmpty())
return;
2010-12-11 16:49:38 +08:00
v8::Local<v8::String> val_ = value->ToString(isolate);
if (val_.IsEmpty())
return;
2010-12-11 16:49:38 +08:00
// Allocate enough space to include the null terminator
size_t len = StringBytes::StorageSize(val_, UTF8) + 1;
char* str = static_cast<char*>(calloc(1, len));
int flags = WRITE_UTF8_FLAGS;
flags |= ~v8::String::NO_NULL_TERMINATION;
2010-12-11 16:49:38 +08:00
length_ = val_->WriteUtf8(str,
len,
0,
flags);
str_ = reinterpret_cast<char*>(str);
}
} // namespace node