Skip to content

Commit

Permalink
Merge pull request #1209 from openkraken/fix/scroll_event_crash
Browse files Browse the repository at this point in the history
fix: fix scroll event crash
  • Loading branch information
andycall authored Mar 11, 2022
2 parents a561143 + 64e2bae commit a40ab6d
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 6 deletions.
3 changes: 0 additions & 3 deletions bridge/bindings/qjs/dom/event.cc
Original file line number Diff line number Diff line change
Expand Up @@ -234,9 +234,6 @@ EventInstance::EventInstance(Event* jsEvent, JSAtom eventType, JSValue eventInit

void EventInstance::finalizer(JSRuntime* rt, JSValue val) {
auto* event = static_cast<EventInstance*>(JS_GetOpaque(val, Event::kEventClassID));
if (event->context()->isValid()) {
JS_FreeValue(event->m_ctx, event->jsObject);
}
delete event;
}

Expand Down
3 changes: 0 additions & 3 deletions bridge/bindings/qjs/host_class.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,6 @@ class HostClass {
friend Instance;
static void proxyFinalize(JSRuntime* rt, JSValue val) {
auto hostObject = static_cast<HostClass*>(JS_GetOpaque(val, ExecutionContext::kHostClassClassId));
if (hostObject->context()->isValid()) {
JS_FreeValue(hostObject->m_ctx, hostObject->jsObject);
}
delete hostObject;
};
static JSValue proxyCall(JSContext* ctx, JSValueConst func_obj, JSValueConst this_val, int argc, JSValueConst* argv, int flags) {
Expand Down
106 changes: 106 additions & 0 deletions bridge/bindings/qjs/host_class_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -410,4 +410,110 @@ TEST(HostClass, setExoticClassProperty) {
EXPECT_EQ(logCalled, true);
}

TEST(HostClass, finalizeShouldNotFree) {
bool static errorCalled = false;
bool static logCalled = false;
auto bridge = TEST_init([](int32_t contextId, const char* errmsg) {
errorCalled = true;
});
kraken::KrakenPage::consoleMessageHandler = [](void* ctx, const std::string& message, int logLevel) {
logCalled = true;
};

auto context = bridge->getContext();
auto* constructor = new ExoticClass(context);
context->defineGlobalProperty("ExoticClass", constructor->jsObject);



auto runGC = [](JSContext* ctx, JSValueConst this_val, int argc, JSValueConst* argv) -> JSValue {
JS_RunGC(JS_GetRuntime(ctx));
return JS_NULL;
};
QJS_GLOBAL_BINDING_FUNCTION(context, runGC, "__kraken_run_gc__", 1);

std::string code = R"(
function throttle(func, wait) {
var ctx;
var args;
var rtn;
var timeoutID;
var last = 0;
function call() {
timeoutID = 0;
last = +new Date();
rtn = func.apply(ctx, args);
ctx = null;
// args = null;
}
return function () {
ctx = this;
args = arguments;
var delta = new Date().getTime() - last;
if (!timeoutID) if (delta >= wait) call();else timeoutID = setTimeout(call, wait - delta);
return rtn;
};
}
var handleScroll = function (e) {
};
{
let div;
function initScroll() {
div = document.createElement('div');
div.style.width = '100px';
div.style.height = '300px';
div.style.overflow = 'scroll';
div.addEventListener('scroll', throttle(handleScroll, 100));
document.body.appendChild(div);
for(let i = 0; i < 1000; i ++) {
div.appendChild(document.createTextNode('abc'));
}
}
function triggerScroll() {
let scrollEvent = new CustomEvent('scroll');
div.dispatchEvent(scrollEvent);
}
initScroll();
window.onclick = () => {
document.body.removeChild(div)
initScroll();
};
window.addEventListener('trigger', () => {
triggerScroll();
});
}
)";
context->evaluateJavaScript(code.c_str(), code.size(), "vm://", 0);

static auto* window = static_cast<EventTargetInstance*>(JS_GetOpaque(context->global(), 1));

auto triggerScrollEventAndLoopTimer = [&context]() {
TEST_dispatchEvent(context->getContextId(), window, "trigger");
TEST_runLoop(context);
};

triggerScrollEventAndLoopTimer();
triggerScrollEventAndLoopTimer();
triggerScrollEventAndLoopTimer();


TEST_dispatchEvent(context->getContextId(), window, "click");

triggerScrollEventAndLoopTimer();
triggerScrollEventAndLoopTimer();
triggerScrollEventAndLoopTimer();

TEST_dispatchEvent(context->getContextId(), window, "click");

triggerScrollEventAndLoopTimer();
triggerScrollEventAndLoopTimer();
}

} // namespace kraken::binding::qjs
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit a40ab6d

Please sign in to comment.