TeaSpeak-Client/native/dist/ext_nan/include/NanStrings.h

83 lines
2.7 KiB
C
Raw Normal View History

2019-11-24 10:24:22 -05:00
#pragma once
2019-11-24 12:38:50 -05:00
#include <nan.h>
2019-11-24 10:24:22 -05:00
#include <string_view>
#include <v8.h>
namespace Nan {
/* UTF-8 Helpers */
2019-11-24 12:38:50 -05:00
inline v8::Local<v8::String> LocalStringUTF8(std::string_view buffer) {
auto isolate = Nan::GetCurrentContext()->GetIsolate();
v8::EscapableHandleScope scope{isolate};
2019-12-07 16:27:20 -05:00
auto data = v8::String::NewFromUtf8(isolate, buffer.data(), v8::NewStringType::kNormal, (int) buffer.length());
2019-11-24 10:24:22 -05:00
v8::Local<v8::String> response{};
if(!data.ToLocal(&response))
throw std::bad_alloc{};
return scope.Escape(response);
}
2019-11-24 12:38:50 -05:00
inline v8::Local<v8::String> LocalStringUTF8(const std::string& buffer) {
return LocalStringUTF8(std::string_view{buffer});
}
inline v8::Local<v8::String> LocalStringUTF8(const char* buffer, size_t length) {
return Nan::LocalStringUTF8(std::string_view{buffer, length});
2019-11-24 10:24:22 -05:00
}
2019-11-24 12:38:50 -05:00
template <size_t S>
inline v8::Local<v8::String> LocalStringUTF8(const char (&buffer)[S]) {
return Nan::LocalStringUTF8((const char*) buffer, S - 1);
2019-11-24 10:24:22 -05:00
}
/* Latin1 Helpers */
2019-11-24 12:38:50 -05:00
inline v8::Local<v8::String> LocalString(std::string_view buffer) {
auto isolate = Nan::GetCurrentContext()->GetIsolate();
//v8::EscapableHandleScope scope{isolate};
2019-12-07 16:27:20 -05:00
auto data = v8::String::NewFromOneByte(isolate, (uint8_t*) buffer.data(), v8::NewStringType::kNormal, (int) buffer.length());
2019-11-24 10:24:22 -05:00
v8::Local<v8::String> response{};
if(!data.ToLocal(&response))
throw std::bad_alloc{};
2019-11-24 12:38:50 -05:00
return response;
//eturn scope.Escape(response);
2019-11-24 10:24:22 -05:00
}
2019-11-24 12:38:50 -05:00
inline v8::Local<v8::String> LocalString(const std::string& buffer) {
return LocalString(std::string_view{buffer});
2019-11-24 10:24:22 -05:00
}
2019-11-24 12:38:50 -05:00
inline v8::Local<v8::String> LocalString(const char* buffer, size_t length) {
return Nan::LocalString(std::string_view{buffer, length});
}
template <size_t S>
inline v8::Local<v8::String> LocalString(const char (&buffer)[S]) {
return Nan::LocalString((const char*) buffer, S - 1);
2019-11-24 10:24:22 -05:00
}
/* Wide char helpers */
2019-11-24 12:38:50 -05:00
inline v8::Local<v8::String> LocalString(std::wstring_view buffer) {
auto isolate = Nan::GetCurrentContext()->GetIsolate();
v8::EscapableHandleScope scope{isolate};
2019-12-07 16:27:20 -05:00
auto data = v8::String::NewFromTwoByte(isolate, (uint16_t*) buffer.data(), v8::NewStringType::kNormal, (int) buffer.length());
2019-11-24 10:24:22 -05:00
v8::Local<v8::String> response{};
if(!data.ToLocal(&response))
throw std::bad_alloc{};
return scope.Escape(response);
}
2019-11-24 12:38:50 -05:00
inline v8::Local<v8::String> LocalString(const std::wstring& buffer) {
return LocalString(std::wstring_view{buffer});
}
2019-11-24 10:24:22 -05:00
template <size_t S>
2019-11-24 12:38:50 -05:00
inline v8::Local<v8::String> LocalString(const wchar_t (&buffer)[S]) {
return Nan::LocalString(buffer, S - 1);
2019-11-24 10:24:22 -05:00
}
2019-11-24 12:38:50 -05:00
inline v8::Local<v8::String> LocalString(const wchar_t* buffer, size_t length) {
return Nan::LocalString(std::wstring_view{buffer, length});
2019-11-24 10:24:22 -05:00
}
}