Skip to content

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

@RReverser

Description

@RReverser

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)'

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugjs-sysIssues related to the `js-sys` crate

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions