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

.dyn_into::<Symbol> / `.dyn_ref::<Symbol> don't work as expected #1370

Closed
RReverser opened this issue Mar 20, 2019 · 0 comments · Fixed by #1405
Closed

.dyn_into::<Symbol> / `.dyn_ref::<Symbol> don't work as expected #1370

RReverser opened this issue Mar 20, 2019 · 0 comments · Fixed by #1405
Labels
bug js-sys Issues related to the `js-sys` crate

Comments

@RReverser
Copy link
Member

This is sort of a child issue to #1367, but, because it's a bug, it seems worth it to track separately since it might be fixed independently from the more generic one.


While looking into #1087, I realised that #1367 is needed not only for prototype-less Objects and cross-realm interaction, but also for new primitives which don't have automatic boxing.

Currently these are:

  • Symbol
  • BigInt

and for both instanceof check will return false when used on a corresponding primitive, because they're not objects, and so dyn_ref and dyn_into fail to recognise them too.

Example:

#[wasm_bindgen]
pub fn symbol_to_string_1(s: Symbol) -> JsString {
	s.to_string()
}

#[wasm_bindgen]
pub fn symbol_to_string_2(s: JsValue) -> JsString {
	s.dyn_into::<Symbol>().unwrap().to_string()
}

#[wasm_bindgen]
pub fn symbol_to_string_3(s: JsValue) -> JsString {
	if s.is_symbol() {
		s.unchecked_into::<Symbol>().to_string()
	} else {
		panic!("Not a symbol")
	}
}

Results:

> require('./pkg').symbol_to_string_1(Symbol('x'))
'Symbol(x)'
> require('./pkg').symbol_to_string_2(Symbol('x'))
panicked at 'called `Result::unwrap()` on an `Err` value: JsValue(Symbol(x))', src/libcore/result.rs:997:5
> require('./pkg').symbol_to_string_3(Symbol('x'))
'Symbol(x)'
@RReverser RReverser added the bug Something isn't working label Mar 20, 2019
@alexcrichton alexcrichton added js-sys Issues related to the `js-sys` crate bug and removed bug Something isn't working labels Mar 20, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug js-sys Issues related to the `js-sys` crate
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants