Skip to content

#[wasm-bindgen] macro does not work for extern functions not in the root of the crate #159

Closed
@rbalicki2

Description

@rbalicki2

Hello. Thank you so much for your help these past two weeks.

So, as of today (perhaps due to upgrading to the latest nightly, I don't know), wasm-bindgen does not seem to work for extern functions not in the root of the crate. I have the following in the root of my crate:

#![feature(proc_macro, wasm_custom_section, wasm_import_module)]

extern crate wasm_bindgen;

use wasm_bindgen::prelude::wasm_bindgen;

#[macro_use]
mod js;

// if the following is uncommented, everything works
// #[wasm_bindgen]
// extern {
//   #[wasm_bindgen]
//   pub fn set_timeout();
// }

and the following in js.rs

use wasm_bindgen::prelude::wasm_bindgen;
#[wasm_bindgen]
extern {
  // #[wasm_bindgen]
  // pub fn set_timeout();
}

If I uncomment that block in the root of the crate, everything compiles, whether or not the pub fn set_timeout is uncommented in js.rs. However, if the block in the root of the crate is commented, and the function in js.rs is uncommented (and only then), do I get the following error:

thread 'main' panicked at 'failed to run export: Function("Module doesn\'t have export __wbindgen_describe___wbg_f_set_timeout_set_timeout_n")', libcore/result.rs:945:5
stack backtrace:
   0: std::sys::unix::backtrace::tracing::imp::unwind_backtrace
   1: std::sys_common::backtrace::print
   2: std::panicking::default_hook::{{closure}}
   3: std::panicking::default_hook
   4: std::panicking::rust_panic_with_hook
   5: std::panicking::begin_panic_fmt
   6: rust_begin_unwind
   7: core::panicking::panic_fmt
   8: core::result::unwrap_failed
   9: wasm_bindgen_cli_support::Bindgen::_generate::{{closure}}
  10: wasm_bindgen_cli_support::js::Context::describe
  11: wasm_bindgen_cli_support::js::SubContext::generate
  12: wasm_bindgen_cli_support::Bindgen::_generate
  13: wasm_bindgen::main
  14: std::rt::lang_start::{{closure}}
  15: std::panicking::try::do_call
  16: __rust_maybe_catch_panic
  17: std::rt::lang_start_internal
  18: main

I'm running these commands:

cargo +nightly build --target wasm32-unknown-unknown
RUST_BACKTRACE=1 wasm-bindgen ./target/wasm32-unknown-unknown/debug/wasm_website_frontend.wasm --out-dir ./dist

versions, etc:

rustc 1.27.0-nightly (ac3c2288f 2018-04-18)
wasm-bindgen = "0.2.5"
wasm-bindgen --version
wasm-bindgen 0.2.5

Thank you so much!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions