Sign in to follow this  
  • entries
    122
  • comments
    121
  • views
    68407

Lua 5.1.1

Sign in to follow this  
Twisol

82 views

Well, Scavenger (on IRC) may/may not be joining Project Harbinger part-time, sinec Programmer16 wants to devote a little more time to Ascension. Meanwhile, I've found a new hobby, which might (read: WILL) make people think I'm crazy, or a loon. Check it out, though, it does make some sense.

On the subject of loons, I'm trying to get into Lua 5.1.1. I'm having MASSIVE trouble getting the luaopen_ functions to work, since they have to be pushed onto the Lua stack, then their arguments, then called from the stack. And apparently, when I call, I'm trying to call a nil value. Here's my code, from memory (since I'm on another computer):


lua_State *L = lua_open();

lua_getfield(L, /**/, "luaopen_io");
lua_pcall(L, 0, 0, 0);



When I check for errors (using a handy function I found on the net), I get a "calling a nil value" or whatever error, so apparently I'm not pushing luaopen_io properly, or using lua_pcall right (but I've tried it every which way). Help would be VERY appreciated... [sad]
Sign in to follow this  


4 Comments


Recommended Comments

I've been messing around with lua lately...

I believe luaopen_*() functions are actually part of the lua c api. See lualib.h ( which is included indirecly via lua.hpp if thats what you use ).

Seems to work for me anyway.

HTH

Share this comment


Link to comment
Ehm, yeah. Read the Lua 5.1.1 docs, it states clearly you need to call luaopen_ from the Lua stack, by pushing it on, then its arguments, then calling it.

(but yeah, I use <lua.hpp> too.)

Share this comment


Link to comment
Quote:
Original post by linkofazeroth
Ehm, yeah. Read the Lua 5.1.1 docs, it states clearly you need to call luaopen_ from the Lua stack, by pushing it on, then its arguments, then calling it.

(but yeah, I use <lua.hpp> too.)


Really... I think I must have my lua initialisation code from an old tutorial or something probably. Sorry bout that.

Well, I tried it on my lua and the function luaopen_io is nil, so its obviously unavailable to raw lua. So maybe you have to set the function value via lua_register or something?

But why expose the functions via the c api if they are designed to be called from lua and not c?

More research...

Ok, well this works for me ( but so does calling them directly, so... [smile] )

state = luaL_newstate();

if( state == NULL )
{
// throw exception
}
/*
luaopen_base(state);
luaopen_table(state);
luaopen_string(state);
luaopen_math(state);
luaopen_io(state);
*/
registerFunction("luaopen_base",luaopen_base);
registerFunction("luaopen_table",luaopen_table);
registerFunction("luaopen_string",luaopen_string);
registerFunction("luaopen_math",luaopen_math);
registerFunction("luaopen_io",luaopen_io);

where register function is like:


[edit: and script function is a typedef of the lua function prototype ]
registerFunction( const std::string &name, ScriptFunction function )
{
lua_register( state, name.c_str(), function );
}


Then either calling it directly from lua code or via lua_getglobal and lua_pcall...

Share this comment


Link to comment
The LUA tutorials I work off of are here. They're pretty decent and I'm pretty sure they're up-to-date.

I'll be on tonight around 5pm EST (it's 1:30pm now) for sure this time.

Share this comment


Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now