8005848: assigning to global toString variable affects Object.prototype.toString

Tue, 08 Jan 2013 21:16:07 +0530

author
sundar
date
Tue, 08 Jan 2013 21:16:07 +0530
changeset 17
548587cfb065
parent 16
69a4f0363d0f
child 18
812b9963b1c7

8005848: assigning to global toString variable affects Object.prototype.toString
Reviewed-by: jlaskey, lagergren

src/jdk/nashorn/internal/runtime/ScriptObject.java file | annotate | diff | comparison | revisions
test/script/basic/JDK_8005848.js file | annotate | diff | comparison | revisions
     1.1 --- a/src/jdk/nashorn/internal/runtime/ScriptObject.java	Tue Jan 08 15:20:40 2013 +0100
     1.2 +++ b/src/jdk/nashorn/internal/runtime/ScriptObject.java	Tue Jan 08 21:16:07 2013 +0530
     1.3 @@ -631,9 +631,34 @@
     1.4       * @return FindPropertyData or null if not found.
     1.5       */
     1.6      public final FindProperty findProperty(final String key, final boolean deep) {
     1.7 +        return findProperty(key, deep, false);
     1.8 +    }
     1.9 +
    1.10 +    /**
    1.11 +     * Low level property API (not using property descriptors)
    1.12 +     * <p>
    1.13 +     * Find a property in the prototype hierarchy. Note: this is final and not
    1.14 +     * a good idea to override. If you have to, use
    1.15 +     * {jdk.nashorn.internal.objects.NativeArray{@link #getProperty(String)} or
    1.16 +     * {jdk.nashorn.internal.objects.NativeArray{@link #getPropertyDescriptor(String)} as the
    1.17 +     * overriding way to find array properties
    1.18 +     *
    1.19 +     * @see jdk.nashorn.internal.objects.NativeArray
    1.20 +     *
    1.21 +     * @param key  Property key.
    1.22 +     * @param deep Whether the search should look up proto chain.
    1.23 +     * @param stopOnNonScope should a deep search stop on the first non-scope object?
    1.24 +     *
    1.25 +     * @return FindPropertyData or null if not found.
    1.26 +     */
    1.27 +    public final FindProperty findProperty(final String key, final boolean deep, final boolean stopOnNonScope) {
    1.28          int depth = 0;
    1.29  
    1.30          for (ScriptObject self = this; self != null; self = self.getProto()) {
    1.31 +            // if doing deep search, stop search on the first non-scope object if asked to do so
    1.32 +            if (stopOnNonScope && depth != 0 && !self.isScope()) {
    1.33 +                break;
    1.34 +            }
    1.35              final PropertyMap selfMap  = self.getMap();
    1.36              final Property    property = selfMap.findProperty(key);
    1.37  
    1.38 @@ -1731,9 +1756,17 @@
    1.39              return findMegaMorphicSetMethod(desc, name);
    1.40          }
    1.41  
    1.42 -        FindProperty find = findProperty(name, true);
    1.43 +        final boolean scope = isScope();
    1.44 +        /*
    1.45 +         * If doing property set on a scope object, we should stop proto search on the first
    1.46 +         * non-scope object. Without this, for exmaple, when assigning "toString" on global scope,
    1.47 +         * we'll end up assigning it on it's proto - which is Object.prototype.toString !!
    1.48 +         *
    1.49 +         * toString = function() { print("global toString"); } // don't affect Object.prototype.toString
    1.50 +         */
    1.51 +        FindProperty find = findProperty(name, true, scope);
    1.52          // If it's not a scope search, then we don't want any inherited properties except those with user defined accessors.
    1.53 -        if (!isScope() && find != null && find.isInherited() && !(find.getProperty() instanceof UserAccessorProperty)) {
    1.54 +        if (!scope && find != null && find.isInherited() && !(find.getProperty() instanceof UserAccessorProperty)) {
    1.55              // We should still check if inherited data property is not writable
    1.56              if (isExtensible() && !find.isWritable()) {
    1.57                  return createEmptySetMethod(desc, "property.not.writable", false);
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/test/script/basic/JDK_8005848.js	Tue Jan 08 21:16:07 2013 +0530
     2.3 @@ -0,0 +1,39 @@
     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-8005848 : assigning to global toString variable affects Object.prototype.toString 
    2.29 + *
    2.30 + * @test
    2.31 + * @run
    2.32 + */
    2.33 +
    2.34 +toString = function() {
    2.35 +    print("global's toString called!!");
    2.36 +}
    2.37 +
    2.38 +var obj = {};
    2.39 +var str = obj.toString();
    2.40 +if (str != "[object Object]") {
    2.41 +    print("FAILED: toString does not return expected value");
    2.42 +}

mercurial