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

Option<enum> returns undefined #1198

Closed
attente opened this issue Jan 21, 2019 · 4 comments
Closed

Option<enum> returns undefined #1198

attente opened this issue Jan 21, 2019 · 4 comments

Comments

@attente
Copy link

attente commented Jan 21, 2019

Hello! I have a reproducer for this bug here: https://github.com/attente/wasm-bindgen-option

Basically:

use wasm_bindgen::prelude::*;

// doesn't work :(
#[wasm_bindgen]
pub fn foo() -> Option<Foo> {
    Some(Foo::Foo)
}

// works!
#[wasm_bindgen]
pub fn bar() -> Foo {
    Foo::Bar
}

// works!
#[wasm_bindgen]
pub fn baz() -> Option<i32> {
    Some(3)
}

pub enum Foo {
    Foo = 1,
    Bar = 2,
}

impl wasm_bindgen::describe::WasmDescribe for Foo {
    fn describe() {
        wasm_bindgen::describe::inform(wasm_bindgen::describe::I32)
    }
}

impl wasm_bindgen::convert::IntoWasmAbi for Foo {
    type Abi = i32;

    fn into_abi(self, _extra: &mut dyn wasm_bindgen::convert::Stack) -> Self::Abi {
        self as Self::Abi
    }
}

impl wasm_bindgen::convert::OptionIntoWasmAbi for Foo {
    fn none() -> Self::Abi {
        0
    }
}

Returning an enum that implementswasm_bindgen::convert::OptionIntoWasmAbi works. (bar())
Returning an Option<i32> works. (baz())
But returning Option<enum: wasm_bindgen::convert::OptionIntoWasmAbi> doesn't work. (foo())

The output I got from the reproducer is:

Some(Foo::Foo) = undefined
Foo::Bar = 2
3 = 3
@alexcrichton
Copy link
Contributor

Thanks for the report! Currently WasmDescribe is an internal trait, however, not intended to be implemented externally (but we have to expose for implementation reasons for now). I suspect there's probably subtelties there, but the exact pieces escape me. Would adding #[wasm_bindgen] directly on the enum work for your use case?

@attente
Copy link
Author

attente commented Jan 22, 2019

Adding #[wasm_bindgen] on the enum causes the compiler to complain that there are multiple implementations of wasm_bindgen::convert::traits::IntoWasmAbi and wasm_bindgen::describe::WasmDescribe for type Foo. Fair enough, but after removing my own definitions, wasm-bindgen then complains:

error: failed to generate bindings for Rust export `foo`
	caused by: unsupported optional return type for calling Rust function from JS: Enum

@alexcrichton
Copy link
Contributor

Oh dear that sounds like a bug! That may just be an unimplemented code path in wasm-bindgen's JS generating for now

alexcrichton added a commit to alexcrichton/wasm-bindgen that referenced this issue Jan 28, 2019
Find a hole automatically to use a sentinel value for `None`, and then
just wire everything up!

Closes rustwasm#1198
@alexcrichton
Copy link
Contributor

Ok option support should be added in #1214

alexcrichton added a commit to alexcrichton/wasm-bindgen that referenced this issue Jan 28, 2019
Find a hole automatically to use a sentinel value for `None`, and then
just wire everything up!

Closes rustwasm#1198
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants