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:
George Kiagiadakis
2022-03-30 18:22:21 +03:00
parent a2472542a9
commit e1d0240bc9
6 changed files with 98 additions and 62 deletions

View File

@@ -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);
}
}

View File

@@ -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);