Fix errors with eval/use of scripts

This commit is contained in:
Jason Turner 2015-04-10 08:20:30 -06:00
parent 63684d0042
commit b489ffe3ed
4 changed files with 29 additions and 8 deletions

View File

@ -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

View File

@ -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);
}

View File

@ -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)
}

View File

@ -1,7 +1,7 @@
def shouldnt_execute()
{
assert(false)
assert_true(false)
}