#include <stdlib.h>
#include <string.h>
#include "luaT.h"
void* luaT_alloc(lua_State *L, long size)
{
void *ptr;
if(size == 0)
return NULL;
if(size < 0)
luaL_error(L, "$ Torch: invalid memory size -- maybe an overflow?");
ptr = malloc(size);
if(!ptr)
luaL_error(L, "$ Torch: not enough memory: you tried to allocate %dGB. Buy new RAM!", size/1073741824);
return ptr;
}
void* luaT_realloc(lua_State *L, void *ptr, long size)
{
if(!ptr)
return(luaT_alloc(L, size));
if(size == 0)
{
luaT_free(L, ptr);
return NULL;
}
if(size < 0)
luaL_error(L, "$ Torch: invalid memory size -- maybe an overflow?");
ptr = realloc(ptr, size);
if(!ptr)
luaL_error(L, "$ Torch: not enough memory: you tried to reallocate %dGB. Buy new RAM!", size/1073741824);
return ptr;
}
void luaT_free(lua_State *L, void *ptr)
{
free(ptr);
}
void luaT_stackdump(lua_State *L)
{
int i;
const char *tname = NULL;
int top = lua_gettop(L);
for(i = 1; i <= top; i++)
{
int t = lua_type(L, i);
printf("%3d. ", i);
switch(t)
{
case LUA_TSTRING:
printf("'%s'", lua_tostring(L,i));
break;
case LUA_TBOOLEAN:
printf(lua_toboolean(L, i) ? "true" : "false");
break;
case LUA_TNUMBER:
printf("%g", lua_tonumber(L,i));
break;
case LUA_TUSERDATA:
tname = luaT_typename(L, i);
printf("userdata %lx [%s]", (long)lua_topointer(L, i), (tname ? tname : "not a Torch object"));
break;
case 10:
tname = luaT_typename(L, i);
printf("cdata %lx [%s]", (long)lua_topointer(L, i), (tname ? tname : "not a Torch object"));
break;
case LUA_TTABLE:
lua_pushvalue(L, i);
lua_rawget(L, LUA_REGISTRYINDEX);
if(lua_isstring(L, -1))
tname = lua_tostring(L, -1); /*luaT_typenameid(L, lua_tostring(L, -1)); */
else
tname = NULL;
lua_pop(L, 1);
if(tname)
printf("metatable [%s]", tname);
else
{
tname = luaT_typename(L, i);
printf("table %lx [%s]", (long)lua_topointer(L, i), (tname ? tname : "not a Torch object"));
}
break;
default:
printf("Lua object type: %s", lua_typename(L,t));
break;
}
printf("\n");
}
printf("---------------------------------------------\n");
}
/* metatable operator methods */
static int luaT_mt__index(lua_State *L);
static int luaT_mt__newindex(lua_State *L);
static int luaT_mt__tostring(lua_State *L);
static int luaT_mt__add(lua_State *L);
static int luaT_mt__sub(lua_State *L);
static int luaT_mt__mul(lua_State *L);
static int luaT_mt__div(lua_State *L);
static int luaT_mt__mod(lua_State *L);
static int luaT_mt__pow(lua_State *L);
static int luaT_mt__unm(lua_State *L);
static int luaT_mt__concat(lua_State *L);
static