8026162: "this" in SAM adapter functions is wrong

Reviewed-by: jlaskey, hannesw
This commit is contained in:
Athijegannathan Sundararajan 2013-10-10 14:43:22 +02:00
parent 8cf4738031
commit a577bf4154
4 changed files with 53 additions and 4 deletions

View File

@ -326,7 +326,7 @@ public abstract class ScriptFunction extends ScriptObject {
* @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);
}

View File

@ -93,7 +93,7 @@ import sun.reflect.CallerSensitive;
* 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

View File

@ -29,6 +29,7 @@ import static jdk.nashorn.internal.runtime.ECMAErrors.typeError;
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 @@ public final class JavaAdapterServices {
* @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);
}
/**

View File

@ -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();