All pastes #405995 Raw Edit

Untitled

public text v1 · immutable
·published 2007-03-22 02:07 UTC
rendered paste body
#include "lua.hpp"


class lua

{
    private:
	lua_State* L;	

    public:

        lua()

        {

            L = lua_open();

            luaL_openlibs(L);

        };
	
	lua(lua_State* P)
	{
		L = P;
	};



        ~lua()

        {

            lua_close(L);

        };



    void dofile(const char* fn);

    void dostring(const char* str);



    //stack queriers

    const char* getstring(int idx);

    double getnumber(int idx);



    //stack pushers

    void pushnumber(double i);

    void pushstring(const char* str);

    void register_func(const char* name, int ptr(lua_State*));
	
    //stack etc.
    void pop(int i);



    //debug

    const char* getError();

};


using namespace std;

void lua::dofile(const char* fn)
{
    cout<<"Opening: "<<fn<<endl;

    if (luaL_dofile(L,fn))
        cout<<"Error: ("<<this->getError()<<")"<<endl;
}

void lua::dostring(const char* str)
{
	if (luaL_loadstring(L,str) != 0 )
	{
		cout<<"Error: ("<<this->getError()<<") in string: ("<<str<<")"<<std::endl;
	}else{
		if (lua_pcall(L,0,0,0) != 0 )
		{
			cout<<"Error: ("<<this->getError()<<") while running string: ("<<str<<")"<<std::endl;
		}
	}
	
}

const char* lua::getError()
{
    const char* err = "";

    //this->stackDump();

    err = lua_tostring(L,-1);
    lua_pop(L,1);
    return err;
}

double lua::getnumber(int idx)
{
    return lua_tonumber(L,idx);
}

const char* lua::getstring(int idx)
{
    return lua_tostring(L,idx);
}

void lua::pushstring(const char* str)
{
    lua_pushstring(L,str);
}

void lua::pushnumber(double i)
{
    lua_pushnumber(L,i);
}

void lua::register_func(const char* name, int ptr(lua_State*))
{
    lua_register(L,name,ptr);
} 

void lua::pop(int i)
{
	lua_pop(L,i);
}