8026162: "this" in SAM adapter functions is wrong

Thu, 10 Oct 2013 14:43:22 +0200

author
sundar
date
Thu, 10 Oct 2013 14:43:22 +0200
changeset 609
34f7a699cdef
parent 607
e60bbcf2f6b6
child 610
ed3da7a574a0

8026162: "this" in SAM adapter functions is wrong
Reviewed-by: jlaskey, hannesw

src/jdk/nashorn/internal/runtime/ScriptFunction.java file | annotate | diff | comparison | revisions
src/jdk/nashorn/internal/runtime/linker/JavaAdapterBytecodeGenerator.java file | annotate | diff | comparison | revisions
src/jdk/nashorn/internal/runtime/linker/JavaAdapterServices.java file | annotate | diff | comparison | revisions
test/script/basic/JDK-8026162.js file | annotate | diff | comparison | revisions
     1.1 --- a/src/jdk/nashorn/internal/runtime/ScriptFunction.java	Thu Oct 10 13:17:57 2013 +0200
     1.2 +++ b/src/jdk/nashorn/internal/runtime/ScriptFunction.java	Thu Oct 10 14:43:22 2013 +0200
     1.3 @@ -326,7 +326,7 @@
     1.4       * @param self self reference
     1.5       * @return bound invoke handle
     1.6       */
     1.7 -    public final MethodHandle getBoundInvokeHandle(final ScriptObject self) {
     1.8 +    public final MethodHandle getBoundInvokeHandle(final Object self) {
     1.9          return MH.bindTo(bindToCalleeIfNeeded(data.getGenericInvoker()), self);
    1.10      }
    1.11  
     2.1 --- a/src/jdk/nashorn/internal/runtime/linker/JavaAdapterBytecodeGenerator.java	Thu Oct 10 13:17:57 2013 +0200
     2.2 +++ b/src/jdk/nashorn/internal/runtime/linker/JavaAdapterBytecodeGenerator.java	Thu Oct 10 14:43:22 2013 +0200
     2.3 @@ -93,7 +93,7 @@
     2.4   * its last argument preceded by original constructor arguments. This constructor will use the passed function as the
     2.5   * implementation for all abstract methods. For consistency, any concrete methods sharing the single abstract method
     2.6   * name will also be overridden by the function. When methods on the adapter instance are invoked, the ScriptFunction is
     2.7 - * invoked with {@code null} as its "this".
     2.8 + * invoked with global or UNDEFINED as its "this" depending whether the function is non-strict or not.
     2.9   * </li>
    2.10   * <li>
    2.11   * If the adapter being generated can have class-level overrides, constructors taking same arguments as the superclass
     3.1 --- a/src/jdk/nashorn/internal/runtime/linker/JavaAdapterServices.java	Thu Oct 10 13:17:57 2013 +0200
     3.2 +++ b/src/jdk/nashorn/internal/runtime/linker/JavaAdapterServices.java	Thu Oct 10 14:43:22 2013 +0200
     3.3 @@ -29,6 +29,7 @@
     3.4  
     3.5  import java.lang.invoke.MethodHandle;
     3.6  import java.lang.invoke.MethodType;
     3.7 +import jdk.nashorn.internal.runtime.Context;
     3.8  import jdk.nashorn.internal.runtime.ScriptFunction;
     3.9  import jdk.nashorn.internal.runtime.ScriptObject;
    3.10  import jdk.nashorn.internal.runtime.ScriptRuntime;
    3.11 @@ -53,8 +54,8 @@
    3.12       * @return the appropriately adapted method handle for invoking the script function.
    3.13       */
    3.14      public static MethodHandle getHandle(final ScriptFunction fn, final MethodType type) {
    3.15 -        // JS "this" will be null for SAMs
    3.16 -        return adaptHandle(fn.getBoundInvokeHandle(null), type);
    3.17 +        // JS "this" will be global object or undefined depending on if 'fn' is strict or not
    3.18 +        return adaptHandle(fn.getBoundInvokeHandle(fn.isStrict()? ScriptRuntime.UNDEFINED : Context.getGlobal()), type);
    3.19      }
    3.20  
    3.21      /**
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/test/script/basic/JDK-8026162.js	Thu Oct 10 14:43:22 2013 +0200
     4.3 @@ -0,0 +1,48 @@
     4.4 +/*
     4.5 + * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
     4.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4.7 + * 
     4.8 + * This code is free software; you can redistribute it and/or modify it
     4.9 + * under the terms of the GNU General Public License version 2 only, as
    4.10 + * published by the Free Software Foundation.
    4.11 + * 
    4.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    4.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    4.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    4.15 + * version 2 for more details (a copy is included in the LICENSE file that
    4.16 + * accompanied this code).
    4.17 + * 
    4.18 + * You should have received a copy of the GNU General Public License version
    4.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    4.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    4.21 + * 
    4.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    4.23 + * or visit www.oracle.com if you need additional information or have any
    4.24 + * questions.
    4.25 + */
    4.26 +
    4.27 +/**
    4.28 + * JDK-8026162: "this" in SAM adapter functions is wrong
    4.29 + *
    4.30 + * @test
    4.31 + * @run
    4.32 + */
    4.33 +
    4.34 +var global = this;
    4.35 +
    4.36 +new java.lang.Runnable(
    4.37 +  function func() {
    4.38 +      if (this !== global) {
    4.39 +          fail("Expected 'this' to be global instance");
    4.40 +      }
    4.41 +  }
    4.42 +).run();
    4.43 +
    4.44 +new java.lang.Runnable(
    4.45 +  function func() {
    4.46 +      'use strict';
    4.47 +      if (typeof this != 'undefined') {
    4.48 +          fail("Expected 'undefined' to be global instance");
    4.49 +      }
    4.50 +  }
    4.51 +).run();

mercurial