Takes care that it's possible to customize the result of the typeof operator. See https://bugzilla.mozilla.org/show_bug.cgi?id=463996 Includes fix and test for https://bugzilla.mozilla.org/show_bug.cgi?id=453360 @author Marc Guillemot
| 24 | * @author Marc Guillemot |
| 25 | */ |
| 26 | public class TypeOfTest { |
| 27 | |
| 28 | public static class Foo extends ScriptableObject { |
| 29 | private static final long serialVersionUID = -8771045033217033529L; |
| 30 | private final String typeOfValue_; |
| 31 | |
| 32 | public Foo(final String _typeOfValue) { |
| 33 | typeOfValue_ = _typeOfValue; |
| 34 | } |
| 35 | |
| 36 | @Override |
| 37 | public String getTypeOf() { |
| 38 | return typeOfValue_; |
| 39 | } |
| 40 | |
| 41 | @Override |
| 42 | public String getClassName() { |
| 43 | return "Foo"; |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | /** ECMA 11.4.3 says that typeof on host object is Implementation-dependent */ |
| 48 | @Test |
| 49 | public void customizeTypeOf() throws Exception { |
| 50 | doTest("object", "typeof myObj", new Foo("object")); |
| 51 | doTest("blabla", "typeof myObj", new Foo("blabla")); |
| 52 | } |
| 53 | |
| 54 | /** ECMA 11.4.3 says that typeof on host object is Implementation-dependent */ |
| 55 | @Test |
| 56 | public void test0() throws Exception { |
| 57 | final Function f = |
| 58 | new BaseFunction() { |
| 59 | @Override |
| 60 | public Object call( |
| 61 | Context _cx, VarScope _scope, Scriptable _thisObj, Object[] _args) { |
| 62 | return _args[0].getClass().getName(); |
| 63 | } |
| 64 | }; |
| 65 | doTest("function", "typeof myObj", f); |
| 66 | } |
| 67 | |
| 68 | /** See https://bugzilla.mozilla.org/show_bug.cgi?id=453360 */ |
| 69 | @Test |
| 70 | public void bug453360() throws Exception { |
| 71 | doTest("object", "typeof new RegExp();", null); |
| 72 | doTest("object", "typeof /foo/;", null); |
| 73 | } |
| 74 | |
| 75 | private static void doTest(String expected, final String script, final Scriptable obj) { |
| 76 | Utils.runWithAllModes( |
| 77 | cx -> { |
| 78 | TopLevel scope = cx.initStandardObjects(); |
| 79 | scope.put("myObj", scope, obj); |
| 80 | |
| 81 | String res = |
| 82 | Context.toString( |
| 83 | cx.evaluateString(scope, script, "test script", 1, null)); |
nothing calls this directly
no outgoing calls
no test coverage detected