18#ifndef INCLUDED_SCRIPTCONVERSIONS
19#define INCLUDED_SCRIPTCONVERSIONS
45inline void ToJSVal<JS::PersistentRootedValue>(
const ScriptRequest&
UNUSED(rq), JS::MutableHandleValue handle,
const JS::PersistentRootedValue& a)
51inline void ToJSVal<JS::Heap<JS::Value> >(
const ScriptRequest&
UNUSED(rq), JS::MutableHandleValue handle,
const JS::Heap<JS::Value>& a)
57inline void ToJSVal<JS::RootedValue>(
const ScriptRequest&
UNUSED(rq), JS::MutableHandleValue handle,
const JS::RootedValue& a)
63inline void ToJSVal<JS::HandleValue>(
const ScriptRequest&
UNUSED(rq), JS::MutableHandleValue handle,
const JS::HandleValue& a)
76 JS::RootedObject obj(rq.
cx, &val.toObject());
79 if (!JS_HasProperty(rq.
cx, obj, name, &hasProperty) || !hasProperty)
82 JS::RootedValue value(rq.
cx);
83 if (!JS_GetProperty(rq.
cx, obj, name, &value))
86 if (strict && value.isNull())
94 JS::RootedObject obj(rq.
cx, JS::NewArrayObject(rq.
cx, 0));
101 ENSURE(val.size() <= std::numeric_limits<u32>::max());
102 for (
u32 i = 0; i < val.size(); ++i)
104 JS::RootedValue el(rq.
cx);
105 Script::ToJSVal<T>(rq, &el, val[i]);
106 JS_SetElement(rq.
cx, obj, i, el);
111#define FAIL(msg) STMT(ScriptException::Raise(rq, msg); return false)
115 JS::RootedObject obj(rq.
cx);
117 FAIL(
"Argument must be an array");
121 if ((!JS::IsArrayObject(rq.
cx, obj, &isArray) || !isArray) && !JS_IsTypedArrayObject(obj))
122 FAIL(
"Argument must be an array");
125 if (!JS::GetArrayLength(rq.
cx, obj, &length))
126 FAIL(
"Failed to get array length");
130 for (
u32 i = 0; i < length; ++i)
132 JS::RootedValue el(rq.
cx);
133 if (!JS_GetElement(rq.
cx, obj, i, &el))
134 FAIL(
"Failed to read array element");
136 if (!Script::FromJSVal<T>(rq, el, el2))
145#define JSVAL_VECTOR(T) \
146template<> void Script::ToJSVal<std::vector<T> >(const ScriptRequest& rq, JS::MutableHandleValue ret, const std::vector<T>& val) \
148 ToJSVal_vector(rq, ret, val); \
150template<> bool Script::FromJSVal<std::vector<T> >(const ScriptRequest& rq, JS::HandleValue v, std::vector<T>& out) \
152 return FromJSVal_vector(rq, v, out); \
#define FAIL(msg)
Definition: ScriptConversions.h:111
Spidermonkey maintains some 'local' state via the JSContext* object.
Definition: ScriptRequest.h:60
JSContext * cx
Definition: ScriptRequest.h:92
#define UNUSED(param)
mark a function parameter as unused and avoid the corresponding compiler warning.
Definition: code_annotation.h:40
#define ENSURE(expr)
ensure the expression <expr> evaluates to non-zero.
Definition: debug.h:277
Wraps SM APIs for manipulating JS objects.
Definition: JSON.h:35
bool FromJSVal(const ScriptRequest &rq, const JS::HandleValue val, T &ret)
Convert a JS::Value to a C++ type.
void ToJSVal(const ScriptRequest &rq, JS::MutableHandleValue ret, T const &val)
Convert a C++ type to a JS::Value.
bool FromJSProperty(const ScriptRequest &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: ScriptConversions.h:71
void ToJSVal_vector(const ScriptRequest &rq, JS::MutableHandleValue ret, const std::vector< T > &val)
Definition: ScriptConversions.h:92
bool FromJSVal_vector(const ScriptRequest &rq, JS::HandleValue v, std::vector< T > &out)
Definition: ScriptConversions.h:113
#define T(string_literal)
Definition: secure_crt.cpp:77
uint32_t u32
Definition: types.h:39
static void out(const wchar_t *fmt,...)
Definition: wdbg_sym.cpp:407