2016-10-14 06:51:43 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2016 Charybdis Development Team
|
|
|
|
* Copyright (C) 2016 Jason Volk <jason@zemos.net>
|
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice is present in all copies.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
|
|
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
|
|
|
|
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
|
|
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
|
|
|
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
|
|
|
|
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
2016-10-15 03:00:12 +02:00
|
|
|
#define HAVE_IRCD_JS_STRING_H
|
2016-10-14 06:51:43 +02:00
|
|
|
|
|
|
|
namespace ircd {
|
|
|
|
namespace js {
|
|
|
|
|
2016-10-15 06:14:18 +02:00
|
|
|
// C++ --> JS
|
2016-10-16 01:53:44 +02:00
|
|
|
JSString &string(const std::string &);
|
|
|
|
JS::RootedString string(const std::string &, rooted_t);
|
2016-10-15 06:14:18 +02:00
|
|
|
|
|
|
|
// JS --> C++
|
2016-10-16 01:53:44 +02:00
|
|
|
std::string string(const JSString &);
|
|
|
|
std::string string(const JSString *const &);
|
2016-10-15 06:14:18 +02:00
|
|
|
|
2016-10-16 01:53:44 +02:00
|
|
|
std::string string(const JS::Value &);
|
|
|
|
std::string string(const jsid &);
|
2016-10-14 06:51:43 +02:00
|
|
|
|
|
|
|
|
|
|
|
inline std::string
|
2016-10-16 01:53:44 +02:00
|
|
|
string(const jsid &hid)
|
2016-10-14 06:51:43 +02:00
|
|
|
{
|
2016-10-16 01:53:44 +02:00
|
|
|
return string(id(hid));
|
2016-10-14 06:51:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
inline std::string
|
2016-10-16 01:53:44 +02:00
|
|
|
string(const JS::Value &s)
|
2016-10-14 06:51:43 +02:00
|
|
|
{
|
2016-10-16 01:53:44 +02:00
|
|
|
return s.isString()? string(s.toString()) : std::string{};
|
2016-10-15 06:14:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
inline std::string
|
2016-10-16 01:53:44 +02:00
|
|
|
string(const JSString *const &s)
|
2016-10-15 06:14:18 +02:00
|
|
|
{
|
2016-10-16 01:53:44 +02:00
|
|
|
return s? string(*s) : std::string{};
|
2016-10-15 06:14:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
inline std::string
|
2016-10-16 01:53:44 +02:00
|
|
|
string(const JSString &s)
|
2016-10-15 06:14:18 +02:00
|
|
|
{
|
2016-10-16 01:53:44 +02:00
|
|
|
std::string ret(JS_GetStringEncodingLength(*cx, const_cast<JSString *>(&s)), char());
|
|
|
|
JS_EncodeStringToBuffer(*cx, const_cast<JSString *>(&s), &ret.front(), ret.size());
|
2016-10-14 06:51:43 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline JS::RootedString
|
2016-10-16 01:53:44 +02:00
|
|
|
string(const std::string &s,
|
2016-10-15 06:14:18 +02:00
|
|
|
rooted_t)
|
|
|
|
{
|
2016-10-16 01:53:44 +02:00
|
|
|
return { *cx, &string(s) };
|
2016-10-15 06:14:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
inline JSString &
|
2016-10-16 01:53:44 +02:00
|
|
|
string(const std::string &s)
|
2016-10-14 06:51:43 +02:00
|
|
|
{
|
2016-10-16 01:53:44 +02:00
|
|
|
const auto ret(JS_NewStringCopyN(*cx, s.data(), s.size()));
|
2016-10-15 06:14:18 +02:00
|
|
|
if(unlikely(!ret))
|
|
|
|
std::terminate(); //TODO: exception
|
|
|
|
|
|
|
|
return *ret;
|
2016-10-14 06:51:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace js
|
|
|
|
} // namespace ircd
|