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

Importing the "import" functionality from JS no longer works since #2855 #3208

Open
moshevds opened this issue Dec 19, 2022 · 3 comments
Open
Labels

Comments

@moshevds
Copy link

Describe the Bug

We use the following snippet in order to use "dynamic import" special syntax from rust:

#[wasm_bindgen]
extern "C" {
    #[wasm_bindgen(js_name = import)]
    pub fn js_dynamic_import(name: &str) -> Promise;
}

This used to work until v0.2.81, but no longer works now. I assume this is due to #2855

import() is not an actual JavaScript function since it is a "special syntax", so perhaps this used to work somewhat by accident. However, the change in #2855 was intended to address something else entirely, and is transforming the generated code into something that definitely does not make sense. It is now trying to call a non-existent function with the name "_import".

Expected Behavior

Either things should continue to work as they did before, or compilation/code-generation should give an error message that using this js_name is not allowed.

Actual Behavior

The generated code looks something like this:

    imports.wbg.__wbg_import_bbc363ee2657dcc6 = function(arg0, arg1) {
        const ret = _import(getStringFromWasm0(arg0, arg1));
        return addHeapObject(ret);
    };
@moshevds moshevds added the bug label Dec 19, 2022
@cab
Copy link

cab commented Jan 6, 2023

If the caveats listed here are acceptable, you can work around this with inline_js:

#[wasm_bindgen(inline_js = "export function import2(path) { return import(path); }")]
extern "C" {
    #[wasm_bindgen(js_name = "import2")]
    pub fn js_import(name: &str) -> js_sys::Promise;
}

@ImUrX
Copy link

ImUrX commented Jan 7, 2023

I proposed in the discord to make import a function in js-sys, any thoughts on that?

@moshevds
Copy link
Author

moshevds commented Jan 7, 2023

I proposed in the discord to make import a function in js-sys, any thoughts on that?

I think it would be a good idea to have that. But if there is interest in that, then I think a new issue should be created specifically for that feature.

IMHO this issue is about the regression bug. Something that used to work does not work anymore, and in addition it breaks in a way that does not trigger an error, but breaks the generated JavaScript code silently.

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

3 participants