lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


I have one remaining problem in trying to embed the
latest Lua 5.0 release.

To recap I install an index hook as follows:

  lua_newtable(pLuaState);
  lua_pushstring(pLuaState, "__index");
  lua_pushcfunction(pLuaState, zeus_hook);
  lua_settable(pLuaState, -3);
  lua_setmetatable(pLuaState, LUA_GLOBALSINDEX);

and this works fine since my zeus_hook is called. The zeus_hook
then redirects the call to the zeus_function and this is also
appears to be working just fine :)

The code for the both the zeus_hook and zeus_function are shown
at the end of this e-mail.

But now to the problem is this. If I try to run the following
Zeus macro:

 function key_macro()
   screen_update_disable()
   set_clipboard_text("test")
 end

the hook and function methods work fine for the call to:

    screen_update_disable

but fail on the call to:

    set_clipboard_text

When set_clipboard_text is called the "test" variable is
correctly popped of the Lua call stack but when I try to
get the function name using this code:

  String szFunction = luaL_check_lstr(pLuaState, 1, 0);

the function name is "test" and not "set_clipboard_text".

So basically my current code works great provided I do not
try run a macro function that takes arguments.

It appears the call stack is not quite correct :(

Can any one see where the problem might be?

Do I have to use a global variable for the function name?

Thanks in advance.

Jussi


int MacroLua::zeus_hook(lua_State *pLuaState)
{
  //--   in: table, index
  //--  out: closed zeus_func
  //-- copy global name
  lua_pushvalue(pLuaState, 2);

  //-- bind it to zeus_func as upvalue
  lua_pushcclosure(pLuaState, zeus_function, 1);

  return 1;
}

int MacroLua::zeus_function(lua_State *pLuaState)
{
  //-- get the number of arguments on the stack
  int n = lua_gettop(pLuaState);

  for (int i = 1; i <= n; i++) {
    //-- add lua stack variables to zeus argument list
    argList.append(new xArgument(pLuaState, i));
  }

  //????
  //-- push macro name (first upvalue)
  lua_pushvalue(pLuaState, lua_upvalueindex(1));

  //-- get the Zeus function to be run
  String szFunction = luaL_check_lstr(pLuaState, 1, 0);
  //????

  //-- try to run the Zeus function
  if (xMacro::pInterface->run(szFunction)) {
    //-- process the return value
  }
  else {
    //-- display an error message
    fprintf(stderr, "Error running '%s' function\n", szFunction);

    //-- stop the macro
    lua_error(pLuaState, "Macro terminated!\n");
  }

  return 1;
}