lua-users home
lua-l archive

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


I am new to lua and I am trying to run a simple test. Can you guys
help me on this problem?

I compiled the lua 5.1 lib using visual C and generated the lua51.dll,
luac.exe and lua.exe

I am trying to write one application to communicate with Lua.

It's very simple and small:

#include <stdio.h>

#include "../src/lua.h"
#include "../src/lualib.h"
#include "../src/lauxlib.h"

lua_State *m_hPrivateState;

int main( void )
    int sum, x=5, y=9;

    m_hPrivateState = lua_open(  );
    luaL_openlibs( m_hPrivateState );

    luaL_loadfile( m_hPrivateState, "teste.lua" );

	/* the function name */
	lua_getglobal(m_hPrivateState, "fadd");

	/* the first argument */
	lua_pushnumber(m_hPrivateState, x);

	/* the second argument */
	lua_pushnumber(m_hPrivateState, y);

	/* call the function with 2
	   arguments, return 1 result */
	lua_call( m_hPrivateState, 2, 1);

	/* get the result */
	sum = (int)lua_tointeger(m_hPrivateState, -1);
	lua_pop(m_hPrivateState, 1);

the teste.lua file is:
function fadd ( x, y )
	return x + y

when I run this program, I got the error message:
PANIC: unprotected error in call to Lua API (attempt to call a nil value)
Press any key to continue . . .

I am following a tutorial that I found on internet and it supposed to
run on lua version 5.1

Any help will be great