From ce5c10c7bb5145b0fc78c06f85dfee5b3748e2a6 Mon Sep 17 00:00:00 2001 From: elexis Date: Sun, 16 Apr 2017 23:59:20 +0000 Subject: [PATCH] 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. --- source/scriptinterface/ScriptConversions.h | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/source/scriptinterface/ScriptConversions.h b/source/scriptinterface/ScriptConversions.h index a88113858c..fda093d0c9 100644 --- a/source/scriptinterface/ScriptConversions.h +++ b/source/scriptinterface/ScriptConversions.h @@ -86,4 +86,26 @@ template<> bool ScriptInterface::FromJSVal >(JSContext* cx, JS::H return FromJSVal_vector(cx, v, out); \ } +template 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