# HG changeset patch # User jbe # Date 1447705136 -3600 # Node ID f0ac171edf62ecc1fdfe45ca55351a251caa2075 # Parent 810c020b0da4153d67a912e3c4784b27ebe34ddd New implementation of (proxy) read access to fields of JSON object in a special column diff -r 810c020b0da4 -r f0ac171edf62 libraries/mondelefant/mondelefant_native.c --- a/libraries/mondelefant/mondelefant_native.c Mon Nov 16 18:31:49 2015 +0100 +++ b/libraries/mondelefant/mondelefant_native.c Mon Nov 16 21:18:56 2015 +0100 @@ -1478,7 +1478,7 @@ if (lua_type(L, 2) != LUA_TSTRING || lua_tostring(L, 2)[0] == '_') { return 0; } - // get value of "_class" attribute, or default class, when unset: + // value of "_class" attribute or default class:on stack position 3: lua_settop(L, 2); lua_getfield(L, 1, "_class"); // 3 if (!lua_toboolean(L, 3)) { @@ -1494,80 +1494,81 @@ // different lookup for lists and objects: if (result_type && !strcmp(result_type, "object")) { // object lua_settop(L, 3); + lua_pushvalue(L, 3); // 4 // try inherited attributes, methods or getter functions: - while (lua_toboolean(L, 3)) { - lua_getfield(L, 3, "object"); // 4 - lua_pushvalue(L, 2); // 5 - lua_gettable(L, 4); // 5 - if (!lua_isnil(L, 5)) return 1; - lua_settop(L, 3); - lua_getfield(L, 3, "object_get"); // 4 - lua_pushvalue(L, 2); // 5 - lua_gettable(L, 4); // 5 - if (lua_toboolean(L, 5)) { - lua_pushvalue(L, 1); // 6 - lua_call(L, 1, 1); // 5 + while (lua_toboolean(L, 4)) { + lua_getfield(L, 4, "object"); // 5 + lua_pushvalue(L, 2); // 6 + lua_gettable(L, 5); // 6 + if (!lua_isnil(L, 6)) return 1; + lua_settop(L, 4); + lua_getfield(L, 4, "object_get"); // 5 + lua_pushvalue(L, 2); // 6 + lua_gettable(L, 5); // 6 + if (lua_toboolean(L, 6)) { + lua_pushvalue(L, 1); // 7 + lua_call(L, 1, 1); // 6 return 1; } - lua_settop(L, 3); - lua_pushliteral(L, "prototype"); // 4 - lua_rawget(L, 3); // 4 - lua_replace(L, 3); + lua_settop(L, 4); + lua_pushliteral(L, "prototype"); // 5 + lua_rawget(L, 4); // 5 + lua_replace(L, 4); } - lua_settop(L, 2); + lua_settop(L, 3); // try primary keys of referenced objects: lua_pushcfunction(L, mondelefant_class_get_foreign_key_reference_name - ); // 3 - lua_getfield(L, 1, "_class"); // 4 - lua_pushvalue(L, 2); // 5 - lua_call(L, 2, 1); // 3 - if (!lua_isnil(L, 3)) { - // reference name at stack position 3 - lua_pushcfunction(L, mondelefant_class_get_reference); // 4 - lua_getfield(L, 1, "_class"); // 5 + ); // 4 + lua_pushvalue(L, 3); // 5 + lua_pushvalue(L, 2); // 6 + lua_call(L, 2, 1); // 4 + if (!lua_isnil(L, 4)) { + // reference name at stack position 4 + lua_pushcfunction(L, mondelefant_class_get_reference); // 5 lua_pushvalue(L, 3); // 6 - lua_call(L, 2, 1); // reference info at stack position 4 - lua_getfield(L, 1, "_ref"); // 5 - lua_getfield(L, 4, "ref"); // 6 - lua_gettable(L, 5); // 6 - if (!lua_isnil(L, 6)) { - if (lua_toboolean(L, 6)) { - lua_getfield(L, 4, "that_key"); // 7 - if (lua_isnil(L, 7)) { + lua_pushvalue(L, 4); // 7 + lua_call(L, 2, 1); // reference info at stack position 5 + lua_getfield(L, 1, "_ref"); // 6 + lua_getfield(L, 5, "ref"); // 7 + lua_gettable(L, 6); // 7 + if (!lua_isnil(L, 7)) { + if (lua_toboolean(L, 7)) { + lua_getfield(L, 5, "that_key"); // 8 + if (lua_isnil(L, 8)) { return luaL_error(L, "Missing 'that_key' entry in model reference."); } - lua_gettable(L, 6); // 7 + lua_gettable(L, 7); // 8 } else { lua_pushnil(L); } return 1; } } - lua_settop(L, 2); + lua_settop(L, 3); + lua_getfield(L, 1, "_data"); // _data table on stack position 4 // try normal data field info: - lua_getfield(L, 1, "_data"); // 3 - lua_pushvalue(L, 2); // 4 - lua_gettable(L, 3); // 4 - if (!lua_isnil(L, 4)) return 1; - lua_settop(L, 2); + lua_pushvalue(L, 2); // 5 + lua_gettable(L, 4); // 5 + if (!lua_isnil(L, 5)) return 1; + lua_settop(L, 4); // keep _data table on stack // try cached referenced object (or cached NULL reference): - lua_getfield(L, 1, "_ref"); // 3 - lua_pushvalue(L, 2); // 4 - lua_gettable(L, 3); // 4 - if (lua_isboolean(L, 4) && !lua_toboolean(L, 4)) { + lua_getfield(L, 1, "_ref"); // 5 + lua_pushvalue(L, 2); // 6 + lua_gettable(L, 5); // 6 + if (lua_isboolean(L, 6) && !lua_toboolean(L, 6)) { lua_pushnil(L); return 1; - } else if (!lua_isnil(L, 4)) { + } else if (!lua_isnil(L, 6)) { return 1; } - lua_settop(L, 2); + lua_settop(L, 4); // try to load a referenced object: - lua_pushcfunction(L, mondelefant_class_get_reference); // 3 - lua_getfield(L, 1, "_class"); // 4 - lua_pushvalue(L, 2); // 5 - lua_call(L, 2, 1); // 3 - if (!lua_isnil(L, 3)) { + lua_pushcfunction(L, mondelefant_class_get_reference); // 5 + lua_pushvalue(L, 3); // 6 + lua_pushvalue(L, 2); // 7 + lua_call(L, 2, 1); // 5 + if (!lua_isnil(L, 5)) { lua_settop(L, 2); lua_getfield(L, 1, "load"); // 3 lua_pushvalue(L, 1); // 4 (self) @@ -1580,6 +1581,17 @@ if (lua_isboolean(L, 4) && !lua_toboolean(L, 4)) lua_pushnil(L); // TODO: use special object instead of false return 1; } + lua_settop(L, 4); + // try proxy access to document in special column: + lua_getfield(L, 3, "document_column"); // 5 + if (lua_toboolean(L, 5)) { + lua_gettable(L, 4); // 5 + if (!lua_isnil(L, 5)) { + lua_pushvalue(L, 2); // 6 + lua_gettable(L, 5); // 6 + if (!lua_isnil(L, 6)) return 1; + } + } return 0; } else if (result_type && !strcmp(result_type, "list")) { // list lua_settop(L, 3);