diff options
Diffstat (limited to 'Sluift/ElementConvertors')
-rw-r--r-- | Sluift/ElementConvertors/FormConvertor.cpp | 36 |
1 files changed, 33 insertions, 3 deletions
diff --git a/Sluift/ElementConvertors/FormConvertor.cpp b/Sluift/ElementConvertors/FormConvertor.cpp index 1720037..e44ca3e 100644 --- a/Sluift/ElementConvertors/FormConvertor.cpp +++ b/Sluift/ElementConvertors/FormConvertor.cpp @@ -19,5 +19,4 @@ using namespace Swift; namespace { - // TODO: add __newindex to set a field value int formIndex(lua_State* L) { lua_getfield(L, 1, "fields"); @@ -38,4 +37,30 @@ namespace { } + int formNewIndex(lua_State* L) { + lua_getfield(L, 1, "fields"); + bool foundField = false; + if (lua_type(L, -1) == LUA_TTABLE) { + for (lua_pushnil(L); lua_next(L, -2) != 0; ) { + lua_getfield(L, -1, "name"); + if (lua_equal(L, -1, 2)) { + lua_pushvalue(L, 3); + lua_setfield(L, -3, "value"); + foundField = true; + lua_pop(L, 3); + break; + } + lua_pop(L, 2); + } + } + lua_pop(L, 1); + + if (!foundField) { + lua_pushvalue(L, 2); + lua_pushvalue(L, 3); + lua_rawset(L, 1); + } + return 0; + } + Lua::Table convertFieldToLua(boost::shared_ptr<FormField> field) { Lua::Table luaField = boost::assign::map_list_of("name", Lua::valueRef(field->getName())); @@ -279,7 +304,10 @@ namespace { Lua::pushValue(L, result); + lua_newtable(L); lua_pushcfunction(L, formIndex); lua_setfield(L, -2, "__index"); + lua_pushcfunction(L, formNewIndex); + lua_setfield(L, -2, "__newindex"); lua_setmetatable(L, -2); } @@ -305,8 +333,10 @@ namespace { form->addField(field); } + form->setType(Form::SubmitType); // Convert back convertFormToLua(L, form); Lua::registerTableToString(L, -1); + return 1; } @@ -326,6 +356,6 @@ void FormConvertor::doConvertToLua(lua_State* L, boost::shared_ptr<Form> payload convertFormToLua(L, payload); + lua_pushstring(L, "create_submission"); lua_pushcfunction(L, createSubmission); - lua_setfield(L, -2, "create_submission"); - + lua_rawset(L, -3); } |