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

Unable to instantiate logger due to error message which originates from wasm-bindgen #3447

Closed
lexi-the-cute opened this issue May 23, 2023 · 6 comments
Labels

Comments

@lexi-the-cute
Copy link

Summary

When running console_log::init().unwrap(); from the below code, I get the error message: function not implemented on non-wasm32 targets on Firefox.

if cfg!(any(target_arch = "wasm32", target_arch = "wasm64")) {
    console_log::init().unwrap();
}

Additional Details

The build script is really complicated right now. I currently call SDL_main as I have not been able to get any indication of the code running in console if I attached the wasm_bindgen macro at all.

// Run as Library (e.g. Android and WebAssembly)
#[no_mangle]
#[allow(non_snake_case)]
pub extern fn SDL_main(_argc: libc::c_int, _argv: *const *const libc::c_char) -> libc::c_int {
    game::start();
    return 0;
}

I'm using:

console_log = { version = "1.0.0", features = ["color", "wasm-bindgen"] }
wasm-bindgen = "0.2.86"

I'm calling my wasm file with the below code after I generate both the js and wasm file with emmc:

<script type="text/javascript">
var Module = {
    onRuntimeInitialized: function() {
        Module._SDL_main(); // SDL_main was exported
    }
};
</script>
<script src="/main.js"></script>

My project is located at https://github.com/alexisart/catgirl-engine

@daxpedda
Copy link
Collaborator

if cfg!(any(target_arch = "wasm32", target_arch = "wasm64")) {

FWIW this can be shortened with target_family = "wasm".

[..] I get the error message: function not implemented on non-wasm32 targets [..]

There's really very little info to go by here. But I'm going to assume that the issue here is the compile target. wasm-bindgen officially only supports wasm32-unknown-unknown, you might have tried a different target. Looking at your repo, did you maybe try to use Emscripten?

It would be really helpful to put up a minimal reproducible example.

@lexi-the-cute
Copy link
Author

Here's the minimal reproducible example

min.zip

@daxpedda
Copy link
Collaborator

You seem to be compiling with --target wasm32-unknown-emscripten, which is not supported in wasm-bindgen. Unfortunately I know nothing about Emscripten so I can't really give you further advice on your project.

By the way, the .cargo/config.toml.sample file doesn't need to be copied to your home folder. Using a local .cargo/config.toml (removing .sample at the end) works too and should have the same effect locally without having to change your global configuration. See the Cargo book for more information.

@lexi-the-cute
Copy link
Author

Okay, so I was trying to remove wasm-bindgen from my project, but it refused to do it. It turns out that the console-log crate depends on wasm-bindgen.

I'm going to reimplement the logger from scratch because I need proper logging to work and I can't use wasm32-unknown-unknown due to other dependencies banning using unknown target OS and requiring specifying using an "OS" even if it's emscripten.

I would like to reopen this issue as a feature request to start supporting emscripten

@daxpedda
Copy link
Collaborator

daxpedda commented May 25, 2023

Unfortunately that's not possible, as wasm-bindgen considers Emscripten to be out of scope.

The deeper problem here is that wasm32-unknown-unknown isn't compatible with the C ABI: rust-lang/rust#71871. This is a common problem that appears when wanting to use wasm-bindgen: #2317.

Unfortunately there is currently no solution for this, though the Rust team is aware of it and is trying to find a solution: rustwasm/team#291. One proposal that might solve that problem is a new Wasm ABI: rust-lang/rust#83788. Though it is currently still unstable.

So as it stands today, wasm-bindgen is not compatible with C, you will either have to drop wasm-bindgen or C to proceed.

@lexi-the-cute
Copy link
Author

Thanks! I dropped wasm-bindgen as I wasn't using it myself, it was just a dependency of one of my dependencies. I've successfully implemented my own logger by using Emscripten's EM_ASM. This requires writing both very basic C and continuing Rust as usual, but it at least works.

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

2 participants