[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: First lua program from cpp
- From: Lloyd <lloyd@...>
- Date: Tue, 12 May 2009 11:15:05 +0530
Hi,
I am a lua newbie. This is my first program to print "Hello world" using
C API. It gives me runtime error!. Where is the problem?
Thank you very much,
Lloyd
#include <iostream>
using namespace std;
extern "C"
{
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
}
static void *my_lua_alloc (void *ud, void *ptr, size_t osize,size_t nsize)
{
(void)ud; (void)osize; //not used
if (nsize == 0)
{
free(ptr);
return NULL;
}
else
return realloc(ptr, nsize);
}
const char* my_lua_reader (lua_State *L,void *data,size_t *size)
{
static int x;
if(x==0)
{
const char *a="print(\"Hello world\")";
*size=strlen(a);
++x;
return a;
}
else
{
*size=0;
data=NULL;
}
return NULL;
}
int main()
{
void *ud=NULL;
lua_State* L=lua_newstate(my_lua_alloc,ud);
if(L==NULL)
{
cout<<"Memory allocation error"<<endl;
return 0;
}
int r=lua_load(L,my_lua_reader,ud,"Test Chunk");
if(r!=0)
{
if(r==LUA_ERRSYNTAX)
{
cout<<"Syntax error in the supplied chunk"<<endl;
}
else if(r==LUA_ERRMEM)
{
cout<<"Memory allocation error"<<endl;
return 0;
}
}
r=lua_pcall(L,0,0,0);
if(r!=0)
{
if(r==LUA_ERRRUN)
{
cout<<"Runtime error"<<endl;
return 0;
}
else if(r==LUA_ERRMEM)
{
cout<<"Memory allocation error"<<endl;
return 0;
}
else if(r==LUA_ERRERR)
{
cout<<"Error handler running error"<<endl;
return 0;
}
}
lua_close(L);
return 0;
}
______________________________________
Scanned and protected by Email scanner