Skip to content

Commit 1420cb7

Browse files
committed
[library_html5] Simplify JSEvent system. NFC
This change extracts a few helper function to make the JS event system smaller and more internally consistent. Firstly, we now use a single storage location for the event data pointer used in event callback. This is allocated on demand in `allocateEventStruct`. We then use a new `dispatchEvent` all the many loction that previously duplicated the event dispatching logic. In order to handler the 3 cases where we need to keep a copy of the last dispatched event we also have two new helpers: `getCachedEvent`, `cacheEvent`. These allocate separate copies of the last generated event, but only for the 3 event types where this is required.
1 parent dab8702 commit 1420cb7

File tree

6 files changed

+160
-254
lines changed

6 files changed

+160
-254
lines changed

src/library_html5.js

Lines changed: 135 additions & 224 deletions
Large diffs are not rendered by default.

test/code_size/hello_webgl2_wasm.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
"a.html.gz": 328,
44
"a.js": 4531,
55
"a.js.gz": 2312,
6-
"a.wasm": 10402,
7-
"a.wasm.gz": 6704,
8-
"total": 15387,
9-
"total_gz": 9344
6+
"a.wasm": 10421,
7+
"a.wasm.gz": 6697,
8+
"total": 15406,
9+
"total_gz": 9337
1010
}
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
{
22
"a.html": 346,
33
"a.html.gz": 262,
4-
"a.js": 22199,
5-
"a.js.gz": 11579,
6-
"total": 22545,
7-
"total_gz": 11841
4+
"a.js": 22320,
5+
"a.js.gz": 11639,
6+
"total": 22666,
7+
"total_gz": 11901
88
}

test/code_size/hello_webgl_wasm.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
"a.html.gz": 328,
44
"a.js": 4069,
55
"a.js.gz": 2158,
6-
"a.wasm": 10402,
7-
"a.wasm.gz": 6704,
8-
"total": 14925,
9-
"total_gz": 9190
6+
"a.wasm": 10421,
7+
"a.wasm.gz": 6697,
8+
"total": 14944,
9+
"total_gz": 9183
1010
}
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
{
22
"a.html": 346,
33
"a.html.gz": 262,
4-
"a.js": 21725,
5-
"a.js.gz": 11413,
6-
"total": 22071,
7-
"total_gz": 11675
4+
"a.js": 21846,
5+
"a.js.gz": 11479,
6+
"total": 22192,
7+
"total_gz": 11741
88
}

test/test_html5_mouse.c

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,12 @@
55
* found in the LICENSE file.
66
*/
77

8+
#include <assert.h>
89
#include <stdio.h>
910
#include <emscripten.h>
1011
#include <string.h>
1112
#include <emscripten/html5.h>
1213

13-
void report_result(int result) {
14-
if (result == 0) {
15-
printf("Test successful!\n");
16-
} else {
17-
printf("Test failed!\n");
18-
}
19-
#ifdef REPORT_RESULT
20-
REPORT_RESULT(result);
21-
#endif
22-
}
23-
2414
static inline const char *emscripten_event_type_to_string(int eventType) {
2515
const char *events[] = { "(invalid)", "(none)", "keypress", "keydown", "keyup", "click", "mousedown", "mouseup", "dblclick", "mousemove", "wheel", "resize",
2616
"scroll", "blur", "focus", "focusin", "focusout", "deviceorientation", "devicemotion", "orientationchange", "fullscreenchange", "pointerlockchange",
@@ -62,7 +52,12 @@ void instruction() {
6252
if (!gotMouseMove) { printf("Please move the mouse on the canvas.\n"); return; }
6353
if (!gotWheel) { printf("Please scroll the mouse wheel.\n"); return; }
6454

65-
if (gotClick && gotMouseDown && gotMouseUp && gotDblClick && gotMouseMove && gotWheel) report_result(0);
55+
if (gotClick && gotMouseDown && gotMouseUp && gotDblClick && gotMouseMove && gotWheel) {
56+
printf("Test successful!\n");
57+
#ifdef REPORT_RESULT
58+
REPORT_RESULT(0);
59+
#endif
60+
}
6661
}
6762

6863
bool mouse_callback(int eventType, const EmscriptenMouseEvent *e, void *userData) {
@@ -82,7 +77,7 @@ bool mouse_callback(int eventType, const EmscriptenMouseEvent *e, void *userData
8277
if (eventType == EMSCRIPTEN_EVENT_CLICK && e->screenX == -500000) {
8378
printf("ERROR! Received an event to a callback that should have been unregistered!\n");
8479
gotClick = 0;
85-
report_result(1);
80+
assert(false && "Received an event to a callback that should have been unregistered");
8681
}
8782

8883
instruction();
@@ -141,7 +136,7 @@ int main() {
141136
if (mouseEvent.screenX != 123 || mouseEvent.screenY != 456
142137
|| mouseEvent.clientX != 123 || mouseEvent.clientY != 456) {
143138
printf("ERROR! Incorrect mouse status\n");
144-
report_result(1);
139+
assert(false && "Incorrect mouse status");
145140
}
146141

147142
// Test that unregistering a callback works. Clicks should no longer be received.

0 commit comments

Comments
 (0)