8026955: for-in should convert primitive values to object

Tue, 22 Oct 2013 11:31:03 +0200

author
hannesw
date
Tue, 22 Oct 2013 11:31:03 +0200
changeset 641
d24a4fabdce1
parent 640
6d339d98074e
child 642
360761288b38

8026955: for-in should convert primitive values to object
Reviewed-by: jlaskey, lagergren

src/jdk/nashorn/internal/runtime/ScriptRuntime.java file | annotate | diff | comparison | revisions
test/script/basic/JDK-8026955.js file | annotate | diff | comparison | revisions
test/script/basic/JDK-8026955.js.EXPECTED file | annotate | diff | comparison | revisions
     1.1 --- a/src/jdk/nashorn/internal/runtime/ScriptRuntime.java	Tue Oct 22 11:12:36 2013 +0200
     1.2 +++ b/src/jdk/nashorn/internal/runtime/ScriptRuntime.java	Tue Oct 22 11:31:03 2013 +0200
     1.3 @@ -47,6 +47,7 @@
     1.4  import jdk.nashorn.api.scripting.ScriptObjectMirror;
     1.5  import jdk.nashorn.internal.codegen.CompilerConstants.Call;
     1.6  import jdk.nashorn.internal.ir.debug.JSONWriter;
     1.7 +import jdk.nashorn.internal.objects.Global;
     1.8  import jdk.nashorn.internal.parser.Lexer;
     1.9  import jdk.nashorn.internal.runtime.linker.Bootstrap;
    1.10  
    1.11 @@ -258,6 +259,11 @@
    1.12              return ((Map<?,?>)obj).keySet().iterator();
    1.13          }
    1.14  
    1.15 +        final Object wrapped = Global.instance().wrapAsObject(obj);
    1.16 +        if (wrapped instanceof ScriptObject) {
    1.17 +            return ((ScriptObject)wrapped).propertyIterator();
    1.18 +        }
    1.19 +
    1.20          return Collections.emptyIterator();
    1.21      }
    1.22  
    1.23 @@ -336,6 +342,11 @@
    1.24              return ((Iterable<?>)obj).iterator();
    1.25          }
    1.26  
    1.27 +        final Object wrapped = Global.instance().wrapAsObject(obj);
    1.28 +        if (wrapped instanceof ScriptObject) {
    1.29 +            return ((ScriptObject)wrapped).valueIterator();
    1.30 +        }
    1.31 +
    1.32          return Collections.emptyIterator();
    1.33      }
    1.34  
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/test/script/basic/JDK-8026955.js	Tue Oct 22 11:31:03 2013 +0200
     2.3 @@ -0,0 +1,51 @@
     2.4 +/*
     2.5 + * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
     2.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     2.7 + * 
     2.8 + * This code is free software; you can redistribute it and/or modify it
     2.9 + * under the terms of the GNU General Public License version 2 only, as
    2.10 + * published by the Free Software Foundation.
    2.11 + * 
    2.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    2.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    2.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    2.15 + * version 2 for more details (a copy is included in the LICENSE file that
    2.16 + * accompanied this code).
    2.17 + * 
    2.18 + * You should have received a copy of the GNU General Public License version
    2.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    2.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    2.21 + * 
    2.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    2.23 + * or visit www.oracle.com if you need additional information or have any
    2.24 + * questions.
    2.25 + */
    2.26 +
    2.27 +/**
    2.28 + * JDK-8026955: for-in should convert primitive values to object
    2.29 + *
    2.30 + * @test
    2.31 + * @run
    2.32 + */
    2.33 +
    2.34 +Object.prototype[4] = "world";
    2.35 +String.prototype[3] = "hello";
    2.36 +Number.prototype[3] = "hello";
    2.37 +Boolean.prototype[3] = "hello";
    2.38 +
    2.39 +function testForIn(x) {
    2.40 +    for (var i in x) {
    2.41 +        print(i, x[i]);
    2.42 +    }
    2.43 +    for each (var i in x) {
    2.44 +        print(i);
    2.45 +    }
    2.46 +}
    2.47 +
    2.48 +testForIn("abc");
    2.49 +testForIn(false);
    2.50 +testForIn(3);
    2.51 +testForIn(null);
    2.52 +testForIn();
    2.53 +testForIn(String.prototype);
    2.54 +
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/test/script/basic/JDK-8026955.js.EXPECTED	Tue Oct 22 11:31:03 2013 +0200
     3.3 @@ -0,0 +1,22 @@
     3.4 +0 a
     3.5 +1 b
     3.6 +2 c
     3.7 +3 hello
     3.8 +4 world
     3.9 +a
    3.10 +b
    3.11 +c
    3.12 +hello
    3.13 +world
    3.14 +3 hello
    3.15 +4 world
    3.16 +hello
    3.17 +world
    3.18 +3 hello
    3.19 +4 world
    3.20 +hello
    3.21 +world
    3.22 +3 hello
    3.23 +4 world
    3.24 +hello
    3.25 +world

mercurial