Fix bugs in HTML5 events when using SAFE_HEAP

This fixes a SAFE_HEAP_STORE is not a function error when using mouse events
and a segfault that occurs when the beforeunload callback returns 0.
Merge pull request #2148 from TV4Fun/html5fixes

Fix bugs in HTML5 events when using SAFE_HEAP