-
Notifications
You must be signed in to change notification settings - Fork 859
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
264 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
172 changes: 172 additions & 0 deletions
172
rhino/src/test/java/org/mozilla/javascript/FunctionPrototypeSymbolHasInstanceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,172 @@ | ||
package org.mozilla.javascript; | ||
|
||
import org.junit.Assert; | ||
import org.junit.Ignore; | ||
import org.junit.Test; | ||
import org.mozilla.javascript.tests.Utils; | ||
|
||
public class FunctionPrototypeSymbolHasInstanceTest { | ||
@Test | ||
public void testSymbolHasInstanceIsPresent() { | ||
String script = | ||
"" | ||
+ "var f = {\n" | ||
+ " [Symbol.hasInstance](value) { " | ||
+ " }" | ||
+ "};\n" | ||
+ "var g = {};\n" | ||
+ "`${f.hasOwnProperty(Symbol.hasInstance)}:${g.hasOwnProperty(Symbol.hasInstance)}`"; | ||
Utils.assertWithAllOptimizationLevelsES6("true:false", script); | ||
} | ||
|
||
@Test | ||
public void testSymbolHasInstanceCanBeCalledLikeAnotherMethod() { | ||
String script = | ||
"" | ||
+ "var f = {\n" | ||
+ " [Symbol.hasInstance](value) { " | ||
+ " return 42;" | ||
+ " }" | ||
+ "};\n" | ||
+ "f[Symbol.hasInstance]() == 42"; | ||
Utils.assertWithAllOptimizationLevelsES6(true, script); | ||
} | ||
|
||
// See: https://tc39.es/ecma262/#sec-function.prototype-%symbol.hasinstance% | ||
@Test | ||
public void testFunctionPrototypeSymbolHasInstanceHasAttributes() { | ||
String script = | ||
"var a = Object.getOwnPropertyDescriptor(Function.prototype, Symbol.hasInstance);\n" | ||
+ "a.writable + ':' + a.configurable + ':' + a.enumerable"; | ||
Utils.assertWithAllOptimizationLevelsES6("false:false:false", script); | ||
} | ||
|
||
// See: https://tc39.es/ecma262/#sec-function.prototype-%symbol.hasinstance% | ||
@Test | ||
public void testFunctionPrototypeSymbolHasInstanceHasAttributesStrictMode() { | ||
String script = | ||
"'use strict';\n" | ||
+ "var t = typeof Function.prototype[Symbol.hasInstance];\n" | ||
+ "var a = Object.getOwnPropertyDescriptor(Function.prototype, Symbol.hasInstance);\n" | ||
+ "var typeErrorThrown = false;\n" | ||
+ "try { \n" | ||
+ " delete Function.prototype[Symbol.hasInstance] \n" | ||
+ "} catch (e) { \n" | ||
+ " typeErrorThrown = true \n" | ||
+ "}\n" | ||
+ "Object.prototype.hasOwnProperty.call(Function.prototype, Symbol.hasInstance) + ':' + typeErrorThrown + ':' + t + ':' + a.writable + ':' + a.configurable + ':' + a.enumerable; \n"; | ||
Utils.assertWithAllOptimizationLevelsES6("true:true:function:false:false:false", script); | ||
} | ||
|
||
@Test | ||
@Ignore("name-length-params-prototype-set-incorrectly") | ||
public void testFunctionPrototypeSymbolHasInstanceHasProperties() { | ||
String script = | ||
"var a = Object.getOwnPropertyDescriptor(Function.prototype[Symbol.hasInstance], 'length');\n" | ||
+ "a.value + ':' + a.writable + ':' + a.configurable + ':' + a.enumerable"; | ||
|
||
String script2 = | ||
"var a = Object.getOwnPropertyDescriptor(Function.prototype[Symbol.hasInstance], 'name');\n" | ||
+ "a.value + ':' + a.writable + ':' + a.configurable + ':' + a.enumerable"; | ||
Utils.assertWithAllOptimizationLevelsES6("1:false:true:false", script); | ||
Utils.assertWithAllOptimizationLevelsES6( | ||
"Symbol(Symbol.hasInstance):false:true:false", script2); | ||
} | ||
|
||
@Test | ||
public void testFunctionPrototypeSymbolHasInstance() { | ||
String script = | ||
"(Function.prototype[Symbol.hasInstance] instanceof Function) + ':' + " | ||
+ "Function.prototype[Symbol.hasInstance].call(Function, Object)\n"; | ||
Utils.assertWithAllOptimizationLevelsES6("true:true", script); | ||
} | ||
|
||
@Test | ||
public void testFunctionPrototypeSymbolHasInstanceOnObjectReturnsTrue() { | ||
String script = | ||
"var f = function() {};\n" | ||
+ "var o = new f();\n" | ||
+ "var o2 = Object.create(o);\n" | ||
+ "(f[Symbol.hasInstance](o)) + ':' + " | ||
+ "(f[Symbol.hasInstance](o2));\n"; | ||
Utils.assertWithAllOptimizationLevelsES6("true:true", script); | ||
} | ||
|
||
@Test | ||
public void testFunctionPrototypeSymbolHasInstanceOnBoundTargetReturnsTrue() { | ||
String script = | ||
"var BC = function() {};\n" | ||
+ "var bc = new BC();\n" | ||
+ "var bound = BC.bind();\n" | ||
+ "bound[Symbol.hasInstance](bc);\n"; | ||
Utils.assertWithAllOptimizationLevelsES6(true, script); | ||
} | ||
|
||
@Test | ||
public void testFunctionInstanceNullVoidEtc() { | ||
String script = | ||
"var f = function() {};\n" | ||
+ "var x;\n" | ||
+ "a = (undefined instanceof f) + ':' +\n" | ||
+ "(x instanceof f) + ':' +\n" | ||
+ "(null instanceof f) + ':' +\n" | ||
+ "(void 0 instanceof f)\n" | ||
+ "a"; | ||
Utils.assertWithAllOptimizationLevelsES6("false:false:false:false", script); | ||
} | ||
|
||
@Test | ||
public void testFunctionPrototypeSymbolHasInstanceReturnsFalseOnUndefinedOrProtoypeNotFound() { | ||
String script = | ||
"Function.prototype[Symbol.hasInstance].call() + ':' +" | ||
+ "Function.prototype[Symbol.hasInstance].call({});"; | ||
Utils.assertWithAllOptimizationLevelsES6("false:false", script); | ||
} | ||
|
||
@Test | ||
public void testSymbolHasInstanceIsInvokedInInstanceOf() { | ||
String script = | ||
"" | ||
+ "var globalSet = 0;" | ||
+ "var f = {\n" | ||
+ " [Symbol.hasInstance](value) { " | ||
+ " globalSet = 1;" | ||
+ " return true;" | ||
+ " }" | ||
+ "}\n" | ||
+ "var g = {}\n" | ||
+ "Object.setPrototypeOf(g, f);\n" | ||
+ "g instanceof f;" | ||
+ "globalSet == 1"; | ||
Utils.assertWithAllOptimizationLevelsES6(true, script); | ||
} | ||
|
||
@Test | ||
public void testThrowTypeErrorOnNonObjectIncludingSymbol() { | ||
String script = | ||
"" | ||
+ "var f = function() {}; \n" | ||
+ "f.prototype = Symbol(); \n" | ||
+ "f[Symbol.hasInstance]({})"; | ||
|
||
Utils.runWithAllOptimizationLevels( | ||
(cx) -> { | ||
cx.setLanguageVersion(Context.VERSION_ES6); | ||
final Scriptable scope = cx.initStandardObjects(); | ||
var error = | ||
Assert.assertThrows( | ||
EcmaError.class, | ||
() -> | ||
cx.evaluateString( | ||
scope, | ||
script, | ||
"testSymbolHasInstance", | ||
0, | ||
null)); | ||
Assert.assertTrue( | ||
error.toString() | ||
.contains("'prototype' property of is not an object.")); | ||
return null; | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters