lua-users home
lua-l archive

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


Hi, 

I am writing  a simple LUA script and trying to run it on my ARM embedded device. I have compiled 5.2.3 version of the Lua source code. When I include any external libary in linit.c - my script crashes intermittently. 

static const luaL_Reg loadedlibs[] = {
  {"_G", luaopen_base},
 // {LUA_LOADLIBNAME, luaopen_package},
 // {LUA_COLIBNAME, luaopen_coroutine},

There is no specific line at which the code crashes...If I comment out the luaL_openlibs - the same script runs fine multiple iterations. Any inputs for this would be of great help.

thanks and Regards
Santosh


Below is my small code that I try to run..

/************************LUA Script****************/
char lua_test7[]="\
function add(a, b) \
    return a+b \
end \
 \
x = add(5, 6) \
y = add(9, 1, 6) \
mydbg(x) \
mydbg(y) \
 \
a = add \
x1 = a(5, 6) \
mydbg(x1) \
 \
 \
function split(num) \
    frac = num % 1 \
    dec = num - frac \
    return dec, frac \
end \
 \
x, y = split(45.96) \
mydbg(x) \
mydbg(y) \
 \
function greet(person) \
mydbg('Hello, ' .. person.firstname .. ' ' .. person.lastname)\
    mydbg(person.firstname) \
    mydbg(person.lastname) \
end \
 \
greet{firstname=\"John\", lastname=\"Doe\"} \
";
/************************LUA Script****************/

void test_lua(void)
{
lua_State *L ;
L = lua_newstate(custom_alloc,NULL);
luaL_openlibs(L);
do_lua(L, lua_test7);
lua_close(L);
}

void do_lua(lua_State *L , char *script)
{
int s;

lua_register(L, "mydbg", lua_mydbg);
s = luaL_loadstring(L,script);
if ( s==0 ) {
s = lua_pcall(L, 0, LUA_MULTRET, 0);
}
report_errors(L, s);
}


static void *custom_alloc (void *ud, void *ptr, size_t osize, size_t nsize) {
  static int totalmem = 0;
  (void)ud; (void)osize;  /* not used */
  if (nsize == 0) {
 totalmem -= osize;
 gtotalmem = totalmem;
    if(ptr)
    my_free(ptr,__FILE__, __LINE__);
    return NULL;
  }
  else {

if (ptr == NULL)
totalmem+=nsize;
else
{
 totalmem += (nsize-osize);
}
 gtotalmem = totalmem;
 return my_realloc(ptr, nsize,__FILE__,__LINE__);
  }
}

int mydbg(lua_State *L)
{
  int n=0;

  int argc = lua_gettop(L);

  for (n=1; n<=argc; ++n ) {
printf( "String [%s]", lua_tostring(L, n));
  }

  //lua_pushnumber(L, 0); // return value
  return 1; // number of return values
}

void report_errors(lua_State *L, int status)
{
  if ( status!=0 ) {
printf( "Error : [%s] \n",lua_tostring(L, -1));
    lua_pop(L, 1); // remove error message
  }
}

void *my_realloc ( void *oldBlock, size_t size, const char * fileName, const short lineNumber )
{
  if ( size == 0 )
  {
 free( oldBlock, fileName, lineNumber );
 oldBlock = NULL;
 return NULL;
  }
  else
  {
 void *newblock = NULL;
 // allocate a new block
 newblock = malloc( size, fileName, lineNumber );
 // overflow
 if ( newblock == NULL ) 
  {
       printf(  "Required [%d] , newblock NULL !!!!",size);//SAN
  return NULL;
  }
 if ( oldBlock )
 {
 memcpy( newblock, oldBlock, size );
 // erase (and check) old copy
 free( oldBlock, fileName, lineNumber );
 }
 return newblock;
  }
}