diff --git a/src/def_configuration.cpp b/src/def_configuration.cpp index 1b14aa4..00e97d8 100644 --- a/src/def_configuration.cpp +++ b/src/def_configuration.cpp @@ -20,12 +20,28 @@ #include namespace wren { + namespace { + template + bool empty(T* message) { + return not (message and *message); + } + } //unnamed namespace + void DefConfiguration::write_fn (VM*, wren_string_t text) { std::cout << text; } void DefConfiguration::error_fn (VM*, ErrorType type, wren_string_t module, int line, wren_string_t message) { - std::cerr << "Wren error: " << message << " in " << module << ':' << line << '\n'; + using std::empty; + + std::cerr << "Wren error: " << message; + if (!empty(module)) { + std::cerr << " in " << module; + if (line > -1) { + std::cerr << ':' << line; + } + } + std::cerr << '\n'; } void* DefConfiguration::reallocate_fn (void* ptr, std::size_t size) { diff --git a/src/vm.cpp b/src/vm.cpp index 237458b..2874dd9 100644 --- a/src/vm.cpp +++ b/src/vm.cpp @@ -47,7 +47,8 @@ namespace wren { auto cb = static_cast(wrenGetUserData(wvm)); assert(cb); assert(cb->error_fn and cb->config_obj and cb->owner); - cb->error_fn(*cb->config_obj, cb->owner, to_error_type(type), module, line, message); + const char* const sane_module = (module ? module : ""); + cb->error_fn(*cb->config_obj, cb->owner, to_error_type(type), sane_module, line, message); } const char* resolve_module_fn (WrenVM* wvm, const char* importer, const char* name) {