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

Use of &mut Context from EventHandler causes BorrowedMutError in wasm #373

Open
zbrachinara opened this issue May 2, 2023 · 1 comment
Labels
bug Something isn't working

Comments

@zbrachinara
Copy link

zbrachinara commented May 2, 2023

On wasm, when GraphicsContext::get_clipboard is called from inside either EventHandler::mouse_button_down_event or EventHandler::key_down_event (but likely all event handlers which take a &mut Context), it causes a panic by RefCell BorrowedMutError.

Can be reproduced using this code (master branch of miniquad patched in using cargo patch) with current version of macroquad on crates.io:

use macroquad::miniquad::EventHandler;

struct Handler;

impl EventHandler for Handler {
    fn update(&mut self, _ctx: &mut macroquad::miniquad::Context) {}
    fn draw(&mut self, _ctx: &mut macroquad::miniquad::Context) {}

    fn key_down_event(
        &mut self,
        ctx: &mut macroquad::miniquad::Context,
        _: macroquad::prelude::KeyCode,
        _: macroquad::miniquad::KeyMods,
        _: bool,
    ) {
        ctx.clipboard_get();
    }
}

#[macroquad::main("")]
async fn main() {
    let mut handler = Handler;
    let id = macroquad::input::utils::register_input_subscriber();
    loop {
        macroquad::input::utils::repeat_all_miniquad_input(&mut handler, id);
        macroquad::window::next_frame().await
    }
}

Compiling for wasm, running the program, then pressing a key while focused on the window causes the panic.

Initially discovered in optozorax/egui-macroquad#34

@not-fl3 not-fl3 added the bug Something isn't working label May 2, 2023
@not-fl3
Copy link
Owner

not-fl3 commented Jun 16, 2024

Hmm, I've tried it. Miniquad do not have &mut Context argument for quite a while, I fixed your example to make it run with a latest miniquad:

use macroquad::miniquad::EventHandler;

struct Handler;

impl EventHandler for Handler {
    fn update(&mut self) {}
    fn draw(&mut self) {}

    fn key_down_event(
        &mut self,
        _: macroquad::prelude::KeyCode,
        _: macroquad::miniquad::KeyMods,
        _: bool,
    ) {
        miniquad::window::clipboard_get();
    }
}

#[macroquad::main("")]
async fn main() {
    let mut handler = Handler;
    let id = macroquad::input::utils::register_input_subscriber();
    loop {
        macroquad::input::utils::repeat_all_miniquad_input(&mut handler, id);
        macroquad::window::next_frame().await
    }
}

and it seems to work fine, I do not have any errors on web. Maybe the bug got fixed in more recent miniquad versions?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants