Skip to content

Commit

Permalink
Merge pull request #1036 from openkraken/fix/fix_global_this
Browse files Browse the repository at this point in the history
fix: fix this_val on global func call
  • Loading branch information
answershuto authored Dec 29, 2021
2 parents 9660c7d + 089d8e9 commit 5732bc4
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
12 changes: 12 additions & 0 deletions bridge/bindings/qjs/dom/event_target_test.cc
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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

0 comments on commit 5732bc4

Please sign in to comment.