int main(int argc, char *argv[]) { lua_State *L; uv_loop_t *loop; argv = uv_setup_args(argc, argv); L = luaL_newstate(); if (L == NULL) { fprintf(stderr, "luaL_newstate has failed\n"); return 1; } luaL_openlibs(L); loop = uv_default_loop(); #ifdef LUV_EXPORTS luvit__suck_in_symbols(); #endif #ifdef USE_OPENSSL luvit_init_ssl(); #endif if (luvit_init(L, loop, argc, argv)) { fprintf(stderr, "luvit_init has failed\n"); return 1; } /* Run the main lua script */ if (luvit_run(L)) { int saved_flags = fcntl(STDOUT_FILENO, F_GETFL); fcntl(STDOUT_FILENO, F_SETFL, saved_flags & ~O_NONBLOCK); fprintf(stderr, "%s\n", lua_tostring(L, -1)); lua_pop(L, 1); lua_close(L); return -1; } lua_close(L); int saved_flags = fcntl(STDOUT_FILENO, F_GETFL); fcntl(STDOUT_FILENO, F_SETFL, saved_flags & ~O_NONBLOCK); return 0; }
static void virgo__lua_luvit_init(virgo_t *v) { /* Hack to keep the linker from killing symbols that luajit pulls in at runtime :( */ lua_State *L; virgo__suck_in_symbols(); luvit__suck_in_symbols(); L = v->L; luvit_init(L, uv_default_loop(), v->argc, v->argv); /* Pull up the preload table */ lua_getglobal(L, "package"); lua_getfield(L, -1, "preload"); lua_remove(L, -2); /* Register constants */ lua_pushcfunction(L, virgo__lua_logging_open); lua_setfield(L, -2, "logging"); lua_pop(L, 1); }
int luvit_init(lua_State *L, uv_loop_t* loop, int argc, char *argv[]) { int index, rc; ares_channel channel; struct ares_options options; luvit__suck_in_symbols(); memset(&options, 0, sizeof(options)); rc = ares_library_init(ARES_LIB_INIT_ALL); assert(rc == ARES_SUCCESS); // Pull up the preload table lua_getglobal(L, "package"); lua_getfield(L, -1, "preload"); lua_remove(L, -2); // Register yajl lua_pushcfunction(L, luaopen_yajl); lua_setfield(L, -2, "yajl"); // Register os lua_pushcfunction(L, luaopen_os_binding); lua_setfield(L, -2, "os_binding"); // Register http_parser lua_pushcfunction(L, luaopen_http_parser); lua_setfield(L, -2, "http_parser"); // Register uv lua_pushcfunction(L, luaopen_uv); lua_setfield(L, -2, "uv"); // Register env lua_pushcfunction(L, luaopen_env); lua_setfield(L, -2, "env"); // Register constants lua_pushcfunction(L, luaopen_constants); lua_setfield(L, -2, "constants"); // We're done with preload, put it away lua_pop(L, 1); // Get argv lua_createtable (L, argc, 0); for (index = 0; index < argc; index++) { lua_pushstring (L, argv[index]); lua_rawseti(L, -2, index); } lua_setglobal(L, "argv"); lua_pushcfunction(L, luvit_exit); lua_setglobal(L, "exit_process"); lua_pushcfunction(L, luvit_print_stderr); lua_setglobal(L, "print_stderr"); lua_pushcfunction(L, luvit_getcwd); lua_setglobal(L, "getcwd"); lua_pushstring(L, LUVIT_VERSION); lua_setglobal(L, "VERSION"); lua_pushstring(L, UV_VERSION); lua_setglobal(L, "UV_VERSION"); lua_pushstring(L, LUAJIT_VERSION); lua_setglobal(L, "LUAJIT_VERSION"); lua_pushstring(L, HTTP_VERSION); lua_setglobal(L, "HTTP_VERSION"); lua_pushstring(L, YAJL_VERSIONISH); lua_setglobal(L, "YAJL_VERSION"); // Hold a reference to the main thread in the registry assert(lua_pushthread(L) == 1); lua_setfield(L, LUA_REGISTRYINDEX, "main_thread"); // Store the loop within the registry luv_set_loop(L, loop); // Store the ARES Channel uv_ares_init_options(luv_get_loop(L), &channel, &options, 0); luv_set_ares_channel(L, &channel); return 0; }