int luaT_lua_setmetatable(lua_State *L) { const char *tname = luaL_checkstring(L, 2); luaL_checktype(L, 1, LUA_TTABLE); lua_pushvalue(L, 1); luaT_pushmetatable(L, luaT_typename2id(L, tname)); if(lua_isnil(L, -1)) luaL_error(L, "unknown typename %s\n", tname); lua_setmetatable(L, -2); return 1; }
const char *luaT_typenameid(lua_State *L, const char *tname) { if(luaT_pushmetatable(L, tname)) { const char *tnameid = NULL; lua_rawget(L, LUA_REGISTRYINDEX); if(lua_isstring(L, -1)) tnameid = lua_tostring(L, -1); lua_pop(L, 1); /* the string/nil */ return tnameid; } return NULL; }
void luaT_pushudata(lua_State *L, void *udata, const char *tname) { if(udata) { void **udata_p = lua_newuserdata(L, sizeof(void*)); *udata_p = udata; if(!luaT_pushmetatable(L, tname)) luaL_error(L, "Torch internal problem: cannot find metatable for type <%s>", tname); lua_setmetatable(L, -2); } else lua_pushnil(L); }
int luaT_lua_factory(lua_State *L) { const char* tname = luaL_checkstring(L, 1); if(luaT_pushmetatable(L, tname) && !lua_isnil(L, -1)) { lua_pushstring(L, "__factory"); lua_rawget(L, -2); } else { lua_pushnil(L); } return 1; }
void luaT_pushudata(lua_State *L, void *udata, const void *id) { if(udata) { void **udata_p = lua_newuserdata(L, sizeof(void*)); *udata_p = udata; luaT_pushmetatable(L, id); if(lua_isnil(L, -1)) luaL_error(L, "Torch internal problem: cannot find a metatable"); lua_setmetatable(L, -2); } else lua_pushnil(L); }
void cltorch_ClStorage_init(lua_State* L) { /* the standard stuff */ torch_ClStorage_init(L); /* the copy methods */ { int i; const void* tnames[8] = {"torch.ByteStorage", "torch.CharStorage", "torch.ShortStorage", "torch.IntStorage", "torch.LongStorage", "torch.FloatStorage", "torch.DoubleStorage", "torch.ClStorage"}; static int (*funcs[8])(lua_State*) = {cltorch_ByteStorage_copy, cltorch_CharStorage_copy, cltorch_ShortStorage_copy, cltorch_IntStorage_copy, cltorch_LongStorage_copy, cltorch_FloatStorage_copy, cltorch_DoubleStorage_copy, cltorch_ClStorage_copy}; for(i = 0; i < 8; i++) { luaT_pushmetatable(L, tnames[i]); lua_pushcfunction(L, funcs[i]); lua_setfield(L, -2, "copy"); lua_pop(L, 1); } } }
int luaT_lua_version(lua_State *L) { luaL_checkany(L, 1); if(luaT_iscdata(L, 1)) { const char *tname = luaT_cdataname(L, 1, NULL); if(tname) { luaT_pushmetatable(L, tname); lua_pushstring(L, "__version"); lua_rawget(L, -2); return 1; } return 0; } else if(lua_getmetatable(L, 1)) { lua_pushstring(L, "__version"); lua_rawget(L, -2); return 1; } return 0; }
void *luaT_toudata(lua_State *L, int ud, const char *tname) { void **p = lua_touserdata(L, ud); if(p != NULL) /* value is a userdata? */ { if(!luaT_pushmetatable(L, tname)) luaL_error(L, "Torch internal problem: cannot find metatable for type <%s>", tname); /* initialize the table we want to get the metatable on */ /* note that we have to be careful with indices, as we just inserted stuff */ lua_pushvalue(L, (ud < 0 ? ud - 1 : ud)); while(lua_getmetatable(L, -1)) /* get the next metatable */ { lua_remove(L, -2); /* remove the previous metatable [or object, if first time] */ if(lua_rawequal(L, -1, -2)) { lua_pop(L, 2); /* remove the two metatables */ return *p; } } lua_pop(L, 2); /* remove the two metatables */ } return NULL; }
static void cr_(AG_init)(lua_State *L) { luaT_pushmetatable(L, torch_Tensor); luaT_registeratname(L, cr_(AG__), "cr"); lua_pop(L,1); }
/* Lua only functions */ int luaT_lua_newmetatable(lua_State *L) { const char* tname = luaL_checkstring(L, 1); char module_name[256]; int is_in_module = 0; is_in_module = luaT_classmodulename(tname, module_name); 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(is_in_module) lua_getglobal(L, module_name); else lua_pushglobaltable(L); if(!lua_istable(L, 6)) luaL_error(L, "while creating metatable %s: bad argument #1 (%s is an invalid module name)", tname, module_name); /* we first create the new metaclass if we have to */ if(!luaT_pushmetatable(L, tname)) { /* create the metatable */ lua_newtable(L); /* registry[name] = metatable */ lua_pushvalue(L, -1); lua_setfield(L, LUA_REGISTRYINDEX, tname); /* registry[metatable] = tname */ lua_pushvalue(L, -1); lua_pushstring(L, tname); lua_rawset(L, LUA_REGISTRYINDEX); /* __index handling */ lua_pushcfunction(L, luaT_mt__index); lua_setfield(L, -2, "__index"); /* __newindex handling */ lua_pushcfunction(L, luaT_mt__newindex); lua_setfield(L, -2, "__newindex"); /* __typename contains the typename */ lua_pushstring(L, tname); lua_setfield(L, -2, "__typename"); /* __metatable is self */ lua_pushvalue(L, -1); lua_setfield(L, -2, "__metatable"); /* by default, __version equals 1 */ lua_pushnumber(L, 1); lua_setfield(L, -2, "__version"); /* assign default operator functions */ lua_pushcfunction(L, luaT_mt__tostring); lua_setfield(L, -2, "__tostring"); lua_pushcfunction(L, luaT_mt__add); lua_setfield(L, -2, "__add"); lua_pushcfunction(L, luaT_mt__sub); lua_setfield(L, -2, "__sub"); lua_pushcfunction(L, luaT_mt__mul); lua_setfield(L, -2, "__mul"); lua_pushcfunction(L, luaT_mt__div); lua_setfield(L, -2, "__div"); lua_pushcfunction(L, luaT_mt__mod); lua_setfield(L, -2, "__mod"); lua_pushcfunction(L, luaT_mt__pow); lua_setfield(L, -2, "__pow"); lua_pushcfunction(L, luaT_mt__unm); lua_setfield(L, -2, "__unm"); lua_pushcfunction(L, luaT_mt__concat); lua_setfield(L, -2, "__concat"); lua_pushcfunction(L, luaT_mt__len); lua_setfield(L, -2, "__len"); lua_pushcfunction(L, luaT_mt__eq); lua_setfield(L, -2, "__eq"); lua_pushcfunction(L, luaT_mt__lt); lua_setfield(L, -2, "__lt"); lua_pushcfunction(L, luaT_mt__le); lua_setfield(L, -2, "__le"); lua_pushcfunction(L, luaT_mt__call); lua_setfield(L, -2, "__call"); } /* 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); if(!luaT_pushmetatable(L, parenttname)) luaL_error(L, "bad argument #2 (invalid parent class name %s)", parenttname); lua_setmetatable(L, -2); } } /* 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, -3); /* metatable */ lua_setfield(L, -2, "__index"); /* so we can get the methods */ lua_pushcfunction(L, luaT_cmt__newindex); lua_setfield(L, -2, "__newindex"); /* so we add new methods */ lua_pushcfunction(L, luaT_cmt__call); lua_setfield(L, -2, "__call"); /* so we can create, we are here for only that */ lua_pushvalue(L, -3); lua_setfield(L, -2, "__metatable"); /* redirect to metatable with methods */ lua_setmetatable(L, -2); /* constructor metatable is ... this fancy metatable */ /* set 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); /* set "new" in the metatable too */ lua_pushstring(L, "new"); lua_pushvalue(L, 3); lua_rawset(L, -5); } else luaL_error(L, "%s has been already assigned a constructor", tname); /* pop constructor metatable */ lua_pop(L, 1); } /* module.name = constructor metatable */ lua_setfield(L, 6, luaT_classrootname(tname)); return 1; /* returns the metatable */ }
void initSparseNLLCriterionCuda(lua_State *L) { luaT_pushmetatable(L, "torch.CudaTensor"); luaT_registeratname(L, functions, "nn"); lua_pop(L, 1); }
static void nn_(VolumetricAveragePooling_init)(lua_State *L) { luaT_pushmetatable(L, torch_Tensor); luaT_registeratname(L, nn_(VolumetricAveragePooling__), "nn"); lua_pop(L,1); }
void torch_TensorOperator_(init)(lua_State *L) { luaT_pushmetatable(L, torch_Tensor); luaT_setfuncs(L, torch_TensorOperator_(_), 0); lua_pop(L, 1); }
DLL_EXPORT int libjpeg_(Main_init)(lua_State *L) { luaT_pushmetatable(L, torch_Tensor); luaT_registeratname(L, libjpeg_(Main__), "libjpeg"); return 1; }
static void nn_(SpatialAdaptiveMaxPooling_init)(lua_State *L) { luaT_pushmetatable(L, torch_Tensor); luaT_registeratname(L, nn_(SpatialAdaptiveMaxPooling__), "nn"); lua_pop(L,1); }
static void nxn_(SpatialMaxPoolingBHWD_init)(lua_State *L) { luaT_pushmetatable(L, torch_Tensor); luaT_registeratname(L, nxn_(SpatialMaxPoolingBHWD__), "nn"); lua_pop(L,1); }
void nn_(LogSoftMax_init)(lua_State *L) { luaT_pushmetatable(L, torch_Tensor); luaT_registeratname(L, nn_(LogSoftMax__), "nn"); lua_pop(L,1); }
void initOneBitQuantizationCuda(lua_State *L) { luaT_pushmetatable(L, "torch.CudaTensor"); luaT_registeratname(L, functions, "nn"); lua_pop(L,1); }
void initSpatialConvolutionCuFFT(lua_State *L) { luaT_pushmetatable(L, "torch.CudaTensor"); luaT_registeratname(L, functions, "nn"); lua_pop(L,1); }
void initFeatureLPPoolingCuda(lua_State *L) { luaT_pushmetatable(L, "torch.CudaTensor"); luaT_registeratname(L, registry, "nn"); lua_pop(L, 1); }
static void gpunn_SpatialConvolutionGPU_init(lua_State *L) { luaT_pushmetatable(L, "torch.GPUTensor"); luaT_registeratname(L, gpunn_SpatialConvolutionGPU__, "nn"); lua_pop(L,1); }
void thffmpeg_(Main_init)(lua_State *L) { luaT_pushmetatable(L, torch_Tensor); luaT_registeratname(L, thffmpeg_(Main__), "libthffmpeg"); }
static void nxn_(Jitter_init)(lua_State *L) { luaT_pushmetatable(L, torch_Tensor); luaT_registeratname(L, nxn_(Jitter__), "nxn"); lua_pop(L,1); }
static void gpunn_Abs_init(lua_State *L) { luaT_pushmetatable(L, "torch.GPUTensor"); luaT_registeratname(L, gpunn_Abs__, "nn"); lua_pop(L,1); }
static void nnconv1d_(SpatialUpSamplingPeriodic_init)(lua_State *L) { luaT_pushmetatable(L, torch_Tensor); luaT_registeratname(L, nnconv1d_(SpatialUpSamplingPeriodic__), "nn"); lua_pop(L,1); }
void torchzfp_(Main_init)(lua_State *L) { luaT_pushmetatable(L, torch_Tensor); luaT_registeratname(L, torchzfp_(Main__), "torchzfp"); }
void initCrossMapNormalizationCuda(lua_State* L) { luaT_pushmetatable(L, "torch.CudaTensor"); luaT_registeratname(L, functions, "nn"); lua_pop(L, 1); }
static void nn_(ClassNLLCriterion_init)(lua_State *L) { luaT_pushmetatable(L, torch_Tensor); luaT_registeratname(L, nn_(ClassNLLCriterion__), "nn"); lua_pop(L,1); }
static void nn_(SpatialConvolution_init)(lua_State *L) { luaT_pushmetatable(L, torch_Tensor); luaT_registeratname(L, nn_(SpatialConvolution__), "nn"); lua_pop(L,1); }
static void nxn_(CrossMapNormalization_init)(lua_State *L) { luaT_pushmetatable(L, torch_Tensor); luaT_registeratname(L, nxn_(CrossMapNormalization__), "nxn"); lua_pop(L,1); }