From 6092852640096c777f700cf669666504b10e2a58 Mon Sep 17 00:00:00 2001 From: Justin Frank Date: Tue, 12 Mar 2019 10:34:30 -0700 Subject: Added 'provide-module' and 'require-module' commands --- src/command_manager.cc | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) (limited to 'src/command_manager.cc') diff --git a/src/command_manager.cc b/src/command_manager.cc index 5e8c8196..91c41bd3 100644 --- a/src/command_manager.cc +++ b/src/command_manager.cc @@ -40,6 +40,33 @@ void CommandManager::register_command(String command_name, std::move(completer) }; } +bool CommandManager::module_defined(StringView module_name) const +{ + return m_modules.find(module_name) != m_modules.end(); +} + +void CommandManager::register_module(String module_name, String commands) +{ + auto module = m_modules.find(module_name); + if (module != m_modules.end() and module->value.loaded) + throw runtime_error{format("module already loaded: '{}'", module_name)}; + + m_modules[module_name] = { false, std::move(commands) }; +} + +void CommandManager::load_module(StringView module_name, Context& context) +{ + auto module = m_modules.find(module_name); + if (module == m_modules.end()) + throw runtime_error{format("no such module: '{}'", module_name)}; + if (module->value.loaded) + return; + + module->value.loaded = true; + execute(module->value.commands, context); + module->value.commands.clear(); +} + struct parse_error : runtime_error { parse_error(StringView error) -- cgit v1.2.3 From 7866d88131837f7c514e73c57773185ba33beb7b Mon Sep 17 00:00:00 2001 From: Justin Frank Date: Wed, 13 Mar 2019 12:46:53 -0700 Subject: Added ModuleLoad hook that uses the module name as the parameter --- src/command_manager.cc | 2 ++ 1 file changed, 2 insertions(+) (limited to 'src/command_manager.cc') diff --git a/src/command_manager.cc b/src/command_manager.cc index 91c41bd3..aff9e3fd 100644 --- a/src/command_manager.cc +++ b/src/command_manager.cc @@ -65,6 +65,8 @@ void CommandManager::load_module(StringView module_name, Context& context) module->value.loaded = true; execute(module->value.commands, context); module->value.commands.clear(); + + context.hooks().run_hook(Hook::ModuleLoad, module_name, context); } struct parse_error : runtime_error -- cgit v1.2.3 From c40bb6fc009bd0933290b61717605c0d5bf68aee Mon Sep 17 00:00:00 2001 From: Justin Frank Date: Sat, 16 Mar 2019 01:38:32 -0700 Subject: Evaluate modules in an empty context --- src/command_manager.cc | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'src/command_manager.cc') diff --git a/src/command_manager.cc b/src/command_manager.cc index aff9e3fd..1fdfa34d 100644 --- a/src/command_manager.cc +++ b/src/command_manager.cc @@ -63,7 +63,8 @@ void CommandManager::load_module(StringView module_name, Context& context) return; module->value.loaded = true; - execute(module->value.commands, context); + Context empty_context{Context::EmptyContextFlag{}}; + execute(module->value.commands, empty_context); module->value.commands.clear(); context.hooks().run_hook(Hook::ModuleLoad, module_name, context); -- cgit v1.2.3