diff --git a/include/chaiscript/language/chaiscript_engine.hpp b/include/chaiscript/language/chaiscript_engine.hpp index d3b245ce..c9f22e58 100644 --- a/include/chaiscript/language/chaiscript_engine.hpp +++ b/include/chaiscript/language/chaiscript_engine.hpp @@ -473,6 +473,12 @@ namespace chaiscript { /// \returns All values in the local thread state, added through the add() function std::map get_locals() const { return m_engine.get_locals(); } + /// \returns All accessible function objects, as a map of name to Boxed_Value + std::map get_function_objects() const { return m_engine.get_function_objects(); } + + /// \returns All accessible scripting objects (locals + globals), as a map of name to Boxed_Value + std::map get_scripting_objects() const { return m_engine.get_scripting_objects(); } + /// \brief Sets all of the locals for the current thread state. /// /// \param[in] t_locals The map set of variables to replace the current state with diff --git a/unittests/compiled_tests.cpp b/unittests/compiled_tests.cpp index 1462c203..750c4c47 100644 --- a/unittests/compiled_tests.cpp +++ b/unittests/compiled_tests.cpp @@ -412,6 +412,25 @@ TEST_CASE("Set and restore chai state") { CHECK_THROWS_AS(chai.eval("i"), chaiscript::exception::eval_error); } +TEST_CASE("Get function objects from public API") { + chaiscript::ChaiScript_Basic chai(create_chaiscript_stdlib(), create_chaiscript_parser()); + + // Add a custom function + chai.add(chaiscript::fun(&set_state_test_myfun), "myfun"); + + // get_function_objects should be accessible from the public API + auto funcs = chai.get_function_objects(); + + // Our custom function should be in the map + CHECK(funcs.count("myfun") == 1); + + // Built-in functions should also be present + CHECK(funcs.count("to_string") == 1); + + // The function should be callable + CHECK(chai.eval("myfun()") == 2); +} + //// Short comparisons class Short_Comparison_Test {