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.