wplua: split the functionality of the load functions into smaller functions
The load functions used to do 3 things: - push the sandbox function on the stack - load the file and push it as a function on the stack - call the sandbox (or the file) Now there are separate functions to do these 3: - wplua_push_sandbox - wplua_load_* - wplua_pcall
This commit is contained in:
@@ -1427,7 +1427,8 @@ wp_lua_scripting_api_init (lua_State *L)
|
||||
wplua_register_type_methods (L, WP_TYPE_IMPL_MODULE,
|
||||
impl_module_new, NULL);
|
||||
|
||||
wplua_load_uri (L, URI_API, 0, 0, &error);
|
||||
if (G_UNLIKELY (error))
|
||||
if (!wplua_load_uri (L, URI_API, &error) ||
|
||||
!wplua_pcall (L, 0, 0, &error)) {
|
||||
wp_critical ("Failed to load api: %s", error->message);
|
||||
}
|
||||
}
|
||||
|
@@ -96,12 +96,17 @@ load_file (const GValue *item, GValue *ret, gpointer data)
|
||||
lua_State *L = data;
|
||||
const gchar *path = g_value_get_string (item);
|
||||
g_autoptr (GError) error = NULL;
|
||||
int nargs;
|
||||
|
||||
if (g_file_test (path, G_FILE_TEST_IS_DIR))
|
||||
return TRUE;
|
||||
|
||||
wp_info ("loading config file: %s", path);
|
||||
if (!wplua_load_path (L, path, 0, 0, &error)) {
|
||||
|
||||
nargs = wplua_push_sandbox (L);
|
||||
if (!wplua_load_path (L, path, &error) ||
|
||||
!wplua_pcall (L, nargs, 0, &error)) {
|
||||
lua_settop (L, 0);
|
||||
g_value_unset (ret);
|
||||
g_value_init (ret, G_TYPE_ERROR);
|
||||
g_value_take_boxed (ret, g_steal_pointer (&error));
|
||||
@@ -134,8 +139,12 @@ wp_lua_scripting_load_configuration (const gchar * conf_file,
|
||||
path = wp_find_file (CONFIG_DIRS_LOOKUP_SET, conf_file, NULL);
|
||||
if (path) {
|
||||
wp_info ("loading config file: %s", path);
|
||||
if (!wplua_load_path (L, path, 0, 0, error))
|
||||
int nargs = wplua_push_sandbox (L);
|
||||
if (!wplua_load_path (L, path, error) ||
|
||||
!wplua_pcall (L, nargs, 0, error)) {
|
||||
lua_settop (L, 0);
|
||||
return FALSE;
|
||||
}
|
||||
nfiles = 1;
|
||||
}
|
||||
g_clear_pointer (&path, g_free);
|
||||
|
@@ -40,11 +40,17 @@ static gboolean
|
||||
execute_script (lua_State *L, struct ScriptData * s, GError ** error)
|
||||
{
|
||||
int nargs = 0;
|
||||
nargs += wplua_push_sandbox (L);
|
||||
|
||||
if (!wplua_load_path (L, s->filename, error)) {
|
||||
lua_pop (L, nargs);
|
||||
return FALSE;
|
||||
}
|
||||
if (s->args) {
|
||||
wplua_gvariant_to_lua (L, s->args);
|
||||
nargs++;
|
||||
}
|
||||
return wplua_load_path (L, s->filename, nargs, 0, error);
|
||||
return wplua_pcall (L, nargs, 0, error);
|
||||
}
|
||||
|
||||
G_DECLARE_FINAL_TYPE (WpLuaScriptingPlugin, wp_lua_scripting_plugin,
|
||||
|
@@ -117,6 +117,11 @@ wplua_enable_sandbox (lua_State * L, WpLuaSandboxFlags flags)
|
||||
g_autoptr (GError) error = NULL;
|
||||
wp_debug ("enabling Lua sandbox");
|
||||
|
||||
if (!wplua_load_uri (L, URI_SANDBOX, &error)) {
|
||||
wp_critical ("Failed to load sandbox: %s", error->message);
|
||||
return;
|
||||
}
|
||||
|
||||
lua_newtable (L);
|
||||
lua_pushliteral (L, "minimal_std");
|
||||
lua_pushboolean (L, (flags & WP_LUA_SANDBOX_MINIMAL_STD));
|
||||
@@ -125,11 +130,17 @@ wplua_enable_sandbox (lua_State * L, WpLuaSandboxFlags flags)
|
||||
lua_pushboolean (L, (flags & WP_LUA_SANDBOX_ISOLATE_ENV));
|
||||
lua_settable (L, -3);
|
||||
|
||||
if (!wplua_load_uri (L, URI_SANDBOX, 1, 0, &error)) {
|
||||
if (!wplua_pcall (L, 1, 0, &error)) {
|
||||
wp_critical ("Failed to load sandbox: %s", error->message);
|
||||
}
|
||||
}
|
||||
|
||||
int
|
||||
wplua_push_sandbox (lua_State * L)
|
||||
{
|
||||
return (lua_getglobal (L, "sandbox") == LUA_TFUNCTION) ? 1 : 0;
|
||||
}
|
||||
|
||||
void
|
||||
wplua_register_type_methods (lua_State * L, GType type,
|
||||
lua_CFunction constructor, const luaL_Reg * methods)
|
||||
@@ -177,17 +188,9 @@ wplua_register_type_methods (lua_State * L, GType type,
|
||||
|
||||
static gboolean
|
||||
_wplua_load_buffer (lua_State * L, const gchar *buf, gsize size,
|
||||
const gchar * name, int nargs, int nres, GError **error)
|
||||
const gchar * name, GError **error)
|
||||
{
|
||||
int ret;
|
||||
int sandbox = 0;
|
||||
int args_top = lua_gettop (L);
|
||||
|
||||
/* wrap with sandbox() if it's loaded */
|
||||
if (lua_getglobal (L, "sandbox") == LUA_TFUNCTION)
|
||||
sandbox = 1;
|
||||
else
|
||||
lua_pop (L, 1);
|
||||
|
||||
/* skip shebang, if present */
|
||||
if (g_str_has_prefix (buf, "#!/")) {
|
||||
@@ -200,26 +203,14 @@ _wplua_load_buffer (lua_State * L, const gchar *buf, gsize size,
|
||||
if (ret != LUA_OK) {
|
||||
g_set_error (error, WP_DOMAIN_LUA, WP_LUA_ERROR_COMPILATION,
|
||||
"Failed to compile: %s", lua_tostring (L, -1));
|
||||
lua_pop (L, nargs + sandbox + 1);
|
||||
lua_pop (L, 1);
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
/* push sandbox() and the chunk below the arguments */
|
||||
lua_rotate (L, args_top, -nargs);
|
||||
|
||||
ret = _wplua_pcall (L, nargs + sandbox, nres);
|
||||
if (ret != LUA_OK) {
|
||||
g_set_error (error, WP_DOMAIN_LUA, WP_LUA_ERROR_RUNTIME,
|
||||
"Runtime error while loading '%s'", name);
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
gboolean
|
||||
wplua_load_buffer (lua_State * L, const gchar *buf, gsize size,
|
||||
int nargs, int nres, GError **error)
|
||||
wplua_load_buffer (lua_State * L, const gchar *buf, gsize size, GError **error)
|
||||
{
|
||||
g_return_val_if_fail (L != NULL, FALSE);
|
||||
g_return_val_if_fail (buf != NULL, FALSE);
|
||||
@@ -227,12 +218,11 @@ wplua_load_buffer (lua_State * L, const gchar *buf, gsize size,
|
||||
|
||||
g_autofree gchar *name =
|
||||
g_strdup_printf ("buffer@%p;size=%" G_GSIZE_FORMAT, buf, size);
|
||||
return _wplua_load_buffer (L, buf, size, name, nargs, nres, error);
|
||||
return _wplua_load_buffer (L, buf, size, name, error);
|
||||
}
|
||||
|
||||
gboolean
|
||||
wplua_load_uri (lua_State * L, const gchar *uri, int nargs, int nres,
|
||||
GError **error)
|
||||
wplua_load_uri (lua_State * L, const gchar *uri, GError **error)
|
||||
{
|
||||
g_autoptr (GFile) file = NULL;
|
||||
g_autoptr (GBytes) bytes = NULL;
|
||||
@@ -253,12 +243,11 @@ wplua_load_uri (lua_State * L, const gchar *uri, int nargs, int nres,
|
||||
|
||||
name = g_path_get_basename (uri);
|
||||
data = g_bytes_get_data (bytes, &size);
|
||||
return _wplua_load_buffer (L, data, size, name, nargs, nres, error);
|
||||
return _wplua_load_buffer (L, data, size, name, error);
|
||||
}
|
||||
|
||||
gboolean
|
||||
wplua_load_path (lua_State * L, const gchar *path, int nargs, int nres,
|
||||
GError **error)
|
||||
wplua_load_path (lua_State * L, const gchar *path, GError **error)
|
||||
{
|
||||
g_autofree gchar *abs_path = NULL;
|
||||
g_autofree gchar *uri = NULL;
|
||||
@@ -274,5 +263,17 @@ wplua_load_path (lua_State * L, const gchar *path, int nargs, int nres,
|
||||
if (!(uri = g_filename_to_uri (abs_path ? abs_path : path, NULL, error)))
|
||||
return FALSE;
|
||||
|
||||
return wplua_load_uri (L, uri, nargs, nres, error);
|
||||
return wplua_load_uri (L, uri, error);
|
||||
}
|
||||
|
||||
gboolean
|
||||
wplua_pcall (lua_State * L, int nargs, int nres, GError **error)
|
||||
{
|
||||
int ret = _wplua_pcall (L, nargs, nres);
|
||||
if (ret != LUA_OK) {
|
||||
g_set_error (error, WP_DOMAIN_LUA, WP_LUA_ERROR_RUNTIME,
|
||||
"Lua runtime error");
|
||||
return FALSE;
|
||||
}
|
||||
return TRUE;
|
||||
}
|
||||
|
@@ -52,6 +52,7 @@ lua_State * wplua_new (void);
|
||||
void wplua_free (lua_State * L);
|
||||
|
||||
void wplua_enable_sandbox (lua_State * L, WpLuaSandboxFlags flags);
|
||||
int wplua_push_sandbox (lua_State * L);
|
||||
|
||||
void wplua_register_type_methods (lua_State * L, GType type,
|
||||
lua_CFunction constructor, const luaL_Reg * methods);
|
||||
@@ -87,11 +88,11 @@ WpProperties * wplua_table_to_properties (lua_State *L, int idx);
|
||||
void wplua_properties_to_table (lua_State *L, WpProperties *p);
|
||||
|
||||
gboolean wplua_load_buffer (lua_State * L, const gchar *buf, gsize size,
|
||||
int nargs, int nres, GError **error);
|
||||
gboolean wplua_load_uri (lua_State * L, const gchar *uri, int nargs, int nres,
|
||||
GError **error);
|
||||
gboolean wplua_load_path (lua_State * L, const gchar *path, int nargs, int nres,
|
||||
GError **error);
|
||||
gboolean wplua_load_uri (lua_State * L, const gchar *uri, GError **error);
|
||||
gboolean wplua_load_path (lua_State * L, const gchar *path, GError **error);
|
||||
|
||||
gboolean wplua_pcall (lua_State * L, int nargs, int nres, GError **error);
|
||||
|
||||
G_DEFINE_AUTOPTR_CLEANUP_FUNC(lua_State, wplua_free)
|
||||
|
||||
|
Reference in New Issue
Block a user