Changeset

12250:e157e5c79daa

util.pluginloader: Support for multiple pluginloader instances, and options
author Matthew Wild <mwild1@gmail.com>
date Thu, 03 Feb 2022 12:53:19 +0000
parents 12249:5173f8a761a0
children 12251:21ed12cfe300
files util/debug.lua util/pluginloader.lua
diffstat 2 files changed, 29 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/util/debug.lua	Thu Feb 03 10:02:26 2022 +0100
+++ b/util/debug.lua	Thu Feb 03 12:53:19 2022 +0000
@@ -31,7 +31,7 @@
 	for local_num = 1, math.huge do
 		local name, value;
 		if thread then
-			name, value = debug.getlocal(thread, level, local_num);
+			name, value = debug.getlocal(thread, level-1, local_num);
 		else
 			name, value = debug.getlocal(level+1, local_num);
 		end
--- a/util/pluginloader.lua	Thu Feb 03 10:02:26 2022 +0100
+++ b/util/pluginloader.lua	Thu Feb 03 12:53:19 2022 +0000
@@ -19,7 +19,10 @@
 local io_open = io.open;
 local envload = require "util.envload".envload;
 
-local function load_file(names)
+local pluginloader_methods = {};
+local pluginloader_mt = { __index = pluginloader_methods };
+
+function pluginloader_methods:load_file(names)
 	local file, err, path;
 	for i=1,#plugin_dir do
 		for j=1,#names do
@@ -35,8 +38,7 @@
 	return file, err;
 end
 
-local function load_resource(plugin, resource)
-	resource = resource or "mod_"..plugin..".lua";
+function pluginloader_methods:load_resource(plugin, resource)
 	local names = {
 		"mod_"..plugin..dir_sep..plugin..dir_sep..resource; -- mod_hello/hello/mod_hello.lua
 		"mod_"..plugin..dir_sep..resource;                  -- mod_hello/mod_hello.lua
@@ -46,10 +48,10 @@
 		"share"..dir_sep.."lua"..dir_sep..lua_version..dir_sep.."mod_"..plugin..dir_sep..resource;
 	};
 
-	return load_file(names);
+	return self:load_file(names);
 end
 
-local function load_code(plugin, resource, env)
+function pluginloader_methods:load_code(plugin, resource, env)
 	local content, err = load_resource(plugin, resource);
 	if not content then return content, err; end
 	local path = err;
@@ -58,7 +60,7 @@
 	return f, path;
 end
 
-local function load_code_ext(plugin, resource, extension, env)
+function pluginloader_methods:load_code_ext(plugin, resource, extension, env)
 	local content, err = load_resource(plugin, resource.."."..extension);
 	if not content and extension == "lib.lua" then
 		content, err = load_resource(plugin, resource..".lua");
@@ -75,9 +77,25 @@
 	return f, path;
 end
 
+local function init(options)
+	return setmetatable({
+		_options = options or {};
+	}, pluginloader_mt);
+end
+
+local function bind(self, method)
+	return function (...)
+		return method(self, ...);
+	end;
+end
+
+local default_loader = init();
+
 return {
-	load_file = load_file;
-	load_resource = load_resource;
-	load_code = load_code;
-	load_code_ext = load_code_ext;
+	load_file = bind(default_loader, default_loader.load_file);
+	load_resource = bind(default_loader, default_loader.load_resource);
+	load_code = bind(default_loader, default_loader.load_code);
+	load_code_ext = bind(default_loader, default_loader.load_code_ext);
+
+	init = init;
 };