1
0
forked from 0ad/0ad

Add FromJSProperty helper function that gets the property of a JS object, thus reducing duplicate checks when getting many properties.

Differential Revision: https://code.wildfiregames.com/D338
Patch By: Vladislav
Refs: https://code.wildfiregames.com/D317

This was SVN commit r19421.
This commit is contained in:
elexis 2017-04-16 23:59:20 +00:00
parent b856ec5ea4
commit ce5c10c7bb

View File

@ -86,4 +86,26 @@ template<> bool ScriptInterface::FromJSVal<std::vector<T> >(JSContext* cx, JS::H
return FromJSVal_vector(cx, v, out); \
}
template<typename T> static bool FromJSProperty(JSContext* cx, JS::HandleValue v, const char* name, T& out)
{
if (!v.isObject())
return false;
JSAutoRequest rq(cx);
JS::RootedObject obj(cx, &v.toObject());
bool hasProperty;
if (!JS_HasProperty(cx, obj, name, &hasProperty))
return false;
JS::RootedValue value(cx);
if (!hasProperty || !JS_GetProperty(cx, obj, name, &value))
return false;
if (!ScriptInterface::FromJSVal(cx, value, out))
return false;
return true;
}
#endif //INCLUDED_SCRIPTCONVERSIONS