mirror of
https://github.com/openjdk/jdk.git
synced 2026-01-30 21:18:25 +00:00
8026367: Add a sync keyword to mozilla_compat
Reviewed-by: sundar, attila, lagergren
This commit is contained in:
parent
8a727caa63
commit
fd29d64bfd
@ -25,6 +25,7 @@
|
||||
|
||||
package jdk.nashorn.api.scripting;
|
||||
|
||||
import jdk.nashorn.internal.runtime.ScriptFunction;
|
||||
import jdk.nashorn.internal.runtime.ScriptRuntime;
|
||||
|
||||
/**
|
||||
@ -57,4 +58,17 @@ public final class ScriptUtils {
|
||||
public static String format(final String format, final Object[] args) {
|
||||
return Formatter.format(format, args);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a wrapper function that calls {@code func} synchronized on {@code sync} or, if that is undefined,
|
||||
* {@code self}. Used to implement "sync" function in resources/mozilla_compat.js.
|
||||
*
|
||||
* @param func the function to invoke
|
||||
* @param sync the object to synchronize on
|
||||
* @return a synchronizing wrapper function
|
||||
*/
|
||||
public static Object makeSynchronizedFunction(final ScriptFunction func, final Object sync) {
|
||||
return func.makeSynchronizedFunction(sync);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -25,6 +25,7 @@
|
||||
|
||||
package jdk.nashorn.internal.objects;
|
||||
|
||||
import static jdk.nashorn.internal.lookup.Lookup.MH;
|
||||
import static jdk.nashorn.internal.runtime.ScriptRuntime.UNDEFINED;
|
||||
|
||||
import java.lang.invoke.MethodHandle;
|
||||
@ -255,6 +256,12 @@ public class ScriptFunctionImpl extends ScriptFunction {
|
||||
return makeFunction(name, methodHandle, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ScriptFunction makeSynchronizedFunction(final Object sync) {
|
||||
final MethodHandle mh = MH.insertArguments(ScriptFunction.INVOKE_SYNC, 0, this, sync);
|
||||
return makeFunction(getName(), mh);
|
||||
}
|
||||
|
||||
/**
|
||||
* Same as {@link ScriptFunction#makeBoundFunction(Object, Object[])}. The only reason we override it is so that we
|
||||
* can expose it to methods in this package.
|
||||
|
||||
@ -53,7 +53,7 @@ import jdk.nashorn.internal.parser.TokenType;
|
||||
public final class RecompilableScriptFunctionData extends ScriptFunctionData {
|
||||
|
||||
/** FunctionNode with the code for this ScriptFunction */
|
||||
private volatile FunctionNode functionNode;
|
||||
private FunctionNode functionNode;
|
||||
|
||||
/** Source from which FunctionNode was parsed. */
|
||||
private final Source source;
|
||||
@ -65,7 +65,7 @@ public final class RecompilableScriptFunctionData extends ScriptFunctionData {
|
||||
private final PropertyMap allocatorMap;
|
||||
|
||||
/** Code installer used for all further recompilation/specialization of this ScriptFunction */
|
||||
private volatile CodeInstaller<ScriptEnvironment> installer;
|
||||
private CodeInstaller<ScriptEnvironment> installer;
|
||||
|
||||
/** Name of class where allocator function resides */
|
||||
private final String allocatorClassName;
|
||||
@ -178,7 +178,7 @@ public final class RecompilableScriptFunctionData extends ScriptFunctionData {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void ensureCodeGenerated() {
|
||||
protected synchronized void ensureCodeGenerated() {
|
||||
if (!code.isEmpty()) {
|
||||
return; // nothing to do, we have code, at least some.
|
||||
}
|
||||
@ -336,7 +336,7 @@ public final class RecompilableScriptFunctionData extends ScriptFunctionData {
|
||||
}
|
||||
|
||||
@Override
|
||||
MethodHandle getBestInvoker(final MethodType callSiteType, final Object[] args) {
|
||||
synchronized MethodHandle getBestInvoker(final MethodType callSiteType, final Object[] args) {
|
||||
final MethodType runtimeType = runtimeType(callSiteType, args);
|
||||
assert runtimeType.parameterCount() == callSiteType.parameterCount();
|
||||
|
||||
|
||||
@ -59,6 +59,9 @@ public abstract class ScriptFunction extends ScriptObject {
|
||||
/** Method handle for name getter for this ScriptFunction */
|
||||
public static final MethodHandle G$NAME = findOwnMH("G$name", Object.class, Object.class);
|
||||
|
||||
/** Method handle used for implementing sync() in mozilla_compat */
|
||||
public static final MethodHandle INVOKE_SYNC = findOwnMH("invokeSync", Object.class, ScriptFunction.class, Object.class, Object.class, Object[].class);
|
||||
|
||||
/** Method handle for allocate function for this ScriptFunction */
|
||||
static final MethodHandle ALLOCATE = findOwnMH("allocate", Object.class);
|
||||
|
||||
@ -300,6 +303,14 @@ public abstract class ScriptFunction extends ScriptObject {
|
||||
*/
|
||||
public abstract void setPrototype(Object prototype);
|
||||
|
||||
/**
|
||||
* Create a function that invokes this function synchronized on {@code sync} or the self object
|
||||
* of the invocation.
|
||||
* @param sync the Object to synchronize on, or undefined
|
||||
* @return synchronized function
|
||||
*/
|
||||
public abstract ScriptFunction makeSynchronizedFunction(Object sync);
|
||||
|
||||
/**
|
||||
* Return the most appropriate invoke handle if there are specializations
|
||||
* @param type most specific method type to look for invocation with
|
||||
@ -614,6 +625,15 @@ public abstract class ScriptFunction extends ScriptObject {
|
||||
return result;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
private static Object invokeSync(final ScriptFunction func, final Object sync, final Object self, final Object... args)
|
||||
throws Throwable {
|
||||
final Object syncObj = sync == UNDEFINED ? self : sync;
|
||||
synchronized (syncObj) {
|
||||
return func.invoke(self, args);
|
||||
}
|
||||
}
|
||||
|
||||
private static MethodHandle findOwnMH(final String name, final Class<?> rtype, final Class<?>... types) {
|
||||
final Class<?> own = ScriptFunction.class;
|
||||
final MethodType mt = MH.type(rtype, types);
|
||||
|
||||
@ -675,7 +675,7 @@ public abstract class ScriptFunctionData {
|
||||
|
||||
/**
|
||||
* Heuristic to figure out if the method handle has a callee argument. If it's type is either
|
||||
* {@code (boolean, Object, ScriptFunction, ...)} or {@code (Object, ScriptFunction, ...)}, then we'll assume it has
|
||||
* {@code (boolean, ScriptFunction, ...)} or {@code (ScriptFunction, ...)}, then we'll assume it has
|
||||
* a callee argument. We need this as the constructor above is not passed this information, and can't just blindly
|
||||
* assume it's false (notably, it's being invoked for creation of new scripts, and scripts have scopes, therefore
|
||||
* they also always receive a callee).
|
||||
@ -692,11 +692,11 @@ public abstract class ScriptFunctionData {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (type.parameterType(0) == boolean.class) {
|
||||
return length > 1 && type.parameterType(1) == ScriptFunction.class;
|
||||
if (type.parameterType(0) == ScriptFunction.class) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return type.parameterType(0) == ScriptFunction.class;
|
||||
return length > 1 && type.parameterType(0) == boolean.class && type.parameterType(1) == ScriptFunction.class;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@ -98,6 +98,17 @@ Object.defineProperty(this, "importPackage", {
|
||||
|
||||
}
|
||||
|
||||
// sync
|
||||
Object.defineProperty(this, "sync", {
|
||||
configurable: true, enumerable: false, writable: true,
|
||||
value: function(func, syncobj) {
|
||||
if (arguments.length < 1 || arguments.length > 2 ) {
|
||||
throw "sync(function [,object]) parameter count mismatch";
|
||||
}
|
||||
return Packages.jdk.nashorn.api.scripting.ScriptUtils.makeSynchronizedFunction(func, syncobj);
|
||||
}
|
||||
});
|
||||
|
||||
// Object.prototype.__defineGetter__
|
||||
Object.defineProperty(Object.prototype, "__defineGetter__", {
|
||||
configurable: true, enumerable: false, writable: true,
|
||||
|
||||
61
nashorn/test/script/basic/JDK-8026367.js
Normal file
61
nashorn/test/script/basic/JDK-8026367.js
Normal file
@ -0,0 +1,61 @@
|
||||
/*
|
||||
* 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-8026367: Add a sync keyword to mozilla_compat
|
||||
*
|
||||
* @test
|
||||
* @run
|
||||
*/
|
||||
|
||||
if (typeof sync === "undefined") {
|
||||
load("nashorn:mozilla_compat.js");
|
||||
}
|
||||
|
||||
var obj = {
|
||||
count: 0,
|
||||
// Sync called with one argument will synchronize on this-object of invocation
|
||||
inc: sync(function(d) {
|
||||
this.count += d;
|
||||
}),
|
||||
// Pass explicit object to synchronize on as second argument
|
||||
dec: sync(function(d) {
|
||||
this.count -= d;
|
||||
}, obj)
|
||||
};
|
||||
|
||||
var t1 = new java.lang.Thread(function() {
|
||||
for (var i = 0; i < 100000; i++) obj.inc(1);
|
||||
});
|
||||
var t2 = new java.lang.Thread(function() {
|
||||
for (var i = 0; i < 100000; i++) obj.dec(1);
|
||||
});
|
||||
|
||||
t1.start();
|
||||
t2.start();
|
||||
t1.join();
|
||||
t2.join();
|
||||
|
||||
if (obj.count !== 0) {
|
||||
throw new Error("Expected count == 0, got " + obj.count);
|
||||
}
|
||||
@ -48,3 +48,7 @@ if (typeof JavaAdapter != 'function') {
|
||||
if (typeof importPackage != 'function') {
|
||||
fail("importPackage function is missing in compatibility script");
|
||||
}
|
||||
|
||||
if (typeof sync != 'function') {
|
||||
fail("sync function is missing in compatibility script");
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user