Skip to content

web_sys::window() in Web Worker not returning None but throwing an error #1982

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

Closed
j-devel opened this issue Jan 28, 2020 · 2 comments
Closed
Labels
bug web-sys Issues related to the `web-sys` crate

Comments

@j-devel
Copy link
Contributor

j-devel commented Jan 28, 2020

Describe the Bug

Calling web_sys::window() in a Web Worker throws an unexpected runtime error.

In this case, according to its signature (pub fn window() -> Option<Window>), I expect it to return None instead.

References:

Steps to Reproduce

  1. Build the custom example:
$ git clone -b issue-window https://github.com/j-devel/wasm-bindgen
$ cd wasm-bindgen/examples/without-a-bundler-no-modules-worker/
$ wasm-pack build --target no-modules
$ python -m SimpleHTTPServer
Serving HTTP on 0.0.0.0 port 8000 ...
  1. Open http://localhost:8000/ in browser and see the Developer Console
  2. See error
    wasm-bindgen-issue-window

Expected Behavior

web_sys::window() returns None. No runtime errors reported in the Developer Console .

Actual Behavior

Get an Uncaught ReferenceError as in the above screenshot.

Additional Context

n/a

@j-devel j-devel added the bug Something isn't working label Jan 28, 2020
@alexcrichton alexcrichton added bug web-sys Issues related to the `web-sys` crate and removed bug Something isn't working labels Jan 28, 2020
@Pauan
Copy link
Contributor

Pauan commented Jan 28, 2020

This is a duplicate of #1046

We've temporarily fixed this in gloo, but we should think about a better more long-term solution for this.

@j-devel
Copy link
Contributor Author

j-devel commented Jan 28, 2020

Woops, I just learnd this is actually a well-known issue; and thanks for the pointers to the existing pragmatic approaches that are basically relying on unchecked_into().

Skimming through the previous discussions in the links, I do share the same view that a more fundamental fix would be for us to do something about instanceof.

The root problem has turned out to be not only about web_sys::window(). I am working on some global-agnostic code where I just want to perform some simple checks like

js_sys::global().has_type::<web_sys::Window>()
js_sys::global().is_instance_of::<web_sys::Window>()
js_sys::global().has_type::<web_sys::WorkerGlobalScope>()
js_sys::global().is_instance_of::<web_sys::WorkerGlobalScope>()

and they all are failing with similar errors thrown when in a 'wrong' context. With the sought-after fundamental fix in mind, I'd like to open a separate issue when I come up with something worth sharing.

And for now, I'm closing this particular issue on resolving global to Window.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug web-sys Issues related to the `web-sys` crate
Projects
None yet
Development

No branches or pull requests

3 participants