Skip to content

Commit dd8a7fd

Browse files
committed
Instanceof operator should check whether the left side is a (foreign) object.
1 parent c103057 commit dd8a7fd

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

graal-js/src/com.oracle.truffle.js/src/com/oracle/truffle/js/nodes/binary/InstanceofNode.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2018, 2023, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2018, 2024, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* The Universal Permissive License (UPL), Version 1.0
@@ -67,6 +67,7 @@
6767
import com.oracle.truffle.js.nodes.access.GetMethodNode;
6868
import com.oracle.truffle.js.nodes.access.GetPrototypeNode;
6969
import com.oracle.truffle.js.nodes.access.IsJSObjectNode;
70+
import com.oracle.truffle.js.nodes.access.IsObjectNode;
7071
import com.oracle.truffle.js.nodes.access.PropertyGetNode;
7172
import com.oracle.truffle.js.nodes.binary.InstanceofNodeGen.IsBoundFunctionCacheNodeGen;
7273
import com.oracle.truffle.js.nodes.binary.InstanceofNodeGen.OrdinaryHasInstanceNodeGen;
@@ -249,18 +250,22 @@ protected boolean doIsBound(Object obj, JSDynamicObject check,
249250

250251
@Specialization(guards = {"!isJSObject(left)", "isForeignObject(left)", "isJSFunction(right)", "!isBoundFunction(right)"})
251252
protected boolean doForeignObject(@SuppressWarnings("unused") Object left, @SuppressWarnings("unused") JSDynamicObject right,
253+
@Cached @Shared IsObjectNode isAnyObjectNode,
252254
@Cached @Shared("foreignPrototypeNode") ForeignObjectPrototypeNode getForeignPrototypeNode,
253255
@Cached @Shared("invalidPrototypeBranch") InlinedBranchProfile invalidPrototypeBranch,
254256
@Cached("create(context)") @Shared("ordinaryHasInstance") OrdinaryHasInstanceNode ordinaryHasInstanceNode) {
255257
if (context.isOptionForeignObjectPrototype()) {
256-
return foreignObjectIntl(left, right, getForeignPrototypeNode, invalidPrototypeBranch, ordinaryHasInstanceNode);
258+
return foreignObjectIntl(left, right, isAnyObjectNode, getForeignPrototypeNode, invalidPrototypeBranch, ordinaryHasInstanceNode);
257259
} else {
258260
return false;
259261
}
260262
}
261263

262-
private boolean foreignObjectIntl(Object left, JSDynamicObject right, ForeignObjectPrototypeNode getForeignPrototypeNode, InlinedBranchProfile invalidPrototypeBranch,
263-
OrdinaryHasInstanceNode ordinaryHasInstanceNode) {
264+
private boolean foreignObjectIntl(Object left, JSDynamicObject right, IsObjectNode isObjectNode, ForeignObjectPrototypeNode getForeignPrototypeNode,
265+
InlinedBranchProfile invalidPrototypeBranch, OrdinaryHasInstanceNode ordinaryHasInstanceNode) {
266+
if (!isObjectNode.executeBoolean(left)) {
267+
return false;
268+
}
264269
Object rightProto = getConstructorPrototype(right, invalidPrototypeBranch);
265270
Object foreignProto = getForeignPrototypeNode.execute(left);
266271
if (foreignProto == rightProto) {
@@ -276,10 +281,11 @@ protected boolean doNotAnObject(@SuppressWarnings("unused") Object left, @Suppre
276281

277282
@Specialization(guards = {"!isJSObject(left)", "isForeignObject(left)", "isJSProxy(right)", "isCallableProxy(right)"})
278283
protected boolean doNotAnObjectProxyForeign(@SuppressWarnings("unused") Object left, @SuppressWarnings("unused") JSDynamicObject right,
284+
@Cached @Shared IsObjectNode isAnyObjectNode,
279285
@Cached @Shared("foreignPrototypeNode") ForeignObjectPrototypeNode getForeignPrototypeNode,
280286
@Cached @Shared("invalidPrototypeBranch") InlinedBranchProfile invalidPrototypeBranch,
281287
@Cached("create(context)") @Shared("ordinaryHasInstance") OrdinaryHasInstanceNode ordinaryHasInstanceNode) {
282-
return doForeignObject(left, right, getForeignPrototypeNode, invalidPrototypeBranch, ordinaryHasInstanceNode);
288+
return doForeignObject(left, right, isAnyObjectNode, getForeignPrototypeNode, invalidPrototypeBranch, ordinaryHasInstanceNode);
283289
}
284290

285291
@Specialization(guards = {"!isJSObject(left)", "!isForeignObject(left)", "isJSProxy(right)", "isCallableProxy(right)"})

0 commit comments

Comments
 (0)