diff --git a/src/library/kernel_bindings.cpp b/src/library/kernel_bindings.cpp index 22accf2ae..3ec2eb04a 100644 --- a/src/library/kernel_bindings.cpp +++ b/src/library/kernel_bindings.cpp @@ -43,6 +43,7 @@ Author: Leonardo de Moura namespace lean { static environment get_global_environment(lua_State * L); io_state * get_io_state(lua_State * L); +io_state get_tmp_io_state(lua_State * L); // Level DECL_UDATA(level) @@ -1161,9 +1162,16 @@ static int import_modules(environment const & env, lua_State * L, int s) { buffer mnames; to_string_buffer(L, s, mnames); unsigned num_threads = 1; - if (lua_gettop(L) > s) + int nargs = lua_gettop(L); + if (nargs > s) num_threads = lua_tonumber(L, s+1); - return push_environment(L, import_modules(env, mnames.size(), mnames.data(), num_threads)); + + if (nargs > s + 1 && is_io_state(L, s+2)) + return push_environment(L, import_modules(env, mnames.size(), mnames.data(), num_threads, to_io_state(L, s+2))); + else if (io_state * ios = get_io_state(L)) + return push_environment(L, import_modules(env, mnames.size(), mnames.data(), num_threads, *ios)); + else + return push_environment(L, import_modules(env, mnames.size(), mnames.data(), num_threads, get_tmp_io_state(L))); } static int import_modules(lua_State * L) { @@ -1397,6 +1405,10 @@ io_state * get_io_state(lua_State * L) { return nullptr; } +io_state get_tmp_io_state(lua_State * L) { + return io_state(get_global_options(L), get_global_formatter(L)); +} + // Justification DECL_UDATA(justification) diff --git a/src/library/module.cpp b/src/library/module.cpp index da06892fa..67cef1fe0 100644 --- a/src/library/module.cpp +++ b/src/library/module.cpp @@ -142,6 +142,7 @@ struct import_modules_fn { typedef std::tuple delayed_update; shared_environment m_senv; unsigned m_num_threads; + io_state m_ios; mutex m_asynch_mutex; condition_variable m_asynch_cv; std::vector m_asynch_tasks; @@ -162,8 +163,8 @@ struct import_modules_fn { typedef std::shared_ptr module_info_ptr; std::unordered_map m_module_info; - import_modules_fn(environment const & env, unsigned num_threads): - m_senv(env), m_num_threads(num_threads), + import_modules_fn(environment const & env, unsigned num_threads, io_state const & ios): + m_senv(env), m_num_threads(num_threads), m_ios(ios), m_import_counter(0), m_all_modules_imported(false) { if (m_num_threads == 0) m_num_threads = 1; @@ -343,7 +344,7 @@ struct import_modules_fn { return std::get<1>(u1) < std::get<1>(u2); }); for (auto const & d : m_delayed_tasks) { - env = std::get<2>(d)(env); + env = std::get<2>(d)(env, m_ios); } return env; } @@ -366,11 +367,13 @@ struct import_modules_fn { } }; -environment import_modules(environment const & env, unsigned num_modules, std::string const * modules, unsigned num_threads) { - return import_modules_fn(env, num_threads)(num_modules, modules); +environment import_modules(environment const & env, unsigned num_modules, std::string const * modules, + unsigned num_threads, io_state const & ios) { + return import_modules_fn(env, num_threads, ios)(num_modules, modules); } -environment import_module(environment const & env, std::string const & module, unsigned num_threads) { - return import_modules(env, 1, &module, num_threads); +environment import_module(environment const & env, std::string const & module, + unsigned num_threads, io_state const & ios) { + return import_modules(env, 1, &module, num_threads, ios); } } diff --git a/src/library/module.h b/src/library/module.h index 0ee5ae25a..13f9fb552 100644 --- a/src/library/module.h +++ b/src/library/module.h @@ -9,6 +9,7 @@ Author: Leonardo de Moura #include #include "util/serializer.h" #include "library/shared_environment.h" +#include "library/io_state.h" namespace lean { /** @@ -16,8 +17,10 @@ namespace lean { Modules included directly or indirectly by them are also imported. The environment \c env is usually an empty environment. */ -environment import_modules(environment const & env, unsigned num_modules, std::string const * modules, unsigned num_threads = 1); -environment import_module(environment const & env, std::string const & module, unsigned num_threads = 1); +environment import_modules(environment const & env, unsigned num_modules, std::string const * modules, + unsigned num_threads, io_state const & ios); +environment import_module(environment const & env, std::string const & module, + unsigned num_threads, io_state const & ios); /** \brief Store/Export module using \c env to the output stream \c out. @@ -32,8 +35,7 @@ typedef std::function asynch_update_fn; Example: if module A was imported before B, then delayed updates from A are executed before the ones from B. */ -typedef std::function delayed_update_fn; - +typedef std::function delayed_update_fn; /** \brief A reader for importing data from a stream using deserializer \c d.