diff options
Diffstat (limited to 'src/util.cc')
-rw-r--r-- | src/util.cc | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/src/util.cc b/src/util.cc index 903fbbba13..095e5582db 100644 --- a/src/util.cc +++ b/src/util.cc @@ -25,4 +25,27 @@ Utf8Value::Utf8Value(v8::Isolate* isolate, v8::Local<v8::Value> value) str_[length_] = '\0'; } + +TwoByteValue::TwoByteValue(v8::Isolate* isolate, v8::Local<v8::Value> value) + : length_(0), str_(str_st_) { + if (value.IsEmpty()) + return; + + v8::Local<v8::String> string = value->ToString(isolate); + if (string.IsEmpty()) + return; + + // Allocate enough space to include the null terminator + size_t len = StringBytes::StorageSize(isolate, string, UCS2) + 1; + if (len > sizeof(str_st_)) { + str_ = static_cast<uint16_t*>(malloc(len)); + CHECK_NE(str_, nullptr); + } + + const int flags = + v8::String::NO_NULL_TERMINATION | v8::String::REPLACE_INVALID_UTF8; + length_ = string->Write(str_, 0, len, flags); + str_[length_] = '\0'; +} + } // namespace node |