From 5871ed0d02ff69b20b65f577dd3be18a2e92dec7 Mon Sep 17 00:00:00 2001 From: Joshua Haberman Date: Sun, 18 Jul 2010 22:45:15 -0700 Subject: First go at Lua bindings. --- lang_ext/lua/upb.c | 254 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 254 insertions(+) create mode 100644 lang_ext/lua/upb.c (limited to 'lang_ext/lua/upb.c') diff --git a/lang_ext/lua/upb.c b/lang_ext/lua/upb.c new file mode 100644 index 0000000..ac7f188 --- /dev/null +++ b/lang_ext/lua/upb.c @@ -0,0 +1,254 @@ +/* + * upb - a minimalist implementation of protocol buffers. + * + * Copyright (c) 2009 Joshua Haberman. See LICENSE for details. + * + * A Lua extension for upb. + */ + +#include "lauxlib.h" +#include "upb_def.h" + +/* lupb_def *******************************************************************/ + +// All the def types share the same C layout, even though they are differen Lua +// types with different metatables. +typedef struct { + upb_def *def; +} lupb_def; + +static void lupb_pushnewdef(lua_State *L, upb_def *def) { + lupb_def *ldef = lua_newuserdata(L, sizeof(lupb_def)); + ldef->def = def; + const char *type_name; + switch(def->type) { + case UPB_DEF_MSG: + type_name = "upb.msgdef"; + break; + case UPB_DEF_ENUM: + type_name = "upb.enumdef"; + break; + default: + luaL_error(L, "unknown deftype %d", def->type); + } + luaL_getmetatable(L, type_name); + lua_setmetatable(L, -2); +} + +static lupb_def *lupb_msgdef_check(lua_State *L, int narg) { + return luaL_checkudata(L, narg, "upb.msgdef"); +} + +static lupb_def *lupb_enumdef_check(lua_State *L, int narg) { + return luaL_checkudata(L, narg, "upb.enumdef"); +} + +static int lupb_msgdef_gc(lua_State *L) { + lupb_def *ldef = lupb_msgdef_check(L, 1); + upb_def_unref(ldef->def); + return 0; +} + +static int lupb_enumdef_gc(lua_State *L) { + lupb_def *ldef = lupb_enumdef_check(L, 1); + upb_def_unref(ldef->def); + return 0; +} + +static const struct luaL_Reg lupb_msgdef_methods[] = { + {"__gc", lupb_msgdef_gc}, + {NULL, NULL} +}; + +static const struct luaL_Reg lupb_enumdef_methods[] = { + {"__gc", lupb_enumdef_gc}, + {NULL, NULL} +}; + + +/* lupb_symtab ****************************************************************/ + +// lupb_symtab caches the Lua objects it vends (defs) via lookup or resolve. +// It does this (instead of creating a new Lua object every time) for two +// reasons: +// * it uses less memory, because we can reuse existing objects. +// * it gives the expected equality semantics, eg. symtab[sym] == symtab[sym]. +// +// The downside is a bit of complexity. We need a place to store these +// cached defs; the only good answer is in the metatable. This means we need +// a new metatable for every symtab instance (instead of one shared by all +// instances). Since this is different than the regular pattern, we can't +// use luaL_checkudata(), we have to implement it ourselves. +typedef struct { + upb_symtab *symtab; +} lupb_symtab; + +static int lupb_symtab_gc(lua_State *L); + +// Inherits a ref on the symtab. +static void lupb_pushnewsymtab(lua_State *L, upb_symtab *symtab) { + lupb_symtab *lsymtab = lua_newuserdata(L, sizeof(lupb_symtab)); + lsymtab->symtab = symtab; + // Create its metatable (see note above about mt-per-object). + lua_createtable(L, 0, 1); + luaL_getmetatable(L, "upb.symtab"); + lua_setfield(L, -2, "__index"); // Uses the type metatable to find methods. + lua_pushcfunction(L, lupb_symtab_gc); + lua_setfield(L, -2, "__gc"); + + // Put this metatable in the registry so we can find it for type validation. + lua_pushlightuserdata(L, lsymtab); + lua_pushvalue(L, -2); + lua_rawset(L, LUA_REGISTRYINDEX); + + // Set the symtab's metatable. + lua_setmetatable(L, -2); +} + +// Checks that narg is a proper lupb_symtab object. If it is, leaves its +// metatable on the stack for cache lookups/updates. +lupb_symtab *lupb_symtab_check(lua_State *L, int narg) { + lupb_symtab *symtab = lua_touserdata(L, narg); + if (symtab != NULL) { + if (lua_getmetatable(L, narg)) { + // We use a metatable-per-object to support memoization of defs. + lua_pushlightuserdata(L, symtab); + lua_rawget(L, LUA_REGISTRYINDEX); + if (lua_rawequal(L, -1, -2)) { // Does it have the correct mt? + lua_pop(L, 1); // Remove one copy of the mt, keep the other. + return symtab; + } + } + } + luaL_typerror(L, narg, "upb.symtab"); + return NULL; // Placate the compiler; luaL_typerror will longjmp out of here. +} + +static int lupb_symtab_gc(lua_State *L) { + lupb_symtab *s = lupb_symtab_check(L, 1); + upb_symtab_unref(s->symtab); + + // Remove its metatable from the registry. + lua_pushlightuserdata(L, s); + lua_pushnil(L); + lua_rawset(L, LUA_REGISTRYINDEX); + return 0; +} + +// "mt" is the index of the metatable, -1 is the fqname of this def. +// Leaves the Lua object for the def at the top of the stack. +// Inherits a ref on "def". +static void lupb_symtab_getorcreate(lua_State *L, upb_def *def, int mt) { + // We may have this def cached, in which case we should return the same Lua + // object (as long as the value in the underlying symtab has not changed. + lua_rawget(L, mt); + if (!lua_isnil(L, -1)) { + // Def is cached, make sure it hasn't changed. + lupb_def *ldef = lua_touserdata(L, -1); + if (!ldef) luaL_error(L, "upb's internal cache is corrupt."); + if (ldef->def == def) { + // Cache is good, we can just return the cached value. + upb_def_unref(def); + return; + } + } + // Cached entry didn't exist or wasn't good. + lua_pop(L, 1); // Remove bad cached value. + lupb_pushnewdef(L, def); + + // Set it in the cache. + lua_pushvalue(L, 2); // push name (arg to this function). + lua_pushvalue(L, -2); // push the new def. + lua_rawset(L, mt); // set in the cache (the mt). +} + +static int lupb_symtab_lookup(lua_State *L) { + lupb_symtab *s = lupb_symtab_check(L, 1); + size_t len; + const char *name = luaL_checklstring(L, 2, &len); + upb_string namestr = UPB_STACK_STRING_LEN(name, len); + upb_def *def = upb_symtab_lookup(s->symtab, &namestr); + if (!def) { + // There shouldn't be a value in our cache either because the symtab + // currently provides no API for deleting syms from a table. In case + // this changes in the future, we explicitly delete from the cache here. + lua_pushvalue(L, 2); // push name (arg to this function). + lua_pushnil(L); + lua_rawset(L, -3); // lupb_symtab_check() left our mt on the stack. + + // Return nil because the symbol was not found. + lua_pushnil(L); + return 1; + } else { + lua_pushvalue(L, 2); + lupb_symtab_getorcreate(L, def, 3); + return 1; + } +} + +static int lupb_symtab_getdefs(lua_State *L) { + lupb_symtab *s = lupb_symtab_check(L, 1); + upb_deftype_t type = luaL_checkint(L, 2); + int count; + upb_def **defs = upb_symtab_getdefs(s->symtab, &count, type); + + // Create the table in which we will return the defs. + lua_createtable(L, 0, count); + int ret = lua_gettop(L); + + for (int i = 0; i < count; i++) { + upb_def *def = defs[i]; + // Look it up in the cache by name. + upb_string *name = def->fqname; + lua_pushlstring(L, upb_string_getrobuf(name), upb_string_len(name)); + lua_pushvalue(L, -1); // Push it again since the getorcreate consumes one. + lupb_symtab_getorcreate(L, def, 3); + + // Add it to our return table. + lua_settable(L, ret); + } + return 1; +} + +static int lupb_symtab_add_descriptorproto(lua_State *L) { + lupb_symtab *s = lupb_symtab_check(L, 1); + upb_symtab_add_descriptorproto(s->symtab); + return 0; // No args to return. +} + +static const struct luaL_Reg lupb_symtab_methods[] = { + {"add_descriptorproto", lupb_symtab_add_descriptorproto}, + //{"addfds", lupb_symtab_addfds}, + {"getdefs", lupb_symtab_getdefs}, + {"lookup", lupb_symtab_lookup}, + //{"resolve", lupb_symtab_resolve}, + {NULL, NULL} +}; + + +/* lupb toplevel **************************************************************/ + +static int lupb_symtab_new(lua_State *L) { + upb_symtab *s = upb_symtab_new(); + lupb_pushnewsymtab(L, s); + return 1; +} + +static const struct luaL_Reg lupb_toplevel_methods[] = { + {"symtab", lupb_symtab_new}, + {NULL, NULL} +}; + +int luaopen_upb(lua_State *L) { + luaL_newmetatable(L, "upb.msgdef"); + luaL_register(L, NULL, lupb_msgdef_methods); + + luaL_newmetatable(L, "upb.enumdef"); + luaL_register(L, NULL, lupb_enumdef_methods); + + luaL_newmetatable(L, "upb.symtab"); + luaL_register(L, NULL, lupb_symtab_methods); + + luaL_register(L, "upb", lupb_toplevel_methods); + return 1; // Return package table. +} -- cgit v1.2.3 From 904a79cec3e77efc0253e4412ab766d602ae126d Mon Sep 17 00:00:00 2001 From: Joshua Haberman Date: Mon, 19 Jul 2010 08:50:43 -0700 Subject: Fix bugs in lookup and getdefs. --- lang_ext/lua/upb.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) (limited to 'lang_ext/lua/upb.c') diff --git a/lang_ext/lua/upb.c b/lang_ext/lua/upb.c index ac7f188..6f50c67 100644 --- a/lang_ext/lua/upb.c +++ b/lang_ext/lua/upb.c @@ -6,6 +6,7 @@ * A Lua extension for upb. */ +#include #include "lauxlib.h" #include "upb_def.h" @@ -141,6 +142,7 @@ static int lupb_symtab_gc(lua_State *L) { static void lupb_symtab_getorcreate(lua_State *L, upb_def *def, int mt) { // We may have this def cached, in which case we should return the same Lua // object (as long as the value in the underlying symtab has not changed. + lua_pushvalue(L, -1); // Copy the name for cache insertion later. lua_rawget(L, mt); if (!lua_isnil(L, -1)) { // Def is cached, make sure it hasn't changed. @@ -148,6 +150,8 @@ static void lupb_symtab_getorcreate(lua_State *L, upb_def *def, int mt) { if (!ldef) luaL_error(L, "upb's internal cache is corrupt."); if (ldef->def == def) { // Cache is good, we can just return the cached value. + lua_insert(L, -2); // Move our cached def before the copy of the name. + lua_pop(L, 1); // Our extra copy of the name. upb_def_unref(def); return; } @@ -155,11 +159,13 @@ static void lupb_symtab_getorcreate(lua_State *L, upb_def *def, int mt) { // Cached entry didn't exist or wasn't good. lua_pop(L, 1); // Remove bad cached value. lupb_pushnewdef(L, def); + lua_insert(L, -2); // Move new def before the name, so stack is [def, name] // Set it in the cache. - lua_pushvalue(L, 2); // push name (arg to this function). - lua_pushvalue(L, -2); // push the new def. + lua_pushvalue(L, -2); // push def. lua_rawset(L, mt); // set in the cache (the mt). + + // Def is left at the top of the stack. } static int lupb_symtab_lookup(lua_State *L) { @@ -207,6 +213,7 @@ static int lupb_symtab_getdefs(lua_State *L) { // Add it to our return table. lua_settable(L, ret); } + free(defs); return 1; } -- cgit v1.2.3 From 4a38d38f96c561d1e0797e23ed97fd2657c21da0 Mon Sep 17 00:00:00 2001 From: Joshua Haberman Date: Mon, 19 Jul 2010 17:42:56 -0700 Subject: Use a weak table to cache objects. This simplifies things considerably, and is more in line with common practice. --- lang_ext/lua/upb.c | 211 ++++++++++++++++++++++------------------------------- 1 file changed, 88 insertions(+), 123 deletions(-) (limited to 'lang_ext/lua/upb.c') diff --git a/lang_ext/lua/upb.c b/lang_ext/lua/upb.c index 6f50c67..7241dc5 100644 --- a/lang_ext/lua/upb.c +++ b/lang_ext/lua/upb.c @@ -10,17 +10,57 @@ #include "lauxlib.h" #include "upb_def.h" +/* object cache ***************************************************************/ + +// We cache all the lua objects (userdata) we vend in a weak table, indexed by +// the C pointer of the object they are caching. + +typedef void (*lupb_unref)(void *cobj); + +static void lupb_cache_getorcreate(lua_State *L, void *cobj, const char *type, + lupb_unref unref) { + // Lookup our cache in the registry (we don't put our objects in the registry + // directly because we need our cache to be a weak table). + lua_getfield(L, LUA_REGISTRYINDEX, "upb.objcache"); + assert(!lua_isnil(L, -1)); // Should have been created by luaopen_upb. + lua_pushlightuserdata(L, cobj); + lua_rawget(L, -2); + // Stack: objcache, cached value. + if (lua_isnil(L, -1)) { + // Remove bad cached value and push new value. + lua_pop(L, 1); + // We take advantage of the fact that all of our objects are currently a + // single pointer, and thus have the same layout. + void **obj = lua_newuserdata(L, sizeof(void*)); + *obj = cobj; + luaL_getmetatable(L, type); + lua_setmetatable(L, -2); + + // Set it in the cache. + lua_pushlightuserdata(L, cobj); + lua_pushvalue(L, -2); + lua_rawset(L, -4); + } else { + unref(cobj); + } + lua_insert(L, -2); + lua_pop(L, 1); +} + + /* lupb_def *******************************************************************/ -// All the def types share the same C layout, even though they are differen Lua +// All the def types share the same C layout, even though they are different Lua // types with different metatables. typedef struct { upb_def *def; } lupb_def; -static void lupb_pushnewdef(lua_State *L, upb_def *def) { - lupb_def *ldef = lua_newuserdata(L, sizeof(lupb_def)); - ldef->def = def; +static void lupb_def_unref(void *cobj) { + upb_def_unref((upb_def*)cobj); +} + +static void lupb_def_getorcreate(lua_State *L, upb_def *def) { const char *type_name; switch(def->type) { case UPB_DEF_MSG: @@ -32,8 +72,7 @@ static void lupb_pushnewdef(lua_State *L, upb_def *def) { default: luaL_error(L, "unknown deftype %d", def->type); } - luaL_getmetatable(L, type_name); - lua_setmetatable(L, -2); + return lupb_cache_getorcreate(L, def, type_name, lupb_def_unref); } static lupb_def *lupb_msgdef_check(lua_State *L, int narg) { @@ -56,116 +95,46 @@ static int lupb_enumdef_gc(lua_State *L) { return 0; } -static const struct luaL_Reg lupb_msgdef_methods[] = { +static const struct luaL_Reg lupb_msgdef_mm[] = { {"__gc", lupb_msgdef_gc}, {NULL, NULL} }; -static const struct luaL_Reg lupb_enumdef_methods[] = { +static const struct luaL_Reg lupb_msgdef_m[] = { + {NULL, NULL} +}; + +static const struct luaL_Reg lupb_enumdef_mm[] = { {"__gc", lupb_enumdef_gc}, {NULL, NULL} }; +static const struct luaL_Reg lupb_enumdef_m[] = { + {NULL, NULL} +}; + /* lupb_symtab ****************************************************************/ -// lupb_symtab caches the Lua objects it vends (defs) via lookup or resolve. -// It does this (instead of creating a new Lua object every time) for two -// reasons: -// * it uses less memory, because we can reuse existing objects. -// * it gives the expected equality semantics, eg. symtab[sym] == symtab[sym]. -// -// The downside is a bit of complexity. We need a place to store these -// cached defs; the only good answer is in the metatable. This means we need -// a new metatable for every symtab instance (instead of one shared by all -// instances). Since this is different than the regular pattern, we can't -// use luaL_checkudata(), we have to implement it ourselves. typedef struct { upb_symtab *symtab; } lupb_symtab; -static int lupb_symtab_gc(lua_State *L); - // Inherits a ref on the symtab. -static void lupb_pushnewsymtab(lua_State *L, upb_symtab *symtab) { - lupb_symtab *lsymtab = lua_newuserdata(L, sizeof(lupb_symtab)); - lsymtab->symtab = symtab; - // Create its metatable (see note above about mt-per-object). - lua_createtable(L, 0, 1); - luaL_getmetatable(L, "upb.symtab"); - lua_setfield(L, -2, "__index"); // Uses the type metatable to find methods. - lua_pushcfunction(L, lupb_symtab_gc); - lua_setfield(L, -2, "__gc"); - - // Put this metatable in the registry so we can find it for type validation. - lua_pushlightuserdata(L, lsymtab); - lua_pushvalue(L, -2); - lua_rawset(L, LUA_REGISTRYINDEX); - - // Set the symtab's metatable. - lua_setmetatable(L, -2); -} - // Checks that narg is a proper lupb_symtab object. If it is, leaves its // metatable on the stack for cache lookups/updates. lupb_symtab *lupb_symtab_check(lua_State *L, int narg) { - lupb_symtab *symtab = lua_touserdata(L, narg); - if (symtab != NULL) { - if (lua_getmetatable(L, narg)) { - // We use a metatable-per-object to support memoization of defs. - lua_pushlightuserdata(L, symtab); - lua_rawget(L, LUA_REGISTRYINDEX); - if (lua_rawequal(L, -1, -2)) { // Does it have the correct mt? - lua_pop(L, 1); // Remove one copy of the mt, keep the other. - return symtab; - } - } - } - luaL_typerror(L, narg, "upb.symtab"); - return NULL; // Placate the compiler; luaL_typerror will longjmp out of here. + return luaL_checkudata(L, narg, "upb.symtab"); } static int lupb_symtab_gc(lua_State *L) { lupb_symtab *s = lupb_symtab_check(L, 1); upb_symtab_unref(s->symtab); - - // Remove its metatable from the registry. - lua_pushlightuserdata(L, s); - lua_pushnil(L); - lua_rawset(L, LUA_REGISTRYINDEX); return 0; } -// "mt" is the index of the metatable, -1 is the fqname of this def. -// Leaves the Lua object for the def at the top of the stack. -// Inherits a ref on "def". -static void lupb_symtab_getorcreate(lua_State *L, upb_def *def, int mt) { - // We may have this def cached, in which case we should return the same Lua - // object (as long as the value in the underlying symtab has not changed. - lua_pushvalue(L, -1); // Copy the name for cache insertion later. - lua_rawget(L, mt); - if (!lua_isnil(L, -1)) { - // Def is cached, make sure it hasn't changed. - lupb_def *ldef = lua_touserdata(L, -1); - if (!ldef) luaL_error(L, "upb's internal cache is corrupt."); - if (ldef->def == def) { - // Cache is good, we can just return the cached value. - lua_insert(L, -2); // Move our cached def before the copy of the name. - lua_pop(L, 1); // Our extra copy of the name. - upb_def_unref(def); - return; - } - } - // Cached entry didn't exist or wasn't good. - lua_pop(L, 1); // Remove bad cached value. - lupb_pushnewdef(L, def); - lua_insert(L, -2); // Move new def before the name, so stack is [def, name] - - // Set it in the cache. - lua_pushvalue(L, -2); // push def. - lua_rawset(L, mt); // set in the cache (the mt). - - // Def is left at the top of the stack. +static void lupb_symtab_unref(void *cobj) { + upb_symtab_unref((upb_symtab*)cobj); } static int lupb_symtab_lookup(lua_State *L) { @@ -174,22 +143,8 @@ static int lupb_symtab_lookup(lua_State *L) { const char *name = luaL_checklstring(L, 2, &len); upb_string namestr = UPB_STACK_STRING_LEN(name, len); upb_def *def = upb_symtab_lookup(s->symtab, &namestr); - if (!def) { - // There shouldn't be a value in our cache either because the symtab - // currently provides no API for deleting syms from a table. In case - // this changes in the future, we explicitly delete from the cache here. - lua_pushvalue(L, 2); // push name (arg to this function). - lua_pushnil(L); - lua_rawset(L, -3); // lupb_symtab_check() left our mt on the stack. - - // Return nil because the symbol was not found. - lua_pushnil(L); - return 1; - } else { - lua_pushvalue(L, 2); - lupb_symtab_getorcreate(L, def, 3); - return 1; - } + lupb_def_getorcreate(L, def); + return 1; } static int lupb_symtab_getdefs(lua_State *L) { @@ -200,18 +155,13 @@ static int lupb_symtab_getdefs(lua_State *L) { // Create the table in which we will return the defs. lua_createtable(L, 0, count); - int ret = lua_gettop(L); - for (int i = 0; i < count; i++) { upb_def *def = defs[i]; - // Look it up in the cache by name. upb_string *name = def->fqname; lua_pushlstring(L, upb_string_getrobuf(name), upb_string_len(name)); - lua_pushvalue(L, -1); // Push it again since the getorcreate consumes one. - lupb_symtab_getorcreate(L, def, 3); - + lupb_def_getorcreate(L, def); // Add it to our return table. - lua_settable(L, ret); + lua_settable(L, -3); } free(defs); return 1; @@ -223,7 +173,7 @@ static int lupb_symtab_add_descriptorproto(lua_State *L) { return 0; // No args to return. } -static const struct luaL_Reg lupb_symtab_methods[] = { +static const struct luaL_Reg lupb_symtab_m[] = { {"add_descriptorproto", lupb_symtab_add_descriptorproto}, //{"addfds", lupb_symtab_addfds}, {"getdefs", lupb_symtab_getdefs}, @@ -232,30 +182,45 @@ static const struct luaL_Reg lupb_symtab_methods[] = { {NULL, NULL} }; +static const struct luaL_Reg lupb_symtab_mm[] = { + {"__gc", lupb_symtab_gc}, + {NULL, NULL} +}; + /* lupb toplevel **************************************************************/ static int lupb_symtab_new(lua_State *L) { upb_symtab *s = upb_symtab_new(); - lupb_pushnewsymtab(L, s); + lupb_cache_getorcreate(L, s, "upb.symtab", lupb_symtab_unref); return 1; } -static const struct luaL_Reg lupb_toplevel_methods[] = { +static const struct luaL_Reg lupb_toplevel_m[] = { {"symtab", lupb_symtab_new}, {NULL, NULL} }; -int luaopen_upb(lua_State *L) { - luaL_newmetatable(L, "upb.msgdef"); - luaL_register(L, NULL, lupb_msgdef_methods); +// Register the given type with the given methods and metamethods. +static void lupb_register_type(lua_State *L, const char *name, + const luaL_Reg *m, const luaL_Reg *mm) { + luaL_newmetatable(L, name); + luaL_register(L, NULL, mm); + lua_createtable(L, 0, 0); + luaL_register(L, NULL, m); + lua_setfield(L, -2, "__index"); + lua_pop(L, 1); // The mt. +} - luaL_newmetatable(L, "upb.enumdef"); - luaL_register(L, NULL, lupb_enumdef_methods); +int luaopen_upb(lua_State *L) { + lupb_register_type(L, "upb.msgdef", lupb_msgdef_m, lupb_msgdef_mm); + lupb_register_type(L, "upb.enumdef", lupb_enumdef_m, lupb_enumdef_mm); + lupb_register_type(L, "upb.symtab", lupb_symtab_m, lupb_symtab_mm); - luaL_newmetatable(L, "upb.symtab"); - luaL_register(L, NULL, lupb_symtab_methods); + // Create our object cache. TODO: need to make this table weak! + lua_createtable(L, 0, 0); + lua_setfield(L, LUA_REGISTRYINDEX, "upb.objcache"); - luaL_register(L, "upb", lupb_toplevel_methods); + luaL_register(L, "upb", lupb_toplevel_m); return 1; // Return package table. } -- cgit v1.2.3 From 71ac83fe7a15b27f34e9da452eaeee8df460a2aa Mon Sep 17 00:00:00 2001 From: Joshua Haberman Date: Wed, 21 Jul 2010 11:58:32 -0700 Subject: Make object cache weak. --- lang_ext/lua/upb.c | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'lang_ext/lua/upb.c') diff --git a/lang_ext/lua/upb.c b/lang_ext/lua/upb.c index 7241dc5..bfc1355 100644 --- a/lang_ext/lua/upb.c +++ b/lang_ext/lua/upb.c @@ -71,6 +71,7 @@ static void lupb_def_getorcreate(lua_State *L, upb_def *def) { break; default: luaL_error(L, "unknown deftype %d", def->type); + type_name = NULL; // Placate the compiler. } return lupb_cache_getorcreate(L, def, type_name, lupb_def_unref); } @@ -219,6 +220,9 @@ int luaopen_upb(lua_State *L) { // Create our object cache. TODO: need to make this table weak! lua_createtable(L, 0, 0); + lua_createtable(L, 0, 1); // Cache metatable. + lua_pushstring(L, "v"); // Values are weak. + lua_setfield(L, -2, "__mode"); lua_setfield(L, LUA_REGISTRYINDEX, "upb.objcache"); luaL_register(L, "upb", lupb_toplevel_m); -- cgit v1.2.3 From 21ee24a7300dbdabef707457d2407b4f9187603b Mon Sep 17 00:00:00 2001 From: Joshua Haberman Date: Wed, 21 Jul 2010 18:59:01 -0700 Subject: Updated Lua extension to handle fielddefs. --- core/upb_def.c | 1 + core/upb_def.h | 22 ++++++++++------- core/upb_table.c | 2 +- lang_ext/lua/upb.c | 71 +++++++++++++++++++++++++++++++++++++++++++----------- tests/test_def.c | 2 ++ 5 files changed, 74 insertions(+), 24 deletions(-) (limited to 'lang_ext/lua/upb.c') diff --git a/core/upb_def.c b/core/upb_def.c index 1feaf9d..e40e1f0 100644 --- a/core/upb_def.c +++ b/core/upb_def.c @@ -355,6 +355,7 @@ static bool upb_addfield(upb_src *src, upb_msgdef *m, upb_status *status) f->name = NULL; f->def = NULL; f->owned = false; + f->msgdef = m; upb_fielddef *parsed_f; int32_t tmp; while((parsed_f = upb_src_getdef(src))) { diff --git a/core/upb_def.h b/core/upb_def.h index ae9e0fa..5c19a7a 100644 --- a/core/upb_def.h +++ b/core/upb_def.h @@ -87,23 +87,27 @@ INLINE void upb_def_unref(upb_def *def) { // is either a field of a upb_msgdef or contained inside a upb_extensiondef. // It is also reference-counted. typedef struct _upb_fielddef { - upb_atomic_refcount_t refcount; - upb_string *name; - upb_field_number_t number; - upb_field_type_t type; - upb_label_t label; upb_value default_value; + upb_string *name; + + struct _upb_msgdef *msgdef; + // For the case of an enum or a submessage, points to the def for that type. upb_def *def; - // True if we own a ref on "def" (above). This is true unless this edge is - // part of a cycle. - bool owned; + upb_atomic_refcount_t refcount; + uint32_t byte_offset; // Where in a upb_msg to find the data. // These are set only when this fielddef is part of a msgdef. - uint32_t byte_offset; // Where in a upb_msg to find the data. upb_field_count_t field_index; // Indicates set bit. + + upb_field_number_t number; + upb_field_type_t type; + upb_label_t label; + // True if we own a ref on "def" (above). This is true unless this edge is + // part of a cycle. + bool owned; } upb_fielddef; // A variety of tests about the type of a field. diff --git a/core/upb_table.c b/core/upb_table.c index b860204..a6e0a56 100644 --- a/core/upb_table.c +++ b/core/upb_table.c @@ -28,7 +28,7 @@ void upb_table_init(upb_table *t, uint32_t size, uint16_t entry_size) { t->count = 0; t->entry_size = entry_size; - t->size_lg2 = 1; + t->size_lg2 = 0; while(size >>= 1) t->size_lg2++; size_t bytes = upb_table_size(t) * t->entry_size; t->mask = upb_table_size(t) - 1; diff --git a/lang_ext/lua/upb.c b/lang_ext/lua/upb.c index bfc1355..a16a187 100644 --- a/lang_ext/lua/upb.c +++ b/lang_ext/lua/upb.c @@ -15,10 +15,14 @@ // We cache all the lua objects (userdata) we vend in a weak table, indexed by // the C pointer of the object they are caching. -typedef void (*lupb_unref)(void *cobj); +typedef void (*lupb_cb)(void *cobj); + +static void lupb_nop(void *foo) { + (void)foo; +} static void lupb_cache_getorcreate(lua_State *L, void *cobj, const char *type, - lupb_unref unref) { + lupb_cb ref, lupb_cb unref) { // Lookup our cache in the registry (we don't put our objects in the registry // directly because we need our cache to be a weak table). lua_getfield(L, LUA_REGISTRYINDEX, "upb.objcache"); @@ -40,6 +44,7 @@ static void lupb_cache_getorcreate(lua_State *L, void *cobj, const char *type, lua_pushlightuserdata(L, cobj); lua_pushvalue(L, -2); lua_rawset(L, -4); + ref(cobj); } else { unref(cobj); } @@ -73,29 +78,21 @@ static void lupb_def_getorcreate(lua_State *L, upb_def *def) { luaL_error(L, "unknown deftype %d", def->type); type_name = NULL; // Placate the compiler. } - return lupb_cache_getorcreate(L, def, type_name, lupb_def_unref); + return lupb_cache_getorcreate(L, def, type_name, lupb_nop, lupb_def_unref); } +// msgdef + static lupb_def *lupb_msgdef_check(lua_State *L, int narg) { return luaL_checkudata(L, narg, "upb.msgdef"); } -static lupb_def *lupb_enumdef_check(lua_State *L, int narg) { - return luaL_checkudata(L, narg, "upb.enumdef"); -} - static int lupb_msgdef_gc(lua_State *L) { lupb_def *ldef = lupb_msgdef_check(L, 1); upb_def_unref(ldef->def); return 0; } -static int lupb_enumdef_gc(lua_State *L) { - lupb_def *ldef = lupb_enumdef_check(L, 1); - upb_def_unref(ldef->def); - return 0; -} - static const struct luaL_Reg lupb_msgdef_mm[] = { {"__gc", lupb_msgdef_gc}, {NULL, NULL} @@ -105,6 +102,18 @@ static const struct luaL_Reg lupb_msgdef_m[] = { {NULL, NULL} }; +// enumdef + +static lupb_def *lupb_enumdef_check(lua_State *L, int narg) { + return luaL_checkudata(L, narg, "upb.enumdef"); +} + +static int lupb_enumdef_gc(lua_State *L) { + lupb_def *ldef = lupb_enumdef_check(L, 1); + upb_def_unref(ldef->def); + return 0; +} + static const struct luaL_Reg lupb_enumdef_mm[] = { {"__gc", lupb_enumdef_gc}, {NULL, NULL} @@ -115,6 +124,40 @@ static const struct luaL_Reg lupb_enumdef_m[] = { }; +/* lupb_fielddef **************************************************************/ + +typedef struct { + upb_fielddef *field; +} lupb_fielddef; + +static void lupb_fielddef_ref(void *cobj) { + upb_def_ref(UPB_UPCAST(((upb_fielddef*)cobj)->msgdef)); +} + +static void lupb_fielddef_getorcreate(lua_State *L, upb_fielddef *f) { + lupb_cache_getorcreate(L, f, "upb.fielddef", lupb_fielddef_ref, lupb_nop); +} + +static lupb_fielddef *lupb_fielddef_check(lua_State *L, int narg) { + return luaL_checkudata(L, narg, "upb.fielddef"); +} + +static int lupb_fielddef_gc(lua_State *L) { + lupb_fielddef *lfielddef = lupb_fielddef_check(L, 1); + upb_def_unref(UPB_UPCAST(lfielddef->field->msgdef)); + return 0; +} + +static const struct luaL_Reg lupb_fielddef_mm[] = { + {"__gc", lupb_fielddef_gc}, + {NULL, NULL} +}; + +static const struct luaL_Reg lupb_fielddef_m[] = { + {NULL, NULL} +}; + + /* lupb_symtab ****************************************************************/ typedef struct { @@ -193,7 +236,7 @@ static const struct luaL_Reg lupb_symtab_mm[] = { static int lupb_symtab_new(lua_State *L) { upb_symtab *s = upb_symtab_new(); - lupb_cache_getorcreate(L, s, "upb.symtab", lupb_symtab_unref); + lupb_cache_getorcreate(L, s, "upb.symtab", lupb_nop, lupb_symtab_unref); return 1; } diff --git a/tests/test_def.c b/tests/test_def.c index e6f95d7..732835d 100644 --- a/tests/test_def.c +++ b/tests/test_def.c @@ -14,6 +14,8 @@ int main() { } free(defs); + printf("Size: %zd\n", sizeof(upb_ntof_ent)); + upb_string *str = upb_strdupc("google.protobuf.FileDescriptorSet"); upb_def *fds = upb_symtab_lookup(s, str); assert(fds != NULL); -- cgit v1.2.3 From d3d939ab7fc14f73d1bb20a6e84a4428e6cde24a Mon Sep 17 00:00:00 2001 From: Joshua Haberman Date: Wed, 21 Jul 2010 21:54:36 -0700 Subject: Fix the case where no def is found for lookup. --- lang_ext/lua/upb.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'lang_ext/lua/upb.c') diff --git a/lang_ext/lua/upb.c b/lang_ext/lua/upb.c index a16a187..a8165c7 100644 --- a/lang_ext/lua/upb.c +++ b/lang_ext/lua/upb.c @@ -187,7 +187,11 @@ static int lupb_symtab_lookup(lua_State *L) { const char *name = luaL_checklstring(L, 2, &len); upb_string namestr = UPB_STACK_STRING_LEN(name, len); upb_def *def = upb_symtab_lookup(s->symtab, &namestr); - lupb_def_getorcreate(L, def); + if (def) { + lupb_def_getorcreate(L, def); + } else { + lua_pushnil(L); + } return 1; } -- cgit v1.2.3 From 672f4617e2ab7923806c6d6a44d16e128e16b3a4 Mon Sep 17 00:00:00 2001 From: Joshua Haberman Date: Wed, 21 Jul 2010 22:36:31 -0700 Subject: Lua support for fielddefs and getting their properties. --- core/upb_def.h | 4 +-- lang_ext/lua/upb.c | 79 +++++++++++++++++++++++++++++++++++++++++++++------- stream/upb_decoder.c | 2 +- 3 files changed, 72 insertions(+), 13 deletions(-) (limited to 'lang_ext/lua/upb.c') diff --git a/core/upb_def.h b/core/upb_def.h index 5c19a7a..3294a8d 100644 --- a/core/upb_def.h +++ b/core/upb_def.h @@ -158,13 +158,13 @@ typedef struct { // Looks up a field by name or number. While these are written to be as fast // as possible, it will still be faster to cache the results of this lookup if // possible. These return NULL if no such field is found. -INLINE upb_fielddef *upb_msg_itof(upb_msgdef *m, uint32_t num) { +INLINE upb_fielddef *upb_msgdef_itof(upb_msgdef *m, uint32_t num) { upb_itof_ent *e = (upb_itof_ent*)upb_inttable_fastlookup(&m->itof, num, sizeof(*e)); return e ? e->f : NULL; } -INLINE upb_fielddef *upb_msg_ntof(upb_msgdef *m, upb_string *name) { +INLINE upb_fielddef *upb_msgdef_ntof(upb_msgdef *m, upb_string *name) { upb_ntof_ent *e = (upb_ntof_ent*)upb_strtable_lookup(&m->ntof, name); return e ? e->f : NULL; } diff --git a/lang_ext/lua/upb.c b/lang_ext/lua/upb.c index a8165c7..5ab07ba 100644 --- a/lang_ext/lua/upb.c +++ b/lang_ext/lua/upb.c @@ -10,6 +10,10 @@ #include "lauxlib.h" #include "upb_def.h" +void lupb_pushstring(lua_State *L, upb_string *str) { + lua_pushlstring(L, upb_string_getrobuf(str), upb_string_len(str)); +} + /* object cache ***************************************************************/ // We cache all the lua objects (userdata) we vend in a weak table, indexed by @@ -38,6 +42,7 @@ static void lupb_cache_getorcreate(lua_State *L, void *cobj, const char *type, void **obj = lua_newuserdata(L, sizeof(void*)); *obj = cobj; luaL_getmetatable(L, type); + assert(!lua_isnil(L, -1)); // Should have been created by luaopen_upb. lua_setmetatable(L, -2); // Set it in the cache. @@ -83,22 +88,53 @@ static void lupb_def_getorcreate(lua_State *L, upb_def *def) { // msgdef -static lupb_def *lupb_msgdef_check(lua_State *L, int narg) { - return luaL_checkudata(L, narg, "upb.msgdef"); +static upb_msgdef *lupb_msgdef_check(lua_State *L, int narg) { + lupb_def *ldef = luaL_checkudata(L, narg, "upb.msgdef"); + return upb_downcast_msgdef(ldef->def); } static int lupb_msgdef_gc(lua_State *L) { - lupb_def *ldef = lupb_msgdef_check(L, 1); + lupb_def *ldef = luaL_checkudata(L, 1, "upb.msgdef"); upb_def_unref(ldef->def); return 0; } +static void lupb_fielddef_getorcreate(lua_State *L, upb_fielddef *f); + +static int lupb_msgdef_fieldbyname(lua_State *L) { + upb_msgdef *m = lupb_msgdef_check(L, 1); + size_t len; + const char *name = luaL_checklstring(L, 2, &len); + upb_string namestr = UPB_STACK_STRING_LEN(name, len); + upb_fielddef *f = upb_msgdef_ntof(m, &namestr); + if (f) { + lupb_fielddef_getorcreate(L, f); + } else { + lua_pushnil(L); + } + return 1; +} + +static int lupb_msgdef_fieldbynum(lua_State *L) { + upb_msgdef *m = lupb_msgdef_check(L, 1); + int num = luaL_checkint(L, 2); + upb_fielddef *f = upb_msgdef_itof(m, num); + if (f) { + lupb_fielddef_getorcreate(L, f); + } else { + lua_pushnil(L); + } + return 1; +} + static const struct luaL_Reg lupb_msgdef_mm[] = { {"__gc", lupb_msgdef_gc}, {NULL, NULL} }; static const struct luaL_Reg lupb_msgdef_m[] = { + {"fieldbyname", lupb_msgdef_fieldbyname}, + {"fieldbynum", lupb_msgdef_fieldbynum}, {NULL, NULL} }; @@ -142,6 +178,29 @@ static lupb_fielddef *lupb_fielddef_check(lua_State *L, int narg) { return luaL_checkudata(L, narg, "upb.fielddef"); } +static int lupb_fielddef_index(lua_State *L) { + lupb_fielddef *f = lupb_fielddef_check(L, 1); + const char *str = luaL_checkstring(L, 2); + if (strcmp(str, "name") == 0) { + lupb_pushstring(L, f->field->name); + } else if (strcmp(str, "number") == 0) { + lua_pushinteger(L, f->field->number); + } else if (strcmp(str, "type") == 0) { + lua_pushinteger(L, f->field->type); + } else if (strcmp(str, "label") == 0) { + lua_pushinteger(L, f->field->label); + } else if (strcmp(str, "def") == 0) { + upb_def_ref(f->field->def); + lupb_def_getorcreate(L, f->field->def); + } else if (strcmp(str, "msgdef") == 0) { + upb_def_ref(UPB_UPCAST(f->field->msgdef)); + lupb_def_getorcreate(L, UPB_UPCAST(f->field->msgdef)); + } else { + lua_pushnil(L); + } + return 1; +} + static int lupb_fielddef_gc(lua_State *L) { lupb_fielddef *lfielddef = lupb_fielddef_check(L, 1); upb_def_unref(UPB_UPCAST(lfielddef->field->msgdef)); @@ -150,10 +209,7 @@ static int lupb_fielddef_gc(lua_State *L) { static const struct luaL_Reg lupb_fielddef_mm[] = { {"__gc", lupb_fielddef_gc}, - {NULL, NULL} -}; - -static const struct luaL_Reg lupb_fielddef_m[] = { + {"__index", lupb_fielddef_index}, {NULL, NULL} }; @@ -206,7 +262,7 @@ static int lupb_symtab_getdefs(lua_State *L) { for (int i = 0; i < count; i++) { upb_def *def = defs[i]; upb_string *name = def->fqname; - lua_pushlstring(L, upb_string_getrobuf(name), upb_string_len(name)); + lupb_pushstring(L, name); lupb_def_getorcreate(L, def); // Add it to our return table. lua_settable(L, -3); @@ -255,14 +311,17 @@ static void lupb_register_type(lua_State *L, const char *name, luaL_newmetatable(L, name); luaL_register(L, NULL, mm); lua_createtable(L, 0, 0); - luaL_register(L, NULL, m); - lua_setfield(L, -2, "__index"); + if (m) { + luaL_register(L, NULL, m); + lua_setfield(L, -2, "__index"); + } lua_pop(L, 1); // The mt. } int luaopen_upb(lua_State *L) { lupb_register_type(L, "upb.msgdef", lupb_msgdef_m, lupb_msgdef_mm); lupb_register_type(L, "upb.enumdef", lupb_enumdef_m, lupb_enumdef_mm); + lupb_register_type(L, "upb.fielddef", NULL, lupb_fielddef_mm); lupb_register_type(L, "upb.symtab", lupb_symtab_m, lupb_symtab_mm); // Create our object cache. TODO: need to make this table weak! diff --git a/stream/upb_decoder.c b/stream/upb_decoder.c index 949ce2d..74ef5c5 100644 --- a/stream/upb_decoder.c +++ b/stream/upb_decoder.c @@ -342,7 +342,7 @@ again: } // Look up field by tag number. - upb_fielddef *f = upb_msg_itof(d->top->msgdef, field_number); + upb_fielddef *f = upb_msgdef_itof(d->top->msgdef, field_number); if (!f) { // Unknown field. If/when the upb_src interface supports reporting -- cgit v1.2.3