/* start optimizer */ static int dojitopt (lua_State *L, const char *opt) { lua_pushliteral(L, "opt"); if (loadjitmodule(L, "LuaJIT optimizer module not installed")) return 1; lua_remove(L, -2); /* drop module name */ if (*opt) lua_pushstring(L, opt); return report(L, lua_pcall(L, *opt ? 1 : 0, 0, 0)); }
/* Save or list bytecode. */ static int dobytecode(lua_State *L, char **argv) { int narg = 0; lua_pushliteral(L, "bcsave"); if (loadjitmodule(L)) return 1; if (argv[0][2]) { narg++; argv[0][1] = '-'; lua_pushstring(L, argv[0]+1); } for (argv++; *argv != NULL; narg++, argv++) lua_pushstring(L, *argv); return report(L, lua_pcall(L, narg, 0, 0)); }
/* JIT engine control command: try jit library first or load add-on module */ static int dojitcmd (lua_State *L, const char *cmd) { const char *val = strchr(cmd, '='); lua_pushlstring(L, cmd, val ? val - cmd : xr_strlen(cmd)); lua_getglobal(L, "jit"); /* get jit.* table */ lua_pushvalue(L, -2); lua_gettable(L, -2); /* lookup library function */ if (!lua_isfunction(L, -1)) { lua_pop(L, 2); /* drop non-function and jit.* table, keep module name */ if (loadjitmodule(L, "unknown luaJIT command")) return 1; } else { lua_remove(L, -2); /* drop jit.* table */ } lua_remove(L, -2); /* drop module name */ if (val) lua_pushstring(L, val+1); return report(L, lua_pcall(L, val ? 1 : 0, 0, 0)); }
/* JIT engine control command: try jit library first or load add-on module. */ static int dojitcmd(lua_State *L, const char *cmd) { const char *opt = strchr(cmd, '='); lua_pushlstring(L, cmd, opt ? (size_t)(opt - cmd) : strlen(cmd)); lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED"); lua_getfield(L, -1, "jit"); /* Get jit.* module table. */ lua_remove(L, -2); lua_pushvalue(L, -2); lua_gettable(L, -2); /* Lookup library function. */ if (!lua_isfunction(L, -1)) { lua_pop(L, 2); /* Drop non-function and jit.* table, keep module name. */ if (loadjitmodule(L)) return 1; } else { lua_remove(L, -2); /* Drop jit.* table. */ } lua_remove(L, -2); /* Drop module name. */ return runcmdopt(L, opt ? opt+1 : opt); }
static apr_status_t vm_construct(lua_State **vm, void *params, apr_pool_t *lifecycle_pool) { lua_State* L; ap_lua_vm_spec *spec = params; L = luaL_newstate(); #ifdef AP_ENABLE_LUAJIT luaopen_jit(L); #endif luaL_openlibs(L); if (spec->package_paths) { munge_path(L, "path", "?.lua", "./?.lua", lifecycle_pool, spec->package_paths, spec->file); } if (spec->package_cpaths) { munge_path(L, "cpath", "?" AP_LUA_MODULE_EXT, "./?" AP_LUA_MODULE_EXT, lifecycle_pool, spec->package_cpaths, spec->file); } if (spec->cb) { spec->cb(L, lifecycle_pool, spec->cb_arg); } if (spec->bytecode && spec->bytecode_len > 0) { luaL_loadbuffer(L, spec->bytecode, spec->bytecode_len, spec->file); lua_pcall(L, 0, LUA_MULTRET, 0); } else { int rc; ap_log_perror(APLOG_MARK, APLOG_DEBUG, 0, lifecycle_pool, APLOGNO(01481) "loading lua file %s", spec->file); rc = luaL_loadfile(L, spec->file); if (rc != 0) { ap_log_perror(APLOG_MARK, APLOG_ERR, 0, lifecycle_pool, APLOGNO(01482) "Error loading %s: %s", spec->file, rc == LUA_ERRMEM ? "memory allocation error" : lua_tostring(L, 0)); return APR_EBADF; } if ( lua_pcall(L, 0, LUA_MULTRET, 0) == LUA_ERRRUN ) { ap_log_perror(APLOG_MARK, APLOG_ERR, 0, lifecycle_pool, APLOGNO(02613) "Error loading %s: %s", spec->file, lua_tostring(L, -1)); return APR_EBADF; } } #ifdef AP_ENABLE_LUAJIT loadjitmodule(L, lifecycle_pool); #endif lua_pushlightuserdata(L, lifecycle_pool); lua_setfield(L, LUA_REGISTRYINDEX, "Apache2.Wombat.pool"); *vm = L; return APR_SUCCESS; }