0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2024-11-29 02:02:38 +01:00

ircd::js: Add id/value/string conversion convenience utils.

This commit is contained in:
Jason Volk 2016-10-14 21:14:18 -07:00
parent c7ea23bece
commit e6c6137fcb
4 changed files with 142 additions and 22 deletions

View file

@ -40,13 +40,10 @@ for_each(context &c,
const closure_mutable_key_val &closure)
{
for_each(c, obj, [&c, &obj, &closure]
(const JS::HandleId &id)
(const JS::HandleId &hid)
{
JS::RootedValue key(c), val(c);
if(!JS_IdToValue(c, id, &key))
return;
JS_GetPropertyById(c, obj, id, &val);
JS::RootedValue key(c, id(c, hid)), val(c);
JS_GetPropertyById(c, obj, hid, &val);
closure(key, &val);
});
}
@ -57,13 +54,10 @@ for_each(context &c,
const closure_key_val &closure)
{
for_each(c, obj, [&c, &obj, &closure]
(const JS::HandleId &id)
(const JS::HandleId &hid)
{
JS::RootedValue key(c), val(c);
if(!JS_IdToValue(c, id, &key))
return;
JS_GetPropertyById(c, obj, id, &val);
JS::RootedValue key(c, id(c, hid)), val(c);
JS_GetPropertyById(c, obj, hid, &val);
closure(key, val);
});
}

89
include/ircd/js/id.h Normal file
View file

@ -0,0 +1,89 @@
/*
* 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
#define HAVE_IRCD_JS_ID_H
namespace ircd {
namespace js {
JS::RootedValue id(context &, const jsid &);
const jsid &id(context &, const JSProtoKey &);
const jsid &id(context &, const uint32_t &);
const jsid &id(context &, const JS::HandleString &);
const jsid &id(context &, const JS::HandleValue &);
inline const jsid &
id(context &c,
const JS::HandleValue &h)
{
JS::RootedId ret(c);
if(!JS_ValueToId(c, h, &ret))
std::terminate(); //TODO: exception
return ret;
}
inline const jsid &
id(context &c,
const JS::HandleString &h)
{
JS::RootedId ret(c);
if(!JS_StringToId(c, h, &ret))
std::terminate(); //TODO: exception
return ret;
}
inline const jsid &
id(context &c,
const uint32_t &index)
{
JS::RootedId ret(c);
if(!JS_IndexToId(c, index, &ret))
std::terminate(); //TODO: exception
return ret;
}
inline const jsid &
id(context &c,
const JSProtoKey &key)
{
JS::RootedId ret(c);
JS::ProtoKeyToId(c, key, &ret);
return ret;
}
inline JS::RootedValue
id(context &c,
const jsid &h)
{
JS::RootedValue ret(c);
if(!JS_IdToValue(c, h, &ret))
std::terminate(); //TODO: exception
return { c, ret };
}
} // namespace js
} // namespace ircd

View file

@ -42,6 +42,10 @@
namespace ircd {
namespace js {
// Used for some convenience overloads which return a value already registered with
// the garbage collection rather than naked pointer.
IRCD_OVERLOAD(rooted)
// Extend this class to store your data with any priv(),
// i.e priv(runtime, privdata*) or priv(context, privdata*) etc
struct privdata
@ -59,6 +63,7 @@ JSVersion version(const char *const &v) { return JS_StringToVersion(v);
#include "context.h"
#include "request_guard.h"
#include "compartment_guard.h"
#include "id.h"
#include "string.h"
#include "for_each.h"
#include "script.h"

View file

@ -25,33 +25,65 @@
namespace ircd {
namespace js {
JS::RootedString string(context &, const std::string &);
std::string string(context &, JSString &);
std::string string(context &, const JS::HandleValue &);
// C++ --> JS
JSString &string(context &, const std::string &);
JS::RootedString string(context &, const std::string &, rooted_t);
// JS --> C++
std::string string(context &, const JSString &);
std::string string(context &, const JSString *const &);
std::string string(context &, const JS::Value &);
std::string string(context &, const jsid &);
inline std::string
string(context &c,
const JS::HandleValue &s)
const jsid &hid)
{
const auto jstr(JS::ToString(c, s));
return jstr? string(c, *jstr) : std::string{};
return string(c, id(c, hid));
}
inline std::string
string(context &c,
JSString &s)
const JS::Value &s)
{
std::string ret(JS_GetStringEncodingLength(c, &s), char());
JS_EncodeStringToBuffer(c, &s, &ret.front(), ret.size());
return s.isString()? string(c, s.toString()) : std::string{};
}
inline std::string
string(context &c,
const JSString *const &s)
{
return s? string(c, *s) : std::string{};
}
inline std::string
string(context &c,
const JSString &s)
{
std::string ret(JS_GetStringEncodingLength(c, const_cast<JSString *>(&s)), char());
JS_EncodeStringToBuffer(c, const_cast<JSString *>(&s), &ret.front(), ret.size());
return ret;
}
inline JS::RootedString
string(context &c,
const std::string &s,
rooted_t)
{
return { c, &string(c, s) };
}
inline JSString &
string(context &c,
const std::string &s)
{
return { c, JS_NewStringCopyN(c, s.data(), s.size()) };
const auto ret(JS_NewStringCopyN(c, s.data(), s.size()));
if(unlikely(!ret))
std::terminate(); //TODO: exception
return *ret;
}
} // namespace js