Skip to the content.

Values and errors

rescripted::Value

Runtime type. Scripts and host functions share this.

Value::Type C++ construction Notes
Null Value() or Value(nullptr)  
Bool Value(true)  
Number Value(3.0) or Value(3) Stored as double
String Value("hi") or Value(std::string{})  
Array Value(std::vector<Value>{...})  
Map Value(std::map<std::string, Value>{...}) Custom entity bodies become maps
Function Value::function(name, HostFn) Prefer engine.bind

Helpers:

bool is_null() / is_bool() / is_number() / is_string() / is_array() / is_map() / is_function();
bool as_bool();          // truthiness: null/0/"" /empty array are false
double as_number();      // throws RuntimeError if impossible
const std::string& as_string();
std::string to_string(); // always succeeds; arrays/maps print <array>/<map>
Value call(const std::vector<Value>& args);
bool equals(const Value& other); // null/bool/number/string only

as_array() / as_map() have const and non-const overloads; they throw if the type is wrong.

When writing host functions, check args.size() and types yourself. Return Value() for “no result.”

Errors

All of these are std::runtime_error subclasses. Catch rescripted::Error to get everything.

Type When
Error Bad engine use: unknown language, missing script, cannot open file, empty language name
ParseError Invalid JSON or script syntax. line(), column()
RuntimeError Undefined variable, bad call, division by zero, wrong Value conversion

Do not swallow these in generated host code unless the user asked for a sandbox. Log e.what().

try {
  engine.run(id);
} catch (const rescripted::ParseError& e) {
  // e.line(), e.column(), e.what()
} catch (const rescripted::RuntimeError& e) {
} catch (const rescripted::Error& e) {
}