| 29 | } |
| 30 | |
| 31 | std::pair<const char*, size_t> jsStringToUTF8(JSStringRef stringRef) { |
| 32 | if (stringRef == nullptr) { |
| 33 | return std::make_pair<const char*, size_t>(nullptr, 0); |
| 34 | } |
| 35 | |
| 36 | auto maxUtf8Size = JSStringGetMaximumUTF8CStringSize(stringRef); |
| 37 | |
| 38 | thread_local static std::vector<char> tBuffer; |
| 39 | auto& buffer = tBuffer; |
| 40 | |
| 41 | if (buffer.size() < maxUtf8Size) { |
| 42 | buffer.resize(std::max(maxUtf8Size * 2, static_cast<size_t>(1024))); |
| 43 | } |
| 44 | |
| 45 | auto written = JSStringGetUTF8CString(stringRef, buffer.data(), maxUtf8Size); |
| 46 | if (written == 0) { |
| 47 | return std::make_pair<const char*, size_t>(nullptr, 0); |
| 48 | } else { |
| 49 | return std::make_pair(buffer.data(), written - 1); |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | Valdi::StringBox jsStringtoStdString(JSStringRef stringRef) { |
| 54 | auto pair = jsStringToUTF8(stringRef); |
no test coverage detected