summaryrefslogtreecommitdiff
path: root/iup/srclua5/iuplua.c
diff options
context:
space:
mode:
Diffstat (limited to 'iup/srclua5/iuplua.c')
-rwxr-xr-xiup/srclua5/iuplua.c118
1 files changed, 50 insertions, 68 deletions
diff --git a/iup/srclua5/iuplua.c b/iup/srclua5/iuplua.c
index c10d95e..4158990 100755
--- a/iup/srclua5/iuplua.c
+++ b/iup/srclua5/iuplua.c
@@ -91,10 +91,12 @@ static int traceback (lua_State *L) {
lua_pushliteral(L, "");
lua_pushinteger(L, 2); /* skip this function */
lua_call(L, 2, 1); /* call debug.traceback */
+
lua_getglobal(L, "iup"); /* store traceback in iup._LASTTRACEBACK */
lua_pushstring(L, "_LASTTRACEBACK");
lua_pushvalue(L, -3);
lua_settable(L, -3);
+
lua_pop(L, 2);
return 1;
}
@@ -107,7 +109,8 @@ static int docall (lua_State *L, int narg, int nret)
lua_insert(L, base); /* put it under chunk and args */
status = lua_pcall(L, narg, nret, base);
lua_remove(L, base); /* remove traceback function */
- if (status != 0) {
+ if (status != 0)
+ {
/* force a complete garbage collection in case of errors */
lua_gc(L, LUA_GCCOLLECT, 0);
/* put _LASTTRACEBACK at stack position 2 */
@@ -115,14 +118,17 @@ static int docall (lua_State *L, int narg, int nret)
lua_pushliteral(L, "_LASTTRACEBACK");
lua_gettable(L, -2);
lua_remove(L, -2);
- if (!lua_isstring(L, -1)) {
+ if (!lua_isstring(L, -1))
+ {
lua_pop(L, 1);
lua_pushliteral(L, "");
+
/* set _LASTTRACEBACK as nil */
lua_getglobal(L, "iup");
lua_pushliteral(L, "_LASTTRACEBACK");
lua_pushnil(L);
lua_settable(L, -3);
+
lua_pop(L, 1);
}
}
@@ -330,9 +336,9 @@ lua_State* iuplua_call_start(Ihandle *ih, const char* name)
{
lua_State *L = iuplua_getstate(ih);
- /* prepare to call iupCallMethod(name, handle, ...) */
+ /* prepare to call iup.CallMethod(name, handle, ...) */
lua_getglobal(L,"iup");
- lua_pushstring(L,"iupCallMethod");
+ lua_pushstring(L,"CallMethod");
lua_gettable(L, -2);
lua_remove(L, -2);
@@ -380,14 +386,16 @@ int iuplua_call_raw(lua_State* L, int nargs, int nresults)
void iuplua_register_cb(lua_State *L, const char* name, lua_CFunction func, const char* type)
{
- lua_getglobal(L, "RegisterCallback");
+ lua_getglobal(L,"iup");
+ lua_pushstring(L,"RegisterCallback");
+ lua_gettable(L, -2);
lua_pushstring(L, name);
lua_pushcfunction(L, func);
lua_pushstring(L, type);
lua_call(L, 3, 0);
}
-/* iupSetCallback(handle, name, func, value) */
+/* iup.SetCallback(handle, name, func, value) */
static int SetCallback(lua_State *L)
{
Icallback func;
@@ -415,10 +423,10 @@ static int SetCallback(lua_State *L)
/*************************************/
/* metatable */
-/* iupNewClass(class_name)
+/* iup.NewClass(class_name)
Calls:
- iupNewClass("iup handle")
- iupNewClass("iup widget")
+ iup.NewClass("iup handle")
+ iup.NewClass("iup widget")
*/
static int NewClass(lua_State *L)
{
@@ -430,10 +438,10 @@ static int NewClass(lua_State *L)
return 0;
}
-/* iupSetClass(t, class_name)
+/* iup.SetClass(t, class_name)
Calls:
- iupSetClass(handle, "iup handle") --Used only in RegisterHandle and WIDGET.constructor
- iupSetClass(object, "iup widget") --Used whenever a new control class is created.
+ iup.SetClass(handle, "iup handle") --Used only in iup.RegisterHandle and WIDGET.constructor
+ iup.SetClass(object, "iup widget") --Used whenever a new control class is created.
*/
static int SetClass(lua_State *L)
{
@@ -447,7 +455,7 @@ static int SetClass(lua_State *L)
return 0;
}
-/* class_name = iupGetClass(t) */
+/* class_name = iup.GetClass(t) */
static int GetClass(lua_State *L)
{
if (lua_istable(L, 1) || lua_isuserdata(L, 1))
@@ -464,9 +472,9 @@ static int GetClass(lua_State *L)
return 1;
}
-/* iupSetMethod(class_name, method, function)
+/* iup.SetMethod(class_name, method, function)
For ex:
- iupSetMethod("iup handle", "__index", ihandle_gettable)
+ iup.SetMethod("iup handle", "__index", ihandle_gettable)
*/
static int SetMethod(lua_State *L)
{
@@ -507,7 +515,7 @@ static int ihandle_compare(lua_State *L)
/*************************************/
/* table <-> ihandle */
-/* local object = iupGetWidget(handle) */
+/* local object = iup.GetWidget(handle) */
static int GetWidget(lua_State *L)
{
/* Pushes a table that is associanted with an ihandle */
@@ -521,7 +529,7 @@ static int GetWidget(lua_State *L)
return 1;
}
-/* iupSetWidget(handle, object) */
+/* iup.SetWidget(handle, object) */
static int SetWidget(lua_State *L)
{
/* Saves the object table reference as an attribute,
@@ -540,18 +548,9 @@ static int SetWidget(lua_State *L)
/*************************************/
/* registration */
-void iuplua_changeEnv(lua_State *L)
+void iuplua_get_env(lua_State *L)
{
- /* Replaces global environment */
- lua_pushvalue(L, LUA_GLOBALSINDEX);
lua_getglobal(L, "iup");
- lua_replace(L, LUA_GLOBALSINDEX);
-}
-
-void iuplua_returnEnv(lua_State *L)
-{
- /* reestablishes global environment */
- lua_replace(L, LUA_GLOBALSINDEX);
}
int iuplua_opencall_internal(lua_State * L)
@@ -568,16 +567,18 @@ int iuplua_opencall_internal(lua_State * L)
return ret;
}
+/* iup[name] = func */
void iuplua_register(lua_State *L, lua_CFunction func, const char* name)
{
lua_pushcfunction(L, func);
- lua_setglobal(L, name);
+ lua_setfield(L, -2, name);
}
+/* iup[name] = s */
void iuplua_regstring(lua_State *L, const char* s, const char* name)
{
lua_pushstring(L, s);
- lua_setglobal(L, name);
+ lua_setfield(L, -2, name);
}
/*****************************************************************************
@@ -675,7 +676,8 @@ static int GetFromC(lua_State *L)
static void register_key(char *name, int code, void* user_data)
{
lua_State *L = (lua_State*)user_data;
- lua_pushnumber(L, code); lua_setglobal(L, name);
+ lua_pushnumber(L, code);
+ lua_setfield(L, -2, name);
}
/* from iupkey.c */
@@ -740,29 +742,23 @@ static void setinfo (lua_State *L)
{
/* table "iup" is at the stack */
- lua_pushliteral (L, "_COPYRIGHT");
lua_pushliteral (L, IUP_COPYRIGHT);
- lua_settable (L, -3);
+ lua_setfield(L, -2, "_COPYRIGHT");
- lua_pushliteral (L, "_DESCRIPTION");
lua_pushliteral (L, IUP_DESCRIPTION);
- lua_settable (L, -3);
+ lua_setfield(L, -2, "_DESCRIPTION");
- lua_pushliteral (L, "_NAME");
lua_pushliteral (L, IUP_NAME);
- lua_settable (L, -3);
+ lua_setfield(L, -2, "_NAME");
- lua_pushliteral (L, "_VERSION");
lua_pushstring (L, IupVersion());
- lua_settable (L, -3);
+ lua_setfield(L, -2, "_VERSION");
- lua_pushliteral (L, "_VERSION_DATE");
lua_pushliteral (L, IUP_VERSION_DATE);
- lua_settable (L, -3);
+ lua_setfield(L, -2, "_VERSION_DATE");
- lua_pushliteral (L, "_VERSION_NUMBER");
lua_pushinteger (L, IupVersionNumber());
- lua_settable (L, -3);
+ lua_setfield(L, -2, "_VERSION_NUMBER");
}
int iuplua_open(lua_State * L)
@@ -775,13 +771,13 @@ int iuplua_open(lua_State * L)
{"Close", iuplua_close},
{"SetIdle", SetIdle},
{"GetFromC", GetFromC},
- {"iupGetWidget", GetWidget},
- {"iupSetWidget", SetWidget},
- {"iupNewClass", NewClass},
- {"iupSetClass", SetClass},
- {"iupGetClass", GetClass},
- {"iupSetMethod", SetMethod},
- {"iupSetCallback", SetCallback},
+ {"GetWidget", GetWidget},
+ {"SetWidget", SetWidget},
+ {"NewClass", NewClass},
+ {"SetClass", SetClass},
+ {"GetClass", GetClass},
+ {"SetMethod", SetMethod},
+ {"SetCallback", SetCallback},
{"ihandle_compare", ihandle_compare},
{"ihandle_tostring", ihandle_tostring},
{NULL, NULL},
@@ -793,35 +789,22 @@ int iuplua_open(lua_State * L)
ret = lua_tointeger(L, -1); /* retrieve IupOpen return value */
lua_pop(L, -1);
- /* Creating global namespace iup */
- lua_newtable(L);
- lua_setglobal(L, "iup");
-
/* Registers functions in iup namespace */
- luaL_openlib(L, "iup", funcs, 0);
+ luaL_register(L, "iup", funcs); /* leave "iup" table at the top of the stack */
iupluaapi_open(L);
/* set version info */
setinfo(L);
/* register if IupOpen was called here or from outside IupLua */
- lua_pushliteral (L, "_IUPOPEN_CALL");
+ /* iup._IUPOPEN_CALL = EXTERNAL|INTERNAL */
if (ret == IUP_OPENED) lua_pushliteral (L, "EXTERNAL");
else lua_pushliteral (L, "INTERNAL");
- lua_settable (L, -3);
+ lua_setfield(L, -2, "_IUPOPEN_CALL");
- /* used by Idle */
+ /* used by Idle in Lua */
IupSetGlobal("_IUP_LUA_DEFAULT_STATE", (char *) L);
- /* Creating new environment with metamethod __index
- * (so that normal Lua functions will be found while GLOBALSINDEX is the "iup" environment) */
- iuplua_dostring(L, "iup._G = _G\
- setmetatable(iup, {__index = iup._G})", "iuplua_setmetatable_global");
-
- /* Changing environment to iup
- * (all created variables and functions from now on will be put in "iup" environment) */
- iuplua_changeEnv(L);
-
#include "clua/iuplua.clua"
#include "clua/constants.clua"
@@ -857,6 +840,7 @@ int iuplua_open(lua_State * L)
iupzboxlua_open(L);
iuptimerlua_open(L);
iupsboxlua_open(L);
+ iupsplitlua_open(L);
iupspinlua_open(L);
iupspinboxlua_open(L);
iupcboxlua_open(L);
@@ -875,8 +859,6 @@ int iuplua_open(lua_State * L)
iuptreelua_open(L);
iupclipboardlua_open(L);
- iuplua_returnEnv(L);
-
return 0; /* nothing in stack */
}