Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fix this_val on global func call #1036

Merged
merged 2 commits into from Dec 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 12 additions & 0 deletions bridge/bindings/qjs/dom/event_target_test.cc
Expand Up @@ -184,3 +184,15 @@ TEST(EventTarget, wontLeakWithStringProperty) {
"img.any = '1234'";
bridge->evaluateScript(code.c_str(), code.size(), "internal://", 0);
}

TEST(EventTarget, globalBindListener) {
bool static logCalled = false;
kraken::KrakenPage::consoleMessageHandler = [](void* ctx, const std::string& message, int logLevel) {
logCalled = true;
EXPECT_STREQ(message.c_str(), "clicked");
};
auto bridge = TEST_init();
std::string code = "addEventListener('click', () => {console.log('clicked'); }); dispatchEvent(new Event('click'))";
bridge->evaluateScript(code.c_str(), code.size(), "internal://", 0);
EXPECT_EQ(logCalled, true);
}
9 changes: 8 additions & 1 deletion bridge/bindings/qjs/executing_context.h
Expand Up @@ -139,7 +139,14 @@ static JSValue handleCallThisOnProxy(JSContext* ctx, JSValueConst this_val, int
if (JS_IsProxy(this_val)) {
result = JS_Call(ctx, f, JS_GetProxyTarget(this_val), argc, argv);
} else {
result = JS_Call(ctx, f, this_val, argc, argv);
// If this_val is undefined or null, this_val should set to globalThis.
if (JS_IsUndefined(this_val) || JS_IsNull(this_val)) {
this_val = JS_GetGlobalObject(ctx);
result = JS_Call(ctx, f, this_val, argc, argv);
JS_FreeValue(ctx, this_val);
} else {
result = JS_Call(ctx, f, this_val, argc, argv);
}
}
return result;
}
Expand Down