emscripten-core / emscripten-core/emscripten

Event handlers never consume events in Asyncify

Open
#13,320 3 comments 0 reactions 0 assignees View on GitHub
asyncify Important
Dominant language
C++
Stars
27.6k
Forks
3.6k
Avg merge
1d 1h
Merged PRs (30d)
105

Description

Per html5.h, event handlers can return TRUE to consume the event, i.e., `event.preventDefault()` is called. This does not work in Asyncify.

Here's a [simple test case](https://gist.github.com/devappd/1895eba742197d4affdee03a6e6eff48) for mouse events:

* [Normal build](https://devappd.github.io/tests/asyncify-events/main.html)
* [Asyncify build](https://devappd.github.io/tests/asyncify-events/main-asyncify.html)

The expected result is that the default handler is never invoked on the canvas, e.g., mouse clicks do not cause the canvas to gain focus.

I saw that the event handlers check the result of `dynCall_iiii()` to determine whether to call `e.preventDefault()`. This result is never passed in Asyncify.

Therefore, I get the desired result if I modify the compiled Module as below:

```diff
function registerMouseEventCallback(target, userData, useCapture, callbackfunc, eventTypeId, eventTypeString, targetThread) {
/* ... */

var mouseEventHandlerFunc = function(ev) {
var e = ev || event;

// TODO: Make this access thread safe, or this could update live while app is reading it.
fillMouseEventData(JSEvents.mouseEvent, e, target);

- if ((function(a1, a2, a3) { dynCall_iiii.apply(null, [callbackfunc, a1, a2, a3]); })(eventTypeId, JSEvents.mouseEvent, userData)) e.preventDefault();
+ if ((function(a1, a2, a3) { return dynCall_iiii.apply(null, [callbackfunc, a1, a2, a3]); })(eventTypeId, JSEvents.mouseEvent, userData)) e.preventDefault();
};

/* ... */
}
```

See [Asyncify build with the fix applied](https://devappd.github.io/tests/asyncify-events/main-asyncify-fixed.html), which passes the test.

Is this fix appropriate? If so, I can use guidance on patching the codebase as I'm unfamiliar with Asyncify modifications.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.