From d5ddb186d99b7d99855d2b5f9335f98ad52166e4 Mon Sep 17 00:00:00 2001 From: Leonardo de Moura Date: Tue, 7 Jan 2014 14:21:24 -0800 Subject: [PATCH] feat(library/kernel_bindings): add load method to Lua API Signed-off-by: Leonardo de Moura --- src/library/kernel_bindings.cpp | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/src/library/kernel_bindings.cpp b/src/library/kernel_bindings.cpp index afecef6e9..2f7a5da74 100644 --- a/src/library/kernel_bindings.cpp +++ b/src/library/kernel_bindings.cpp @@ -1141,24 +1141,37 @@ static int environment_is_opaque(lua_State * L) { return 1; } -static int environment_import(lua_State * L) { +template +static int environment_import_core(lua_State * L, F && import) { rw_shared_environment env(L, 1); int nargs = lua_gettop(L); if (nargs == 3) { - env->import(luaL_checkstring(L, 2), to_io_state(L, 3)); + import(env, luaL_checkstring(L, 2), to_io_state(L, 3)); } else { io_state * ios = get_io_state(L); if (ios) { - env->import(luaL_checkstring(L, 2), *ios); + import(env, luaL_checkstring(L, 2), *ios); } else { io_state ios(mk_simple_formatter()); ios.set_options(get_global_options(L)); - env->import(luaL_checkstring(L, 2), ios); + import(env, luaL_checkstring(L, 2), ios); } } return 0; } +static int environment_import(lua_State * L) { + return environment_import_core(L, [](rw_shared_environment & env, char const * fname, io_state const & ios) { + return env->import(fname, ios); + }); +} + +static int environment_load(lua_State * L) { + return environment_import_core(L, [](rw_shared_environment & env, char const * fname, io_state const & ios) { + return env->load(fname, ios); + }); +} + static int environment_get_universe_distance(lua_State * L) { ro_shared_environment env(L, 1); auto r = env->get_universe_distance(to_name_ext(L, 2), to_name_ext(L, 3)); @@ -1194,6 +1207,7 @@ static const struct luaL_Reg environment_m[] = { {"set_opaque", safe_function}, {"is_opaque", safe_function}, {"import", safe_function}, + {"load", safe_function}, {0, 0} };