-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(lua): move lua init functions from main
- Loading branch information
Showing
4 changed files
with
65 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#ifndef LASER_INIT_LUA_H | ||
#define LASER_INIT_LUA_H | ||
|
||
#include <lauxlib.h> | ||
#include <lua.h> | ||
#include <lualib.h> | ||
|
||
extern lua_State *L; | ||
|
||
lua_State *laser_init_lua(void); | ||
void laser_lua_load_script(const char *script_path); | ||
|
||
#endif |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
#include "init_lua.h" | ||
#include "laser.h" | ||
#include <string.h> | ||
|
||
lua_State *L; | ||
|
||
lua_State *laser_init_lua(void) | ||
{ | ||
L = luaL_newstate(); | ||
luaL_openlibs(L); | ||
|
||
return L; | ||
} | ||
|
||
void laser_lua_load_script(const char *script_path) | ||
{ | ||
char script_dir[LASER_PATH_MAX]; | ||
|
||
// set the package.path so that user can do relative requires | ||
const char *last_slash = strrchr(script_path, '/'); | ||
if (last_slash != NULL) | ||
{ | ||
size_t len = last_slash - script_path; | ||
strncpy(script_dir, script_path, len); | ||
script_dir[len] = '\0'; | ||
|
||
lua_getglobal(L, "package"); | ||
lua_getfield(L, -1, "path"); | ||
const char *current_path = lua_tostring(L, -1); | ||
|
||
char new_path[2 * LASER_PATH_MAX]; | ||
snprintf(new_path, sizeof(new_path), "%s;%s/?.lua", current_path, | ||
script_dir); | ||
lua_pop(L, 1); | ||
|
||
lua_pushstring(L, new_path); | ||
lua_setfield(L, -2, "path"); | ||
lua_pop(L, 1); | ||
} | ||
|
||
if (luaL_dofile(L, script_path) != LUA_OK) | ||
{ | ||
fprintf(stderr, "lsr: error loading Lua script: %s\n", | ||
lua_tostring(L, -1)); | ||
lua_pop(L, 1); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters