template<> bool ScriptInterface::FromJSVal<Entity>(JSContext* cx, JS::HandleValue v, Entity& out) { JSAutoRequest rq(cx); if (!v.isObject()) FAIL("Argument must be an object"); JS::RootedObject obj(cx, &v.toObject()); JS::RootedValue templateName(cx); JS::RootedValue id(cx); JS::RootedValue player(cx); JS::RootedValue position(cx); JS::RootedValue rotation(cx); // TODO: Report type errors if (!JS_GetProperty(cx, obj, "player", &player) || !FromJSVal(cx, player, out.playerID)) FAIL("Failed to read Entity.player property"); if (!JS_GetProperty(cx, obj, "templateName", &templateName) || !FromJSVal(cx, templateName, out.templateName)) FAIL("Failed to read Entity.templateName property"); if (!JS_GetProperty(cx, obj, "id", &id) || !FromJSVal(cx, id, out.entityID)) FAIL("Failed to read Entity.id property"); if (!JS_GetProperty(cx, obj, "position", &position) || !FromJSVal(cx, position, out.position)) FAIL("Failed to read Entity.position property"); if (!JS_GetProperty(cx, obj, "rotation", &rotation) || !FromJSVal(cx, rotation, out.rotation)) FAIL("Failed to read Entity.rotation property"); return true; }
template<> bool ScriptInterface::FromJSVal<CFixedVector2D>(JSContext* cx, JS::HandleValue v, CFixedVector2D& out) { JSAutoRequest rq(cx); if (!v.isObject()) return false; // TODO: report type error JS::RootedObject obj(cx, &v.toObject()); JS::RootedValue p(cx); if (!JS_GetProperty(cx, obj, "x", &p)) return false; // TODO: report type errors if (!FromJSVal(cx, p, out.X)) return false; if (!JS_GetProperty(cx, obj, "y", &p)) return false; if (!FromJSVal(cx, p, out.Y)) return false; return true; }
template<> bool ScriptInterface::FromJSVal<Path>(JSContext* cx, JS::HandleValue v, Path& out) { std::wstring string; if (!FromJSVal(cx, v, string)) return false; out = string; return true; }
template<> bool ScriptInterface::FromJSVal<Entity>(JSContext* cx, jsval v, Entity& out) { JSObject* obj; if (!JS_ValueToObject(cx, v, &obj) || obj == NULL) FAIL("Argument must be an object"); jsval templateName, id, player, position, rotation; // TODO: Report type errors if(!JS_GetProperty(cx, obj, "player", &player) || !FromJSVal(cx, player, out.playerID)) FAIL("Failed to read Entity.player property"); if (!JS_GetProperty(cx, obj, "templateName", &templateName) || !FromJSVal(cx, templateName, out.templateName)) FAIL("Failed to read Entity.templateName property"); if (!JS_GetProperty(cx, obj, "id", &id) || !FromJSVal(cx, id, out.entityID)) FAIL("Failed to read Entity.id property"); if (!JS_GetProperty(cx, obj, "position", &position) || !FromJSVal(cx, position, out.position)) FAIL("Failed to read Entity.position property"); if (!JS_GetProperty(cx, obj, "rotation", &rotation) || !FromJSVal(cx, rotation, out.rotation)) FAIL("Failed to read Entity.rotation property"); return true; }
template<> bool ScriptInterface::FromJSVal<CColor>(JSContext* cx, JS::HandleValue v, CColor& out) { if (!v.isObject()) FAIL("JS::HandleValue not an object"); JSAutoRequest rq(cx); JS::RootedObject obj(cx, &v.toObject()); 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)) FAIL("Failed to get property CColor.b"); if (!JS_GetProperty(cx, obj, "a", &a) || !FromJSVal(cx, a, out.a)) FAIL("Failed to get property CColor.a"); return true; }