lua-users home
lua-l archive

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


Hi.

I'm having problems loading lua functions in c. I'm using version 5.1
When running the program below, this line in luaV_gettable will return a blank res.
const TValue *res = luaH_get(h, key); /* do a primitive get */

luaH_get will call luaH_getstr, and the in the line
Node *n = hashstr(t, key);
hashstr function returns a blank node, so the first call to gnext will return 0, and the function will return a nil lua object.

I've tested it on cygwin and windows, and I get the same result (no result :/ ).

Does anyone know whats wrong?

/* test.c */
#include <stdio.h>
#include "lua.h"
#include "lauxlib.h"
#include "lualib.h"

int main()
{
 int result;
 lua_State *L;

 L = luaL_newstate();
 luaL_openlibs(L);

 result = luaL_loadfile(L, "foobar.lua"); /* result == 0, so this works ok. */

 lua_getfield(L, LUA_GLOBALSINDEX, "itworks"); /* pushes a nil value to the stack instead of my function*/

 if (lua_pcall(L, 0, 0, 0) != 0)
   printf("%s\n", lua_tostring(L, -1)); /* unable to call a nil value */

 lua_close(L);
 return 0;
}

-- foobar.lua
function itworks () print("it finally works") end


-Simen