Skip to content

Commit

Permalink
AP_Scripting: allow loading moduels from ROMFS
Browse files Browse the repository at this point in the history
  • Loading branch information
IamPete1 committed Feb 23, 2024
1 parent 26d4e0d commit 9a6e83d
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 0 deletions.
4 changes: 4 additions & 0 deletions libraries/AP_Scripting/lua/src/loadlib.c
Original file line number Diff line number Diff line change
Expand Up @@ -798,7 +798,11 @@ LUAMOD_API int luaopen_package (lua_State *L) {
luaL_newlib(L, pk_funcs); /* create 'package' table */
// createsearcherstable(L);
/* set paths */
#if defined(ARDUPILOT_BUILD)
setpath(L, "path", LUA_PATH_VAR, lua_get_modules_path());
#else
setpath(L, "path", LUA_PATH_VAR, LUA_PATH_DEFAULT);
#endif
// setpath(L, "cpath", LUA_CPATH_VAR, LUA_CPATH_DEFAULT);
/* store config information */
lua_pushliteral(L, LUA_DIRSEP "\n" LUA_PATH_SEP "\n" LUA_PATH_MARK "\n"
Expand Down
4 changes: 4 additions & 0 deletions libraries/AP_Scripting/lua/src/luaconf.h
Original file line number Diff line number Diff line change
Expand Up @@ -195,11 +195,13 @@
#define LUA_LDIR "!\\lua\\"
#define LUA_CDIR "!\\"
#define LUA_SHRDIR "!\\..\\share\\lua\\" LUA_VDIR "\\"
#if !defined(ARDUPILOT_BUILD)
#define LUA_PATH_DEFAULT \
LUA_LDIR"?.lua;" LUA_LDIR"?\\init.lua;" \
LUA_CDIR"?.lua;" LUA_CDIR"?\\init.lua;" \
LUA_SHRDIR"?.lua;" LUA_SHRDIR"?\\init.lua;" \
".\\?.lua;" ".\\?\\init.lua"
#endif
#define LUA_CPATH_DEFAULT \
LUA_CDIR"?.dll;" \
LUA_CDIR"..\\lib\\lua\\" LUA_VDIR "\\?.dll;" \
Expand All @@ -210,9 +212,11 @@
#define LUA_ROOT "/usr/local/"
#define LUA_LDIR SCRIPTING_DIRECTORY "/modules/"
#define LUA_CDIR LUA_ROOT "lib/lua/" LUA_VDIR "/"
#if !defined(ARDUPILOT_BUILD)
#define LUA_PATH_DEFAULT \
LUA_LDIR"?.lua;" LUA_LDIR"?/init.lua;" \
"./?.lua;" "./?/init.lua"
#endif
#define LUA_CPATH_DEFAULT \
LUA_CDIR"?.so;" LUA_CDIR"loadall.so;" "./?.so"
#endif /* } */
Expand Down
27 changes: 27 additions & 0 deletions libraries/AP_Scripting/lua_bindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -920,6 +920,33 @@ int lua_get_current_ref()
return scripting->get_current_ref();
}

// This is used when loading modules with require, lua must only look in enabled directory's
const char* lua_get_modules_path()
{
#define LUA_PATH_ROMFS "@ROMFS/scripts/modules/?.lua;" "@ROMFS/scripts/modules/?/init.lua"
#define LUA_PATH_SCRIPTS LUA_LDIR"?.lua;" LUA_LDIR"?/init.lua"

uint16_t dir_disable = AP_Scripting::get_singleton()->get_disabled_dir();
dir_disable &= uint16_t(AP_Scripting::SCR_DIR::SCRIPTS) | uint16_t(AP_Scripting::SCR_DIR::ROMFS);
if (dir_disable == 0) {
// Both directory's are enabled, return both, ROMFS takes priority
return LUA_PATH_ROMFS ";" LUA_PATH_SCRIPTS;
}

if ((dir_disable & uint16_t(AP_Scripting::SCR_DIR::SCRIPTS)) == 0) {
// Only scripts enabled
return LUA_PATH_SCRIPTS;
}

if ((dir_disable & uint16_t(AP_Scripting::SCR_DIR::ROMFS)) == 0) {
// Only ROMFS enabled
return LUA_PATH_ROMFS;
}

// Nothing enabled?
return "";
}

// Simple print to GCS or over CAN
int lua_print(lua_State *L) {
// Only support a single argument
Expand Down
1 change: 1 addition & 0 deletions libraries/AP_Scripting/lua_common_defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,6 @@
#endif // REPL_OUT

int lua_get_current_ref();
const char* lua_get_modules_path();
void lua_abort(void) __attribute__((noreturn));

0 comments on commit 9a6e83d

Please sign in to comment.