lua-users home
lua-l archive

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


Hi, there

Thank you for your responses...


Attaching the code, Please let me know if any issues within the calling...

Rgds
Santosh




/************************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****************/

=========================
static const luaL_Reg base_funcs[] = {
  {"assert", luaB_assert},
  {"collectgarbage", luaB_collectgarbage},
//  {"dofile", luaB_dofile},
  {"error", luaB_error},
  {"getmetatable", luaB_getmetatable},
  {"ipairs", luaB_ipairs},
//  {"loadfile", luaB_loadfile},
//  {"load", luaB_load},
#if defined(LUA_COMPAT_LOADSTRING)
  {"loadstring", luaB_load},
#endif
//  {"next", luaB_next},
//  {"pairs", luaB_pairs},
//  {"pcall", luaB_pcall},
//  {"print", luaB_print},
//  {"rawequal", luaB_rawequal},
//  {"rawlen", luaB_rawlen},
//  {"rawget", luaB_rawget},
//  {"rawset", luaB_rawset},
//  {"select", luaB_select},
//  {"setmetatable", luaB_setmetatable},
//  {"tonumber", luaB_tonumber},
//  {"tostring", luaB_tostring},
//  {"type", luaB_type},
//  {"xpcall", luaB_xpcall},
  {NULL, NULL}
};

void register_base_lib_funcs (lua_State *L) {
  const luaL_Reg *lib;
  /* call open functions from 'loadedlibs' and set results to global table */
  for (lib = base_funcs; lib->func; lib++) {
  XIP_UNIT_TEST_DBG_RELEASE( "LUA", "Registering Base lib Function [%s", lib->name);
  lua_register(L, lib->name,lib->func);
}
  return ;
}

===========================
void test_lua(void)
{
lua_State *L ;
L = lua_newstate(custom_alloc,NULL);
//luaL_openlibs(L);
register_base_lib_funcs (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;
  }
}

---------------------------



On Monday, 8 December 2014 5:55 PM, Alexey Melnichuk <alexeymelnichuck@gmail.com> wrote:


Hello, Santosh.


> Hi There,
>
> I am calling lua_register with a series of around 40-50 c functions in a for loop. Post this register call - I execute a small script, which doesn't make a call to any of the registered functions (although - calling registered functions is the final intent.)
>
> what I have observed that the LUA Crashes after a few line of statements. Please do let me know if there are any inputs in this regards.


May be you forgot NULL terminator in function list?


---
Это сообщение проверено на вирусы антивирусом Avast.
http://www.avast.com