コード例 #1
0
static void nnOmp_(SpatialMaxPooling_init)(lua_State *L)
{
  luaT_pushmetaclass(L, torch_(Tensor_id));
  luaT_registeratname(L, nnOmp_(SpatialMaxPooling__), "nn");
  lua_pop(L,1);
}
コード例 #2
0
ファイル: luaT.c プロジェクト: Johnson13/xLearn
int luaT_lua_getmetatable(lua_State *L)
{
  const char *tname = luaL_checkstring(L, 1);
  luaT_pushmetaclass(L, luaT_typename2id(L, tname)); /* note: in Lua, root-metatable is hidden, so... you get it eh... */
  return 1;
}
コード例 #3
0
ファイル: luaT.c プロジェクト: Johnson13/xLearn
/* Lua only functions */
int luaT_lua_newmetatable(lua_State *L)
{
  const char* tname = luaL_checkstring(L, 1);
  const void *id;

  lua_settop(L, 5);
  luaL_argcheck(L, lua_isnoneornil(L, 2) || lua_isstring(L, 2), 2, "parent class name or nil expected");
  luaL_argcheck(L, lua_isnoneornil(L, 3) || lua_isfunction(L, 3), 3, "constructor function or nil expected");
  luaL_argcheck(L, lua_isnoneornil(L, 4) || lua_isfunction(L, 4), 4, "destructor function or nil expected");
  luaL_argcheck(L, lua_isnoneornil(L, 5) || lua_isfunction(L, 5), 5, "factory function or nil expected");

  if(luaT_classmodulename(tname))
    lua_getfield(L, LUA_GLOBALSINDEX, luaT_classmodulename(tname));
  else
    lua_pushvalue(L, LUA_GLOBALSINDEX);
  if(!lua_istable(L, 6))
    luaL_error(L, "while creating metatable %s: bad ardument #1 (%s is an invalid module name)", tname, luaT_classmodulename(tname));

  /* we first create the new metaclass if we have to */
  if(!luaT_typename2id(L, tname))
  {
    /* create the metaclass */
    lua_newtable(L);
    id = lua_topointer(L, -1); /* id = pointer on metaclass */

    /* __index points on itself */
    lua_pushvalue(L, -1);
    lua_setfield(L, -2, "__index");

    /* __typename contains the typename */
    lua_pushstring(L, tname);
    lua_setfield(L, -2, "__typename");

    /* by default, __version equals 1 */
    lua_pushnumber(L, 1);
    lua_setfield(L, -2, "__version");

    /* register in "*torch.id2tname*" registry table 
       (id -> typename) */
    lua_getfield(L, LUA_REGISTRYINDEX, "*torch.id2tname*");
    if(lua_isnil(L, -1))
    {
      lua_pop(L, 1);
      lua_newtable(L);
      lua_setfield(L, LUA_REGISTRYINDEX, "*torch.id2tname*");
      lua_getfield(L, LUA_REGISTRYINDEX, "*torch.id2tname*");
    }
    lua_pushlightuserdata(L, (void*)id);
    lua_pushstring(L, tname);
    lua_settable(L, -3);
    lua_pop(L, 1);

    /* register in "*torch.tname2id*" registry table 
       (typename -> id) */
    lua_getfield(L, LUA_REGISTRYINDEX, "*torch.tname2id*");
    if(lua_isnil(L, -1))
    {
      lua_pop(L, 1);
      lua_newtable(L);
      lua_setfield(L, LUA_REGISTRYINDEX, "*torch.tname2id*");
      lua_getfield(L, LUA_REGISTRYINDEX, "*torch.tname2id*");
    }
    lua_pushstring(L, tname);
    lua_pushlightuserdata(L, (void*)id);
    lua_settable(L, -3);
    lua_pop(L, 1);
  }

  /* we retrieve the existing metaclass */
  else
  {
    id = luaT_typename2id(L, tname);
    luaT_pushmetaclass(L, id);
  }

  /* we assign the parent class if necessary */
  if(!lua_isnoneornil(L, 2))
  {
    if(lua_getmetatable(L, -1))
      luaL_error(L, "class %s has been already assigned a parent class\n", tname);
    else
    {
      const char* parenttname = luaL_checkstring(L, 2);
      luaT_pushmetaclass(L, luaT_typename2id(L, parenttname));
      if(lua_isnil(L, -1))
        luaL_error(L, "bad argument #2 (invalid parent class name %s)", parenttname);
      lua_setmetatable(L, -2);
    }
  }

  /******** root-metatable ********/

  /* id is the pointer on the metatable
     registry[id] = root-metatable, so try to see if it exists */

  lua_pushlightuserdata(L, (void*)id); /* id */
  lua_rawget(L, LUA_REGISTRYINDEX);

  /* not existing? we create a new one! */
  if(lua_isnil(L, -1))
  {
    lua_pop(L, 1); /* remove nil on stack */
    lua_newtable(L);
    
    /* __index handling */
    lua_pushcfunction(L, luaT_rmt__index);
    lua_setfield(L, -2, "__index");
    
    /* __newindex handling */
    lua_pushcfunction(L, luaT_rmt__newindex);
    lua_setfield(L, -2, "__newindex");

    /* __metatable field (point on the metaclass) */
    lua_pushvalue(L, -2);
    lua_setfield(L, -2, "__metatable");

    /* __typename contains the typename */
    lua_pushstring(L, tname);
    lua_setfield(L, -2, "__typename");
    
    /* operators handling */
#define MT_ADD_OPERATOR(name) \
  lua_pushcfunction(L, luaT_rmt__##name); \
  lua_setfield(L, -2, "__" #name)
    
    MT_ADD_OPERATOR(tostring);
    MT_ADD_OPERATOR(add);
    MT_ADD_OPERATOR(sub);
    MT_ADD_OPERATOR(mul);
    MT_ADD_OPERATOR(div);
    MT_ADD_OPERATOR(mod);
    MT_ADD_OPERATOR(pow);
    MT_ADD_OPERATOR(unm);
    MT_ADD_OPERATOR(concat);
    MT_ADD_OPERATOR(len);
    MT_ADD_OPERATOR(eq);
    MT_ADD_OPERATOR(lt);
    MT_ADD_OPERATOR(le);
    MT_ADD_OPERATOR(call);
    
    /* assign the metaclass as metatable... */
    lua_pushvalue(L, -2);
    lua_setmetatable(L, -2);

    /* id is the pointer on the metatable
       set registry[id] = root-metatable */
    lua_pushlightuserdata(L, (void*)id); /* id */
    lua_pushvalue(L, -2);                /* metatable */
    lua_rawset(L, LUA_REGISTRYINDEX);    /* registry[id] = metatable */

  } /* ok, so now we have the root-metatable on the stack */

  /* register the destructor function  */
  if(!lua_isnoneornil(L, 4))
  {
    /* does it exists already? */
    lua_pushstring(L, "__gc");
    lua_rawget(L, -2);

    if(lua_isnil(L, -1))
    {
      lua_pop(L, 1); /* pop nil */
      lua_pushstring(L, "__gc");
      lua_pushvalue(L, 4);
      lua_rawset(L, -3);
    }
    else
      luaL_error(L, "%s has been already assigned a destructor", tname);
  }

  /* register the factory function  */
  if(!lua_isnoneornil(L, 5))
  {
    /* does it exists already? */
    lua_pushstring(L, "__factory");
    lua_rawget(L, -2);

    if(lua_isnil(L, -1))
    {
      lua_pop(L, 1); /* pop nil */
      lua_pushstring(L, "__factory");
      lua_pushvalue(L, 5);
      lua_rawset(L, -3);
    }
    else
      luaL_error(L, "%s has been already assigned a factory", tname);
  }

  /******** Constructor table and metatable ********/
  lua_pushstring(L, "__constructor");
  lua_rawget(L, -2);

  if(lua_isnil(L, -1))
  {
    lua_pop(L, 1);                        /* pop nil */
    lua_newtable(L);                      /* fancy table */
    lua_newtable(L);                      /* fancy metatable */

    lua_pushvalue(L, -4);                 /* metaclass */
    lua_setfield(L, -2, "__index");       /* so we can get the methods */

    lua_pushcfunction(L, luaT_cmt__newindex);
    lua_setfield(L, -2, "__newindex");    /* so we cannot messup */

    lua_pushcfunction(L, luaT_cmt__call);
    lua_setfield(L, -2, "__call");        /* so we can create */

    lua_pushvalue(L, -4); 
    lua_setfield(L, -2, "__metatable");   /* redirect to metatable with methods */

    lua_setmetatable(L, -2);              /* metatable is ... the fancy metatable */

    /* set root-metatable[__constructor] = constructor-metatable */
    lua_pushstring(L, "__constructor");
    lua_pushvalue(L, -2);
    lua_rawset(L, -4);
  }

  /* register the constructor function  */
  if(!lua_isnoneornil(L, 3))
  {
    /* get constructor metatable */
    lua_getmetatable(L, -1);

    /* does it exists already? */
    lua_pushstring(L, "__new");
    lua_rawget(L, -2);

    if(lua_isnil(L, -1))
    {
      lua_pop(L, 1); /* pop nil */
      lua_pushstring(L, "__new");
      lua_pushvalue(L, 3);
      lua_rawset(L, -3);
    }
    else
      luaL_error(L, "%s has been already assigned a constructor", tname);

    /* pop constructor metatable */
    lua_pop(L, 1);
  }
  
  lua_setfield(L, 6, luaT_classrootname(tname)); /* module.name = constructor-metatable */
  lua_pop(L, 1);                        /* pop the root-metatable */

  return 1; /* returns the metaclass */
}
コード例 #4
0
void Lffmpeg_(Init)(lua_State *L)
{
  luaT_pushmetaclass(L, torch_(Tensor_id));
  luaT_registeratname(L, Lffmpeg_(Methods), "ffmpeg");
}
コード例 #5
0
ファイル: jpeg.c プロジェクト: CharcoalApple/lua---image
DLL_EXPORT int libjpeg_(Main_init)(lua_State *L)
{
  luaT_pushmetaclass(L, torch_(Tensor_id));
  luaT_registeratname(L, libjpeg_(Main__), "libjpeg");
  return 1;
}
コード例 #6
0
ファイル: SparseLinear.c プロジェクト: abeschneider/torch
void nn_(SparseLinear_init)(lua_State *L)
{
  luaT_pushmetaclass(L, torch_(Tensor_id));
  luaT_registeratname(L, nn_(SparseLinear__), "nn");
  lua_pop(L,1);
}