~panda-roux/MoonGem

20b05889ad4b5d309e5fd52cd8010d105df7bc62 — panda-roux 6 months ago 134c089
Appending Lua package paths relevant to whatever script is being executed prior to execution, rather than just once when the script context is created.  This ensures that document-level scripts can find their sibling modules even if there's a pre-request script.
1 files changed, 7 insertions(+), 10 deletions(-)

M src/script.c
M src/script.c => src/script.c +7 -10
@@ 173,23 173,17 @@ static void set_response_buffer(lua_State* L, struct evbuffer* buffer) {
  lua_setfield(L, LUA_REGISTRYINDEX, FLD_BUFFER);
}

static void append_package_path(script_ctx_t* ctx) {
  lua_State* L = ctx->L;
  const char* path = ctx->request->uri->path;

static void append_dir_to_package_path(lua_State* L, const char* path) {
  if (path == NULL) {
    return;
  }

  /*
  // skip the first forward-slash
  ++path;
  */

  // add the request path to package.path
  lua_getglobal(L, "package");
  lua_getfield(L, -1, "path");
  if (strrchr(path, '.') != NULL) {
    // if this looks like a file, then append the directory that the file is in
    // rather than the file path itself
    char* path_copy = strdup(path);
    char* dir = dirname(path_copy);
    lua_pushfstring(L, ";./%s/?.lua", dir);


@@ 218,7 212,6 @@ script_ctx_t* create_script_ctx(gemini_context_t* gemini) {
  ctx->request = &gemini->request;
  ctx->response = &gemini->response;

  append_package_path(ctx);
  set_registry_data(ctx);
  set_api_methods(L);



@@ 247,6 240,8 @@ script_result_t exec_script(script_ctx_t* ctx, char* script, size_t script_len,

  lua_State* L = ctx->L;

  append_dir_to_package_path(L, ctx->request->uri->path);

  set_response_buffer(L, output);

  // we have to copy the script body to a buffer so that we can null-terminate


@@ 282,6 277,8 @@ script_result_t exec_script_file(script_ctx_t* ctx, const char* path,
                                 struct evbuffer* output) {
  lua_State* L = ctx->L;

  append_dir_to_package_path(L, path);

  set_response_buffer(L, output);

  if (luaL_dofile(ctx->L, path) != LUA_OK) {