rhaiscript / rhai

Rhai - An embedded scripting language for Rust.
https://crates.io/crates/rhai
Apache License 2.0
3.79k stars 177 forks source link

Runtime Error Handling: A Minimal Proposal #78

Closed stevedonovan closed 4 years ago

stevedonovan commented 6 years ago

Not having error handling (other than panicking) is a problem when integrating non-trivial Rust code with Rhai. This proposal basically adds unchecked exceptions so that functions can terminate script execution with a clean error.

First, EvalAltResult gets a new variant, ErrorRuntime(String). A user-defined function may 'throw' a Rhai exception by returning a Result<T,EvalAltResult>; a Rhai function may call a predefined throw function.

Implementation is fairly straightforward. We keep a map of type ids for all Result<T,EvalAltResult> of interest, providing a closure that can match this for each specific T and return a Result<Box<Any>,EvalAltResult> which Rhai passes on. These closures are created in register_type (finally, no longer a no-op!)

There will be some overhead with doing a hash lookup on all returned values, and with re-boxing 'exception' values.

This is implementable now, as an incremental step towards either Rust-style explicit result handling or catchable exceptions.

stevedonovan commented 6 years ago

There's an experimental implementation at https://github.com/stevedonovan/rhai, runtime-errors branch. Seems to work fine, although I wonder if raise is not a better word than throw :)

But I won't immediately submit a PR since this is something that needs to be discussed first.

schungx commented 4 years ago

I can take a whack at it. Probably simple to extend the return functionality.

schungx commented 4 years ago

Turns out it is quite easy to add. Already did.

My implementation takes the easy way out by requiring throw to always throw nothing or throw a string, which then gets mapped to EvalAltResult::ErrorRuntime(reason, position) with reason capturing the thrown string. Throwing anything other than a string will lose the value.

schungx commented 4 years ago

Closed by https://github.com/jonathandturner/rhai/pull/104