mirror of
https://github.com/matrix-construct/construct
synced 2024-06-01 17:48:56 +02:00
ircd::js: Add string utf conversions; improve string related.
This commit is contained in:
parent
4cc0b16086
commit
dffb39f8cc
|
@ -333,6 +333,8 @@ RB_CHK_SYSHEADER([vector], [VECTOR])
|
|||
RB_CHK_SYSHEADER([forward_list], [FORWARD_LIST])
|
||||
RB_CHK_SYSHEADER([unordered_map], [UNORDERED_MAP])
|
||||
RB_CHK_SYSHEADER([string], [STRING])
|
||||
RB_CHK_SYSHEADER([locale], [LOCALE])
|
||||
RB_CHK_SYSHEADER([codecvt], [CODECVT])
|
||||
RB_CHK_SYSHEADER([sstream], [SSTREAM])
|
||||
RB_CHK_SYSHEADER([fstream], [FSTREAM])
|
||||
RB_CHK_SYSHEADER([iostream], [IOSTREAM])
|
||||
|
|
|
@ -25,9 +25,16 @@
|
|||
namespace ircd {
|
||||
namespace js {
|
||||
|
||||
// SpiderMonkey may use utf-16/char16_t strings; these will help you then
|
||||
std::string string_convert(const char16_t *const &);
|
||||
std::string string_convert(const std::u16string &);
|
||||
std::u16string string_convert(const char *const &);
|
||||
std::u16string string_convert(const std::string &);
|
||||
|
||||
// C++ --> JS
|
||||
JSString &string(const char *const &, const size_t &len);
|
||||
JSString &string(const char *const &);
|
||||
JSString &string(const std::string &);
|
||||
JS::RootedString string(const std::string &, rooted_t);
|
||||
|
||||
// JS --> C++
|
||||
std::string string(const JSString &);
|
||||
|
@ -73,7 +80,20 @@ string(const std::string &s,
|
|||
inline JSString &
|
||||
string(const std::string &s)
|
||||
{
|
||||
const auto ret(JS_NewStringCopyN(*cx, s.data(), s.size()));
|
||||
return string(s.data(), s.size());
|
||||
}
|
||||
|
||||
inline JSString &
|
||||
string(const char *const &s)
|
||||
{
|
||||
return string(s, strlen(s));
|
||||
}
|
||||
|
||||
inline JSString &
|
||||
string(const char *const &s,
|
||||
const size_t &len)
|
||||
{
|
||||
const auto ret(JS_NewStringCopyN(*cx, s, len));
|
||||
if(unlikely(!ret))
|
||||
std::terminate(); //TODO: exception
|
||||
|
||||
|
|
|
@ -70,6 +70,8 @@ extern "C" {
|
|||
#include <RB_INC_ARRAY
|
||||
#include <RB_INC_VECTOR
|
||||
#include <RB_INC_STRING
|
||||
#include <RB_INC_LOCALE
|
||||
#include <RB_INC_CODECVT
|
||||
#include <RB_INC_MAP
|
||||
#include <RB_INC_SET
|
||||
#include <RB_INC_LIST
|
||||
|
|
44
ircd/js.cc
44
ircd/js.cc
|
@ -380,6 +380,43 @@ ircd::js::trap::on_add(const JSObject &obj,
|
|||
return false;
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// ircd/js/string.h
|
||||
//
|
||||
|
||||
inline std::u16string
|
||||
ircd::js::string_convert(const std::string &s)
|
||||
{
|
||||
static std::wstring_convert<std::codecvt_utf8<char16_t>, char16_t> converter;
|
||||
|
||||
return converter.from_bytes(s);
|
||||
}
|
||||
|
||||
inline std::u16string
|
||||
ircd::js::string_convert(const char *const &s)
|
||||
{
|
||||
static std::wstring_convert<std::codecvt_utf8<char16_t>, char16_t> converter;
|
||||
|
||||
return s? converter.from_bytes(s) : std::u16string{};
|
||||
}
|
||||
|
||||
inline std::string
|
||||
ircd::js::string_convert(const std::u16string &s)
|
||||
{
|
||||
static std::wstring_convert<std::codecvt_utf8<char16_t>, char16_t> converter;
|
||||
|
||||
return converter.to_bytes(s);
|
||||
}
|
||||
|
||||
inline std::string
|
||||
ircd::js::string_convert(const char16_t *const &s)
|
||||
{
|
||||
static std::wstring_convert<std::codecvt_utf8<char16_t>, char16_t> converter;
|
||||
|
||||
return s? converter.to_bytes(s) : std::string{};
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// ircd/js/debug.h
|
||||
|
@ -426,10 +463,11 @@ ircd::js::debug(const JSErrorReport &r)
|
|||
if(r.exnType)
|
||||
ss << reflect(JSExnType(r.exnType)) << " ";
|
||||
|
||||
// ss << "\"" << std::u16string(r.ucmessage) << "\" ";
|
||||
if(r.ucmessage)
|
||||
ss << "\"" << string_convert(r.ucmessage) << "\" ";
|
||||
|
||||
// for(auto it(r.messageArgs); it && *it; ++it)
|
||||
// ss << "\"" << std::u16string(*it) << "\" ";
|
||||
for(auto it(r.messageArgs); it && *it; ++it)
|
||||
ss << "\"" << string_convert(*it) << "\" ";
|
||||
|
||||
return ss.str();
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue