LEFT | RIGHT |
1 // Copyright (C) 2007 Google Inc. | 1 // Copyright (C) 2007 Google Inc. |
2 // | 2 // |
3 // Licensed under the Apache License, Version 2.0 (the "License"); | 3 // Licensed under the Apache License, Version 2.0 (the "License"); |
4 // you may not use this file except in compliance with the License. | 4 // you may not use this file except in compliance with the License. |
5 // You may obtain a copy of the License at | 5 // You may obtain a copy of the License at |
6 // | 6 // |
7 // http://www.apache.org/licenses/LICENSE-2.0 | 7 // http://www.apache.org/licenses/LICENSE-2.0 |
8 // | 8 // |
9 // Unless required by applicable law or agreed to in writing, software | 9 // Unless required by applicable law or agreed to in writing, software |
10 // distributed under the License is distributed on an "AS IS" BASIS, | 10 // distributed under the License is distributed on an "AS IS" BASIS, |
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 // See the License for the specific language governing permissions and | 12 // See the License for the specific language governing permissions and |
13 // limitations under the License. | 13 // limitations under the License. |
14 | 14 |
15 package com.google.caja.parser.quasiliteral; | 15 package com.google.caja.parser.quasiliteral; |
16 | 16 |
17 import com.google.caja.lexer.FilePosition; | 17 import com.google.caja.lexer.FilePosition; |
18 import com.google.caja.lexer.ParseException; | 18 import com.google.caja.lexer.ParseException; |
19 import com.google.caja.parser.ParseTreeNode; | 19 import com.google.caja.parser.ParseTreeNode; |
20 import com.google.caja.parser.ParseTreeNodes; | 20 import com.google.caja.parser.ParseTreeNodes; |
21 import com.google.caja.parser.js.Block; | 21 import com.google.caja.parser.js.Block; |
22 import com.google.caja.parser.js.FormalParam; | 22 import com.google.caja.parser.js.FormalParam; |
23 import com.google.caja.parser.js.FunctionConstructor; | 23 import com.google.caja.parser.js.FunctionConstructor; |
24 import com.google.caja.parser.js.FunctionDeclaration; | 24 import com.google.caja.parser.js.FunctionDeclaration; |
25 import com.google.caja.parser.js.Identifier; | 25 import com.google.caja.parser.js.Identifier; |
26 import com.google.caja.parser.js.Operator; | 26 import com.google.caja.parser.js.Operator; |
27 import com.google.caja.parser.js.Statement; | 27 import com.google.caja.parser.js.Statement; |
| 28 import com.google.caja.parser.js.StringLiteral; |
28 import com.google.caja.parser.js.SyntheticNodes; | 29 import com.google.caja.parser.js.SyntheticNodes; |
29 import com.google.caja.parser.js.UncajoledModule; | 30 import com.google.caja.parser.js.UncajoledModule; |
30 import com.google.caja.reporting.MessageLevel; | 31 import com.google.caja.reporting.MessageLevel; |
31 import com.google.caja.reporting.MessageType; | 32 import com.google.caja.reporting.MessageType; |
32 import com.google.caja.reporting.TestBuildInfo; | 33 import com.google.caja.reporting.TestBuildInfo; |
33 import com.google.caja.util.RhinoTestBed; | 34 import com.google.caja.util.RhinoTestBed; |
34 | 35 |
35 import java.io.IOException; | 36 import java.io.IOException; |
36 import java.util.ArrayList; | 37 import java.util.ArrayList; |
| 38 import java.util.Arrays; |
37 import java.util.Collections; | 39 import java.util.Collections; |
38 import java.util.EnumSet; | 40 import java.util.EnumSet; |
39 import java.util.List; | 41 import java.util.List; |
40 | 42 |
41 import junit.framework.AssertionFailedError; | 43 import junit.framework.AssertionFailedError; |
42 | 44 |
43 /** | 45 /** |
44 * @author ihab.awad@gmail.com | 46 * @author ihab.awad@gmail.com |
45 */ | 47 */ |
46 public class CajitaRewriterTest extends CommonJsRewriterTestCase { | 48 public class CajitaRewriterTest extends CommonJsRewriterTestCase { |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
86 private static String weldReadPub(String obj, String varName, String tempObj,
boolean flag) { | 88 private static String weldReadPub(String obj, String varName, String tempObj,
boolean flag) { |
87 return | 89 return |
88 "(" + | 90 "(" + |
89 "(" + tempObj + " = " + obj + ")," + | 91 "(" + tempObj + " = " + obj + ")," + |
90 "(" + tempObj + "." + varName + "_canRead___ ?" + | 92 "(" + tempObj + "." + varName + "_canRead___ ?" + |
91 " " + tempObj + "." + varName + ":" + | 93 " " + tempObj + "." + varName + ":" + |
92 " ___.readPub(" + tempObj + ", '" + varName + "'" + (flag ? ", true"
: "") + "))"+ | 94 " ___.readPub(" + tempObj + ", '" + varName + "'" + (flag ? ", true"
: "") + "))"+ |
93 ")"; | 95 ")"; |
94 } | 96 } |
95 | 97 |
96 public static String weldPrelude(String name) { | 98 private static String weldPreludes(String... names) { |
97 return "var " + name + " = ___.readImport(IMPORTS___, '" + name + "');"; | 99 Arrays.sort(names); |
98 } | 100 StringBuilder sb = new StringBuilder(); |
99 | 101 for (String name : names) { |
100 public static String weldPrelude(String name, String permitsUsed) { | 102 sb.append("var ").append(name).append(" = ___.readImport(IMPORTS___, ") |
| 103 .append(StringLiteral.toQuotedValue(name)).append(");\n"); |
| 104 } |
| 105 return sb.toString(); |
| 106 } |
| 107 |
| 108 private static String weldPrelude(String name) { return weldPreludes(name); } |
| 109 |
| 110 private static String weldPrelude(String name, String permitsUsed) { |
101 return "var " + name + " = ___.readImport(IMPORTS___, '" + name + | 111 return "var " + name + " = ___.readImport(IMPORTS___, '" + name + |
102 "', " + permitsUsed + ");"; | 112 "', " + permitsUsed + ");"; |
103 } | 113 } |
104 | 114 |
105 public void testToString() throws Exception { | 115 public void testToString() throws Exception { |
106 assertConsistent( | 116 assertConsistent( |
107 "var z = { toString: function () { return 'blah'; } };" + | 117 "var z = { toString: function () { return 'blah'; } };" + |
108 "try {" + | 118 "try {" + |
109 " '' + z;" + | 119 " '' + z;" + |
110 "} catch (e) {" + | 120 "} catch (e) {" + |
(...skipping 395 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
506 "}")), | 516 "}")), |
507 MessageType.MASKING_SYMBOL, | 517 MessageType.MASKING_SYMBOL, |
508 MessageLevel.ERROR); | 518 MessageLevel.ERROR); |
509 checkAddsMessage(js(fromString( | 519 checkAddsMessage(js(fromString( |
510 "try {} catch (x__) { }")), | 520 "try {} catch (x__) { }")), |
511 RewriterMessageType.VARIABLES_CANNOT_END_IN_DOUBLE_UNDERSCORE); | 521 RewriterMessageType.VARIABLES_CANNOT_END_IN_DOUBLE_UNDERSCORE); |
512 // TODO(ihab.awad): The below should throw MessageType.MASKING_SYMBOL at | 522 // TODO(ihab.awad): The below should throw MessageType.MASKING_SYMBOL at |
513 // MessageLevel.ERROR. See bug #313. For the moment, we merely check that | 523 // MessageLevel.ERROR. See bug #313. For the moment, we merely check that |
514 // it cajoles to something secure. | 524 // it cajoles to something secure. |
515 checkSucceeds( | 525 checkSucceeds( |
516 "try {" + | 526 "var x;" + |
517 " g[0];" + | 527 "try {" + |
| 528 " g[x + 0];" + |
518 " e;" + | 529 " e;" + |
519 " g[1];" + | 530 " g[x + 1];" + |
520 "} catch (e) {" + | 531 "} catch (e) {" + |
521 " g[2];" + | 532 " g[x + 2];" + |
522 " e;" + | 533 " e;" + |
523 " g[3];" + | 534 " g[x + 3];" + |
524 "}", | 535 "}", |
525 weldPrelude("e") + | 536 weldPreludes("e", "g") + |
526 weldPrelude("g") + | 537 "var x;" + |
527 "try {" + | 538 "try {" + |
528 " ___.readPub(g, 0);" + | 539 " ___.readPub(g, x + 0);" + |
529 " e;" + | 540 " e;" + |
530 " ___.readPub(g, 1);" + | 541 " ___.readPub(g, x + 1);" + |
531 "} catch (ex___) {" + | 542 "} catch (ex___) {" + |
532 " try {" + | 543 " try {" + |
533 " throw ___.tameException(ex___);" + | 544 " throw ___.tameException(ex___);" + |
534 " } catch (e) {" + | 545 " } catch (e) {" + |
535 " ___.readPub(g, 2);" + | 546 " ___.readPub(g, x + 2);" + |
536 " e;" + | 547 " e;" + |
537 " ___.readPub(g, 3);" + | 548 " ___.readPub(g, x + 3);" + |
538 " }" + | 549 " }" + |
539 "}"); | 550 "}"); |
540 rewriteAndExecute( | 551 rewriteAndExecute( |
541 "var handled = false;" + | 552 "var handled = false;" + |
542 "try {" + | 553 "try {" + |
543 " throw null;" + | 554 " throw null;" + |
544 "} catch (ex) {" + | 555 "} catch (ex) {" + |
545 " assertEquals(null, ex);" + // Right value in ex. | 556 " assertEquals(null, ex);" + // Right value in ex. |
546 " handled = true;" + | 557 " handled = true;" + |
547 "}" + | 558 "}" + |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
636 "try {" + | 647 "try {" + |
637 "} catch (e) {" + | 648 "} catch (e) {" + |
638 "} finally {" + | 649 "} finally {" + |
639 "}")), | 650 "}")), |
640 MessageType.MASKING_SYMBOL, | 651 MessageType.MASKING_SYMBOL, |
641 MessageLevel.ERROR); | 652 MessageLevel.ERROR); |
642 checkAddsMessage(js(fromString( | 653 checkAddsMessage(js(fromString( |
643 "try {} catch (x__) { } finally { }")), | 654 "try {} catch (x__) { } finally { }")), |
644 RewriterMessageType.VARIABLES_CANNOT_END_IN_DOUBLE_UNDERSCORE); | 655 RewriterMessageType.VARIABLES_CANNOT_END_IN_DOUBLE_UNDERSCORE); |
645 checkSucceeds( | 656 checkSucceeds( |
646 "try {" + | 657 "var x;" + |
647 " g[0];" + | 658 "try {" + |
| 659 " g[x + 0];" + |
648 " e;" + | 660 " e;" + |
649 " g[1];" + | 661 " g[x + 1];" + |
650 "} catch (e) {" + | 662 "} catch (e) {" + |
651 " g[2];" + | 663 " g[x + 2];" + |
652 " e;" + | 664 " e;" + |
653 " g[3];" + | 665 " g[x + 3];" + |
654 "} finally {" + | 666 "} finally {" + |
655 " g[4];" + | 667 " g[x + 4];" + |
656 " e;" + | 668 " e;" + |
657 " g[5];" + | 669 " g[x + 5];" + |
658 "}", | 670 "}", |
659 weldPrelude("e") + | 671 weldPreludes("e", "g") + |
660 weldPrelude("g") + | 672 "var x;" + |
661 "try {" + | 673 "try {" + |
662 " ___.readPub(g, 0);" + | 674 " ___.readPub(g, x + 0);" + |
663 " e;" + | 675 " e;" + |
664 " ___.readPub(g, 1);" + | 676 " ___.readPub(g, x + 1);" + |
665 "} catch (ex___) {" + | 677 "} catch (ex___) {" + |
666 " try {" + | 678 " try {" + |
667 " throw ___.tameException(ex___);" + | 679 " throw ___.tameException(ex___);" + |
668 " } catch (e) {" + | 680 " } catch (e) {" + |
669 " ___.readPub(g, 2);" + | 681 " ___.readPub(g, x + 2);" + |
670 " e;" + | 682 " e;" + |
671 " ___.readPub(g, 3);" + | 683 " ___.readPub(g, x + 3);" + |
672 " }" + | 684 " }" + |
673 "} finally {" + | 685 "} finally {" + |
674 " ___.readPub(g, 4);" + | 686 " ___.readPub(g, x + 4);" + |
675 " e;" + | 687 " e;" + |
676 " ___.readPub(g, 5);" + | 688 " ___.readPub(g, x + 5);" + |
677 "}"); | 689 "}"); |
678 } | 690 } |
679 | 691 |
680 public void testTryFinally() throws Exception { | 692 public void testTryFinally() throws Exception { |
681 assertConsistent( | 693 assertConsistent( |
682 "var out = 0;" + | 694 "var out = 0;" + |
683 "try {" + | 695 "try {" + |
684 " try {" + | 696 " try {" + |
685 " throw 2;" + | 697 " throw 2;" + |
686 " } finally {" + | 698 " } finally {" + |
687 " out = 1;" + | 699 " out = 1;" + |
688 " }" + | 700 " }" + |
689 " out = 2;" + | 701 " out = 2;" + |
690 "} catch (e) {" + | 702 "} catch (e) {" + |
691 "}" + | 703 "}" + |
692 "out;"); | 704 "out;"); |
693 checkSucceeds( | 705 checkSucceeds( |
694 "try {" + | 706 "var x;" + |
695 " g[0];" + | 707 "try {" + |
| 708 " g[x + 0];" + |
696 " e;" + | 709 " e;" + |
697 " g[1];" + | 710 " g[x + 1];" + |
698 "} finally {" + | 711 "} finally {" + |
699 " g[2];" + | 712 " g[x + 2];" + |
700 " e;" + | 713 " e;" + |
701 " g[3];" + | 714 " g[x + 3];" + |
702 "}", | 715 "}", |
703 weldPrelude("e") + | 716 weldPreludes("e", "g") + |
704 weldPrelude("g") + | 717 "var x;" + |
705 "try {" + | 718 "try {" + |
706 " ___.readPub(g, 0);" + | 719 " ___.readPub(g, x + 0);" + |
707 " e;" + | 720 " e;" + |
708 " ___.readPub(g, 1);" + | 721 " ___.readPub(g, x + 1);" + |
709 "} finally {" + | 722 "} finally {" + |
710 " ___.readPub(g, 2);" + | 723 " ___.readPub(g, x + 2);" + |
711 " e;" + | 724 " e;" + |
712 " ___.readPub(g, 3);" + | 725 " ___.readPub(g, x + 3);" + |
713 "}"); | 726 "}"); |
714 } | 727 } |
715 | 728 |
716 public void testVarArgs() throws Exception { | 729 public void testVarArgs() throws Exception { |
717 checkSucceeds( | 730 checkSucceeds( |
718 "var p;" + | 731 "var p;" + |
719 "var foo = function() {" + | 732 "var foo = function() {" + |
720 " p = arguments;" + | 733 " p = arguments;" + |
721 "};", | 734 "};", |
722 "var p;" + | 735 "var p;" + |
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
853 | 866 |
854 public void testReadBadPrototype() throws Exception { | 867 public void testReadBadPrototype() throws Exception { |
855 checkAddsMessage( | 868 checkAddsMessage( |
856 js(fromString("function foo() {} foo.prototype;")), | 869 js(fromString("function foo() {} foo.prototype;")), |
857 RewriterMessageType.PROTOTYPICAL_INHERITANCE_NOT_IN_CAJITA); | 870 RewriterMessageType.PROTOTYPICAL_INHERITANCE_NOT_IN_CAJITA); |
858 checkAddsMessage( | 871 checkAddsMessage( |
859 js(fromString("var q = function foo() { foo.prototype; };")), | 872 js(fromString("var q = function foo() { foo.prototype; };")), |
860 RewriterMessageType.PROTOTYPICAL_INHERITANCE_NOT_IN_CAJITA); | 873 RewriterMessageType.PROTOTYPICAL_INHERITANCE_NOT_IN_CAJITA); |
861 } | 874 } |
862 | 875 |
| 876 public void testReadPublicLength() throws Exception { |
| 877 checkSucceeds( |
| 878 "var arr = [1, 2, 3];" + |
| 879 "arr.length;", |
| 880 "var arr = [1, 2, 3];" + |
| 881 "arr.length;"); |
| 882 checkSucceeds( |
| 883 "var arr = [1, 2, 3];" + |
| 884 "arr.length = 4;", |
| 885 "var arr = [1, 2, 3];" + |
| 886 "arr.length_canSet___ ? (arr.length = 4) : ___.setPub(arr, 'length', 4)" |
| 887 ); |
| 888 checkSucceeds( |
| 889 "var arr = [1, 2, 3];" + |
| 890 "--arr.length;", |
| 891 "var x0___;" + |
| 892 "var arr = [1, 2, 3];" + |
| 893 "x0___ = arr.length - 1," + |
| 894 "arr.length_canSet___" + |
| 895 " ? (arr.length = x0___)" + |
| 896 " : ___.setPub(arr, 'length', x0___);" |
| 897 ); |
| 898 } |
| 899 |
863 public void testReadPublic() throws Exception { | 900 public void testReadPublic() throws Exception { |
864 checkSucceeds( | 901 checkSucceeds( |
865 "var p;" + | 902 "var p;" + |
866 "p = foo().p;", | 903 "p = foo().p;", |
867 weldPrelude("foo") + | 904 weldPrelude("foo") + |
868 "var x0___;" + | 905 "var x0___;" + |
869 "var p;" + | 906 "var p;" + |
870 "p = " + weldReadPub("foo.CALL___()", "p", "x0___") + ";"); | 907 "p = " + weldReadPub("foo.CALL___()", "p", "x0___") + ";"); |
871 } | 908 } |
872 | 909 |
873 public void testReadIndexPublic() throws Exception { | 910 public void testReadNumPublic() throws Exception { |
| 911 checkSucceeds( |
| 912 "var p, q, x;" + |
| 913 "p = q[+x];", |
| 914 "var p, q, x;" + |
| 915 "p = q[+x];"); |
| 916 // Make sure that setPub is used on assignment to test frozenness. |
| 917 checkSucceeds( |
| 918 "var p, q, x;" + |
| 919 "q[+x] = p;", |
| 920 "var p, q, x;" + |
| 921 "___.setPub(q, +x, p);"); |
| 922 checkSucceeds( |
| 923 "var p, q;" + |
| 924 "p[+q] += 2;", |
| 925 "var x0___;" + |
| 926 "var x1___;" + |
| 927 "var p, q;" + |
| 928 "x0___ = p," + |
| 929 "x1___ = +q," + |
| 930 "___.setPub(x0___, +x1___, x0___[+x1___] + 2);"); |
| 931 checkSucceeds( |
| 932 "var p, q;" + |
| 933 "p[+q]--;", |
| 934 "var x0___;" + |
| 935 "var x1___;" + |
| 936 "var x2___;" + |
| 937 "var p, q;" + |
| 938 "x0___ = p," + |
| 939 "x1___ = +q," + |
| 940 "x2___ = +x0___[+x1___]," + |
| 941 "___.setPub(x0___, +x1___, x2___ - 1), x2___;"); |
| 942 checkSucceeds( |
| 943 "var p, q;" + |
| 944 "--p[+q];", |
| 945 "var x0___;" + |
| 946 "var x1___;" + |
| 947 "var p, q;" + |
| 948 "x0___ = p," + |
| 949 "x1___ = +q," + |
| 950 "___.setPub(x0___, +x1___, x0___[+x1___] - 1);"); |
| 951 } |
| 952 |
| 953 public void testNumWithConstantIndex() throws Exception { |
874 checkSucceeds( | 954 checkSucceeds( |
875 "var p, q;" + | 955 "var p, q;" + |
876 "p = q[3];", | 956 "p = q[3];", |
877 "var p, q;" + | 957 "var p, q;" + |
878 "p = ___.readPub(q, 3);"); | 958 "p = q[3];"); |
| 959 // Make sure that setPub is used on assignment to test frozenness. |
| 960 checkSucceeds( |
| 961 "var p, q;" + |
| 962 "q[3] = p;", |
| 963 "var p, q;" + |
| 964 "___.setPub(q, 3, p);"); |
| 965 checkSucceeds( |
| 966 "var p;" + |
| 967 "p[3] -= 2", |
| 968 "var p;" + |
| 969 "___.setPub(p, 3, p[3] - 2);"); |
| 970 checkSucceeds( |
| 971 "var p;" + |
| 972 "--p[3]", |
| 973 "var p;" + |
| 974 "___.setPub(p, 3, p[3] - 1);"); |
| 975 } |
| 976 |
| 977 public void testReadIndexPublic() throws Exception { |
| 978 checkSucceeds( |
| 979 "var p, q, x;" + |
| 980 "p = q[x];", |
| 981 "var p, q, x;" + |
| 982 "p = ___.readPub(q, x);"); |
879 } | 983 } |
880 | 984 |
881 public void testSetBadAssignToFunctionName() throws Exception { | 985 public void testSetBadAssignToFunctionName() throws Exception { |
882 checkAddsMessage(js(fromString( | 986 checkAddsMessage(js(fromString( |
883 " function foo() {};" | 987 " function foo() {};" |
884 + "foo = 3;")), | 988 + "foo = 3;")), |
885 RewriterMessageType.CANNOT_ASSIGN_TO_FUNCTION_NAME); | 989 RewriterMessageType.CANNOT_ASSIGN_TO_FUNCTION_NAME); |
886 checkAddsMessage(js(fromString( | 990 checkAddsMessage(js(fromString( |
887 " function foo() {};" | 991 " function foo() {};" |
888 + "foo += 3;")), | 992 + "foo += 3;")), |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
933 checkAddsMessage( | 1037 checkAddsMessage( |
934 js(fromString("function foo() {} foo.prototype = 3;")), | 1038 js(fromString("function foo() {} foo.prototype = 3;")), |
935 RewriterMessageType.PROTOTYPICAL_INHERITANCE_NOT_IN_CAJITA); | 1039 RewriterMessageType.PROTOTYPICAL_INHERITANCE_NOT_IN_CAJITA); |
936 checkAddsMessage( | 1040 checkAddsMessage( |
937 js(fromString("var q = function foo() { foo.prototype = 3; };")), | 1041 js(fromString("var q = function foo() { foo.prototype = 3; };")), |
938 RewriterMessageType.PROTOTYPICAL_INHERITANCE_NOT_IN_CAJITA); | 1042 RewriterMessageType.PROTOTYPICAL_INHERITANCE_NOT_IN_CAJITA); |
939 } | 1043 } |
940 | 1044 |
941 public void testSetPublic() throws Exception { | 1045 public void testSetPublic() throws Exception { |
942 checkSucceeds( | 1046 checkSucceeds( |
943 "x().p = g[0];", | 1047 "x().p = g[h];", |
944 weldPrelude("g") + | 1048 weldPreludes("g", "h", "x") + |
945 weldPrelude("x") + | |
946 "var x0___;" + | 1049 "var x0___;" + |
947 "var x1___;" + | 1050 "var x1___;" + |
948 weldSetPub("x.CALL___()", "p", "___.readPub(g, 0)", "x0___", "x1___") + | 1051 weldSetPub("x.CALL___()", "p", "___.readPub(g, h)", "x0___", "x1___") + |
949 ";"); | 1052 ";"); |
950 } | 1053 } |
951 | 1054 |
952 public void testSetIndexPublic() throws Exception { | 1055 public void testSetIndexPublic() throws Exception { |
953 checkSucceeds( | 1056 checkSucceeds( |
954 "g[0][g[1]] = g[2];", | 1057 "g[0][g[1]] = g[2];", |
955 weldPrelude("g") + | 1058 weldPrelude("g") + |
956 "___.setPub(___.readPub(g, 0), ___.readPub(g, 1), ___.readPub(g, 2));"); | 1059 "___.setPub(g[0], g[1], g[2]);"); |
| 1060 checkSucceeds( |
| 1061 "g[a][g[b]] = g[c];", |
| 1062 weldPreludes("a", "b", "c", "g") + |
| 1063 "___.setPub(___.readPub(g, a), ___.readPub(g, b), ___.readPub(g, c));"); |
957 } | 1064 } |
958 | 1065 |
959 public void testSetBadInitialize() throws Exception { | 1066 public void testSetBadInitialize() throws Exception { |
960 checkFails( | 1067 checkFails( |
961 "var x__ = 3;", | 1068 "var x__ = 3;", |
962 "Variables cannot end in \"__\""); | 1069 "Variables cannot end in \"__\""); |
963 } | 1070 } |
964 | 1071 |
965 public void testSetInitialize() throws Exception { | 1072 public void testSetInitialize() throws Exception { |
966 checkSucceeds( | 1073 checkSucceeds( |
967 "var v = g[0];", | 1074 "var v = g[h];", |
968 weldPrelude("g") + | 1075 weldPreludes("g", "h") + |
969 "var v = ___.readPub(g, 0);"); | 1076 "var v = ___.readPub(g, h);"); |
970 } | 1077 } |
971 | 1078 |
972 public void testSetBadDeclare() throws Exception { | 1079 public void testSetBadDeclare() throws Exception { |
973 checkFails( | 1080 checkFails( |
974 "var x__;", | 1081 "var x__;", |
975 "Variables cannot end in \"__\""); | 1082 "Variables cannot end in \"__\""); |
976 } | 1083 } |
977 | 1084 |
978 public void testSetDeclare() throws Exception { | 1085 public void testSetDeclare() throws Exception { |
979 checkSucceeds( | 1086 checkSucceeds( |
980 "var v;", | 1087 "var v;", |
981 "var v;"); | 1088 "var v;"); |
982 checkSucceeds( | 1089 checkSucceeds( |
983 "try { } catch (e) { var v; }", | 1090 "try { } catch (e) { var v; }", |
984 "try {" + | 1091 "try {" + |
985 "} catch (ex___) {" + | 1092 "} catch (ex___) {" + |
986 " try {" + | 1093 " try {" + |
987 " throw ___.tameException(ex___);" + | 1094 " throw ___.tameException(ex___);" + |
988 " } catch (e) {" + | 1095 " } catch (e) {" + |
989 " var v;" + | 1096 " var v;" + |
990 " }" + | 1097 " }" + |
991 "}"); | 1098 "}"); |
992 } | 1099 } |
993 | 1100 |
994 public void testSetVar() throws Exception { | 1101 public void testSetVar() throws Exception { |
995 checkAddsMessage( | 1102 checkAddsMessage( |
996 js(fromString("try {} catch (x__) { x__ = 3; }")), | 1103 js(fromString("try {} catch (x__) { x__ = 3; }")), |
997 RewriterMessageType.VARIABLES_CANNOT_END_IN_DOUBLE_UNDERSCORE); | 1104 RewriterMessageType.VARIABLES_CANNOT_END_IN_DOUBLE_UNDERSCORE); |
998 checkSucceeds( | 1105 checkSucceeds( |
999 "var x;" + | 1106 "var x, y;" + |
1000 "x = g[0];", | 1107 "x = g[y];", |
1001 weldPrelude("g") + | 1108 weldPrelude("g") + |
1002 "var x;" + | 1109 "var x, y;" + |
1003 "x = ___.readPub(g, 0);"); | 1110 "x = ___.readPub(g, y);"); |
1004 } | 1111 } |
1005 | 1112 |
1006 public void testSetReadModifyWriteLocalVar() throws Exception { | 1113 public void testSetReadModifyWriteLocalVar() throws Exception { |
1007 checkFails("x__ *= 2;", ""); | 1114 checkFails("x__ *= 2;", ""); |
1008 checkFails("x *= y__;", ""); | 1115 checkFails("x *= y__;", ""); |
1009 checkSucceeds( | 1116 checkSucceeds( |
1010 "var x; x += g[0];", | 1117 "var x, y; x += g[y];", |
1011 weldPrelude("g") | 1118 weldPrelude("g") |
1012 + "var x; x = x + ___.readPub(g, 0);"); | 1119 + "var x, y; x = x + ___.readPub(g, y);"); |
1013 checkSucceeds( | 1120 checkSucceeds( |
1014 "myArray().key += 1;", | 1121 "myArray().key += 1;", |
1015 weldPrelude("myArray") | 1122 weldPrelude("myArray") |
1016 + "var x0___;" | 1123 + "var x0___;" |
| 1124 + "var x1___;" |
1017 + "x0___ = myArray.CALL___()," | 1125 + "x0___ = myArray.CALL___()," |
1018 + "___.setPub(x0___, 'key'," | 1126 + "x1___ = (x0___.key_canRead___" |
1019 + " ___.readPub(x0___, 'key') + 1);"); | 1127 + " ? x0___.key : ___.readPub(x0___, 'key')) + 1," |
| 1128 + "x0___.key_canSet___" |
| 1129 + " ? (x0___.key = x1___) : ___.setPub(x0___, 'key', x1___);"); |
1020 checkSucceeds( | 1130 checkSucceeds( |
1021 "myArray()[myKey()] += 1;", | 1131 "myArray()[myKey()] += 1;", |
1022 weldPrelude("myArray") | 1132 weldPrelude("myArray") |
1023 + weldPrelude("myKey") | 1133 + weldPrelude("myKey") |
1024 + "var x0___;" | 1134 + "var x0___;" |
1025 + "var x1___;" | 1135 + "var x1___;" |
1026 + "x0___ = myArray.CALL___()," | 1136 + "x0___ = myArray.CALL___()," |
1027 + "x1___ = myKey.CALL___()," | 1137 + "x1___ = myKey.CALL___()," |
1028 + "___.setPub(x0___, x1___," | 1138 + "___.setPub(x0___, x1___," |
1029 + " ___.readPub(x0___, x1___) + 1);"); | 1139 + " ___.readPub(x0___, x1___) + 1);"); |
(...skipping 20 matching lines...) Expand all Loading... |
1050 Operator.ASSIGN_SUB, | 1160 Operator.ASSIGN_SUB, |
1051 Operator.ASSIGN_LSH, | 1161 Operator.ASSIGN_LSH, |
1052 Operator.ASSIGN_RSH, | 1162 Operator.ASSIGN_RSH, |
1053 Operator.ASSIGN_USH, | 1163 Operator.ASSIGN_USH, |
1054 Operator.ASSIGN_AND, | 1164 Operator.ASSIGN_AND, |
1055 Operator.ASSIGN_XOR, | 1165 Operator.ASSIGN_XOR, |
1056 Operator.ASSIGN_OR | 1166 Operator.ASSIGN_OR |
1057 ); | 1167 ); |
1058 for (Operator op : ops) { | 1168 for (Operator op : ops) { |
1059 checkSucceeds( | 1169 checkSucceeds( |
1060 "var x; x " + op.getSymbol() + " g[0];", | 1170 "var x, y; x " + op.getSymbol() + " g[y];", |
1061 weldPrelude("g") | 1171 weldPrelude("g") |
1062 + "var x;" | 1172 + "var x, y;" |
1063 + "x = x " + op.getAssignmentDelegate().getSymbol() | 1173 + "x = x " + op.getAssignmentDelegate().getSymbol() |
1064 + "___.readPub(g, 0);"); | 1174 + "___.readPub(g, y);"); |
1065 } | 1175 } |
1066 } | 1176 } |
1067 | 1177 |
1068 public void testSetIncrDecr() throws Exception { | 1178 public void testSetIncrDecr() throws Exception { |
1069 checkFails("x__--;", ""); | 1179 checkFails("x__--;", ""); |
1070 checkSucceeds( | 1180 checkSucceeds( |
1071 "g[0]++;", | 1181 "g[i]++;", |
1072 weldPrelude("g") + | 1182 weldPreludes("g", "i") + |
| 1183 "var x0___;" + |
| 1184 "var x1___;" + |
| 1185 "var x2___;" + |
| 1186 "x0___ = g," + |
| 1187 "x1___ = i," + |
| 1188 "x2___ = +___.readPub(x0___, x1___)," + |
| 1189 "___.setPub(x0___, x1___, x2___ + 1)," + |
| 1190 "x2___;"); |
| 1191 checkSucceeds( |
| 1192 "g[i]--;", |
| 1193 weldPreludes("g", "i") + |
| 1194 "var x0___;" + |
| 1195 "var x1___;" + |
| 1196 "var x2___;" + |
| 1197 "x0___ = g," + |
| 1198 "x1___ = i," + |
| 1199 "x2___ = +___.readPub(x0___, x1___)," + |
| 1200 "___.setPub(x0___, x1___, x2___ - 1)," + |
| 1201 "x2___;"); |
| 1202 checkSucceeds( |
| 1203 "++g[i];", |
| 1204 weldPreludes("g", "i") + |
1073 "var x0___;" + | 1205 "var x0___;" + |
1074 "var x1___;" + | 1206 "var x1___;" + |
1075 "x0___ = g," + | 1207 "x0___ = g," + |
1076 "x1___ = +___.readPub(x0___, 0)," + | 1208 "x1___ = i," + |
1077 "___.setPub(x0___, 0, x1___ + 1)," + | 1209 "___.setPub(x0___, x1___, ___.readPub(x0___, x1___) - -1);"); |
1078 "x1___;"); | |
1079 checkSucceeds( | |
1080 "g[0]--;", | |
1081 weldPrelude("g") + | |
1082 "var x0___;" + | |
1083 "var x1___;" + | |
1084 "x0___ = g," + | |
1085 "x1___ = +___.readPub(x0___, 0)," + | |
1086 "___.setPub(x0___, 0, x1___ - 1)," + | |
1087 "x1___;"); | |
1088 checkSucceeds( | |
1089 "++g[0];", | |
1090 weldPrelude("g") + | |
1091 "var x0___;" + | |
1092 "x0___ = g," + | |
1093 "___.setPub(x0___, 0, ___.readPub(x0___, 0) - -1);"); | |
1094 | 1210 |
1095 assertConsistent( | 1211 assertConsistent( |
1096 "var x = 2;" + | 1212 "var x = 2;" + |
1097 "var arr = [--x, x, x--, x, ++x, x, x++, x];" + | 1213 "var arr = [--x, x, x--, x, ++x, x, x++, x];" + |
1098 "assertEquals('1,1,1,0,1,1,1,2', arr.join(','));" + | 1214 "assertEquals('1,1,1,0,1,1,1,2', arr.join(','));" + |
1099 "arr;"); | 1215 "arr;"); |
1100 assertConsistent( | 1216 assertConsistent( |
1101 "var x = '2';" + | 1217 "var x = '2';" + |
1102 "var arr = [--x, x, x--, x, ++x, x, x++, x];" + | 1218 "var arr = [--x, x, x--, x, ++x, x, x++, x];" + |
1103 "assertEquals('1,1,1,0,1,1,1,2', arr.join(','));" + | 1219 "assertEquals('1,1,1,0,1,1,1,2', arr.join(','));" + |
(...skipping 18 matching lines...) Expand all Loading... |
1122 | 1238 |
1123 public void testSetIncrDecrOfComplexLValues() throws Exception { | 1239 public void testSetIncrDecrOfComplexLValues() throws Exception { |
1124 checkFails("arr[x__]--;", "Variables cannot end in \"__\""); | 1240 checkFails("arr[x__]--;", "Variables cannot end in \"__\""); |
1125 checkFails("arr__[x]--;", "Variables cannot end in \"__\""); | 1241 checkFails("arr__[x]--;", "Variables cannot end in \"__\""); |
1126 | 1242 |
1127 checkSucceeds( | 1243 checkSucceeds( |
1128 "o.x++;", | 1244 "o.x++;", |
1129 weldPrelude("o") + | 1245 weldPrelude("o") + |
1130 "var x0___;" + | 1246 "var x0___;" + |
1131 "var x1___;" + | 1247 "var x1___;" + |
| 1248 "var x2___;" + |
1132 "x0___ = o," + | 1249 "x0___ = o," + |
1133 "x1___ = +___.readPub(x0___, 'x')," + | 1250 "x1___ = +(x0___.x_canRead___ ? x0___.x : ___.readPub(x0___, 'x'))," + |
1134 "___.setPub(x0___, 'x', x1___ + 1)," + | 1251 "(x2___ = x1___ + 1," + |
| 1252 " x0___.x_canSet___" + |
| 1253 " ? (x0___.x = x2___)" + |
| 1254 " : ___.setPub(x0___, 'x', x2___))," + |
1135 "x1___;"); | 1255 "x1___;"); |
1136 | 1256 |
1137 assertConsistent( | 1257 assertConsistent( |
1138 "(function () {" + | 1258 "(function () {" + |
1139 " var o = { x: 2 };" + | 1259 " var o = { x: 2 };" + |
1140 " var arr = [--o.x, o.x, o.x--, o.x, ++o.x, o.x, o.x++, o.x];" + | 1260 " var arr = [--o.x, o.x, o.x--, o.x, ++o.x, o.x, o.x++, o.x];" + |
1141 " assertEquals('1,1,1,0,1,1,1,2', arr.join(','));" + | 1261 " assertEquals('1,1,1,0,1,1,1,2', arr.join(','));" + |
1142 " return arr;" + | 1262 " return arr;" + |
1143 "})();"); | 1263 "})();"); |
1144 } | 1264 } |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1183 checkSucceeds( | 1303 checkSucceeds( |
1184 "(new Date);", | 1304 "(new Date);", |
1185 weldPrelude("Date", "{}") | 1305 weldPrelude("Date", "{}") |
1186 + "___.construct(Date, []);"); | 1306 + "___.construct(Date, []);"); |
1187 } | 1307 } |
1188 | 1308 |
1189 public void testDeletePub() throws Exception { | 1309 public void testDeletePub() throws Exception { |
1190 checkFails("delete x.foo___;", "Properties cannot end in \"__\""); | 1310 checkFails("delete x.foo___;", "Properties cannot end in \"__\""); |
1191 checkSucceeds( | 1311 checkSucceeds( |
1192 "delete foo()[bar()];", | 1312 "delete foo()[bar()];", |
1193 weldPrelude("bar") + | 1313 weldPreludes("bar", "foo") + |
1194 weldPrelude("foo") + | |
1195 "___.deletePub(foo.CALL___()," + | 1314 "___.deletePub(foo.CALL___()," + |
1196 " bar.CALL___());"); | 1315 " bar.CALL___());"); |
1197 checkSucceeds( | 1316 checkSucceeds( |
1198 "delete foo().bar;", | 1317 "delete foo().bar;", |
1199 weldPrelude("foo") + | 1318 weldPrelude("foo") + |
1200 "___.deletePub(foo.CALL___(), 'bar');"); | 1319 "___.deletePub(foo.CALL___(), 'bar');"); |
1201 assertConsistent( | 1320 assertConsistent( |
1202 "(function() {" + | 1321 "(function() {" + |
1203 " var o = { x: 3, y: 4 };" + // A JSON object. | 1322 " var o = { x: 3, y: 4 };" + // A JSON object. |
1204 " function ptStr(o) { return '(' + o.x + ',' + o.y + ')'; }" + | 1323 " function ptStr(o) { return '(' + o.x + ',' + o.y + ')'; }" + |
(...skipping 25 matching lines...) Expand all Loading... |
1230 "}" + | 1349 "}" + |
1231 "status;"); | 1350 "status;"); |
1232 } | 1351 } |
1233 | 1352 |
1234 public void testDeleteNonLvalue() throws Exception { | 1353 public void testDeleteNonLvalue() throws Exception { |
1235 checkFails("delete 4;", "Invalid operand to delete"); | 1354 checkFails("delete 4;", "Invalid operand to delete"); |
1236 } | 1355 } |
1237 | 1356 |
1238 public void testCallPublic() throws Exception { | 1357 public void testCallPublic() throws Exception { |
1239 checkSucceeds( | 1358 checkSucceeds( |
1240 "g[0].m(g[1], g[2]);", | 1359 "g[i + 0].m(g[i + 1], g[i + 2]);", |
1241 weldPrelude("g") + | 1360 weldPreludes("g", "i") + |
1242 "var x0___;" + | 1361 "var x0___;" + |
1243 "var x1___;" + | 1362 "var x1___;" + |
1244 "var x2___;" + | 1363 "var x2___;" + |
1245 "x0___ = ___.readPub(g, 0)," + | 1364 "x0___ = ___.readPub(g, i + 0)," + |
1246 "x1___ = ___.readPub(g, 1), x2___ = ___.readPub(g, 2)," + | 1365 "x1___ = ___.readPub(g, i + 1)," + |
| 1366 "x2___ = ___.readPub(g, i + 2)," + |
1247 "x0___.m_canCall___ ?" + | 1367 "x0___.m_canCall___ ?" + |
1248 " x0___.m(x1___, x2___) :" + | 1368 " x0___.m(x1___, x2___) :" + |
1249 " ___.callPub(x0___, 'm', [x1___, x2___]);"); | 1369 " ___.callPub(x0___, 'm', [x1___, x2___]);"); |
1250 } | 1370 } |
1251 | 1371 |
1252 public void testCallIndexPublic() throws Exception { | 1372 public void testCallIndexPublic() throws Exception { |
1253 checkSucceeds( | 1373 checkSucceeds( |
1254 "g[0][g[1]](g[2], g[3]);", | 1374 "g[i + 0][g[i + 1]](g[i + 2], g[i + 3]);", |
1255 weldPrelude("g") + | 1375 weldPreludes("g", "i") + |
1256 "___.callPub(" + | 1376 "___.callPub(" + |
1257 " ___.readPub(g, 0)," + | 1377 " ___.readPub(g, i + 0)," + |
1258 " ___.readPub(g, 1)," + | 1378 " ___.readPub(g, i + 1)," + |
1259 " [___.readPub(g, 2), ___.readPub(g, 3)]);"); | 1379 " [___.readPub(g, i + 2), ___.readPub(g, i + 3)]);"); |
1260 } | 1380 } |
1261 | 1381 |
1262 public void testCallFunc() throws Exception { | 1382 public void testCallFunc() throws Exception { |
1263 checkSucceeds( | 1383 checkSucceeds( |
1264 "g(g[1], g[2]);", | 1384 "g(g[i + 1], g[i + 2]);", |
1265 weldPrelude("g") + | 1385 weldPreludes("g", "i") + |
1266 "g.CALL___" + | 1386 "g.CALL___" + |
1267 " (___.readPub(g, 1), ___.readPub(g, 2));"); | 1387 " (___.readPub(g, i + 1), ___.readPub(g, i + 2));"); |
1268 } | 1388 } |
1269 | 1389 |
1270 public void testPermittedCall() throws Exception { | 1390 public void testPermittedCall() throws Exception { |
1271 // TODO(ihab.awad): Once permit templates can be loaded dynamically, create | 1391 // TODO(ihab.awad): Once permit templates can be loaded dynamically, create |
1272 // one here for testing rather than rely on the Valija permits. | 1392 // one here for testing rather than rely on the Valija permits. |
1273 String fixture = | 1393 String fixture = |
1274 " var x = 0;" | 1394 " var x = 0;" |
1275 + "var callPubCalled = false;" | 1395 + "var callPubCalled = false;" |
1276 + "var origCallPub = ___.callPub;" | 1396 + "var origCallPub = ___.callPub;" |
1277 + "___.callPub = function(obj, name, args) {" | 1397 + "___.callPub = function(obj, name, args) {" |
(...skipping 15 matching lines...) Expand all Loading... |
1293 + " $v.dis(42);" | 1413 + " $v.dis(42);" |
1294 + "})();", | 1414 + "})();", |
1295 " assertTrue(callPubCalled);" | 1415 " assertTrue(callPubCalled);" |
1296 + "assertEquals(0, x);"); | 1416 + "assertEquals(0, x);"); |
1297 } | 1417 } |
1298 | 1418 |
1299 public void testFuncAnonSimple() throws Exception { | 1419 public void testFuncAnonSimple() throws Exception { |
1300 // TODO(ihab.awad): The below test is not as complete as it should be | 1420 // TODO(ihab.awad): The below test is not as complete as it should be |
1301 // since it does not test the "@stmts*" substitution in the rule | 1421 // since it does not test the "@stmts*" substitution in the rule |
1302 checkSucceeds( | 1422 checkSucceeds( |
1303 "function(x, y) { x = arguments; y = g[0]; };", | 1423 "function(x, y, i) { x = arguments; y = g[i]; };", |
1304 weldPrelude("g") + | 1424 weldPrelude("g") + |
1305 "___.frozenFunc(function(x, y) {" + | 1425 "___.frozenFunc(function(x, y, i) {" + |
1306 " var a___ = ___.args(arguments);" + | 1426 " var a___ = ___.args(arguments);" + |
1307 " x = a___;" + | 1427 " x = a___;" + |
1308 " y = ___.readPub(g, 0);" + | 1428 " y = ___.readPub(g, i);" + |
1309 "});"); | 1429 "});"); |
1310 rewriteAndExecute( | 1430 rewriteAndExecute( |
1311 "(function () {" + | 1431 "(function () {" + |
1312 " var foo = function () {};" + | 1432 " var foo = function () {};" + |
1313 " foo();" + | 1433 " foo();" + |
1314 " try {" + | 1434 " try {" + |
1315 " foo.x = 3;" + | 1435 " foo.x = 3;" + |
1316 " } catch (e) { return; }" + | 1436 " } catch (e) { return; }" + |
1317 " fail('mutate frozen function');" + | 1437 " fail('mutate frozen function');" + |
1318 "})();"); | 1438 "})();"); |
(...skipping 20 matching lines...) Expand all Loading... |
1339 rewriteAndExecute( | 1459 rewriteAndExecute( |
1340 " assertThrows(function() {" | 1460 " assertThrows(function() {" |
1341 + " var foo = function() {};" | 1461 + " var foo = function() {};" |
1342 + " foo.x = 3;" | 1462 + " foo.x = 3;" |
1343 + "});"); | 1463 + "});"); |
1344 rewriteAndExecute( | 1464 rewriteAndExecute( |
1345 " function foo() {}" | 1465 " function foo() {}" |
1346 + "foo.x = 3;"); | 1466 + "foo.x = 3;"); |
1347 checkSucceeds( | 1467 checkSucceeds( |
1348 "function() {" + | 1468 "function() {" + |
1349 " function foo(x, y) {" + | 1469 " function foo(x, y, i) {" + |
1350 " x = arguments;" + | 1470 " x = arguments;" + |
1351 " y = g[0];" + | 1471 " y = g[i];" + |
1352 " return foo(x - 1, y - 1);" + | 1472 " return foo(x - 1, y - 1);" + |
1353 " }" + | 1473 " }" + |
1354 "};", | 1474 "};", |
1355 weldPrelude("g") + | 1475 weldPrelude("g") + |
1356 "___.frozenFunc(function() {" + | 1476 "___.frozenFunc(function() {" + |
1357 " function foo(x, y) {\n" + | 1477 " function foo(x, y, i) {\n" + |
1358 " var a___ = ___.args(arguments);\n" + | 1478 " var a___ = ___.args(arguments);\n" + |
1359 " x = a___;\n" + | 1479 " x = a___;\n" + |
1360 " y = ___.readPub(g, 0);\n" + | 1480 " y = ___.readPub(g, i);\n" + |
1361 " return foo.CALL___(x - 1, y - 1);\n" + | 1481 " return foo.CALL___(x - 1, y - 1);\n" + |
1362 " }\n" + | 1482 " }\n" + |
1363 " ___.func(foo, \'foo\');" + | 1483 " ___.func(foo, \'foo\');" + |
1364 " ;"+ | 1484 " ;"+ |
1365 "});"); | 1485 "});"); |
1366 checkSucceeds( | 1486 checkSucceeds( |
1367 "function foo(x, y ) {" + | 1487 "function foo(x, y ) {" + |
1368 " return foo(x - 1, y - 1);" + | 1488 " return foo(x - 1, y - 1);" + |
1369 "}", | 1489 "}", |
1370 " function foo(x, y) {\n" + | 1490 " function foo(x, y) {\n" + |
1371 " return foo.CALL___(x - 1, y - 1);\n" + | 1491 " return foo.CALL___(x - 1, y - 1);\n" + |
1372 " }\n" + | 1492 " }\n" + |
1373 " ___.func(foo, \'foo\');" + | 1493 " ___.func(foo, \'foo\');" + |
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1470 // causes an exception. | 1590 // causes an exception. |
1471 rewriteAndExecute( | 1591 rewriteAndExecute( |
1472 "testImports.f = function() {};", | 1592 "testImports.f = function() {};", |
1473 "assertThrows(function(){({ isPrototypeOf : f });});", | 1593 "assertThrows(function(){({ isPrototypeOf : f });});", |
1474 ";"); | 1594 ";"); |
1475 } | 1595 } |
1476 | 1596 |
1477 public void testOtherInstanceof() throws Exception { | 1597 public void testOtherInstanceof() throws Exception { |
1478 checkSucceeds( | 1598 checkSucceeds( |
1479 "function foo() {}" + | 1599 "function foo() {}" + |
1480 "g[0] instanceof foo;", | 1600 "g[void 0] instanceof foo;", |
1481 weldPrelude("g") + | 1601 weldPrelude("g") + |
1482 "function foo() {\n" + | 1602 "function foo() {\n" + |
1483 " }\n" + | 1603 "}\n" + |
1484 " ___.func(foo, \'foo\');" + | 1604 "___.func(foo, \'foo\');" + |
1485 ";" + | 1605 ";" + |
1486 "___.readPub(g, 0) instanceof ___.primFreeze(foo);"); | 1606 "___.readPub(g, void 0) instanceof ___.primFreeze(foo);"); |
1487 checkSucceeds( | 1607 checkSucceeds( |
1488 "g[0] instanceof Object;", | 1608 "g[i] instanceof Object;", |
1489 weldPrelude("Object") + | 1609 weldPreludes("Object", "g", "i") + |
1490 weldPrelude("g") + | 1610 "___.readPub(g, i) instanceof Object;"); |
1491 "___.readPub(g, 0) instanceof Object;"); | |
1492 | 1611 |
1493 assertConsistent("[ (({}) instanceof Object)," + | 1612 assertConsistent("[ (({}) instanceof Object)," + |
1494 " ((new Date) instanceof Date)," + | 1613 " ((new Date) instanceof Date)," + |
1495 " (({}) instanceof Date)" + | 1614 " (({}) instanceof Date)" + |
1496 "];"); | 1615 "];"); |
1497 assertConsistent("function foo() {}; (new foo) instanceof foo;"); | 1616 assertConsistent("function foo() {}; (new foo) instanceof foo;"); |
1498 assertConsistent("function foo() {}; !(({}) instanceof foo);"); | 1617 assertConsistent("function foo() {}; !(({}) instanceof foo);"); |
1499 } | 1618 } |
1500 | 1619 |
1501 public void testOtherTypeof() throws Exception { | 1620 public void testOtherTypeof() throws Exception { |
1502 checkSucceeds( | 1621 checkSucceeds( |
1503 "typeof g[0];", | 1622 "typeof g[i];", |
1504 weldPrelude("g") + | 1623 weldPreludes("g", "i") + |
1505 "___.typeOf(___.readPub(g, 0));"); | 1624 "___.typeOf(___.readPub(g, i));"); |
1506 checkFails("typeof ___;", "Variables cannot end in \"__\""); | 1625 checkFails("typeof ___;", "Variables cannot end in \"__\""); |
1507 } | 1626 } |
1508 | 1627 |
1509 public void testLabeledStatement() throws Exception { | 1628 public void testLabeledStatement() throws Exception { |
1510 checkFails("IMPORTS___: 1;", "Labels cannot end in \"__\""); | 1629 checkFails("IMPORTS___: 1;", "Labels cannot end in \"__\""); |
1511 checkFails("IMPORTS___: while (1);", "Labels cannot end in \"__\""); | 1630 checkFails("IMPORTS___: while (1);", "Labels cannot end in \"__\""); |
1512 checkSucceeds("foo: 1;", "foo: 1;"); | 1631 checkSucceeds("foo: 1;", "foo: 1;"); |
1513 checkFails("while (1) { break x__; }", "Labels cannot end in \"__\""); | 1632 checkFails("while (1) { break x__; }", "Labels cannot end in \"__\""); |
1514 checkSucceeds("break foo;", "break foo;"); | 1633 checkSucceeds("break foo;", "break foo;"); |
1515 checkFails("while (1) { continue x__; }", "Labels cannot end in \"__\""); | 1634 checkFails("while (1) { continue x__; }", "Labels cannot end in \"__\""); |
(...skipping 23 matching lines...) Expand all Loading... |
1539 checkAddsMessage( | 1658 checkAddsMessage( |
1540 js(fromString("var y = /x/;")), | 1659 js(fromString("var y = /x/;")), |
1541 RewriterMessageType.REGEX_LITERALS_NOT_IN_CAJITA); | 1660 RewriterMessageType.REGEX_LITERALS_NOT_IN_CAJITA); |
1542 } | 1661 } |
1543 | 1662 |
1544 public void testOtherSpecialOp() throws Exception { | 1663 public void testOtherSpecialOp() throws Exception { |
1545 checkSucceeds("void 0;", "void 0;"); | 1664 checkSucceeds("void 0;", "void 0;"); |
1546 checkSucceeds("void g();", | 1665 checkSucceeds("void g();", |
1547 weldPrelude("g") + | 1666 weldPrelude("g") + |
1548 "void g.CALL___();"); | 1667 "void g.CALL___();"); |
1549 checkSucceeds("g[0], g[1];", | 1668 checkSucceeds("g[i], g[1];", |
1550 weldPrelude("g") + | 1669 weldPreludes("g", "i") + |
1551 "___.readPub(g, 0), ___.readPub(g, 1);"); | 1670 "___.readPub(g, i), g[1];"); |
1552 } | 1671 } |
1553 | 1672 |
1554 public void testMultiDeclaration2() throws Exception { | 1673 public void testMultiDeclaration2() throws Exception { |
1555 // 'var' in global scope, part of a block | 1674 // 'var' in global scope, part of a block |
1556 checkSucceeds( | 1675 checkSucceeds( |
1557 "var x, y;", | 1676 "var x, y;", |
1558 "var x, y;"); | 1677 "var x, y;"); |
1559 checkSucceeds( | 1678 checkSucceeds( |
1560 "var x = g[0], y = g[1];", | 1679 "var x = g[0], y = g[x];", |
1561 weldPrelude("g") + | 1680 weldPrelude("g") + |
1562 "var x = ___.readPub(g, 0), y = ___.readPub(g, 1);"); | 1681 "var x = g[0], y = ___.readPub(g, x);"); |
1563 checkSucceeds( | 1682 checkSucceeds( |
1564 "var x, y = g[0];", | 1683 "var x, y = g[0];", |
1565 weldPrelude("g") + | 1684 weldPrelude("g") + |
1566 "var x, y = ___.readPub(g, 0);"); | 1685 "var x, y = g[0];"); |
1567 // 'var' in global scope, 'for' statement | 1686 // 'var' in global scope, 'for' statement |
1568 checkSucceeds( | 1687 checkSucceeds( |
1569 "for (var x, y; ; ) {}", | 1688 "for (var x, y; ; ) {}", |
1570 "for (var x, y; ; ) {}"); | 1689 "for (var x, y; ; ) {}"); |
1571 checkSucceeds( | 1690 checkSucceeds( |
1572 "for (var x = g[0], y = g[1]; ; ) {}", | 1691 "for (var x = g[i], y = g[x]; ; ) {}", |
1573 weldPrelude("g") + | 1692 weldPreludes("g", "i") + |
1574 "for (var x = ___.readPub(g, 0), y = ___.readPub(g, 1); ; ) {}"); | 1693 "for (var x = ___.readPub(g, i), y = ___.readPub(g, x); ; ) {}"); |
1575 checkSucceeds( | 1694 checkSucceeds( |
1576 "for (var x, y = g[0]; ; ) {}", | 1695 "for (var x, y = g[i]; ; ) {}", |
1577 weldPrelude("g") + | 1696 weldPreludes("g", "i") + |
1578 "for (var x, y = ___.readPub(g, 0); ; ) {}"); | 1697 "for (var x, y = ___.readPub(g, i); ; ) {}"); |
1579 // 'var' in global scope, part of a block | 1698 // 'var' in global scope, part of a block |
1580 checkSucceeds( | 1699 checkSucceeds( |
1581 "function() {" + | 1700 "function() {" + |
1582 " var x, y;" + | 1701 " var x, y;" + |
1583 "};", | 1702 "};", |
1584 "___.frozenFunc(function() {" + | 1703 "___.frozenFunc(function() {" + |
1585 " var x, y;" + | 1704 " var x, y;" + |
1586 "});"); | 1705 "});"); |
1587 checkSucceeds( | 1706 checkSucceeds( |
1588 "function() {" + | 1707 "function (i) {" + |
1589 " var x = g[0], y = g[1];" + | 1708 " var x = g[i], y = g[i + 1];" + |
1590 "};", | 1709 "};", |
1591 weldPrelude("g") + | 1710 weldPrelude("g") + |
1592 "___.frozenFunc(function() {" + | 1711 "___.frozenFunc(function (i) {" + |
1593 " var x = ___.readPub(g, 0), y = ___.readPub(g, 1);" + | 1712 " var x = ___.readPub(g, i), y = ___.readPub(g, i + 1);" + |
1594 "});"); | 1713 "});"); |
1595 checkSucceeds( | 1714 checkSucceeds( |
1596 "function() {" + | 1715 "function (i) {" + |
1597 " var x, y = g[0];" + | 1716 " var x, y = g[i];" + |
1598 "};", | 1717 "};", |
1599 weldPrelude("g") + | 1718 weldPrelude("g") + |
1600 "___.frozenFunc(function() {" + | 1719 "___.frozenFunc(function (i) {" + |
1601 " var x, y = ___.readPub(g, 0);" + | 1720 " var x, y = ___.readPub(g, i);" + |
1602 "});"); | 1721 "});"); |
1603 // 'var' in global scope, 'for' statement | 1722 // 'var' in global scope, 'for' statement |
1604 checkSucceeds( | 1723 checkSucceeds( |
1605 "function() {" + | 1724 "function() {" + |
1606 " for (var x, y; ; ) {}" + | 1725 " for (var x, y; ; ) {}" + |
1607 "};", | 1726 "};", |
1608 "___.frozenFunc(function() {" + | 1727 "___.frozenFunc(function() {" + |
1609 " for (var x, y; ; ) {}" + | 1728 " for (var x, y; ; ) {}" + |
1610 "});"); | 1729 "});"); |
1611 checkSucceeds( | 1730 checkSucceeds( |
1612 "function() {" + | 1731 "function (a, b) {" + |
1613 " for (var x = g[0], y = g[1]; ; ) {}" + | 1732 " for (var x = g[a], y = g[b]; ; ) {}" + |
1614 "};", | 1733 "};", |
1615 weldPrelude("g") + | 1734 weldPrelude("g") + |
1616 "___.frozenFunc(function() {" + | 1735 "___.frozenFunc(function (a, b) {" + |
1617 " for (var x = ___.readPub(g, 0), " + | 1736 " for (var x = ___.readPub(g, a), " + |
1618 " y = ___.readPub(g, 1); ; ) {}" + | 1737 " y = ___.readPub(g, b); ; ) {}" + |
1619 "});"); | 1738 "});"); |
1620 checkSucceeds( | 1739 checkSucceeds( |
1621 "function() {" + | 1740 "function (i) {" + |
1622 " for (var x, y = g[0]; ; ) {}" + | 1741 " for (var x, y = g[i]; ; ) {}" + |
1623 "};", | 1742 "};", |
1624 weldPrelude("g") + | 1743 weldPrelude("g") + |
1625 "___.frozenFunc(function() {" + | 1744 "___.frozenFunc(function (i) {" + |
1626 " for (var x, y = ___.readPub(g, 0); ; ) {}" + | 1745 " for (var x, y = ___.readPub(g, i); ; ) {}" + |
1627 "});"); | 1746 "});"); |
1628 assertConsistent( | 1747 assertConsistent( |
1629 "var arr = [1, 2, 3], k = -1;" + | 1748 "var arr = [1, 2, 3], k = -1;" + |
1630 "(function () {" + | 1749 "(function () {" + |
1631 " var a = arr[++k], b = arr[++k], c = arr[++k];" + | 1750 " var a = arr[++k], b = arr[++k], c = arr[++k];" + |
1632 " return [a, b, c];" + | 1751 " return [a, b, c];" + |
1633 "})();"); | 1752 "})();"); |
1634 // Check exceptions on read of uninitialized variables. | 1753 // Check exceptions on read of uninitialized variables. |
1635 assertConsistent( | 1754 assertConsistent( |
1636 "(function () {" + | 1755 "(function () {" + |
1637 " var a = [];" + | 1756 " var a = [];" + |
1638 " for (var i = 0, j = 10; i < j; ++i) { a.push(i); }" + | 1757 " for (var i = 0, j = 10; i < j; ++i) { a.push(i); }" + |
1639 " return a;" + | 1758 " return a;" + |
1640 "})();"); | 1759 "})();"); |
1641 assertConsistent( | 1760 assertConsistent( |
1642 "var a = [];" + | 1761 "var a = [];" + |
1643 "for (var i = 0, j = 10; i < j; ++i) { a.push(i); }" + | 1762 "for (var i = 0, j = 10; i < j; ++i) { a.push(i); }" + |
1644 "a;"); | 1763 "a;"); |
1645 } | 1764 } |
1646 | 1765 |
1647 public void testRecurseParseTreeNodeContainer() throws Exception { | 1766 public void testRecurseParseTreeNodeContainer() throws Exception { |
1648 // Tested implicitly by other cases | 1767 // Tested implicitly by other cases |
1649 } | 1768 } |
1650 | 1769 |
1651 public void testRecurseArrayConstructor() throws Exception { | 1770 public void testRecurseArrayConstructor() throws Exception { |
1652 checkSucceeds( | 1771 checkSucceeds( |
1653 "var foo = [ g[0], g[1] ];", | 1772 "var foo = [ g[a], g[b] ];", |
1654 weldPrelude("g") + | 1773 weldPreludes("a", "b", "g") + |
1655 "var foo = [___.readPub(g, 0), ___.readPub(g, 1)];"); | 1774 "var foo = [___.readPub(g, a), ___.readPub(g, b)];"); |
1656 } | 1775 } |
1657 | 1776 |
1658 public void testRecurseBlock() throws Exception { | 1777 public void testRecurseBlock() throws Exception { |
1659 // Tested implicitly by other cases | 1778 // Tested implicitly by other cases |
1660 } | 1779 } |
1661 | 1780 |
1662 public void testRecurseBreakStmt() throws Exception { | 1781 public void testRecurseBreakStmt() throws Exception { |
1663 checkSucceeds( | 1782 checkSucceeds( |
1664 "while (true) { break; }", | 1783 "while (true) { break; }", |
1665 "while (true) { break; }"); | 1784 "while (true) { break; }"); |
1666 } | 1785 } |
1667 | 1786 |
1668 public void testRecurseCaseStmt() throws Exception { | 1787 public void testRecurseCaseStmt() throws Exception { |
1669 checkSucceeds( | 1788 checkSucceeds( |
1670 "switch (g[0]) { case 1: break; }", | 1789 "switch (g[i]) { case 1: break; }", |
1671 weldPrelude("g") + | 1790 weldPreludes("g", "i") + |
1672 "switch (___.readPub(g, 0)) { case 1: break; }"); | 1791 "switch (___.readPub(g, i)) { case 1: break; }"); |
1673 } | 1792 } |
1674 | 1793 |
1675 public void testRecurseConditional() throws Exception { | 1794 public void testRecurseConditional() throws Exception { |
1676 checkSucceeds( | 1795 checkSucceeds( |
1677 "if (g[0] === g[1]) {" + | 1796 "if (g[i] === g[i + 1]) {" + |
1678 " g[2];" + | 1797 " g[i + 2];" + |
1679 "} else if (g[3] === g[4]) {" + | 1798 "} else if (g[i + 3] === g[i + 4]) {" + |
1680 " g[5];" + | 1799 " g[i + 5];" + |
1681 "} else {" + | 1800 "} else {" + |
1682 " g[6];" + | 1801 " g[i + 6];" + |
| 1802 "}", |
| 1803 weldPreludes("g", "i") + |
| 1804 "if (___.readPub(g, i) === ___.readPub(g, i + 1)) {" + |
| 1805 " ___.readPub(g, i + 2);" + |
| 1806 "} else if (___.readPub(g, i + 3) === ___.readPub(g, i + 4)) {" + |
| 1807 " ___.readPub(g, i + 5);" + |
| 1808 "} else {" + |
| 1809 " ___.readPub(g, i + 6);" + |
| 1810 "}"); |
| 1811 } |
| 1812 |
| 1813 public void testRecurseContinueStmt() throws Exception { |
| 1814 checkSucceeds( |
| 1815 "while (true) { continue; }", |
| 1816 "while (true) { continue; }"); |
| 1817 } |
| 1818 |
| 1819 public void testRecurseDebuggerStmt() throws Exception { |
| 1820 checkSucceeds("debugger;", "debugger;"); |
| 1821 } |
| 1822 |
| 1823 public void testRecurseDefaultCaseStmt() throws Exception { |
| 1824 checkSucceeds( |
| 1825 "switch (g[i]) { default: break; }", |
| 1826 weldPreludes("g", "i") + |
| 1827 "switch(___.readPub(g, i)) { default: break; }"); |
| 1828 } |
| 1829 |
| 1830 public void testRecurseExpressionStmt() throws Exception { |
| 1831 // Tested implicitly by other cases |
| 1832 } |
| 1833 |
| 1834 public void testRecurseIdentifier() throws Exception { |
| 1835 // Tested implicitly by other cases |
| 1836 } |
| 1837 |
| 1838 public void testRecurseLiteral() throws Exception { |
| 1839 checkSucceeds( |
| 1840 "3;", |
| 1841 "3;"); |
| 1842 } |
| 1843 |
| 1844 public void testRecurseLoop() throws Exception { |
| 1845 checkSucceeds( |
| 1846 "for (var i, k = 0; k < g[i]; k++) {" + |
| 1847 " g[i + 1];" + |
1683 "}", | 1848 "}", |
1684 weldPrelude("g") + | 1849 weldPrelude("g") + |
1685 "if (___.readPub(g, 0) === ___.readPub(g, 1)) {" + | 1850 "for (var i, k = 0; k < ___.readPub(g, i); k++) {" + |
1686 " ___.readPub(g, 2);" + | 1851 " ___.readPub(g, i + 1);" + |
1687 "} else if (___.readPub(g, 3) === ___.readPub(g, 4)) {" + | |
1688 " ___.readPub(g, 5);" + | |
1689 "} else {" + | |
1690 " ___.readPub(g, 6);" + | |
1691 "}"); | 1852 "}"); |
1692 } | 1853 checkSucceeds( |
1693 | 1854 "while (g[i]) { g[i + 1]; }", |
1694 public void testRecurseContinueStmt() throws Exception { | 1855 weldPreludes("g", "i") + |
1695 checkSucceeds( | 1856 "while (___.readPub(g, i)) { ___.readPub(g, i + 1); }"); |
1696 "while (true) { continue; }", | |
1697 "while (true) { continue; }"); | |
1698 } | |
1699 | |
1700 public void testRecurseDebuggerStmt() throws Exception { | |
1701 checkSucceeds("debugger;", "debugger;"); | |
1702 } | |
1703 | |
1704 public void testRecurseDefaultCaseStmt() throws Exception { | |
1705 checkSucceeds( | |
1706 "switch (g[0]) { default: break; }", | |
1707 weldPrelude("g") + | |
1708 "switch(___.readPub(g, 0)) { default: break; }"); | |
1709 } | |
1710 | |
1711 public void testRecurseExpressionStmt() throws Exception { | |
1712 // Tested implicitly by other cases | |
1713 } | |
1714 | |
1715 public void testRecurseIdentifier() throws Exception { | |
1716 // Tested implicitly by other cases | |
1717 } | |
1718 | |
1719 public void testRecurseLiteral() throws Exception { | |
1720 checkSucceeds( | |
1721 "3;", | |
1722 "3;"); | |
1723 } | |
1724 | |
1725 public void testRecurseLoop() throws Exception { | |
1726 checkSucceeds( | |
1727 "for (var k = 0; k < g[0]; k++) {" + | |
1728 " g[1];" + | |
1729 "}", | |
1730 weldPrelude("g") + | |
1731 "for (var k = 0; k < ___.readPub(g, 0); k++) {" + | |
1732 " ___.readPub(g, 1);" + | |
1733 "}"); | |
1734 checkSucceeds( | |
1735 "while (g[0]) { g[1]; }", | |
1736 weldPrelude("g") + | |
1737 "while (___.readPub(g, 0)) { ___.readPub(g, 1); }"); | |
1738 } | 1857 } |
1739 | 1858 |
1740 public void testRecurseNoop() throws Exception { | 1859 public void testRecurseNoop() throws Exception { |
1741 checkSucceeds( | 1860 checkSucceeds( |
1742 ";", | 1861 ";", |
1743 ";"); | 1862 ";"); |
1744 } | 1863 } |
1745 | 1864 |
1746 public void testRecurseOperation() throws Exception { | 1865 public void testRecurseOperation() throws Exception { |
1747 checkSucceeds( | 1866 checkSucceeds( |
1748 "g[0] + g[1];", | 1867 "g[i] + g[j];", |
1749 weldPrelude("g") + | 1868 weldPreludes("g", "i", "j") + |
1750 "___.readPub(g, 0) + ___.readPub(g, 1);"); | 1869 "___.readPub(g, i) + ___.readPub(g, j);"); |
1751 checkSucceeds( | 1870 checkSucceeds( |
1752 "1 + 2 * 3 / 4 - -5;", | 1871 "1 + 2 * 3 / 4 - -5;", |
1753 "1 + 2 * 3 / 4 - -5;"); | 1872 "1 + 2 * 3 / 4 - -5;"); |
1754 checkSucceeds( | 1873 checkSucceeds( |
1755 "var x, y;" + | 1874 "var x, y, z;" + |
1756 "x = y = g[0];", | 1875 "x = y = g[z];", |
1757 weldPrelude("g") + | 1876 weldPrelude("g") + |
1758 "var x, y;" + | 1877 "var x, y, z;" + |
1759 "x = y = ___.readPub(g, 0);"); | 1878 "x = y = ___.readPub(g, z);"); |
1760 } | 1879 } |
1761 | 1880 |
1762 public void testRecurseReturnStmt() throws Exception { | 1881 public void testRecurseReturnStmt() throws Exception { |
1763 checkSucceeds( | 1882 checkSucceeds( |
1764 "return g[0];", | 1883 "return g[i];", |
1765 weldPrelude("g") + | 1884 weldPreludes("g", "i") + |
1766 "return ___.readPub(g, 0);"); | 1885 "return ___.readPub(g, i);"); |
1767 } | 1886 } |
1768 | 1887 |
1769 public void testRecurseSwitchStmt() throws Exception { | 1888 public void testRecurseSwitchStmt() throws Exception { |
1770 checkSucceeds( | 1889 checkSucceeds( |
1771 "switch (g[0]) { }", | 1890 "switch (g[i]) { }", |
1772 weldPrelude("g") + | 1891 weldPreludes("g", "i") + |
1773 "switch (___.readPub(g, 0)) { }"); | 1892 "switch (___.readPub(g, i)) { }"); |
1774 } | 1893 } |
1775 | 1894 |
1776 public void testRecurseThrowStmt() throws Exception { | 1895 public void testRecurseThrowStmt() throws Exception { |
1777 checkSucceeds( | 1896 checkSucceeds( |
1778 "throw g[0];", | 1897 "throw g[i];", |
1779 weldPrelude("g") + | 1898 weldPreludes("g", "i") + |
1780 "throw ___.readPub(g, 0);"); | 1899 "throw ___.readPub(g, i);"); |
1781 checkSucceeds( | 1900 checkSucceeds( |
1782 "function() {" + | 1901 "function() {" + |
1783 " var x;" + | 1902 " var x;" + |
1784 " throw x;" + | 1903 " throw x;" + |
1785 "};", | 1904 "};", |
1786 "___.frozenFunc(function() {" + | 1905 "___.frozenFunc(function() {" + |
1787 " var x;" + | 1906 " var x;" + |
1788 " throw x;" + | 1907 " throw x;" + |
1789 "});"); | 1908 "});"); |
1790 } | 1909 } |
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1887 */ | 2006 */ |
1888 public void testFrozenObject() throws Exception { | 2007 public void testFrozenObject() throws Exception { |
1889 rewriteAndExecute( | 2008 rewriteAndExecute( |
1890 "var success = false;" + | 2009 "var success = false;" + |
1891 "try {" + | 2010 "try {" + |
1892 "Object.prototype.x = 'X';" + | 2011 "Object.prototype.x = 'X';" + |
1893 "} catch (e){" + | 2012 "} catch (e){" + |
1894 "success = true;" + | 2013 "success = true;" + |
1895 "}" + | 2014 "}" + |
1896 "if (!success) fail('Object not frozen.');"); | 2015 "if (!success) fail('Object not frozen.');"); |
1897 } | |
1898 | |
1899 /** | |
1900 * Tests that cajoled code can't construct new Function objects. | |
1901 */ | |
1902 public void testFunction() throws Exception { | |
1903 rewriteAndExecute( | |
1904 "var success=false;" + | |
1905 "try{var f=new Function('1');}catch(e){success=true;}" + | |
1906 "if (!success)fail('Function constructor is accessible.')"); | |
1907 } | |
1908 | |
1909 /** | |
1910 * Tests that constructors are inaccessible. | |
1911 */ | |
1912 public void testConstructor() throws Exception { | |
1913 try { | |
1914 rewriteAndExecute( | |
1915 "function x(){}; var F = x.constructor;"); | |
1916 } catch (junit.framework.AssertionFailedError e) { | |
1917 // pass | |
1918 } | |
1919 } | 2016 } |
1920 | 2017 |
1921 public void testStamp() throws Exception { | 2018 public void testStamp() throws Exception { |
1922 rewriteAndExecute( | 2019 rewriteAndExecute( |
1923 "___.getNewModuleHandler().getImports().stamp =" + | 2020 "___.getNewModuleHandler().getImports().stamp =" + |
1924 " ___.frozenFunc(___.stamp);" + | 2021 " ___.frozenFunc(___.stamp);" + |
1925 "___.grantRead(___.getNewModuleHandler().getImports(), 'stamp');", | 2022 "___.grantRead(___.getNewModuleHandler().getImports(), 'stamp');", |
1926 "function Foo(){}" + | 2023 "function Foo(){}" + |
1927 "var foo = new Foo;" + | 2024 "var foo = new Foo;" + |
1928 "var passed = false;" + | 2025 "var passed = false;" + |
(...skipping 229 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2158 rewriteAndExecute( | 2255 rewriteAndExecute( |
2159 "var x;" + | 2256 "var x;" + |
2160 "try { x = toString; } catch (e) {}" + | 2257 "try { x = toString; } catch (e) {}" + |
2161 "if (x) { cajita.fail('Inherited global properties are readable'); }"); | 2258 "if (x) { cajita.fail('Inherited global properties are readable'); }"); |
2162 } | 2259 } |
2163 | 2260 |
2164 @Override | 2261 @Override |
2165 protected Object executePlain(String caja) throws IOException { | 2262 protected Object executePlain(String caja) throws IOException { |
2166 mq.getMessages().clear(); | 2263 mq.getMessages().clear(); |
2167 return RhinoTestBed.runJs( | 2264 return RhinoTestBed.runJs( |
| 2265 new RhinoTestBed.Input( |
| 2266 getClass(), "../../../../../js/json_sans_eval/json_sans_eval.js"), |
2168 new RhinoTestBed.Input(getClass(), "/com/google/caja/cajita.js"), | 2267 new RhinoTestBed.Input(getClass(), "/com/google/caja/cajita.js"), |
2169 new RhinoTestBed.Input( | 2268 new RhinoTestBed.Input( |
2170 getClass(), "../../../../../js/jsunit/2.2/jsUnitCore.js"), | 2269 getClass(), "../../../../../js/jsunit/2.2/jsUnitCore.js"), |
2171 new RhinoTestBed.Input(caja, getName() + "-uncajoled")); | 2270 new RhinoTestBed.Input(caja, getName() + "-uncajoled")); |
2172 } | 2271 } |
2173 | 2272 |
2174 @Override | 2273 @Override |
2175 protected Object rewriteAndExecute(String pre, String caja, String post) | 2274 protected Object rewriteAndExecute(String pre, String caja, String post) |
2176 throws IOException, ParseException { | 2275 throws IOException, ParseException { |
2177 mq.getMessages().clear(); | 2276 mq.getMessages().clear(); |
(...skipping 20 matching lines...) Expand all Loading... |
2198 for (String f : assertFunctions) { | 2297 for (String f : assertFunctions) { |
2199 importsSetup | 2298 importsSetup |
2200 .append("testImports." + f + " = ___.func(" + f + ");") | 2299 .append("testImports." + f + " = ___.func(" + f + ");") |
2201 .append("___.grantRead(testImports, '" + f + "');"); | 2300 .append("___.grantRead(testImports, '" + f + "');"); |
2202 } | 2301 } |
2203 importsSetup.append("___.getNewModuleHandler().setImports(testImports);"); | 2302 importsSetup.append("___.getNewModuleHandler().setImports(testImports);"); |
2204 | 2303 |
2205 Object result = RhinoTestBed.runJs( | 2304 Object result = RhinoTestBed.runJs( |
2206 new RhinoTestBed.Input( | 2305 new RhinoTestBed.Input( |
2207 getClass(), "/com/google/caja/plugin/console-stubs.js"), | 2306 getClass(), "/com/google/caja/plugin/console-stubs.js"), |
| 2307 new RhinoTestBed.Input( |
| 2308 getClass(), "../../../../../js/json_sans_eval/json_sans_eval.js"), |
2208 new RhinoTestBed.Input(getClass(), "/com/google/caja/cajita.js"), | 2309 new RhinoTestBed.Input(getClass(), "/com/google/caja/cajita.js"), |
2209 new RhinoTestBed.Input( | 2310 new RhinoTestBed.Input( |
2210 getClass(), "../../../../../js/jsunit/2.2/jsUnitCore.js"), | 2311 getClass(), "../../../../../js/jsunit/2.2/jsUnitCore.js"), |
2211 new RhinoTestBed.Input( | 2312 new RhinoTestBed.Input( |
2212 getClass(), "/com/google/caja/log-to-console.js"), | 2313 getClass(), "/com/google/caja/log-to-console.js"), |
2213 new RhinoTestBed.Input( | 2314 new RhinoTestBed.Input( |
2214 importsSetup.toString(), | 2315 importsSetup.toString(), |
2215 getName() + "-test-fixture"), | 2316 getName() + "-test-fixture"), |
2216 new RhinoTestBed.Input(pre, getName()), | 2317 new RhinoTestBed.Input(pre, getName()), |
2217 // Load the cajoled code. | 2318 // Load the cajoled code. |
2218 new RhinoTestBed.Input(cajoledJs, getName() + "-cajoled"), | 2319 new RhinoTestBed.Input(cajoledJs, getName() + "-cajoled"), |
2219 new RhinoTestBed.Input(post, getName()), | 2320 new RhinoTestBed.Input(post, getName()), |
2220 // Return the output field as the value of the run. | 2321 // Return the output field as the value of the run. |
2221 new RhinoTestBed.Input( | 2322 new RhinoTestBed.Input( |
2222 "___.getNewModuleHandler().getLastValue();", getName())); | 2323 "___.getNewModuleHandler().getLastValue();", getName())); |
2223 | 2324 |
2224 assertNoErrors(); | 2325 assertNoErrors(); |
2225 return result; | 2326 return result; |
2226 } | 2327 } |
2227 } | 2328 } |
LEFT | RIGHT |