2014-06-11 07:36:04 +08:00
|
|
|
#include "util.h"
|
2010-12-11 16:49:38 +08:00
|
|
|
|
2014-06-11 07:36:04 +08:00
|
|
|
#include "string_bytes.h"
|
2010-12-11 16:49:38 +08:00
|
|
|
|
|
|
|
namespace node {
|
|
|
|
|
2015-01-08 06:13:35 +08:00
|
|
|
Utf8Value::Utf8Value(v8::Isolate* isolate, v8::Handle<v8::Value> value)
|
2014-10-22 09:29:32 +08:00
|
|
|
: length_(0), str_(nullptr) {
|
2014-06-11 07:36:04 +08:00
|
|
|
if (value.IsEmpty())
|
|
|
|
return;
|
2010-12-11 16:49:38 +08:00
|
|
|
|
2015-01-08 06:13:35 +08:00
|
|
|
v8::Local<v8::String> val_ = value->ToString(isolate);
|
2014-10-08 18:34:51 +08:00
|
|
|
if (val_.IsEmpty())
|
|
|
|
return;
|
2010-12-11 16:49:38 +08:00
|
|
|
|
2014-06-11 07:36:04 +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
|
|
|
|
2014-06-11 07:36:04 +08:00
|
|
|
length_ = val_->WriteUtf8(str,
|
|
|
|
len,
|
|
|
|
0,
|
|
|
|
flags);
|
|
|
|
|
|
|
|
str_ = reinterpret_cast<char*>(str);
|
|
|
|
}
|
|
|
|
} // namespace node
|