18#ifndef INCLUDED_SCRIPTCONVERSIONS
19#define INCLUDED_SCRIPTCONVERSIONS
28#include <js/PropertyAndElement.h>
29#include <js/RootingAPI.h>
30#include <js/TypeDecls.h>
32#include <js/experimental/TypedData.h>
49 if (v.isNullOrUndefined())
57 out = std::move(value);
68template<
typename T>
void ToJSVal(
const Request& rq, JS::MutableHandleValue ret,
T const& val);
70template<std::
size_t N>
71void ToJSVal(
const Request& rq, JS::MutableHandleValue ret,
const char (&val)[N])
73 ToJSVal(rq, ret,
static_cast<const char*
>(val));
75template<std::
size_t N>
76void ToJSVal(
const Request& rq, JS::MutableHandleValue ret,
const wchar_t (&val)[N])
78 ToJSVal(rq, ret,
static_cast<const wchar_t*
>(val));
82inline void ToJSVal<JS::PersistentRootedValue>(
const Request&, JS::MutableHandleValue handle,
83 const JS::PersistentRootedValue& a)
89inline void ToJSVal<JS::Heap<JS::Value> >(
const Request&, JS::MutableHandleValue handle,
96inline void ToJSVal<JS::RootedValue>(
const Request&, JS::MutableHandleValue handle,
97 const JS::RootedValue& a)
103inline void ToJSVal<JS::HandleValue>(
const Request&, JS::MutableHandleValue handle,
104 const JS::HandleValue& a)
112template<
typename T>
inline bool FromJSProperty(
const Request& rq,
const JS::HandleValue val,
const char* name,
T& ret,
bool strict =
false)
117 JS::RootedObject obj(rq.
cx, &val.toObject());
120 if (!JS_HasProperty(rq.
cx, obj, name, &hasProperty) || !hasProperty)
123 JS::RootedValue value(rq.
cx);
124 if (!JS_GetProperty(rq.
cx, obj, name, &value))
127 if (strict && value.isNull())
133template<
typename T>
inline void ToJSVal_vector(
const Request& rq, JS::MutableHandleValue ret,
const std::vector<T>& val)
135 JS::RootedObject obj(rq.
cx, JS::NewArrayObject(rq.
cx, 0));
142 ENSURE(val.size() <= std::numeric_limits<std::uint32_t>::max());
143 for (std::uint32_t i = 0; i < val.size(); ++i)
145 JS::RootedValue el(rq.
cx);
146 ToJSVal<T>(rq, &el, val[i]);
147 JS_SetElement(rq.
cx, obj, i, el);
152#define FAIL(msg) STMT(Exception::Raise(rq, msg); return false)
156 JS::RootedObject obj(rq.
cx);
158 FAIL(
"Argument must be an array");
162 if ((!JS::IsArrayObject(rq.
cx, obj, &isArray) || !isArray) && !JS_IsTypedArrayObject(obj))
163 FAIL(
"Argument must be an array");
165 std::uint32_t length;
166 if (!JS::GetArrayLength(rq.
cx, obj, &length))
167 FAIL(
"Failed to get array length");
171 for (std::uint32_t i = 0; i < length; ++i)
173 JS::RootedValue el(rq.
cx);
174 if (!JS_GetElement(rq.
cx, obj, i, &el))
175 FAIL(
"Failed to read array element");
177 if (!FromJSVal<T>(rq, el, el2))
188#define JSVAL_VECTOR(T) \
190template<> void ToJSVal<std::vector<T> >(const Request& rq, JS::MutableHandleValue ret, const std::vector<T>& val) \
192 ToJSVal_vector(rq, ret, val); \
194template<> bool FromJSVal<std::vector<T> >(const Request& rq, JS::HandleValue v, std::vector<T>& out) \
196 return FromJSVal_vector(rq, v, out); \
#define FAIL(msg)
Definition: Conversions.h:152
JSContext * cx
Definition: Request.h:83
#define ENSURE(expr)
ensure the expression <expr> evaluates to non-zero.
Definition: debug.h:222
Abstraction around a SpiderMonkey JSContext.
Definition: DapInterface.h:32
bool FromJSVal_vector(const Request &rq, JS::HandleValue v, std::vector< T > &out)
Definition: Conversions.h:154
bool FromJSVal(const Request &rq, const JS::HandleValue val, T &ret)
Convert a JS::Value to a C++ type.
void ToJSVal(const Request &rq, JS::MutableHandleValue ret, T const &val)
Convert a C++ type to a JS::Value.
bool FromJSProperty(const Request &rq, const JS::HandleValue val, const char *name, T &ret, bool strict=false)
Convert a named property of an object to a C++ type.
Definition: Conversions.h:112
void ToJSVal_vector(const Request &rq, JS::MutableHandleValue ret, const std::vector< T > &val)
Definition: Conversions.h:133
#define T(string_literal)
Definition: secure_crt.cpp:81
static void out(const wchar_t *fmt,...)
Definition: wdbg_sym.cpp:407