Пример #1
0
LUALIB_API int luaopen_io (lua_State *L) {
    createmeta(L);
#if LUA_OPTIMIZE_MEMORY != 2
    /* create (private) environment (with fields IO_INPUT, IO_OUTPUT, __close) */
    newfenv(L, io_fclose);
    lua_replace(L, LUA_ENVIRONINDEX);
    /* open library */
    luaL_register(L, LUA_IOLIBNAME, iolib);
    newfenv(L, io_noclose);  /* close function for default files */
#else
    luaL_register_light(L, LUA_IOLIBNAME, iolib);
    lua_pushvalue(L, -1);
    lua_setmetatable(L, -2);
#endif
    /* create (and set) default files */
    createstdfile(L, stdin, IO_INPUT, "stdin");
    createstdfile(L, stdout, IO_OUTPUT, "stdout");
    createstdfile(L, stderr, IO_STDERR, "stderr");
#if LUA_OPTIMIZE_MEMORY != 2
    lua_pop(L, 1);  /* pop environment for default files */
    lua_getfield(L, -1, "popen");
    newfenv(L, io_pclose);  /* create environment for 'popen' */
    lua_setfenv(L, -2);  /* set fenv for 'popen' */
    lua_pop(L, 1);  /* pop 'popen' */
#endif
    return 1;
}
Пример #2
0
static void base_open (lua_State *L) {
  /* set global _G */
  lua_pushvalue(L, LUA_GLOBALSINDEX);
  lua_setglobal(L, "_G");
  /* open lib into global table */
  luaL_register_light(L, "_G", base_funcs);
#if LUA_OPTIMIZE_MEMORY > 0
  lua_pushvalue(L, -1);
  lua_setmetatable(L, -2);  
#else
  lua_pushliteral(L, LUA_VERSION);
  lua_setglobal(L, "_VERSION");  /* set global _VERSION */
#endif
  /* `ipairs' and `pairs' need auxliliary functions as upvalues */
  auxopen(L, "ipairs", luaB_ipairs, ipairsaux);
  auxopen(L, "pairs", luaB_pairs, luaB_next);
  /* `newproxy' needs a weaktable as upvalue */
  lua_createtable(L, 0, 1);  /* new table `w' */
  lua_pushvalue(L, -1);  /* `w' will be its own metatable */
  lua_setmetatable(L, -2);
  lua_pushliteral(L, "kv");
  lua_setfield(L, -2, "__mode");  /* metatable(w).__mode = "kv" */
  lua_pushcclosure(L, luaB_newproxy, 1);
  lua_setglobal(L, "newproxy");  /* set global `newproxy' */
}
Пример #3
0
static void createmeta (lua_State *L) {
  luaL_newmetatable(L, LUA_FILEHANDLE);  /* create metatable for file handles */
#if NUTLUA_OPTIMIZE_MEMORY != 2
  lua_pushvalue(L, -1);  /* push metatable */
  lua_setfield(L, -2, "__index");  /* metatable.__index = metatable */
  luaL_register(L, NULL, flib);  /* file methods */
#else
  luaL_register_light(L, NULL, flib);
#endif
}
Пример #4
0
LUALIB_API int luaopen_package (lua_State *L) {
  int i;
  /* create new type _LOADLIB */
#if LUA_OPTIMIZE_MEMORY == 0
  luaL_newmetatable(L, "_LOADLIB");
  lua_pushlightfunction(L, gctm);
  lua_setfield(L, -2, "__gc");
#else
  luaL_rometatable(L, "_LOADLIB", (void*)lmt);
#endif
  /* create `package' table */
  luaL_register_light(L, LUA_LOADLIBNAME, pk_funcs);
#if defined(LUA_COMPAT_LOADLIB) 
  lua_getfield(L, -1, "loadlib");
  lua_setfield(L, LUA_GLOBALSINDEX, "loadlib");
#endif
  lua_pushvalue(L, -1);
  lua_replace(L, LUA_ENVIRONINDEX);
  /* create `loaders' table */
  lua_createtable(L, sizeof(loaders)/sizeof(loaders[0]) - 1, 0);
  /* fill it with pre-defined loaders */
  for (i=0; loaders[i] != NULL; i++) {
    lua_pushcfunction(L, loaders[i]);
    lua_rawseti(L, -2, i+1);
  }
  lua_setfield(L, -2, "loaders");  /* put it in field `loaders' */
  setpath(L, "path", LUA_PATH, LUA_PATH_DEFAULT);  /* set field `path' */
  setpath(L, "cpath", LUA_CPATH, LUA_CPATH_DEFAULT); /* set field `cpath' */
  /* store config information */
  lua_pushliteral(L, LUA_DIRSEP "\n" LUA_PATHSEP "\n" LUA_PATH_MARK "\n"
                     LUA_EXECDIR "\n" LUA_IGMARK);
  lua_setfield(L, -2, "config");
  /* set field `loaded' */
  luaL_findtable(L, LUA_REGISTRYINDEX, "_LOADED", 2);
  lua_setfield(L, -2, "loaded");
  /* set field `preload' */
  lua_newtable(L);
  lua_setfield(L, -2, "preload");
  lua_pushvalue(L, LUA_GLOBALSINDEX);
  luaL_register(L, NULL, ll_funcs);  /* open lib into global table */
  lua_pop(L, 1);
  return 1;  /* return 'package' table */
}