Description
Describe the Bug
I wanted to have function that accepts a JsValue
argument and checked at run-time which of several #[wasm_bindgen] structs it was and does something slightly different for each one. I instead got a hard to diagnose panic in a different crate caused by ::js_sys::Reflect::set
returning a leftover error.
Steps to Reproduce
The code I wrote looked like this:
if let Ok(x) = FirstStruct::try_from_js_value(value.clone()) {
...
} else if let Ok(mesh) = SecondStruct::try_from_js_value(value.clone()) {
...
} else {
return Err(JsError::new("should be FirstStruct or SecondStruct"))
}
Shortly after this I made some wgpu
calls, and one of them panicked out of nowhere on an assertion that said "setting properties should never fail".
Changing the order of the types checked fixed the problem, until I tried the other type.
Expected Behavior
Any error set by try_from_js_value
should be contained in its return value, and not be able to affect other code. I should not be required to return an error when try_from_js_value
fails; checking multiple types should be a valid.
Alternatively, this pattern should be prevented by the compiler somehow, or worst case documented as causing problems.
Actual Behavior
A subsequent call to js_sys::Reflect::set
inside wgpu::backend::webgpu::webgpu_sys::gen_GpuBufferDescriptor::GpuBufferDescriptor::size
failed and caused a panic. This assertion is debug only, in release mode I instead got an error from WebGPU due to the missing field.
This was running on Chrome on Windows in case that matters.