#include int myfun() { return 2; } int main() { chaiscript::ChaiScript chai; // save the initial state of globals and locals chaiscript::ChaiScript::State firststate = chai.get_state(); std::map locals = chai.get_locals(); // add some new globals and locals chai.add(chaiscript::var(1), "i"); chai.add(chaiscript::fun(&myfun), "myfun"); bool didcall = chai.eval("myfun()") == 2; bool hadi = chai.eval("i") == 1; chai.set_state(firststate); // set state should have reverted the state of the functions and dropped // the 'myfun' bool didnotcall = false; try { chai.eval("myfun()"); } catch (const chaiscript::exception::eval_error &) { didnotcall = true; } // set state should not affect the local variables bool stillhasid = chai.eval("i") == 1; // After resetting the locals we expect the 'i' to be gone chai.set_locals(locals); bool nolongerhasid = false; try { chai.eval("i"); } catch (const chaiscript::exception::eval_error &) { nolongerhasid = true; } if (didcall && hadi && didnotcall && stillhasid && nolongerhasid) { return EXIT_SUCCESS; } else { return EXIT_FAILURE; } }