Skip to content

Commit d2d9d28

Browse files
committed
[MERGE #5918 @boingoing] instanceof with bound function should call [Symbol.hasInstance] accessor
Merge pull request #5918 from boingoing:fix_5883 Fixes #5883
2 parents 4565a28 + 8f73cae commit d2d9d28

File tree

4 files changed

+43
-0
lines changed

4 files changed

+43
-0
lines changed

lib/Runtime/Language/JavascriptOperators.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7600,6 +7600,16 @@ using namespace Js;
76007600
RecyclableObject* constructor = VarTo<RecyclableObject>(aClass);
76017601
if (scriptContext->GetConfig()->IsES6HasInstanceEnabled())
76027602
{
7603+
if (VarIs<JavascriptFunction>(constructor))
7604+
{
7605+
JavascriptFunction* func = VarTo<JavascriptFunction>(constructor);
7606+
if (func->IsBoundFunction())
7607+
{
7608+
BoundFunction* boundFunc = (BoundFunction*)func;
7609+
constructor = boundFunc->GetTargetFunction();
7610+
}
7611+
}
7612+
76037613
Var instOfHandler = JavascriptOperators::GetPropertyNoCache(constructor,
76047614
PropertyIds::_symbolHasInstance, scriptContext);
76057615
if (JavascriptOperators::IsUndefinedObject(instOfHandler)

lib/Runtime/Runtime.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,7 @@ enum tagDEBUG_EVENT_INFO_TYPE
429429

430430
#include "Library/JavascriptNumber.h"
431431
#include "Library/JavascriptFunction.h"
432+
#include "Library/BoundFunction.h"
432433
#include "Library/RuntimeFunction.h"
433434
#include "Library/JavascriptExternalFunction.h"
434435
#include "Library/CustomExternalIterator.h"

test/Bugs/bug_5883.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//-------------------------------------------------------------------------------------------------------
2+
// Copyright (C) Microsoft. All rights reserved.
3+
// Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
4+
//-------------------------------------------------------------------------------------------------------
5+
6+
WScript.LoadScriptFile("..\\UnitTestFramework\\UnitTestFramework.js");
7+
8+
var tests = [
9+
{
10+
name: "#5883 - instanceof on a bound function fails to call [Symbol.hasInstance] accessor",
11+
body: function () {
12+
let called = false;
13+
class A {
14+
static [Symbol.hasInstance]() {
15+
called = true;
16+
}
17+
}
18+
const B = A.bind();
19+
20+
({} instanceof B);
21+
assert.isTrue(called);
22+
}
23+
},
24+
];
25+
26+
testRunner.runTests(tests, { verbose: WScript.Arguments[0] != "summary" });

test/Bugs/rlexe.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -576,4 +576,10 @@
576576
<compile-flags>-force:deferparse</compile-flags>
577577
</default>
578578
</test>
579+
<test>
580+
<default>
581+
<files>bug_5883.js</files>
582+
<compile-flags>-args summary -endargs</compile-flags>
583+
</default>
584+
</test>
579585
</regress-exe>

0 commit comments

Comments
 (0)