2 ms·
> handle.value(&mut cx) as i32 What happens when the user in JS land passes in an incorrect data type? Does it segfault? Does it truncate the first 32 bits?
by trinovantes 5y ago
> handle.value(&mut cx) as i32
What happens when the user in JS land passes in an incorrect data type? Does it segfault? Does it truncate the first 32 bits?
- skwee357 5y agoAs @brundolf said above me, it will return Error because I explicitly downcast it to `JsNumber` and unwrap the result. So from JS side you'll get an error. Obviously, you should handle such cases properly.
- brundolf 5y agoIf they don't pass a number at all, looks like cx.argument() will return a Result::Err (which in the example will panic because of the unwrap(), but can instead be bubbled up as another commenter pointed out here: https://news.ycombinator.com/item?id=28977697 https://news.ycombinator.com/item?id=28977697) JsNumber::value() then returns an f64 to encapsulate all possible JS numbers, and `as i32` is just casting that float into an int. The exact behavior for this cast is that a) any fractional component will be dropped, and b) if the number is larger than the destination type, the destination's maximum value will be used. Both of these behaviors could of course be controlled by adding logic, like an explicit call to .round()