Skip to content

Commit 6474e6c

Browse files
Function.prototype[Symbol.hasInstance] fix
Fix Function.prototype[Symbol.hasInstance] so that it properly checks if the property is an object in all cases. Co-authored-by: andrea.bergia <andrea.bergia@servicenow.com>
1 parent b5b2bba commit 6474e6c

3 files changed

Lines changed: 53 additions & 6 deletions

File tree

rhino/src/main/java/org/mozilla/javascript/BaseFunction.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -405,8 +405,7 @@ public Object execIdCall(
405405
((NativeFunction) ((BoundFunction) thisObj).getTargetFunction())
406406
.getPrototypeProperty();
407407
else protoProp = ScriptableObject.getProperty(thisObj, "prototype");
408-
if (protoProp instanceof NativeObject
409-
|| protoProp instanceof IdScriptableObject) {
408+
if (ScriptRuntime.isObject(protoProp)) {
410409
return ScriptRuntime.jsDelegatesTo(obj, (Scriptable) protoProp);
411410
}
412411
throw ScriptRuntime.typeErrorById(

rhino/src/main/resources/org/mozilla/javascript/resources/Messages.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -684,7 +684,7 @@ msg.instanceof.not.object = \
684684
Can''t use ''instanceof'' on a non-object.
685685

686686
msg.instanceof.bad.prototype = \
687-
''prototype'' property of {0} is not an object.
687+
''prototype'' property of ''{0}'' is not an object.
688688

689689
msg.in.not.object = \
690690
Can''t use ''in'' on a non-object.

rhino/src/test/java/org/mozilla/javascript/FunctionPrototypeSymbolHasInstanceTest.java

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package org.mozilla.javascript;
22

3-
import org.junit.Ignore;
43
import org.junit.Test;
54
import org.mozilla.javascript.testutils.Utils;
65

@@ -58,7 +57,6 @@ public void testFunctionPrototypeSymbolHasInstanceHasAttributesStrictMode() {
5857
}
5958

6059
@Test
61-
@Ignore("name-length-params-prototype-set-incorrectly")
6260
public void testFunctionPrototypeSymbolHasInstanceHasProperties() {
6361
String script =
6462
"var a = Object.getOwnPropertyDescriptor(Function.prototype[Symbol.hasInstance], 'length');\n"
@@ -147,6 +145,56 @@ public void testThrowTypeErrorOnNonObjectIncludingSymbol() {
147145
+ "f.prototype = Symbol(); \n"
148146
+ "f[Symbol.hasInstance]({})";
149147
Utils.assertEcmaErrorES6(
150-
"TypeError: 'prototype' property of is not an object. (test#3)", script);
148+
"TypeError: 'prototype' property of '' is not an object. (test#3)", script);
149+
}
150+
151+
@Test
152+
public void testFunctionPrototypeSymbolHasInstanceStringProto() {
153+
String script =
154+
"var f = function() {}; \n"
155+
+ "f.prototype = new String(); \n"
156+
+ "f[Symbol.hasInstance]({})";
157+
158+
Utils.assertWithAllModes(false, script);
159+
}
160+
161+
@Test
162+
public void testFunctionPrototypeSymbolHasInstanceBooleanProto() {
163+
String script =
164+
"var f = function() {}; \n"
165+
+ "f.prototype = new Boolean(); \n"
166+
+ "f[Symbol.hasInstance]({})";
167+
168+
Utils.assertWithAllModes(false, script);
169+
}
170+
171+
@Test
172+
public void testFunctionPrototypeSymbolHasInstanceArrayProto() {
173+
String script =
174+
"var f = function() {}; \n"
175+
+ "f.prototype = new Array(); \n"
176+
+ "f[Symbol.hasInstance]({})";
177+
178+
Utils.assertWithAllModes(false, script);
179+
}
180+
181+
@Test
182+
public void testFunctionPrototypeSymbolHasInstanceNumberProto() {
183+
String script =
184+
"var f = function() {}; \n"
185+
+ "f.prototype = new Number(); \n"
186+
+ "f[Symbol.hasInstance]({})";
187+
188+
Utils.assertWithAllModes(false, script);
189+
}
190+
191+
@Test
192+
public void testFunctionPrototypeSymbolHasInstanceFunctionProto() {
193+
String script =
194+
"var f = function() {}; \n"
195+
+ "f.prototype = function() {}\n"
196+
+ "f[Symbol.hasInstance]({})";
197+
198+
Utils.assertWithAllModes(false, script);
151199
}
152200
}

0 commit comments

Comments
 (0)