Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Delayed error using a chain of try_from_js_value on #[wasm_bindgen] structs #4289

Open
Tim-Evans-Seequent opened this issue Nov 27, 2024 · 0 comments
Labels

Comments

@Tim-Evans-Seequent
Copy link

Tim-Evans-Seequent commented Nov 27, 2024

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.

@Tim-Evans-Seequent Tim-Evans-Seequent changed the title Delayed error using a chain of try_from_js_value on a #[wasm_bindgen] structs Delayed error using a chain of try_from_js_value on #[wasm_bindgen] structs Nov 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant