changeset 622:34f7a699cdef

8026162: "this" in SAM adapter functions is wrong Reviewed-by: jlaskey, hannesw
author sundar
date Thu, 10 Oct 2013 14:43:22 +0200
parents e60bbcf2f6b6
children ed3da7a574a0
files src/jdk/nashorn/internal/runtime/ScriptFunction.java src/jdk/nashorn/internal/runtime/linker/JavaAdapterBytecodeGenerator.java src/jdk/nashorn/internal/runtime/linker/JavaAdapterServices.java test/script/basic/JDK-8026162.js
diffstat 4 files changed, 53 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/src/jdk/nashorn/internal/runtime/ScriptFunction.java	Thu Oct 10 13:17:57 2013 +0200
+++ b/src/jdk/nashorn/internal/runtime/ScriptFunction.java	Thu Oct 10 14:43:22 2013 +0200
@@ -326,7 +326,7 @@
      * @param self self reference
      * @return bound invoke handle
      */
-    public final MethodHandle getBoundInvokeHandle(final ScriptObject self) {
+    public final MethodHandle getBoundInvokeHandle(final Object self) {
         return MH.bindTo(bindToCalleeIfNeeded(data.getGenericInvoker()), self);
     }
 
--- a/src/jdk/nashorn/internal/runtime/linker/JavaAdapterBytecodeGenerator.java	Thu Oct 10 13:17:57 2013 +0200
+++ b/src/jdk/nashorn/internal/runtime/linker/JavaAdapterBytecodeGenerator.java	Thu Oct 10 14:43:22 2013 +0200
@@ -93,7 +93,7 @@
  * its last argument preceded by original constructor arguments. This constructor will use the passed function as the
  * implementation for all abstract methods. For consistency, any concrete methods sharing the single abstract method
  * name will also be overridden by the function. When methods on the adapter instance are invoked, the ScriptFunction is
- * invoked with {@code null} as its "this".
+ * invoked with global or UNDEFINED as its "this" depending whether the function is non-strict or not.
  * </li>
  * <li>
  * If the adapter being generated can have class-level overrides, constructors taking same arguments as the superclass
--- a/src/jdk/nashorn/internal/runtime/linker/JavaAdapterServices.java	Thu Oct 10 13:17:57 2013 +0200
+++ b/src/jdk/nashorn/internal/runtime/linker/JavaAdapterServices.java	Thu Oct 10 14:43:22 2013 +0200
@@ -29,6 +29,7 @@
 
 import java.lang.invoke.MethodHandle;
 import java.lang.invoke.MethodType;
+import jdk.nashorn.internal.runtime.Context;
 import jdk.nashorn.internal.runtime.ScriptFunction;
 import jdk.nashorn.internal.runtime.ScriptObject;
 import jdk.nashorn.internal.runtime.ScriptRuntime;
@@ -53,8 +54,8 @@
      * @return the appropriately adapted method handle for invoking the script function.
      */
     public static MethodHandle getHandle(final ScriptFunction fn, final MethodType type) {
-        // JS "this" will be null for SAMs
-        return adaptHandle(fn.getBoundInvokeHandle(null), type);
+        // JS "this" will be global object or undefined depending on if 'fn' is strict or not
+        return adaptHandle(fn.getBoundInvokeHandle(fn.isStrict()? ScriptRuntime.UNDEFINED : Context.getGlobal()), type);
     }
 
     /**
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/script/basic/JDK-8026162.js	Thu Oct 10 14:43:22 2013 +0200
@@ -0,0 +1,48 @@
+/*
+ * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ * 
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ * 
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ * 
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ * 
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/**
+ * JDK-8026162: "this" in SAM adapter functions is wrong
+ *
+ * @test
+ * @run
+ */
+
+var global = this;
+
+new java.lang.Runnable(
+  function func() {
+      if (this !== global) {
+          fail("Expected 'this' to be global instance");
+      }
+  }
+).run();
+
+new java.lang.Runnable(
+  function func() {
+      'use strict';
+      if (typeof this != 'undefined') {
+          fail("Expected 'undefined' to be global instance");
+      }
+  }
+).run();