[][src]Function rosy::vm::eval

pub unsafe fn eval(script: &CStr) -> AnyObject

Evaluates script in an isolated binding without handling exceptions.

Variables:

  • __FILE__: "(eval)"
  • __LINE__: starts at 1

Safety

Code executed from script may void the type safety of objects accessible from Rust. For example, if one calls push on Array<A> with an object of type B, then the inserted object will be treated as being of type A.

use std::ffi::CStr;
use rosy::prelude::*;

let array: Array<Integer> = (1..=3).collect(); // [1, 2, 3]

let module = Module::def("Evil").unwrap();
unsafe { module.set_const("ARR", array) };

let script = b"Evil::ARR.push('hi')\0";
let script = CStr::from_bytes_with_nul(script).unwrap();

unsafe { rosy::vm::eval(script) };
let hi = array.get(3).unwrap();

If we try using hi as an Integer here, we will get a segmentation fault:

let val = hi.to_truncated::<i32>();

However, we can see that hi is actually a String despite being typed as an Integer:

let hi = unsafe { String::cast_unchecked(hi) };
assert_eq!(hi, "hi");

...also, any exceptions raised in script must be handled in Rust-land.