Fix errors with eval/use of scripts
This commit is contained in:
@@ -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
|
// 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
|
||||||
|
|
||||||
|
|
||||||
|
@@ -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) {
|
const Boxed_Value internal_eval_file(const std::string &t_filename) {
|
||||||
try {
|
for (const auto &path : m_usepaths)
|
||||||
return do_eval(load_file(t_filename), t_filename, true);
|
{
|
||||||
} catch (const exception::eval_error &t_ee) {
|
try {
|
||||||
throw Boxed_Value(t_ee);
|
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);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@@ -1,10 +1,19 @@
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
eval_file("use.inc")
|
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
|
//we expect this second eval_file to fail because of a function redefinition
|
||||||
eval_file("use.inc")
|
eval_file("use.inc")
|
||||||
assert(false)
|
assert_true(false)
|
||||||
|
print("used use.inc x2")
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
assert_true(true)
|
||||||
}
|
}
|
||||||
|
@@ -1,7 +1,7 @@
|
|||||||
|
|
||||||
def shouldnt_execute()
|
def shouldnt_execute()
|
||||||
{
|
{
|
||||||
assert(false)
|
assert_true(false)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user