lua-users home
lua-l archive

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


--- In lua-l@yahoogroups.com, "paul_winwood" <paul_winwood@y...> 
wrote:

> With Lua 5 globals are just another table accessed by the special 
> index LUA_GLOBALSINDEX, therefore the getglobal tag method is 
> redundant. You should be able to set a metatable on the globals 
> table and then install __newindex or __index or __call (as 
> required) metamethods. 

I took your advice an did some more research into metatables but 
unfortunately with not a great deal of luck :(

What I did was try to use the "__call" index to trap the macro 
function calls using the following code snipet:

  //-- create table on the stack
  lua_newtable(pLuaState);
  lua_pushstring(pLuaState, "__call");
  lua_pushcfunction(pLuaState, zeus_hook);

  //-- populate table with __call = zeus_hook
  lua_settable(pLuaState, -3);

  //-- set new meta-table
  lua_setmetatable(pLuaState, LUA_GLOBALSINDEX);

Then I run this script inside the editor:

  function key_macro()
  	screen_update_disable()
  	set_line_text("this is a test\n")
  	screen_update_enable()
  	screen_update()
  end
  
  key_macro() -- run the macro

but even though the macro contains several function calls my 
call hook does not get triggered. 

The macro does seem to be running run since the line hook that 
was added using:

  lua_sethook(pLuaState, linehook, LUA_MASKLINE, 0);

does get called.

Next just out of curiosity I changed the code to read:

  //-- create table on the stack
  lua_newtable(pLuaState);
  lua_pushstring(pLuaState, "__index");
  lua_pushcfunction(pLuaState, zeus_hook);

and this time my call hook gets trigger. But of course since 
the hook is expecting a function object on the stack things 
go wrong.

So my question is am I using the __call metatable correctly?

Thanks in advance.

Cheers Jussi