diff --git a/cheatsheet.md b/cheatsheet.md index fc229ac..25e8806 100644 --- a/cheatsheet.md +++ b/cheatsheet.md @@ -269,4 +269,6 @@ use("filename") // evals file exactly once and returns value of last statement // if the file had already been 'used' nothing happens and undefined is returned ``` +Both `use` and `eval_file` search the 'usepaths' passed to the ChaiScript constructor + diff --git a/include/chaiscript/language/chaiscript_engine.hpp b/include/chaiscript/language/chaiscript_engine.hpp index 9ee75d6..d65badd 100644 --- a/include/chaiscript/language/chaiscript_engine.hpp +++ b/include/chaiscript/language/chaiscript_engine.hpp @@ -297,13 +297,23 @@ namespace chaiscript } } - /// Evaluates the given string, used during eval() inside of a script + /// Evaluates the given file and looks in the 'use' paths const Boxed_Value internal_eval_file(const std::string &t_filename) { - try { - return do_eval(load_file(t_filename), t_filename, true); - } catch (const exception::eval_error &t_ee) { - throw Boxed_Value(t_ee); + for (const auto &path : m_usepaths) + { + try { + const auto appendedpath = path + t_filename; + return do_eval(load_file(appendedpath), appendedpath, true); + } catch (const exception::file_not_found_error &) { + // failed to load, try the next path + } catch (const exception::eval_error &t_ee) { + throw Boxed_Value(t_ee); + } } + + // failed to load by any name + throw exception::file_not_found_error(t_filename); + } diff --git a/unittests/eval_file.chai b/unittests/eval_file.chai index be9934e..25e3fbb 100644 --- a/unittests/eval_file.chai +++ b/unittests/eval_file.chai @@ -1,10 +1,19 @@ try { eval_file("use.inc") - assert(true) + assert_true(true) + print("used use.inc") +} catch (e) { + print("error: " + e.what()) + assert_true(false) +} + +try { //we expect this second eval_file to fail because of a function redefinition eval_file("use.inc") - assert(false) + assert_true(false) + print("used use.inc x2") } catch (e) { + assert_true(true) } diff --git a/unittests/logical_short_circuiting.chai b/unittests/logical_short_circuiting.chai index 55cae32..f43cf28 100644 --- a/unittests/logical_short_circuiting.chai +++ b/unittests/logical_short_circuiting.chai @@ -1,7 +1,7 @@ def shouldnt_execute() { - assert(false) + assert_true(false) }