Adds a helper, because GetProperty still returns true, even if the property doesn't exist, what makes writing of HasPropery, GetProperty, FromJsVal too annoying.
It'd be useful for the CinemaPath conversation patch D317.
Differential D338
Add helper for the getting of an object's property vladislavbelov on Apr 15 2017, 7:10 PM. Authored by
Details
Adds a helper, because GetProperty still returns true, even if the property doesn't exist, what makes writing of HasPropery, GetProperty, FromJsVal too annoying. It'd be useful for the CinemaPath conversation patch D317.
Diff Detail
Event TimelineComment Actions Build is green Updating workspaces. Build (release)... Build (debug)... Running release tests... Running cxxtest tests (305 tests).................................................................................................................................................................................................................................................................................................................OK! Running debug tests... Running cxxtest tests (305 tests).................................................................................................................................................................................................................................................................................................................OK! http://jw:8080/job/phabricator/775/ for more details. Comment Actions We also have MutableHandleObject, this would avoid repeating the isObject and toObject calls for each FromJSProperty call. I've tested this with the following diff and we can observe that the code becomes nicer: Index: source/simulation2/scripting/EngineScriptConversions.cpp =================================================================== --- source/simulation2/scripting/EngineScriptConversions.cpp (revision 19418) +++ source/simulation2/scripting/EngineScriptConversions.cpp (working copy) @@ -87,23 +87,16 @@ template<> bool ScriptInterface::FromJSVal<CColor>(JSContext* cx, JS::HandleValue v, CColor& out) { - if (!v.isObject()) - FAIL("JS::HandleValue not an object"); + if (!FromJSProperty(cx, v, "r", out.r)) + FAIL("Failed to get property CColor.r"); - JSAutoRequest rq(cx); - JS::RootedObject obj(cx, &v.toObject()); + if (!FromJSProperty(cx, v, "g", out.g)) + FAIL("Failed to get property CColor.g"); - JS::RootedValue r(cx); - JS::RootedValue g(cx); - JS::RootedValue b(cx); - JS::RootedValue a(cx); - if (!JS_GetProperty(cx, obj, "r", &r) || !FromJSVal(cx, r, out.r)) - FAIL("Failed to get property CColor.r"); - if (!JS_GetProperty(cx, obj, "g", &g) || !FromJSVal(cx, g, out.g)) - FAIL("Failed to get property CColor.g"); - if (!JS_GetProperty(cx, obj, "b", &b) || !FromJSVal(cx, b, out.b)) + if (!FromJSProperty(cx, v, "b", out.b)) FAIL("Failed to get property CColor.b"); - if (!JS_GetProperty(cx, obj, "a", &a) || !FromJSVal(cx, a, out.a)) + + if (!FromJSProperty(cx, v, "a", out.a)) FAIL("Failed to get property CColor.a"); return true; Notice if we change the property name of f.e. "r" to "BIGFAILURE" we will just get that, so the patch works. Thanks for the patch!
|