diff --git a/modules/lua/lua_vmprep.c b/modules/lua/lua_vmprep.c index 66eabc673b..1623590cb7 100644 --- a/modules/lua/lua_vmprep.c +++ b/modules/lua/lua_vmprep.c @@ -258,7 +258,7 @@ static void munge_path(lua_State *L, } lua_State *apl_get_lua_state(apr_pool_t *lifecycle_pool, - char *file, + apl_vm_spec *spec, apr_array_header_t *package_paths, apr_array_header_t *package_cpaths, apl_lua_state_open_callback cb, void *btn) @@ -267,29 +267,37 @@ lua_State *apl_get_lua_state(apr_pool_t *lifecycle_pool, lua_State *L; ap_log_perror(APLOG_MARK, APLOG_WARNING, 0, lifecycle_pool, "obtaining lua_State"); - if (!apr_pool_userdata_get((void **) &L, file, lifecycle_pool)) { + if (!apr_pool_userdata_get((void **) &L, spec->file, lifecycle_pool)) { ap_log_perror(APLOG_MARK, APLOG_WARNING, 0, lifecycle_pool, - "creating lua_State with file %s", file); + "creating lua_State with file %s", spec->file); /* not available, so create */ L = luaL_newstate(); luaL_openlibs(L); if (package_paths) munge_path(L, "path", "?.lua", "./?.lua", lifecycle_pool, - package_paths, file); + package_paths, spec->file); if (package_cpaths) munge_path(L, "cpath", "?.so", "./?.so", lifecycle_pool, - package_cpaths, file); + package_cpaths, spec->file); if (cb) { cb(L, lifecycle_pool, btn); } - luaL_loadfile(L, file); - lua_pcall(L, 0, LUA_MULTRET, 0); - apr_pool_userdata_set(L, file, &cleanup_lua, lifecycle_pool); + apr_pool_userdata_set(L, spec->file, &cleanup_lua, lifecycle_pool); + + 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 { + luaL_loadfile(L, spec->file); + lua_pcall(L, 0, LUA_MULTRET, 0); + } lua_pushlightuserdata(L, lifecycle_pool); lua_setfield(L, LUA_REGISTRYINDEX, "Apache2.Wombat.pool"); } + return L; } diff --git a/modules/lua/lua_vmprep.h b/modules/lua/lua_vmprep.h index b2e4481319..a0c37259bc 100644 --- a/modules/lua/lua_vmprep.h +++ b/modules/lua/lua_vmprep.h @@ -65,6 +65,10 @@ typedef struct /* pool to use for lifecycle if APL_SCOPE_ONCE is set, otherwise unused */ apr_pool_t *pool; + /* Pre-compiled Lua Byte code to load directly. If bytecode_len is >0, + * the file part of this structure is ignored for loading purposes, but + * it is used for error messages. + */ const char *bytecode; apr_size_t bytecode_len; } apl_vm_spec; @@ -127,7 +131,7 @@ typedef void (*apl_lua_state_open_callback) (lua_State *L, apr_pool_t *p, * @ctx a baton passed to cb */ lua_State *apl_get_lua_state(apr_pool_t *lifecycle_pool, - char *file, + apl_vm_spec *spec, apr_array_header_t *package_paths, apr_array_header_t *package_cpaths, apl_lua_state_open_callback cb, void *btn); diff --git a/modules/lua/mod_lua.c b/modules/lua/mod_lua.c index 328614d27b..cd262caee1 100644 --- a/modules/lua/mod_lua.c +++ b/modules/lua/mod_lua.c @@ -127,7 +127,7 @@ static int lua_handler(request_rec *r) const apl_dir_cfg *cfg = ap_get_module_config(r->per_dir_config, &lua_module); lua_State *L = apl_get_lua_state(r->pool, - d->spec->file, + d->spec, cfg->package_paths, cfg->package_cpaths, &lua_open_callback, NULL); @@ -206,8 +206,8 @@ static int apl_alias_munger(request_rec *r) static int lua_request_rec_hook_harness(request_rec *r, const char *name) { - char *fixed_filename; - + apl_server_cfg *server_cfg = ap_get_module_config(r->server->module_config, + &lua_module); const apl_dir_cfg *cfg = (apl_dir_cfg *) ap_get_module_config(r->per_dir_config, &lua_module); @@ -216,10 +216,13 @@ static int lua_request_rec_hook_harness(request_rec *r, const char *name) if (hook_specs) { int i; for (i = 0; i < hook_specs->nelts; i++) { + char *fixed_filename = NULL; apl_mapped_handler_spec *hook_spec = ((apl_mapped_handler_spec **) hook_specs->elts)[i]; - if (hook_spec == NULL) + + if (hook_spec == NULL) { continue; + } apl_vm_spec *spec = apr_pcalloc(r->pool, sizeof(apl_vm_spec)); spec->file = hook_spec->file_name; @@ -229,20 +232,10 @@ static int lua_request_rec_hook_harness(request_rec *r, const char *name) spec->bytecode_len = hook_spec->bytecode_len; spec->pool = r->pool; - /* - const apl_dir_cfg* cfg = ap_get_module_config(r->per_dir_config, &lua_module); - lua_State *L = apl_get_lua_state(r->pool, - d->spec->file, - cfg->package_paths, - cfg->package_cpaths, - &lua_open_callback, NULL); - */ - apl_server_cfg *server_cfg = - ap_get_module_config(r->server->module_config, &lua_module); - apr_filepath_merge(&fixed_filename, server_cfg->root_path, + apr_filepath_merge(&spec->file, server_cfg->root_path, spec->file, APR_FILEPATH_NOTRELATIVE, r->pool); lua_State *L = apl_get_lua_state(r->pool, - fixed_filename, + spec, cfg->package_paths, cfg->package_cpaths, &lua_open_callback, NULL);