| 1168 | class Utf8String { |
| 1169 | public: |
| 1170 | inline explicit Utf8String(v8::Local<v8::Value> from) : |
| 1171 | length_(0), str_(str_st_) { |
| 1172 | HandleScope scope; |
| 1173 | if (!from.IsEmpty()) { |
| 1174 | #if NODE_MAJOR_VERSION >= 10 |
| 1175 | v8::Local<v8::Context> context = GetCurrentContext(); |
| 1176 | v8::Local<v8::String> string = |
| 1177 | from->ToString(context).FromMaybe(v8::Local<v8::String>()); |
| 1178 | #else |
| 1179 | v8::Local<v8::String> string = from->ToString(); |
| 1180 | #endif |
| 1181 | if (!string.IsEmpty()) { |
| 1182 | size_t len = 3 * string->Length() + 1; |
| 1183 | assert(len <= INT_MAX); |
| 1184 | if (len > sizeof (str_st_)) { |
| 1185 | str_ = static_cast<char*>(malloc(len)); |
| 1186 | assert(str_ != 0); |
| 1187 | } |
| 1188 | #if NODE_MAJOR_VERSION >= 11 |
| 1189 | #if defined(V8_MAJOR_VERSION) && (V8_MAJOR_VERSION > 13 || \ |
| 1190 | (V8_MAJOR_VERSION == 13 && defined(V8_MINOR_VERSION) && V8_MINOR_VERSION >= 4)) |
| 1191 | length_ = string->WriteUtf8V2(v8::Isolate::GetCurrent(), str_, |
| 1192 | static_cast<int>(len), imp::kReplaceInvalidUtf8); |
| 1193 | #else |
| 1194 | const int flags = |
| 1195 | v8::String::NO_NULL_TERMINATION | imp::kReplaceInvalidUtf8; |
| 1196 | length_ = string->WriteUtf8(v8::Isolate::GetCurrent(), str_, |
| 1197 | static_cast<int>(len), 0, flags); |
| 1198 | #endif |
| 1199 | |
| 1200 | #else |
| 1201 | // See https://github.com/nodejs/nan/issues/832. |
| 1202 | // Disable the warning as there is no way around it. |
| 1203 | #ifdef _MSC_VER |
| 1204 | #pragma warning(push) |
| 1205 | #pragma warning(disable : 4996) |
| 1206 | #endif |
| 1207 | #ifdef __GNUC__ |
| 1208 | #pragma GCC diagnostic push |
| 1209 | #pragma GCC diagnostic ignored "-Wdeprecated-declarations" |
| 1210 | #endif |
| 1211 | const int flags = |
| 1212 | v8::String::NO_NULL_TERMINATION | imp::kReplaceInvalidUtf8; |
| 1213 | length_ = string->WriteUtf8(str_, static_cast<int>(len), 0, flags); |
| 1214 | #ifdef __GNUC__ |
| 1215 | #pragma GCC diagnostic pop |
| 1216 | #endif |
| 1217 | #ifdef _MSC_VER |
| 1218 | #pragma warning(pop) |
| 1219 | #endif |
| 1220 | #endif // NODE_MAJOR_VERSION < 11 |
| 1221 | str_[length_] = '\0'; |
| 1222 | } |
| 1223 | } |
| 1224 | } |
| 1225 | |
| 1226 | inline int length() const { |
| 1227 | return length_; |
nothing calls this directly
no test coverage detected