bladecoder / blade-ink-rs

Inkle Ink runtime implementation in Rust
Apache License 2.0
31 stars 6 forks source link

fails to load when built to WASM #11

Closed jaminhaber closed 10 months ago

jaminhaber commented 10 months ago

I have a bevy project with the story added as a NonSend resource but I am encountering the following error when exporting as WASM and embedding on a web page.

The error may or may not be related to this package but just wanted to flag it

Main fn:

let story = Story::new(include_str!("./story/story.json")).unwrap();
let mut app = App::new();
app.insert_non_send_resource(story);

Error:

Uncaught (in promise) RuntimeError: unreachable
    at __rust_start_panic (game_bg.e0ece47b.wasm:0x171f90e)
    at rust_panic (game_bg.e0ece47b.wasm:0x1711605)
    at std::panicking::rust_panic_with_hook::hbf46ef0245cc9589 (game_bg.e0ece47b.wasm:0x14847aa)
    at std::panicking::begin_panic_handler::{{closure}}::hc07db454214d2c87 (game_bg.e0ece47b.wasm:0x1521f4e)
    at std::sys_common::backtrace::__rust_end_short_backtrace::hf9e2f055fb5ef672 (game_bg.e0ece47b.wasm:0x171c477)
    at rust_begin_unwind (game_bg.e0ece47b.wasm:0x161d76d)
    at core::panicking::panic_fmt::h14c85a61aa3d538e (game_bg.e0ece47b.wasm:0x167406c)
    at std::time::Instant::now::he2d8cf30b81330fa (game_bg.e0ece47b.wasm:0x1673f23)
    at bladeink::story::progress::<impl bladeink::story::Story>::continue_internal::he05d60203bd9bce5 (game_bg.e0ece47b.wasm:0x19c58a)
    at bladeink::story::state::<impl bladeink::story::Story>::reset_globals::h272fc682d3f4ee58 (game_bg.e0ece47b.wasm:0xf3e9bf)
    at bladeink::story::misc::<impl bladeink::story::Story>::new::h5d9d13532e11e82e (game_bg.e0ece47b.wasm:0x650187)
    at los_angeles_simulator::main::h1f457f74b5faf998 (game_bg.e0ece47b.wasm:0x3be4b7)
    at std::sys_common::backtrace::__rust_begin_short_backtrace::h3e02078434879a59 (game_bg.e0ece47b.wasm:0x171c4b5)
    at std::rt::lang_start::{{closure}}::hf46eb1497fcb98e8 (game_bg.e0ece47b.wasm:0x168d364)
    at std::rt::lang_start_internal::h20a8087e5817ee7c (game_bg.e0ece47b.wasm:0x1395d21)
    at main (game_bg.e0ece47b.wasm:0x1686c65)
    at game_bg.e0ece47b.wasm:0x1711713
    at __wbg_finalize_init (game.js:1901:10)
    at Module.__wbg_init [as default] (game.js:1937:12)
    at async runWasm (game.tsx:13:7)
jaminhaber commented 10 months ago

Seems to be coming from this line which seems to be a known issue with Instant::now when compiling for wasm32-unknown-unknown. Is it possible to disable some of the features behind a "no-std" or "wasm" flag?

jaminhaber commented 10 months ago

I can confirm it works with my fork here where i simply delete that code - I can use that for now since I am not taking advantage of any async code 👍🏻 If you want I can look about adding a flag for async stuff?

IFcoltransG commented 10 months ago

That code should only need to run Instant::now if the is_async_time_limited value is true. Maybe it should be an Option<Instant>, set to None if is_async_time_limited is false, and its contents could be checked instead of this condition.

IFcoltransG commented 10 months ago

(As a bigger refactor, I was thinking this code could be made into more of a co-routine pattern, meaning the code related to time and milliseconds could be abstracted out. The timing aspect is just a function that says "keep going" or "stop", and it feels weird to have this core function dealing directly in milliseconds.)

bladecoder commented 10 months ago

I think that this PR should fix this issue and enables the use of async feature in WASM too: https://github.com/bladecoder/blade-ink-rs/pull/12

I could not test it. @jaminhaber , could you test it before I merge?

@IFcoltransG, what do you think about this solution?

jaminhaber commented 10 months ago

Yep! I am able to run the game in wasm with this:

bladeink = { git = "https://github.com/bladecoder/blade-ink-rs", branch = "fix/wasm_instant_now" }

bladecoder commented 10 months ago

Branch merged in master.