2016-10-19 02:17:29 +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
|
|
|
|
#define HAVE_IRCD_JS_VALUE_H
|
|
|
|
|
|
|
|
namespace ircd {
|
|
|
|
namespace js {
|
|
|
|
|
2016-10-26 19:34:58 +02:00
|
|
|
// Use Value to carry a non-gc host pointer value
|
|
|
|
template<class T> T *pointer_value(const JS::Value &);
|
|
|
|
JS::Value pointer_value(const void *const &);
|
|
|
|
JS::Value pointer_value(void *const &);
|
|
|
|
|
2016-10-19 02:17:29 +02:00
|
|
|
struct value
|
2016-11-25 09:15:27 +01:00
|
|
|
:root<JS::Value>
|
2016-10-19 02:17:29 +02:00
|
|
|
{
|
2016-10-29 14:15:36 +02:00
|
|
|
IRCD_OVERLOAD(pointer)
|
|
|
|
|
2016-10-20 02:05:58 +02:00
|
|
|
explicit operator std::string() const;
|
2016-10-19 02:17:29 +02:00
|
|
|
explicit operator double() const;
|
2016-10-20 02:05:58 +02:00
|
|
|
explicit operator uint64_t() const;
|
|
|
|
explicit operator int64_t() const;
|
|
|
|
explicit operator uint32_t() const;
|
|
|
|
explicit operator int32_t() const;
|
|
|
|
explicit operator uint16_t() const;
|
2016-10-24 07:40:45 +02:00
|
|
|
explicit operator bool() const;
|
2016-10-19 02:17:29 +02:00
|
|
|
|
2016-10-27 10:55:26 +02:00
|
|
|
template<class T> value(const JS::MutableHandle<T> &h);
|
|
|
|
template<class T> value(const JS::Handle<T> &h);
|
2016-10-29 14:15:36 +02:00
|
|
|
value(pointer_t, void *const &);
|
2016-10-27 10:55:26 +02:00
|
|
|
value(const std::string &);
|
2016-10-26 06:09:30 +02:00
|
|
|
value(const char *const &);
|
2016-10-23 03:18:02 +02:00
|
|
|
value(const nullptr_t &);
|
|
|
|
value(const double &);
|
|
|
|
value(const float &);
|
2016-10-31 20:55:10 +01:00
|
|
|
value(const uint64_t &);
|
2016-10-23 03:18:02 +02:00
|
|
|
value(const int32_t &);
|
|
|
|
value(const bool &);
|
2016-10-19 02:17:29 +02:00
|
|
|
value(const jsid &);
|
|
|
|
value(JSObject &);
|
2016-10-26 06:09:30 +02:00
|
|
|
value(JSObject *const &);
|
2016-10-19 02:17:29 +02:00
|
|
|
value(JSString *const &);
|
2016-10-23 06:08:04 +02:00
|
|
|
value(JSFunction *const &);
|
2016-10-19 02:17:29 +02:00
|
|
|
value(JS::Symbol *const &);
|
|
|
|
value(const JS::Value &);
|
|
|
|
value();
|
|
|
|
};
|
|
|
|
|
2016-11-25 09:15:27 +01:00
|
|
|
JSType type(const handle<value> &val);
|
|
|
|
JSType type(const value &val);
|
|
|
|
bool undefined(const handle<value> &val);
|
|
|
|
bool undefined(const value &val);
|
|
|
|
bool is_array(const handle<value> &val);
|
|
|
|
bool is_array(const value &val);
|
2016-11-25 03:21:46 +01:00
|
|
|
|
2016-11-25 09:15:27 +01:00
|
|
|
inline
|
|
|
|
value::value()
|
|
|
|
:value::root::type{JS::UndefinedValue()}
|
2016-11-25 03:21:46 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2016-11-25 09:15:27 +01:00
|
|
|
inline
|
|
|
|
value::value(const JS::Value &val)
|
|
|
|
:value::root::type{val}
|
2016-11-25 03:21:46 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2016-11-25 09:15:27 +01:00
|
|
|
inline
|
|
|
|
value::value(JS::Symbol *const &val)
|
|
|
|
:value::root::type{JS::SymbolValue(val)}
|
2016-11-25 03:21:46 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2016-11-25 09:15:27 +01:00
|
|
|
inline
|
|
|
|
value::value(JSObject *const &val)
|
|
|
|
:value::root::type
|
2016-10-26 06:09:30 +02:00
|
|
|
{
|
|
|
|
val? JS::ObjectValue(*val) : throw internal_error("NULL JSObject")
|
|
|
|
}
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2016-11-25 09:15:27 +01:00
|
|
|
inline
|
|
|
|
value::value(JSObject &val)
|
|
|
|
:value::root::type{JS::ObjectValue(val)}
|
2016-10-19 02:17:29 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2016-11-25 09:15:27 +01:00
|
|
|
inline
|
|
|
|
value::value(JSString *const &val)
|
|
|
|
:value::root::type{JS::StringValue(val)}
|
2016-10-19 02:17:29 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2016-11-25 09:15:27 +01:00
|
|
|
inline
|
|
|
|
value::value(JSFunction *const &val)
|
|
|
|
:value::root::type{}
|
2016-10-23 06:08:04 +02:00
|
|
|
{
|
|
|
|
auto *const obj(JS_GetFunctionObject(val));
|
|
|
|
if(unlikely(!obj))
|
|
|
|
throw type_error("Function cannot convert to Object");
|
|
|
|
|
2016-11-25 09:15:27 +01:00
|
|
|
(*this) = JS::ObjectValue(*obj);
|
2016-10-23 06:08:04 +02:00
|
|
|
}
|
|
|
|
|
2016-11-25 09:15:27 +01:00
|
|
|
inline
|
|
|
|
value::value(const jsid &val)
|
|
|
|
:value::root::type{}
|
2016-10-19 02:17:29 +02:00
|
|
|
{
|
|
|
|
if(!JS_IdToValue(*cx, val, &(*this)))
|
|
|
|
throw type_error("Failed to construct value from Id");
|
|
|
|
}
|
|
|
|
|
2016-11-25 09:15:27 +01:00
|
|
|
inline
|
|
|
|
value::value(const bool &b)
|
|
|
|
:value::root::type{JS::BooleanValue(b)}
|
2016-10-19 02:17:29 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2016-11-25 09:15:27 +01:00
|
|
|
inline
|
|
|
|
value::value(const int32_t &val)
|
|
|
|
:value::root::type{JS::Int32Value(val)}
|
2016-10-19 02:17:29 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2016-11-25 09:15:27 +01:00
|
|
|
inline
|
|
|
|
value::value(const uint64_t &val)
|
|
|
|
:value::root::type{JS::DoubleValue(val)}
|
2016-10-31 20:55:10 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2016-11-25 09:15:27 +01:00
|
|
|
inline
|
|
|
|
value::value(const float &val)
|
|
|
|
:value::root::type{JS::Float32Value(val)}
|
2016-10-19 02:17:29 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2016-11-25 09:15:27 +01:00
|
|
|
inline
|
|
|
|
value::value(const double &val)
|
|
|
|
:value::root::type{JS::DoubleValue(val)}
|
2016-10-19 02:17:29 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2016-11-25 09:15:27 +01:00
|
|
|
inline
|
|
|
|
value::value(const nullptr_t &)
|
|
|
|
:value::root::type{JS::NullValue()}
|
2016-10-19 02:17:29 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2016-11-25 09:15:27 +01:00
|
|
|
inline
|
|
|
|
value::value(const std::string &s)
|
|
|
|
:value::root::type{[&s]
|
2016-10-27 10:55:26 +02:00
|
|
|
{
|
2016-11-25 04:09:17 +01:00
|
|
|
if(s.empty())
|
2017-08-23 23:25:22 +02:00
|
|
|
return JS::StringValue(JS_GetEmptyString(*cx));
|
2016-11-25 04:09:17 +01:00
|
|
|
|
2016-10-27 10:55:26 +02:00
|
|
|
auto buf(native_external_copy(s));
|
2016-11-07 08:12:38 +01:00
|
|
|
const auto ret(JS_NewExternalString(*cx, buf.get(), s.size(), &native_external_delete));
|
|
|
|
buf.release();
|
2016-10-27 10:55:26 +02:00
|
|
|
return JS::StringValue(ret);
|
|
|
|
}()}
|
2016-10-19 02:17:29 +02:00
|
|
|
{
|
2016-10-20 02:05:58 +02:00
|
|
|
}
|
2016-10-27 10:55:26 +02:00
|
|
|
|
2016-11-25 09:15:27 +01:00
|
|
|
inline
|
|
|
|
value::value(const char *const &s)
|
|
|
|
:value::root::type{[&s]
|
2016-10-27 10:55:26 +02:00
|
|
|
{
|
2016-11-25 04:09:17 +01:00
|
|
|
if(!s || !*s)
|
2017-08-23 23:25:22 +02:00
|
|
|
return JS::StringValue(JS_GetEmptyString(*cx));
|
2016-11-25 04:09:17 +01:00
|
|
|
|
2016-10-27 10:55:26 +02:00
|
|
|
const auto len(strlen(s));
|
|
|
|
auto buf(native_external_copy(s, len));
|
2016-11-07 08:12:38 +01:00
|
|
|
const auto ret(JS_NewExternalString(*cx, buf.get(), len, &native_external_delete));
|
|
|
|
buf.release();
|
2016-10-27 10:55:26 +02:00
|
|
|
return JS::StringValue(ret);
|
|
|
|
}()}
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2016-11-25 09:15:27 +01:00
|
|
|
inline
|
|
|
|
value::value(pointer_t,
|
2017-08-23 23:04:45 +02:00
|
|
|
void *const &ptr)
|
2016-11-25 09:15:27 +01:00
|
|
|
:value::root::type{pointer_value(ptr)}
|
2016-10-29 14:15:36 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2016-10-27 10:55:26 +02:00
|
|
|
template<class T>
|
2016-11-25 09:15:27 +01:00
|
|
|
value::value(const JS::Handle<T> &h)
|
2016-10-27 10:55:26 +02:00
|
|
|
:value(h.get())
|
2016-10-20 02:05:58 +02:00
|
|
|
{
|
|
|
|
}
|
2016-10-19 02:17:29 +02:00
|
|
|
|
2016-10-27 10:55:26 +02:00
|
|
|
template<class T>
|
2016-11-25 09:15:27 +01:00
|
|
|
value::value(const JS::MutableHandle<T> &h)
|
2016-10-27 10:55:26 +02:00
|
|
|
:value(h.get())
|
2016-10-20 02:05:58 +02:00
|
|
|
{
|
|
|
|
}
|
2016-10-27 10:55:26 +02:00
|
|
|
|
2016-11-25 09:15:27 +01:00
|
|
|
inline
|
|
|
|
value::operator bool()
|
2016-10-24 07:40:45 +02:00
|
|
|
const
|
|
|
|
{
|
|
|
|
return JS::ToBoolean(*this);
|
|
|
|
}
|
|
|
|
|
2016-11-25 09:15:27 +01:00
|
|
|
inline
|
|
|
|
value::operator uint16_t()
|
2016-10-19 02:17:29 +02:00
|
|
|
const
|
|
|
|
{
|
2016-10-20 02:05:58 +02:00
|
|
|
uint16_t ret;
|
|
|
|
if(!JS::ToUint16(*cx, *this, &ret))
|
|
|
|
throw type_error("Failed cast to uint16_t");
|
2016-10-19 02:17:29 +02:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-11-25 09:15:27 +01:00
|
|
|
inline
|
|
|
|
value::operator int32_t()
|
2016-10-19 02:17:29 +02:00
|
|
|
const
|
|
|
|
{
|
|
|
|
int32_t ret;
|
|
|
|
if(!JS::ToInt32(*cx, *this, &ret))
|
|
|
|
throw type_error("Failed cast to int32_t");
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-11-25 09:15:27 +01:00
|
|
|
inline
|
|
|
|
value::operator uint32_t()
|
2016-10-19 02:17:29 +02:00
|
|
|
const
|
|
|
|
{
|
|
|
|
uint32_t ret;
|
|
|
|
if(!JS::ToUint32(*cx, *this, &ret))
|
|
|
|
throw type_error("Failed cast to uint32_t");
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-11-25 09:15:27 +01:00
|
|
|
inline
|
|
|
|
value::operator int64_t()
|
2016-10-19 02:17:29 +02:00
|
|
|
const
|
|
|
|
{
|
2016-10-20 02:05:58 +02:00
|
|
|
int64_t ret;
|
|
|
|
if(!JS::ToInt64(*cx, *this, &ret))
|
|
|
|
throw type_error("Failed cast to int64_t");
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-11-25 09:15:27 +01:00
|
|
|
inline
|
|
|
|
value::operator uint64_t()
|
2016-10-20 02:05:58 +02:00
|
|
|
const
|
|
|
|
{
|
|
|
|
uint64_t ret;
|
|
|
|
if(!JS::ToUint64(*cx, *this, &ret))
|
|
|
|
throw type_error("Failed cast to uint64_t");
|
2016-10-19 02:17:29 +02:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-11-25 09:15:27 +01:00
|
|
|
inline
|
|
|
|
value::operator double()
|
2016-10-20 02:05:58 +02:00
|
|
|
const
|
|
|
|
{
|
|
|
|
double ret;
|
|
|
|
if(!JS::ToNumber(*cx, *this, &ret))
|
|
|
|
throw type_error("Failed cast to double");
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-11-25 09:15:27 +01:00
|
|
|
inline
|
|
|
|
value::operator std::string()
|
2016-10-20 02:05:58 +02:00
|
|
|
const
|
|
|
|
{
|
|
|
|
const auto s(JS::ToString(*cx, *this));
|
|
|
|
return s? native(s) : throw type_error("Failed to cast to string");
|
|
|
|
}
|
|
|
|
|
2016-11-25 09:15:27 +01:00
|
|
|
inline bool
|
2016-11-01 15:21:30 +01:00
|
|
|
is_array(const value &val)
|
2016-11-25 09:15:27 +01:00
|
|
|
{
|
|
|
|
return is_array(handle<value>(val));
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool
|
|
|
|
is_array(const handle<value> &val)
|
2016-10-24 07:40:45 +02:00
|
|
|
{
|
2016-10-31 14:31:58 +01:00
|
|
|
bool ret;
|
|
|
|
if(!JS_IsArrayObject(*cx, val, &ret))
|
|
|
|
throw internal_error("Failed to query if value is array");
|
|
|
|
|
|
|
|
return ret;
|
2016-10-24 07:40:45 +02:00
|
|
|
}
|
2016-11-25 09:15:27 +01:00
|
|
|
|
|
|
|
inline bool
|
2016-11-01 15:21:30 +01:00
|
|
|
undefined(const value &val)
|
2016-10-24 07:40:45 +02:00
|
|
|
{
|
2016-11-25 09:15:27 +01:00
|
|
|
return type(val) == JSTYPE_VOID;
|
2016-10-24 07:40:45 +02:00
|
|
|
}
|
2016-11-25 09:15:27 +01:00
|
|
|
|
|
|
|
inline bool
|
|
|
|
undefined(const handle<value> &val)
|
|
|
|
{
|
|
|
|
return type(val) == JSTYPE_VOID;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline JSType
|
|
|
|
type(const value &val)
|
|
|
|
{
|
|
|
|
return JS_TypeOfValue(*cx, val);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline JSType
|
|
|
|
type(const handle<value> &val)
|
|
|
|
{
|
|
|
|
return JS_TypeOfValue(*cx, val);
|
|
|
|
}
|
|
|
|
|
2016-10-26 19:34:58 +02:00
|
|
|
inline JS::Value
|
|
|
|
pointer_value(const void *const &ptr)
|
|
|
|
{
|
|
|
|
return pointer_value(const_cast<void *>(ptr));
|
|
|
|
}
|
|
|
|
|
|
|
|
inline JS::Value
|
|
|
|
pointer_value(void *const &ptr)
|
|
|
|
{
|
|
|
|
JS::Value ret;
|
|
|
|
ret.setPrivate(ptr);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
T *
|
|
|
|
pointer_value(const JS::Value &val)
|
|
|
|
{
|
|
|
|
const auto ret(val.toPrivate());
|
|
|
|
return reinterpret_cast<T *>(ret);
|
|
|
|
}
|
|
|
|
|
2016-10-19 02:17:29 +02:00
|
|
|
} // namespace js
|
|
|
|
} // namespace ircd
|