mirror of
https://github.com/ChaiScript/ChaiScript.git
synced 2026-04-30 19:09:26 +08:00
* Fix #256: Expose get_function_objects() and get_scripting_objects() in public API The ChaiScript_Basic public API only exposed get_locals() for inspecting runtime state from C++, requiring users to work around this via chai.eval("get_functions()") to access function objects. Added get_function_objects() and get_scripting_objects() as public methods on ChaiScript_Basic, delegating to the existing Dispatch_Engine methods, matching the pattern already used by get_locals(). Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * Address review: add tests for get_scripting_objects() Requested by @lefticus in PR #640 review. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> --------- Co-authored-by: leftibot <leftibot@users.noreply.github.com> Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
parent
fc574c320b
commit
e42497a8b3
@ -473,6 +473,12 @@ namespace chaiscript {
|
||||
/// \returns All values in the local thread state, added through the add() function
|
||||
std::map<std::string, Boxed_Value> get_locals() const { return m_engine.get_locals(); }
|
||||
|
||||
/// \returns All accessible function objects, as a map of name to Boxed_Value
|
||||
std::map<std::string, Boxed_Value> 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<std::string, Boxed_Value> 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<name, value> set of variables to replace the current state with
|
||||
|
||||
@ -412,6 +412,44 @@ TEST_CASE("Set and restore chai state") {
|
||||
CHECK_THROWS_AS(chai.eval<int>("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<int>("myfun()") == 2);
|
||||
}
|
||||
|
||||
TEST_CASE("Get scripting objects from public API") {
|
||||
chaiscript::ChaiScript_Basic chai(create_chaiscript_stdlib(), create_chaiscript_parser());
|
||||
|
||||
// Define some variables in script
|
||||
chai.eval("var x = 42");
|
||||
chai.eval("var name = \"hello\"");
|
||||
|
||||
// get_scripting_objects should be accessible from the public API
|
||||
auto objects = chai.get_scripting_objects();
|
||||
|
||||
// Our scripting variables should be in the map
|
||||
CHECK(objects.count("x") == 1);
|
||||
CHECK(objects.count("name") == 1);
|
||||
|
||||
// Verify the values are correct
|
||||
CHECK(chaiscript::boxed_cast<int>(objects["x"]) == 42);
|
||||
CHECK(chaiscript::boxed_cast<std::string>(objects["name"]) == "hello");
|
||||
}
|
||||
|
||||
//// Short comparisons
|
||||
|
||||
class Short_Comparison_Test {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user