static int PrivateTable__newindex(lua_State* L) { /* Sets the text of a specific entry. */ PrivateTable priv = checkPrivateTable(L,1); const gchar* name = luaL_checkstring(L,2); const gchar* string = NULL; if (lua_isstring(L,3)) { /* This also catches numbers, which is converted to string */ string = luaL_checkstring(L,3); } else if (lua_isboolean(L,3)) { /* We support boolean by setting a empty string if true and NULL if false */ string = lua_toboolean(L,3) ? "" : NULL; } else if (!lua_isnil(L,3)) { luaL_error(L,"unsupported type: %s", lua_typename(L,3)); return 0; } if (string) { g_hash_table_replace (priv->table, (gpointer) g_strdup(name), (gpointer) g_strdup(string)); } else { g_hash_table_remove (priv->table, (gconstpointer) name); } return 1; }
WSLUA_METAMETHOD PrivateTable__tostring(lua_State* L) { PrivateTable priv = checkPrivateTable(L,1); GString *key_string; GList *keys, *key; if (!priv) return 0; key_string = g_string_new (""); keys = g_hash_table_get_keys (priv->table); key = g_list_first (keys); while (key) { key_string = g_string_append (key_string, key->data); key = g_list_next (key); if (key) { key_string = g_string_append_c (key_string, ','); } } lua_pushstring(L,key_string->str); g_string_free (key_string, TRUE); g_list_free (keys); WSLUA_RETURN(1); /* A string with all keys in the table, mostly for debugging. */ }
static int PrivateTable__index(lua_State* L) { /* Gets the text of a specific entry. */ PrivateTable priv = checkPrivateTable(L,1); const gchar* name = luaL_checkstring(L,2); const gchar* string; string = (const gchar *)(g_hash_table_lookup (priv->table, name)); if (string) { lua_pushstring(L, string); } else { lua_pushnil(L); } return 1; }
static int PrivateTable__gc(lua_State* L) { PrivateTable priv = checkPrivateTable(L,1); if (!priv) return 0; if (!priv->expired) { priv->expired = TRUE; } else { if (priv->is_allocated) { g_hash_table_destroy (priv->table); } g_free(priv); } return 0; }
static int PrivateTable__index(lua_State* L) { /* Gets the text of a specific entry */ PrivateTable priv = checkPrivateTable(L,1); const gchar* name = luaL_checkstring(L,2); const gchar* string; if (! (priv && name) ) return 0; if (priv->expired) { luaL_error(L,"expired private_table"); return 0; } string = g_hash_table_lookup (priv->table, (gpointer) name); if (string) { lua_pushstring(L, string); } else { lua_pushnil(L); } return 1; }