wplua: remove TypeClass, push constructors as ClassName_new

This commit is contained in:
George Kiagiadakis 2020-12-16 23:13:56 +02:00
parent 5edfc090c6
commit 70931969b4
2 changed files with 16 additions and 36 deletions

View file

@ -42,20 +42,6 @@ _wplua_openlibs (lua_State *L)
}
}
static int
_wplua_typeclass___call (lua_State *L)
{
luaL_checktype (L, 1, LUA_TTABLE);
lua_pushliteral (L, "new");
if (lua_rawget (L, 1) != LUA_TFUNCTION) {
luaL_error (L, "class has no constructor");
return 0;
}
lua_replace (L, 1);
lua_call (L, lua_gettop (L) - 1, LUA_MULTRET);
return lua_gettop (L);
}
lua_State *
wplua_new (void)
{
@ -74,17 +60,6 @@ wplua_new (void)
_wplua_init_gobject (L);
_wplua_init_closure (L);
{
static const luaL_Reg typeclass_meta[] = {
{ "__call", _wplua_typeclass___call },
{ NULL, NULL }
};
luaL_newmetatable (L, "TypeClass");
luaL_setfuncs (L, typeclass_meta, 0);
lua_pop (L, 1);
}
{
GHashTable *t = g_hash_table_new (g_direct_hash, g_direct_equal);
wplua_pushboxed (L, G_TYPE_HASH_TABLE, t);
@ -139,14 +114,18 @@ wplua_register_type_methods (lua_State * L, GType type,
/* register constructor */
if (constructor) {
luaL_Buffer b;
wp_debug ("Registering class for '%s'", g_type_name (type));
lua_newtable (L);
luaL_setmetatable (L, "TypeClass");
lua_pushliteral (L, "new");
luaL_buffinit (L, &b);
luaL_addstring (&b, g_type_name (type));
luaL_addchar (&b, '_');
luaL_addstring (&b, "new");
luaL_pushresult (&b);
lua_pushcfunction (L, constructor);
lua_settable (L, -3);
lua_setglobal (L, g_type_name (type));
lua_setglobal (L, lua_tostring (L, -2));
lua_pop (L, 1);
}
}

View file

@ -258,7 +258,7 @@ test_wplua_construct ()
l_test_object_new, l_test_object_methods);
const gchar code[] =
"o = TestObject.new()\n"
"o = TestObject_new()\n"
"assert (type(o) == 'userdata')\n";
wplua_load_buffer (L, code, sizeof (code) - 1, &error);
g_assert_no_error (error);
@ -284,7 +284,7 @@ test_wplua_properties ()
l_test_object_new, l_test_object_methods);
const gchar code[] =
"o = TestObject.new()\n"
"o = TestObject_new()\n"
"o['test-string'] = 'string from lua'\n"
"o['test-int'] = -15\n"
"o['test-uint'] = 1123456789\n"
@ -377,7 +377,7 @@ test_wplua_signals ()
l_test_object_new, l_test_object_methods);
const gchar code[] =
"o = TestObject.new()\n"
"o = TestObject_new()\n"
"\n"
"o:connect('acquire', function (obj)\n"
" assert(obj == o)\n"
@ -414,7 +414,7 @@ test_wplua_sandbox ()
const gchar code[] =
"SANDBOX_EXPORT = {\n"
" Test = TestObject.new,\n"
" Test = TestObject_new,\n"
" Table = { test = 'foobar' }\n"
"}\n";
wplua_load_buffer (L, code, sizeof (code) - 1, &error);
@ -423,14 +423,15 @@ test_wplua_sandbox ()
wplua_enable_sandbox (L);
const gchar code2[] =
"o = TestObject.new()\n";
"o = TestObject_new()\n";
wplua_load_buffer (L, code2, sizeof (code2) - 1, &error);
g_debug ("expected error: %s", error ? error->message : "null");
g_assert_error (error, WP_DOMAIN_LUA, WP_LUA_ERROR_RUNTIME);
g_clear_error (&error);
const gchar code3[] =
"o = Test()\n";
"o = Test()\n"
"o:toggle()\n";
wplua_load_buffer (L, code3, sizeof (code3) - 1, &error);
g_assert_no_error (error);