# # # patch "HACKING" # from [156e40439898b2df8d1bfe980bbf63a9e19eac35] # to [8e322b6697a91dc479e0a6fc46ee33ed0a305fc8] # # patch "lua.cc" # from [a86b67e6ece4d5e661bbfd6f0c439e3fc8a69f7c] # to [b24e1956c4529dc205ac3168de9119d2048cf481] # # patch "lua.hh" # from [67c90ab30ca8d0bb1f2897242fb55d9b056555c9] # to [a133cf4720fdd0213f3fad854be18d28f6828646] # # patch "lua_hooks.cc" # from [ee5e4a8c502ed3318a66166d0378c7afefd17513] # to [10b40cfc6a783fb5e80f7221d15a72918e475bcf] # ============================================================ --- HACKING 156e40439898b2df8d1bfe980bbf63a9e19eac35 +++ HACKING 8e322b6697a91dc479e0a6fc46ee33ed0a305fc8 @@ -145,7 +145,17 @@ continued to adhere to the dialect we wr the function name, so that the function name sits flush with the first column. this makes grepping for function definitions easier. +Interfacing with Lua +-------------------- +monotone is extended with hooks written in the Lua language. Code that +interfaces with Lua must obey the following rules: + + - Lua arrays (tables with numeric indices) are 1-indexed. This is not + mandated by the language per se, but the standard libraries assume that + and using the arrays otherwise may break hooks that use standard + functions. + Test suites, and writing test cases ---------------------------------- ============================================================ --- lua.cc a86b67e6ece4d5e661bbfd6f0c439e3fc8a69f7c +++ lua.cc b24e1956c4529dc205ac3168de9119d2048cf481 @@ -208,7 +208,7 @@ Lua::extract_int(int & i) fail("isnumber() in extract_int"); return *this; } - i = static_cast(lua_tonumber(st, -1)); + i = lua_tointeger(st, -1); L(FL("lua: extracted int = %i") % i); return *this; } @@ -343,6 +343,14 @@ Lua & } Lua & +Lua::set_field(const string& key, int idx) +{ + if (failed) return *this; + lua_setfield(st, idx, key.c_str()); + return *this; +} + +Lua & Lua::call(int in, int out) { if (failed) return *this; @@ -436,9 +444,8 @@ void add_functions(lua_State * st) if (table != "") { lua_newtable(st); - lua_pushstring(st, table.c_str()); - lua_pushvalue(st, -2); - lua_settable(st, LUA_GLOBALSINDEX); + lua_pushvalue(st, -1); + lua_setfield(st, LUA_GLOBALSINDEX, table.c_str()); } for (luaext::fmap::const_iterator j = i->second.begin(); j != i->second.end(); ++j) @@ -447,9 +454,8 @@ void add_functions(lua_State * st) lua_register(st, j->first.c_str(), j->second); else { - lua_pushstring(st, j->first.c_str()); lua_pushcfunction(st, j->second); - lua_settable(st, -3); + lua_setfield(st, -2, j->first.c_str()); } } if (table != "") ============================================================ --- lua.hh 67c90ab30ca8d0bb1f2897242fb55d9b056555c9 +++ lua.hh a133cf4720fdd0213f3fad854be18d28f6828646 @@ -57,6 +57,7 @@ Lua Lua & push_bool(bool b); Lua & push_nil(); Lua & push_table(); + Lua & set_field(std::string const & key, int idx = -2); Lua & set_table(int idx = -3); Lua & call(int in, int out); Lua & pop(int count = 1); ============================================================ --- lua_hooks.cc ee5e4a8c502ed3318a66166d0378c7afefd17513 +++ lua_hooks.cc 10b40cfc6a783fb5e80f7221d15a72918e475bcf @@ -845,16 +845,13 @@ lua_hooks::hook_note_netsync_revision_re { ll.push_int(n++); ll.push_table(); - ll.push_str("key"); ll.push_str(i->first()); - ll.set_table(); - ll.push_str("name"); + ll.set_field("key"); ll.push_str(i->second.first()); - ll.set_table(); - ll.push_str("value"); + ll.set_field("name"); ll.push_str(i->second.second()); + ll.set_field("value"); ll.set_table(); - ll.set_table(); } ll.push_int(session_id);