15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /** 25 * JDK-8067215: Disable dual fields when not using optimistic types 26 * 27 * @test 28 * @run 29 * @option -Dnashorn.debug=true 30 * @fork 31 */ 32 33 var intType = Java.type("int"); 34 var doubleType = Java.type("double"); 35 var longType = Java.type("long"); 36 var objectType = Java.type("java.lang.Object"); 37 38 var Context = Java.type("jdk.nashorn.internal.runtime.Context"); 39 var JSType = Java.type("jdk.nashorn.internal.runtime.JSType"); 40 41 var context = Context.getContext(); 42 var dualFields = context.useDualFields(); 43 var optimisticTypes = context.getEnv()._optimistic_types; 44 45 if (dualFields != optimisticTypes) { 46 throw new Error("Wrong dual fields setting"); 47 } 48 49 function testMap(obj) { 50 obj.x = "foo"; 51 obj["y"] = 0; 52 Object.defineProperty(obj, "z", {value: 0.5}); 53 var map = Debug.map(obj); 54 for (var key in obj) { 55 var prop = map.findProperty(key); 56 if (prop.hasDualFields() !== dualFields) { 57 throw new Error("Wrong property flags: " + prop); 58 } 59 if (prop.getType() != getExpectedType(obj[key])) { 60 throw new Error("Wrong property type: " + prop.getType() + " // " + getExpectedType(obj[key])); 61 62 } 63 } 64 } 65 66 function getExpectedType(value) { 67 if (!dualFields) { 68 return objectType.class; 69 } 70 if (JSType.isRepresentableAsInt(value)) { 71 return intType.class; 72 } 73 if (JSType.isRepresentableAsLong(value)) { 74 return longType.class; 75 } 76 if (JSType.isNumber(value)) { 77 return doubleType.class; 78 } 79 return objectType.class; 80 } 81 82 var o = { 83 a: 1, 84 b: 2.5, 85 c: 0x10000000000, 86 d: true 87 }; 88 89 function C() { 90 this.a = 1; 91 this.b = 2.5; 92 this.c = 0x10000000000; 93 this.d = true; 94 } 95 96 var a = 1; 97 var b = 2.5; | 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /** 25 * JDK-8067215: Disable dual fields when not using optimistic types 26 * 27 * @test 28 * @run 29 * @option -Dnashorn.debug=true 30 * @fork 31 */ 32 33 var intType = Java.type("int"); 34 var doubleType = Java.type("double"); 35 var objectType = Java.type("java.lang.Object"); 36 37 var Context = Java.type("jdk.nashorn.internal.runtime.Context"); 38 var JSType = Java.type("jdk.nashorn.internal.runtime.JSType"); 39 40 var context = Context.getContext(); 41 var dualFields = context.useDualFields(); 42 var optimisticTypes = context.getEnv()._optimistic_types; 43 44 if (dualFields != optimisticTypes) { 45 throw new Error("Wrong dual fields setting"); 46 } 47 48 function testMap(obj) { 49 obj.x = "foo"; 50 obj["y"] = 0; 51 Object.defineProperty(obj, "z", {value: 0.5}); 52 var map = Debug.map(obj); 53 for (var key in obj) { 54 var prop = map.findProperty(key); 55 if (prop.hasDualFields() !== dualFields) { 56 throw new Error("Wrong property flags: " + prop); 57 } 58 if (prop.getType() != getExpectedType(obj[key])) { 59 throw new Error("Wrong property type: " + prop.getType() + " // " + getExpectedType(obj[key])); 60 } 61 } 62 } 63 64 function getExpectedType(value) { 65 if (!dualFields) { 66 return objectType.class; 67 } 68 if (typeof value === "number") { 69 return JSType.isRepresentableAsInt(value) ? intType.class : doubleType.class; 70 } 71 return objectType.class; 72 } 73 74 var o = { 75 a: 1, 76 b: 2.5, 77 c: 0x10000000000, 78 d: true 79 }; 80 81 function C() { 82 this.a = 1; 83 this.b = 2.5; 84 this.c = 0x10000000000; 85 this.d = true; 86 } 87 88 var a = 1; 89 var b = 2.5; |