Bind enum values

This commit is contained in:
Yevgen Muntyan 2011-01-22 02:10:12 -08:00
parent e5328da2b7
commit 170f823f5a
4 changed files with 71 additions and 14 deletions

View File

@ -438,9 +438,6 @@ static void *%(func)s (void)
all_method_cfuncs[cls.name] = method_cfuncs all_method_cfuncs[cls.name] = method_cfuncs
all_static_method_cfuncs[cls.short_name] = static_method_cfuncs all_static_method_cfuncs[cls.short_name] = static_method_cfuncs
# for enum in module.get_enums():
# self.__write_enum_decl(enum)
dic = dict(module=module.name.lower()) dic = dict(module=module.name.lower())
all_func_cfuncs = [] all_func_cfuncs = []
@ -485,6 +482,10 @@ void %(module)s_lua_api_add_to_lua (lua_State *L, const char *package_name)
continue continue
self.out.write(' moo_lua_register_static_methods (L, package_name, "%s", %s_lua_functions);\n' % (cls_name, cls_name)) self.out.write(' moo_lua_register_static_methods (L, package_name, "%s", %s_lua_functions);\n' % (cls_name, cls_name))
self.out.write('\n')
for enum in module.get_enums():
self.out.write(' moo_lua_register_enum (L, package_name, %s, "%s");\n' % (enum.gtype_id, module.name.upper() + '_'))
self.out.write("}\n") self.out.write("}\n")
del self.module del self.module

View File

@ -4,6 +4,7 @@
#include "mooutils/mooutils.h" #include "mooutils/mooutils.h"
#include <vector> #include <vector>
#include <string> #include <string>
#include <string.h>
MOO_DEFINE_QUARK_STATIC ("moo-lua-methods", moo_lua_methods_quark) MOO_DEFINE_QUARK_STATIC ("moo-lua-methods", moo_lua_methods_quark)
@ -263,6 +264,57 @@ moo_lua_register_static_methods (lua_State *L,
} }
static void
moo_lua_add_enum_value (lua_State *L,
const char *name,
int value,
const char *prefix)
{
if (g_str_has_prefix (name, prefix))
name += strlen (prefix);
lua_pushinteger (L, value);
lua_setfield (L, -2, name);
}
void
moo_lua_register_enum (lua_State *L,
const char *package_name,
GType type,
const char *prefix)
{
lua_getfield (L, LUA_GLOBALSINDEX, package_name);
g_return_if_fail (!lua_isnil (L, -1));
if (g_type_is_a (type, G_TYPE_ENUM))
{
GEnumClass *klass = (GEnumClass*) g_type_class_peek_static (type);
g_return_if_fail (klass != NULL);
for (guint i = 0; i < klass->n_values; ++i)
moo_lua_add_enum_value (L,
klass->values[i].value_name,
klass->values[i].value,
prefix);
}
else if (g_type_is_a (type, G_TYPE_FLAGS))
{
GFlagsClass *klass = (GFlagsClass*) g_type_class_peek_static (type);
g_return_if_fail (klass != NULL);
for (guint i = 0; i < klass->n_values; ++i)
moo_lua_add_enum_value (L,
klass->values[i].value_name,
klass->values[i].value,
prefix);
}
else
{
g_return_if_reached ();
}
lua_pop (L, 1);
}
static LGInstance * static LGInstance *
get_arg_instance (lua_State *L, int arg, const char *param_name) get_arg_instance (lua_State *L, int arg, const char *param_name)
{ {
@ -697,33 +749,29 @@ signal_closure_marshal (SignalClosure *closure,
g_return_if_fail (L != NULL); g_return_if_fail (L != NULL);
int n_pushed = 0; int old_top = lua_gettop (L);
try try
{ {
lua_rawgeti (L, LUA_REGISTRYINDEX, closure->cb_ref); lua_rawgeti (L, LUA_REGISTRYINDEX, closure->cb_ref);
n_pushed++;
int n_args = 0;
for (guint i = 0; i < n_param_values; ++i) for (guint i = 0; i < n_param_values; ++i)
{ {
int pushed_here = push_param_gvalue (L, &param_values[i]); int pushed_here = push_param_gvalue (L, &param_values[i]);
if (!pushed_here) if (!pushed_here)
goto out; goto out;
n_pushed += pushed_here; n_args += pushed_here;
} }
// ok, pushed the arguments successfully // ok, pushed the arguments successfully
int n_args = n_pushed - 1;
n_pushed = 0;
if (lua_pcall (L, n_args, 1, 0) == 0) if (lua_pcall (L, n_args, 1, 0) == 0)
{ {
if (return_value != NULL) if (return_value != NULL)
get_ret_gvalue (L, return_value); get_ret_gvalue (L, return_value);
lua_pop (L, 1);
#ifdef MOO_ENABLE_COVERAGE #ifdef MOO_ENABLE_COVERAGE
moo_test_coverage_record ("lua", closure->full_signal_name); moo_test_coverage_record ("lua", closure->full_signal_name);
#endif #endif
@ -732,7 +780,6 @@ signal_closure_marshal (SignalClosure *closure,
{ {
const char *msg = lua_tostring (L, -1); const char *msg = lua_tostring (L, -1);
g_critical ("%s", msg ? msg : "ERROR"); g_critical ("%s", msg ? msg : "ERROR");
lua_pop (L, 1);
} }
} }
catch (...) catch (...)
@ -740,9 +787,9 @@ signal_closure_marshal (SignalClosure *closure,
g_critical ("oops"); g_critical ("oops");
} }
out: out:
if (n_pushed > 0) lua_settop (L, old_top);
lua_pop (L, n_pushed);
} }
static GClosure *signal_closure_new (lua_State *L, GObject *instance, int cb_ref) static GClosure *signal_closure_new (lua_State *L, GObject *instance, int cb_ref)

View File

@ -27,10 +27,15 @@ MooLuaMethod moo_lua_lookup_method (lua_State *L,
const char *meth); const char *meth);
void moo_lua_register_methods (GType type, void moo_lua_register_methods (GType type,
MooLuaMethodEntry *entries); MooLuaMethodEntry *entries);
void moo_lua_register_static_methods (lua_State *L, void moo_lua_register_static_methods (lua_State *L,
const char *package_name, const char *package_name,
const char *class_name, const char *class_name,
const luaL_Reg *methods); const luaL_Reg *methods);
void moo_lua_register_enum (lua_State *L,
const char *package_name,
GType type,
const char *prefix);
void moo_lua_register_gobject (void); void moo_lua_register_gobject (void);

View File

@ -17,6 +17,10 @@
* class:MooWindow: (parent GtkWindow) * class:MooWindow: (parent GtkWindow)
**/ **/
/**
* enum:MooCloseResponse
**/
#include "mooutils/moowindow.h" #include "mooutils/moowindow.h"
#include "mooutils/mooaction-private.h" #include "mooutils/mooaction-private.h"
#include "mooutils/mooactionbase-private.h" #include "mooutils/mooactionbase-private.h"