typecoerce.js revision 56:59970b70ebb5
125603Skjc/*
255205Speter * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
325603Skjc * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
425603Skjc *
525603Skjc * This code is free software; you can redistribute it and/or modify it
625603Skjc * under the terms of the GNU General Public License version 2 only, as
725603Skjc * published by the Free Software Foundation.
825603Skjc *
925603Skjc * This code is distributed in the hope that it will be useful, but WITHOUT
1025603Skjc * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1125603Skjc * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
1225603Skjc * version 2 for more details (a copy is included in the LICENSE file that
1325603Skjc * accompanied this code).
1425603Skjc *
1525603Skjc * You should have received a copy of the GNU General Public License version
1625603Skjc * 2 along with this work; if not, write to the Free Software Foundation,
1725603Skjc * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1825603Skjc *
1925603Skjc * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2025603Skjc * or visit www.oracle.com if you need additional information or have any
2125603Skjc * questions.
2225603Skjc */
2325603Skjc
2425603Skjc/**
2525603Skjc * There was a bug in the old Lower that didn't fully propagate type information
2625603Skjc * by way of assignment. 'q' in the example below would have finished a double
2725603Skjc * even though it can get an object value through the assignment 'q = l'
2825603Skjc *
2925603Skjc * Furthermore, this caused type coercion to be done at q = l, and not a q = q * 2,
3025603Skjc * which is a bug. This test ensures it happens in the correct order
3125603Skjc *
3225603Skjc * @test
3325603Skjc * @run
3425603Skjc */
3525603Skjc
3625603Skjcfunction createObject() {
3725603Skjc    var obj = { valueOf: function() { print("toNumber coercion"); return 17; }}
3825603Skjc    return obj;
3925603Skjc}
40114739Sharti
41114739Shartifunction f() {
42114739Sharti    var l = 1.2; //number
43114739Sharti    var q = 2.3; //number
44114739Sharti    for (var i = 0; i < 2; i++) {
45114739Sharti	q = l; // q = toNumber(l), no coercion here
46114739Sharti	print("assignment done");
47114739Sharti	q = q * 2; // q = q * 2, coercion here
48114739Sharti	print("multiplication done");
49116441Sharti	l = createObject();
50116441Sharti    }
51116441Sharti}
52116441Sharti
53114739Shartif();
54114739Sharti