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; }
//should report error if no Lua and kill object creation void lua_setup_lua(t_jit_gl_lua *x) { //load lua_listener class jit_lua_listener_init(); x->lua = lua_open(); luaL_openlibs(x->lua); //standard Lua libs // get traceback function lua_getglobal(x->lua, "debug"); lua_pushliteral(x->lua, "traceback"); lua_gettable(x->lua, -2); lua_setfield(x->lua, LUA_REGISTRYINDEX, JIT_GL_LUA_TRACEBACK); lua_pop(x->lua, 1); //append package search path { char path[1024]; char new_cpath[1024]; const char *cpath; get_module_path(path); lua_getglobal(x->lua, "package"); lua_pushstring(x->lua, "cpath"); lua_rawget(x->lua, -2); cpath = lua_tostring(x->lua, -1); strcpy(new_cpath, cpath); strcat(new_cpath, ";"); //add delimiter strcat(new_cpath, path); //add module path #if TARGET_RT_MAC_MACHO strcat(new_cpath, "?.so"); //add so matcher #else strcat(new_cpath, "?.dll"); //add dll matcher #endif lua_pop(x->lua, 1); //get rid of old cpath lua_pushstring(x->lua, "cpath"); lua_pushstring(x->lua, new_cpath); lua_rawset(x->lua, -3); lua_pop(x->lua, 1); //pop table from stack } luaopen_opengl(x->lua); //LuaGL for OpenGL API bindings luaopen_glu(x->lua); //LuaGLU for glu* OpenGL functions luaopen_jit(x->lua); //Jitlib bindings jit.* luaopen_vecmath(x->lua); //jit.vecmath.c bindings vec2.*/vec3.*/vec4.*/quat.*/mat3.*/mat4.* //print function from Lua that uses post() lua_pushcfunction(x->lua, lua_post); lua_setglobal(x->lua, "print"); //outlet() function lua_pushcfunction(x->lua, lua_outlet); lua_setglobal(x->lua, "outlet"); //importfile() function for loading in scripts within scripts lua_pushcfunction(x->lua, lua_importfile); lua_setglobal(x->lua, "importfile"); //stackdump for printing the Lua stack // lua_pushcfunction(x->lua, stackDump); // lua_setglobal(x->lua, "stackdump"); //setting global "this" variable to this instance of jit.gl.lua Jitobj_push(x->lua, x, 0); lua_setglobal(x->lua, "this"); //put "this" in the global registry so it wont be garbage collected lua_pushstring(x->lua, x->lua_unique_name->s_name); lua_getglobal(x->lua, "this"); lua_settable(x->lua, LUA_REGISTRYINDEX); //create a meta-patcher-message handler lua_getglobal(x->lua, "_G"); //get globals table luaL_newmetatable(x->lua, "_G_meta"); //create a new metatable lua_setmetatable(x->lua, -2); //set the new metatable to be the globals metatable lua_pop(x->lua, 1); //pop globals table from stack luaopen_jit_gl(x->lua); }
const char *luaex_cmd_Reslist(cmd_parms *cmd, void *dcfg, const char *resource, const char *script) { struct dir_config *conf = dcfg; const char *err = ap_check_cmd_context(cmd, NOT_IN_LIMIT); module* lua_module = ml_find_module(cmd->server, "lua_module"); if (err != NULL) return err; if (conf->resource == NULL) { conf->resource = apr_hash_make(cmd->pool); } if (conf->L == NULL) { conf->L = luaL_newstate(); #ifdef AP_ENABLE_LUAJIT luaopen_jit(conf->L); #endif luaL_openlibs(conf->L); } if (apr_hash_get(conf->resource, resource, strlen(resource)) == NULL) { lua_State *L = conf->L; int err = luaL_loadfile(L, script); if (err == LUA_ERRFILE) return apr_psprintf(cmd->pool, "cannot open/read: %s. ", script); if (err == LUA_ERRSYNTAX) return apr_psprintf(cmd->pool, "syntax error during pre-compilation for: %s. ", script); if (err == LUA_ERRMEM) return apr_psprintf(cmd->pool, "memory allocation error for load: %s. ", script); if (err) return apr_psprintf(cmd->pool, "unknown error)(%d) for load: %s. ", err, script); err = lua_pcall(L, 0, LUA_MULTRET, 0); if (err == LUA_ERRRUN) return apr_psprintf(cmd->pool, "a runtime error. %s", lua_tostring(L, -1)); if (err == LUA_ERRMEM) return apr_psprintf(cmd->pool, "memory allocation error. %s", lua_tostring(L, -1)); if (err == LUA_ERRERR) return apr_psprintf(cmd->pool, "error while running the error handler function. %s", lua_tostring(L, -1)); if (err) return apr_psprintf(cmd->pool, "unknown error(%d:%s) for load: %s. ", err, lua_tostring(L, -1), script); { int min, smax, hmax, ttl; apr_reslist_t* reslist; reslist_cb_t* cb = apr_palloc(cmd->pool, sizeof(reslist_cb_t)); luaL_getmetatable(L, resource); if (lua_isnil(L, -1)) return apr_psprintf(cmd->pool, "%s not support %s object(metatable)", script, resource); cb->name = resource; lua_pop(L, 1); if (!lua_istable(L, -1)) return apr_psprintf(cmd->pool, "%s not return a table which makes a reslist for %s", script, resource); cb->L = conf->L; lua_getfield(L, -1, "constructor"); if (!lua_isfunction(L, -1)) return apr_psprintf(cmd->pool, "%s not have a table field(constructor) function", script); cb->constructor_ref = luaL_ref(L, LUA_REGISTRYINDEX); lua_getfield(L, -1, "destructor"); if (!lua_isfunction(L, -1)) return apr_psprintf(cmd->pool, "%s not have a table field(destructor) function", script); cb->destructor_ref = luaL_ref(L, LUA_REGISTRYINDEX); lua_getfield(L, -1, "min"); min = luaL_optint(L, -1, 0); lua_pop(L, 1); lua_getfield(L, -1, "smax"); smax = luaL_optint(L, -1, 16); lua_pop(L, 1); lua_getfield(L, -1, "hmax"); hmax = luaL_optint(L, -1, 16); lua_pop(L, 1); lua_getfield(L, -1, "ttl"); ttl = luaL_optint(L, -1, 0); lua_pop(L, 1); if (apr_reslist_create(&reslist, min, smax, hmax, ttl, ml_apr_reslist_constructor, ml_apr_reslist_destructor, cb, cmd->pool) == APR_SUCCESS) { apr_hash_set(conf->resource, resource, strlen(resource), reslist); } else return "apr_reslist_create failed"; } } if (conf->resource == NULL) return "Out of memory"; return NULL; }