lua-users home
lua-l archive

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



One cannot use the dot notation as an index (an index could contain dots, so your code is looking for _G["vom.getloop"] ):

	lua_getglobal(L,"vom.getloop");

-->

	lua_checkstack( L, 1 );

	lua_getglobal( L, "vom" );
	assert( lua_istable( L, -1 );
		//
		// [-1]: vom table

	lua_pushliteral( L, "getloop" );
	lua_gettable( L, -2 );
		//
		// [-1]: vom.getloop


Lua has 'lua_getfield' for accessing numerical indices of a global table. Maybe it could also have similar for string fields, though making your own help routine is trivial.

-asko
	


Marc Vollmer kirjoitti 6.1.2008 kello 1:49:

Hello.

I have wrote a small library "vom" for LUA. The interpreter works fine:

--begin output --
Lua 5.1.1  Copyright (C) 1994-2006 Lua.org, PUC-Rio
require "vom"
print(vom.getloop())
25

-- end output --

Now, I want to call the function "vom.getloop()" from the API, but I
don't have any results. I try this:

-- begin source --
LUASTATE init(void) {
  //lua_State *L = lua_open();
  lua_State *L = luaL_newstate();
  printf("L:%d\n",L);
  luaL_openlibs(L);
  luaL_dofile(L,"testlib.lua");
  return L;
}

double get_counter(LUASTATE L) {
  double tmp;
  lua_getglobal(L,"vom.getloop");
  lua_pushnil(L);
  lua_resume(L,1);
  tmp = lua_tonumber(L,-1);
  printf("Error:%s",lua_tostring(L,-2));
  return tmp;
}
-- end source--

At first I make a "init" and load the "testlib.lua", because here is the
the require "vom" integrated.

The error message is:
Error:attempt to call a nil value

It is possible to call the function "vom.getloop()" directly from the
API, in this case from the C-Function "get_counter" ???

THX
Marc