aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorhannesw <none@none>2014-03-12 11:26:00 +0100
committerhannesw <none@none>2014-03-12 11:26:00 +0100
commit88255f8931b252105c2e1b9886783b33b834d2af (patch)
tree8e99b4f8b13da4c893ab76fd7c5d8280e26c5e44 /src
parent7b7176399812992b93f01a7bb93a43d049a612c2 (diff)
downloadnashorn-88255f8931b252105c2e1b9886783b33b834d2af.tar.gz
8021350: Share script classes between threads/globals within context
Reviewed-by: lagergren, sundar
Diffstat (limited to 'src')
-rw-r--r--src/jdk/nashorn/internal/codegen/Attr.java9
-rw-r--r--src/jdk/nashorn/internal/codegen/CodeGenerator.java4
-rw-r--r--src/jdk/nashorn/internal/codegen/SharedScopeCall.java2
-rw-r--r--src/jdk/nashorn/internal/ir/FunctionNode.java16
-rw-r--r--src/jdk/nashorn/internal/objects/Global.java73
-rw-r--r--src/jdk/nashorn/internal/objects/NativeBoolean.java17
-rw-r--r--src/jdk/nashorn/internal/objects/NativeJSAdapter.java4
-rw-r--r--src/jdk/nashorn/internal/objects/NativeNumber.java17
-rw-r--r--src/jdk/nashorn/internal/objects/NativeString.java17
-rw-r--r--src/jdk/nashorn/internal/objects/ScriptFunctionImpl.java22
-rw-r--r--src/jdk/nashorn/internal/parser/Parser.java1
-rw-r--r--src/jdk/nashorn/internal/runtime/AccessorProperty.java12
-rw-r--r--src/jdk/nashorn/internal/runtime/Context.java88
-rw-r--r--src/jdk/nashorn/internal/runtime/FinalScriptFunctionData.java30
-rw-r--r--src/jdk/nashorn/internal/runtime/GlobalObject.java16
-rw-r--r--src/jdk/nashorn/internal/runtime/Property.java16
-rw-r--r--src/jdk/nashorn/internal/runtime/RecompilableScriptFunctionData.java94
-rw-r--r--src/jdk/nashorn/internal/runtime/ScriptFunction.java46
-rw-r--r--src/jdk/nashorn/internal/runtime/ScriptFunctionData.java71
-rw-r--r--src/jdk/nashorn/internal/runtime/ScriptObject.java79
-rw-r--r--src/jdk/nashorn/internal/runtime/SetMethodCreator.java22
-rw-r--r--src/jdk/nashorn/internal/runtime/WithObject.java7
-rw-r--r--src/jdk/nashorn/internal/runtime/linker/NashornGuards.java60
-rw-r--r--src/jdk/nashorn/internal/runtime/linker/PrimitiveLookup.java25
24 files changed, 446 insertions, 302 deletions
diff --git a/src/jdk/nashorn/internal/codegen/Attr.java b/src/jdk/nashorn/internal/codegen/Attr.java
index 177cfef4..61ccb712 100644
--- a/src/jdk/nashorn/internal/codegen/Attr.java
+++ b/src/jdk/nashorn/internal/codegen/Attr.java
@@ -375,10 +375,11 @@ final class Attr extends NodeOperatorVisitor<LexicalContext> {
* @return Symbol for given name or null for redefinition.
*/
private Symbol defineSymbol(final Block block, final String name, final int symbolFlags) {
- int flags = symbolFlags;
- Symbol symbol = findSymbol(block, name); // Locate symbol.
+ int flags = symbolFlags;
+ Symbol symbol = findSymbol(block, name); // Locate symbol.
+ boolean isGlobal = (flags & KINDMASK) == IS_GLOBAL;
- if ((flags & KINDMASK) == IS_GLOBAL) {
+ if (isGlobal) {
flags |= IS_SCOPE;
}
@@ -414,6 +415,8 @@ final class Attr extends NodeOperatorVisitor<LexicalContext> {
// Determine where to create it.
if ((flags & Symbol.KINDMASK) == IS_VAR && ((flags & IS_INTERNAL) == IS_INTERNAL || (flags & IS_LET) == IS_LET)) {
symbolBlock = block; //internal vars are always defined in the block closest to them
+ } else if (isGlobal) {
+ symbolBlock = lc.getOutermostFunction().getBody();
} else {
symbolBlock = lc.getFunctionBody(function);
}
diff --git a/src/jdk/nashorn/internal/codegen/CodeGenerator.java b/src/jdk/nashorn/internal/codegen/CodeGenerator.java
index 6ad92829..e0ca67a4 100644
--- a/src/jdk/nashorn/internal/codegen/CodeGenerator.java
+++ b/src/jdk/nashorn/internal/codegen/CodeGenerator.java
@@ -685,7 +685,7 @@ final class CodeGenerator extends NodeOperatorVisitor<CodeGeneratorLexicalContex
private void scopeCall(final IdentNode node, final int flags) {
load(node, Type.OBJECT); // Type.OBJECT as foo() makes no sense if foo == 3
// ScriptFunction will see CALLSITE_SCOPE and will bind scope accordingly.
- method.loadNull(); //the 'this'
+ method.loadUndefined(Type.OBJECT); //the 'this' object
method.dynamicCall(callNodeType, 2 + loadArgs(args), flags);
}
@@ -818,7 +818,7 @@ final class CodeGenerator extends NodeOperatorVisitor<CodeGeneratorLexicalContex
protected boolean enterDefault(final Node node) {
// Load up function.
load(function, Type.OBJECT); //TODO, e.g. booleans can be used as functions
- method.loadNull(); // ScriptFunction will figure out the correct this when it sees CALLSITE_SCOPE
+ method.loadUndefined(Type.OBJECT); // ScriptFunction will figure out the correct this when it sees CALLSITE_SCOPE
method.dynamicCall(callNodeType, 2 + loadArgs(args), getCallSiteFlags() | CALLSITE_SCOPE);
return false;
diff --git a/src/jdk/nashorn/internal/codegen/SharedScopeCall.java b/src/jdk/nashorn/internal/codegen/SharedScopeCall.java
index c7dfb40a..da29ac05 100644
--- a/src/jdk/nashorn/internal/codegen/SharedScopeCall.java
+++ b/src/jdk/nashorn/internal/codegen/SharedScopeCall.java
@@ -156,7 +156,7 @@ class SharedScopeCall {
if (isCall) {
method.convert(Type.OBJECT);
// ScriptFunction will see CALLSITE_SCOPE and will bind scope accordingly.
- method.loadNull();
+ method.loadUndefined(Type.OBJECT);
int slot = 2;
for (final Type type : paramTypes) {
method.load(type, slot++);
diff --git a/src/jdk/nashorn/internal/ir/FunctionNode.java b/src/jdk/nashorn/internal/ir/FunctionNode.java
index 1437eb40..a0f6a788 100644
--- a/src/jdk/nashorn/internal/ir/FunctionNode.java
+++ b/src/jdk/nashorn/internal/ir/FunctionNode.java
@@ -164,11 +164,11 @@ public final class FunctionNode extends LexicalContextExpression implements Flag
public static final int HAS_EVAL = 1 << 5;
/** Does a nested function contain eval? If it does, then all variables in this function might be get/set by it. */
- public static final int HAS_NESTED_EVAL = 1 << 6;
+ public static final int HAS_NESTED_EVAL = 1 << 6;
/** Does this function have any blocks that create a scope? This is used to determine if the function needs to
* have a local variable slot for the scope symbol. */
- public static final int HAS_SCOPE_BLOCK = 1 << 7;
+ public static final int HAS_SCOPE_BLOCK = 1 << 7;
/**
* Flag this function as one that defines the identifier "arguments" as a function parameter or nested function
@@ -197,6 +197,9 @@ public final class FunctionNode extends LexicalContextExpression implements Flag
/** Can this function be specialized? */
public static final int CAN_SPECIALIZE = 1 << 14;
+ /** Does this function use the "this" keyword? */
+ public static final int USES_THIS = 1 << 15;
+
/** Does this function or any nested functions contain an eval? */
private static final int HAS_DEEP_EVAL = HAS_EVAL | HAS_NESTED_EVAL;
@@ -591,6 +594,15 @@ public final class FunctionNode extends LexicalContextExpression implements Flag
}
/**
+ * Return {@code true} if this function makes use of the {@code this} object.
+ *
+ * @return true if function uses {@code this} object
+ */
+ public boolean usesThis() {
+ return getFlag(USES_THIS);
+ }
+
+ /**
* Get the identifier for this function, this is its symbol.
* @return the identifier as an IdentityNode
*/
diff --git a/src/jdk/nashorn/internal/objects/Global.java b/src/jdk/nashorn/internal/objects/Global.java
index e8b12cb8..82f2db45 100644
--- a/src/jdk/nashorn/internal/objects/Global.java
+++ b/src/jdk/nashorn/internal/objects/Global.java
@@ -33,11 +33,8 @@ import java.io.IOException;
import java.io.PrintWriter;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
-import java.lang.ref.ReferenceQueue;
-import java.lang.ref.SoftReference;
import java.lang.reflect.Field;
import java.util.Arrays;
-import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;
@@ -59,10 +56,10 @@ import jdk.nashorn.internal.runtime.PropertyMap;
import jdk.nashorn.internal.runtime.Scope;
import jdk.nashorn.internal.runtime.ScriptEnvironment;
import jdk.nashorn.internal.runtime.ScriptFunction;
+import jdk.nashorn.internal.runtime.ScriptFunctionData;
import jdk.nashorn.internal.runtime.ScriptObject;
import jdk.nashorn.internal.runtime.ScriptRuntime;
import jdk.nashorn.internal.runtime.ScriptingFunctions;
-import jdk.nashorn.internal.runtime.Source;
import jdk.nashorn.internal.runtime.arrays.ArrayData;
import jdk.nashorn.internal.runtime.linker.Bootstrap;
import jdk.nashorn.internal.runtime.linker.InvokeByName;
@@ -373,9 +370,6 @@ public final class Global extends ScriptObject implements GlobalObject, Scope {
// Flag to indicate that a split method issued a return statement
private int splitState = -1;
- // class cache
- private ClassCache classCache;
-
// Used to store the last RegExp result to support deprecated RegExp constructor properties
private RegExpResult lastRegExpResult;
@@ -426,11 +420,6 @@ public final class Global extends ScriptObject implements GlobalObject, Scope {
super(checkAndGetMap(context));
this.context = context;
this.setIsScope();
-
- final int cacheSize = context.getEnv()._class_cache_size;
- if (cacheSize > 0) {
- classCache = new ClassCache(cacheSize);
- }
}
/**
@@ -488,7 +477,7 @@ public final class Global extends ScriptObject implements GlobalObject, Scope {
@Override
public ScriptFunction newScriptFunction(final String name, final MethodHandle handle, final ScriptObject scope, final boolean strict) {
- return new ScriptFunctionImpl(name, handle, scope, null, strict, false, true);
+ return new ScriptFunctionImpl(name, handle, scope, null, strict ? ScriptFunctionData.IS_STRICT_CONSTRUCTOR : ScriptFunctionData.IS_CONSTRUCTOR);
}
@Override
@@ -664,62 +653,6 @@ public final class Global extends ScriptObject implements GlobalObject, Scope {
}
- /**
- * Cache for compiled script classes.
- */
- @SuppressWarnings("serial")
- private static class ClassCache extends LinkedHashMap<Source, ClassReference> {
- private final int size;
- private final ReferenceQueue<Class<?>> queue;
-
- ClassCache(int size) {
- super(size, 0.75f, true);
- this.size = size;
- this.queue = new ReferenceQueue<>();
- }
-
- void cache(final Source source, final Class<?> clazz) {
- put(source, new ClassReference(clazz, queue, source));
- }
-
- @Override
- protected boolean removeEldestEntry(final Map.Entry<Source, ClassReference> eldest) {
- return size() > size;
- }
-
- @Override
- public ClassReference get(Object key) {
- for (ClassReference ref; (ref = (ClassReference)queue.poll()) != null; ) {
- remove(ref.source);
- }
- return super.get(key);
- }
-
- }
-
- private static class ClassReference extends SoftReference<Class<?>> {
- private final Source source;
-
- ClassReference(final Class<?> clazz, final ReferenceQueue<Class<?>> queue, final Source source) {
- super(clazz, queue);
- this.source = source;
- }
- }
-
- // Class cache management
- @Override
- public Class<?> findCachedClass(final Source source) {
- assert classCache != null : "Class cache used without being initialized";
- ClassReference ref = classCache.get(source);
- return ref != null ? ref.get() : null;
- }
-
- @Override
- public void cacheClass(final Source source, final Class<?> clazz) {
- assert classCache != null : "Class cache used without being initialized";
- classCache.cache(source, clazz);
- }
-
private static <T> T getLazilyCreatedValue(final Object key, final Callable<T> creator, final Map<Object, T> map) {
final T obj = map.get(key);
if (obj != null) {
@@ -1838,7 +1771,7 @@ public final class Global extends ScriptObject implements GlobalObject, Scope {
anon.deleteOwnProperty(anon.getMap().findProperty("prototype"));
// use "getter" so that [[ThrowTypeError]] function's arity is 0 - as specified in step 10 of section 13.2.3
- this.typeErrorThrower = new ScriptFunctionImpl("TypeErrorThrower", Lookup.TYPE_ERROR_THROWER_GETTER, null, null, false, false, false);
+ this.typeErrorThrower = new ScriptFunctionImpl("TypeErrorThrower", Lookup.TYPE_ERROR_THROWER_GETTER, null, null, 0);
typeErrorThrower.setPrototype(UNDEFINED);
// Non-constructor built-in functions do not have "prototype" property
typeErrorThrower.deleteOwnProperty(typeErrorThrower.getMap().findProperty("prototype"));
diff --git a/src/jdk/nashorn/internal/objects/NativeBoolean.java b/src/jdk/nashorn/internal/objects/NativeBoolean.java
index bd955079..bde6c608 100644
--- a/src/jdk/nashorn/internal/objects/NativeBoolean.java
+++ b/src/jdk/nashorn/internal/objects/NativeBoolean.java
@@ -30,6 +30,7 @@ import static jdk.nashorn.internal.lookup.Lookup.MH;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
+import java.lang.invoke.MethodType;
import jdk.internal.dynalink.linker.GuardedInvocation;
import jdk.internal.dynalink.linker.LinkRequest;
import jdk.nashorn.internal.objects.annotations.Attribute;
@@ -50,7 +51,10 @@ import jdk.nashorn.internal.runtime.linker.PrimitiveLookup;
public final class NativeBoolean extends ScriptObject {
private final boolean value;
- final static MethodHandle WRAPFILTER = findWrapFilter();
+ // Method handle to create an object wrapper for a primitive boolean
+ private static final MethodHandle WRAPFILTER = findOwnMH("wrapFilter", MH.type(NativeBoolean.class, Object.class));
+ // Method handle to retrieve the Boolean prototype object
+ private static final MethodHandle PROTOFILTER = findOwnMH("protoFilter", MH.type(Object.class, Object.class));
// initialized by nasgen
private static PropertyMap $nasgenmap$;
@@ -164,7 +168,7 @@ public final class NativeBoolean extends ScriptObject {
* @return Link to be invoked at call site.
*/
public static GuardedInvocation lookupPrimitive(final LinkRequest request, final Object receiver) {
- return PrimitiveLookup.lookupPrimitive(request, Boolean.class, new NativeBoolean((Boolean)receiver), WRAPFILTER);
+ return PrimitiveLookup.lookupPrimitive(request, Boolean.class, new NativeBoolean((Boolean)receiver), WRAPFILTER, PROTOFILTER);
}
/**
@@ -178,7 +182,12 @@ public final class NativeBoolean extends ScriptObject {
return new NativeBoolean((Boolean)receiver);
}
- private static MethodHandle findWrapFilter() {
- return MH.findStatic(MethodHandles.lookup(), NativeBoolean.class, "wrapFilter", MH.type(NativeBoolean.class, Object.class));
+ @SuppressWarnings("unused")
+ private static Object protoFilter(final Object object) {
+ return Global.instance().getBooleanPrototype();
+ }
+
+ private static MethodHandle findOwnMH(final String name, final MethodType type) {
+ return MH.findStatic(MethodHandles.lookup(), NativeBoolean.class, name, type);
}
}
diff --git a/src/jdk/nashorn/internal/objects/NativeJSAdapter.java b/src/jdk/nashorn/internal/objects/NativeJSAdapter.java
index c77b2fe0..ed879dc6 100644
--- a/src/jdk/nashorn/internal/objects/NativeJSAdapter.java
+++ b/src/jdk/nashorn/internal/objects/NativeJSAdapter.java
@@ -622,7 +622,7 @@ public final class NativeJSAdapter extends ScriptObject {
case "getMethod":
final FindProperty find = adaptee.findProperty(__call__, true);
if (find != null) {
- final Object value = getObjectValue(find);
+ final Object value = find.getObjectValue();
if (value instanceof ScriptFunction) {
final ScriptFunctionImpl func = (ScriptFunctionImpl)value;
// TODO: It's a shame we need to produce a function bound to this and name, when we'd only need it bound
@@ -691,7 +691,7 @@ public final class NativeJSAdapter extends ScriptObject {
final MethodType type = desc.getMethodType();
if (findData != null) {
final String name = desc.getNameTokenCount() > 2 ? desc.getNameToken(2) : null;
- final Object value = getObjectValue(findData);
+ final Object value = findData.getObjectValue();
if (value instanceof ScriptFunction) {
final ScriptFunction func = (ScriptFunction)value;
diff --git a/src/jdk/nashorn/internal/objects/NativeNumber.java b/src/jdk/nashorn/internal/objects/NativeNumber.java
index 09289a01..02d9b99e 100644
--- a/src/jdk/nashorn/internal/objects/NativeNumber.java
+++ b/src/jdk/nashorn/internal/objects/NativeNumber.java
@@ -34,6 +34,7 @@ import static jdk.nashorn.internal.lookup.Lookup.MH;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
+import java.lang.invoke.MethodType;
import java.text.NumberFormat;
import java.util.Locale;
import jdk.internal.dynalink.linker.GuardedInvocation;
@@ -57,7 +58,10 @@ import jdk.nashorn.internal.runtime.linker.PrimitiveLookup;
@ScriptClass("Number")
public final class NativeNumber extends ScriptObject {
- static final MethodHandle WRAPFILTER = findWrapFilter();
+ // Method handle to create an object wrapper for a primitive number
+ private static final MethodHandle WRAPFILTER = findOwnMH("wrapFilter", MH.type(NativeNumber.class, Object.class));
+ // Method handle to retrieve the Number prototype object
+ private static final MethodHandle PROTOFILTER = findOwnMH("protoFilter", MH.type(Object.class, Object.class));
/** ECMA 15.7.3.2 largest positive finite value */
@Property(attributes = Attribute.NON_ENUMERABLE_CONSTANT, where = Where.CONSTRUCTOR)
@@ -322,7 +326,7 @@ public final class NativeNumber extends ScriptObject {
* @return Link to be invoked at call site.
*/
public static GuardedInvocation lookupPrimitive(final LinkRequest request, final Object receiver) {
- return PrimitiveLookup.lookupPrimitive(request, Number.class, new NativeNumber(((Number)receiver).doubleValue()), WRAPFILTER);
+ return PrimitiveLookup.lookupPrimitive(request, Number.class, new NativeNumber(((Number)receiver).doubleValue()), WRAPFILTER, PROTOFILTER);
}
@SuppressWarnings("unused")
@@ -330,6 +334,11 @@ public final class NativeNumber extends ScriptObject {
return new NativeNumber(((Number)receiver).doubleValue());
}
+ @SuppressWarnings("unused")
+ private static Object protoFilter(final Object object) {
+ return Global.instance().getNumberPrototype();
+ }
+
private static double getNumberValue(final Object self) {
if (self instanceof Number) {
return ((Number)self).doubleValue();
@@ -378,7 +387,7 @@ public final class NativeNumber extends ScriptObject {
return str;
}
- private static MethodHandle findWrapFilter() {
- return MH.findStatic(MethodHandles.lookup(), NativeNumber.class, "wrapFilter", MH.type(NativeNumber.class, Object.class));
+ private static MethodHandle findOwnMH(final String name, final MethodType type) {
+ return MH.findStatic(MethodHandles.lookup(), NativeNumber.class, name, type);
}
}
diff --git a/src/jdk/nashorn/internal/objects/NativeString.java b/src/jdk/nashorn/internal/objects/NativeString.java
index 2370f514..53ae9a23 100644
--- a/src/jdk/nashorn/internal/objects/NativeString.java
+++ b/src/jdk/nashorn/internal/objects/NativeString.java
@@ -32,6 +32,7 @@ import static jdk.nashorn.internal.runtime.ScriptRuntime.UNDEFINED;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
+import java.lang.invoke.MethodType;
import java.text.Collator;
import java.util.ArrayList;
import java.util.Arrays;
@@ -69,7 +70,10 @@ public final class NativeString extends ScriptObject {
private final CharSequence value;
- static final MethodHandle WRAPFILTER = findWrapFilter();
+ // Method handle to create an object wrapper for a primitive string
+ private static final MethodHandle WRAPFILTER = findOwnMH("wrapFilter", MH.type(NativeString.class, Object.class));
+ // Method handle to retrieve the String prototype object
+ private static final MethodHandle PROTOFILTER = findOwnMH("protoFilter", MH.type(Object.class, Object.class));
// initialized by nasgen
private static PropertyMap $nasgenmap$;
@@ -1199,7 +1203,7 @@ public final class NativeString extends ScriptObject {
*/
public static GuardedInvocation lookupPrimitive(final LinkRequest request, final Object receiver) {
final MethodHandle guard = NashornGuards.getInstanceOf2Guard(String.class, ConsString.class);
- return PrimitiveLookup.lookupPrimitive(request, guard, new NativeString((CharSequence)receiver), WRAPFILTER);
+ return PrimitiveLookup.lookupPrimitive(request, guard, new NativeString((CharSequence)receiver), WRAPFILTER, PROTOFILTER);
}
@SuppressWarnings("unused")
@@ -1207,6 +1211,11 @@ public final class NativeString extends ScriptObject {
return new NativeString((CharSequence)receiver);
}
+ @SuppressWarnings("unused")
+ private static Object protoFilter(final Object object) {
+ return Global.instance().getStringPrototype();
+ }
+
private static CharSequence getCharSequence(final Object self) {
if (self instanceof String || self instanceof ConsString) {
return (CharSequence)self;
@@ -1254,7 +1263,7 @@ public final class NativeString extends ScriptObject {
return key >= 0 && key < value.length();
}
- private static MethodHandle findWrapFilter() {
- return MH.findStatic(MethodHandles.lookup(), NativeString.class, "wrapFilter", MH.type(NativeString.class, Object.class));
+ private static MethodHandle findOwnMH(final String name, final MethodType type) {
+ return MH.findStatic(MethodHandles.lookup(), NativeString.class, name, type);
}
}
diff --git a/src/jdk/nashorn/internal/objects/ScriptFunctionImpl.java b/src/jdk/nashorn/internal/objects/ScriptFunctionImpl.java
index 9852c962..fe18be99 100644
--- a/src/jdk/nashorn/internal/objects/ScriptFunctionImpl.java
+++ b/src/jdk/nashorn/internal/objects/ScriptFunctionImpl.java
@@ -75,7 +75,7 @@ public class ScriptFunctionImpl extends ScriptFunction {
private static final Object LAZY_PROTOTYPE = new Object();
private ScriptFunctionImpl(final String name, final MethodHandle invokeHandle, final MethodHandle[] specs, final Global global) {
- super(name, invokeHandle, getInitialMap(), null, specs, false, true, true);
+ super(name, invokeHandle, getInitialMap(), null, specs, ScriptFunctionData.IS_BUILTIN_CONSTRUCTOR);
init(global);
}
@@ -92,7 +92,7 @@ public class ScriptFunctionImpl extends ScriptFunction {
}
private ScriptFunctionImpl(final String name, final MethodHandle invokeHandle, final PropertyMap map, final MethodHandle[] specs, final Global global) {
- super(name, invokeHandle, map.addAll(getInitialMap()), null, specs, false, true, true);
+ super(name, invokeHandle, map.addAll(getInitialMap()), null, specs, ScriptFunctionData.IS_BUILTIN_CONSTRUCTOR);
init(global);
}
@@ -109,8 +109,8 @@ public class ScriptFunctionImpl extends ScriptFunction {
this(name, invokeHandle, map, specs, Global.instance());
}
- private ScriptFunctionImpl(final String name, final MethodHandle methodHandle, final ScriptObject scope, final MethodHandle[] specs, final boolean isStrict, final boolean isBuiltin, final boolean isConstructor, final Global global) {
- super(name, methodHandle, getMap(global, isStrict), scope, specs, isStrict, isBuiltin, isConstructor);
+ private ScriptFunctionImpl(final String name, final MethodHandle methodHandle, final ScriptObject scope, final MethodHandle[] specs, final int flags, final Global global) {
+ super(name, methodHandle, getMap(global, isStrict(flags)), scope, specs, flags);
init(global);
}
@@ -121,12 +121,10 @@ public class ScriptFunctionImpl extends ScriptFunction {
* @param methodHandle handle for invocation
* @param scope scope object
* @param specs specialized versions of this method, if available, null otherwise
- * @param isStrict are we in strict mode
- * @param isBuiltin is this a built-in function
- * @param isConstructor can the function be used as a constructor (most can; some built-ins are restricted).
+ * @param flags {@link ScriptFunctionData} flags
*/
- ScriptFunctionImpl(final String name, final MethodHandle methodHandle, final ScriptObject scope, final MethodHandle[] specs, final boolean isStrict, final boolean isBuiltin, final boolean isConstructor) {
- this(name, methodHandle, scope, specs, isStrict, isBuiltin, isConstructor, Global.instance());
+ ScriptFunctionImpl(final String name, final MethodHandle methodHandle, final ScriptObject scope, final MethodHandle[] specs, final int flags) {
+ this(name, methodHandle, scope, specs, flags, Global.instance());
}
private ScriptFunctionImpl(final RecompilableScriptFunctionData data, final ScriptObject scope, final Global global) {
@@ -173,6 +171,10 @@ public class ScriptFunctionImpl extends ScriptFunction {
return newMap;
}
+ private static boolean isStrict(final int flags) {
+ return (flags & ScriptFunctionData.IS_STRICT) != 0;
+ }
+
// Choose the map based on strict mode!
private static PropertyMap getMap(final Global global, final boolean strict) {
return strict ? getInitialStrictMap() : getInitialMap();
@@ -211,7 +213,7 @@ public class ScriptFunctionImpl extends ScriptFunction {
* @return new ScriptFunction
*/
static ScriptFunction makeFunction(final String name, final MethodHandle methodHandle, final MethodHandle[] specs) {
- final ScriptFunctionImpl func = new ScriptFunctionImpl(name, methodHandle, null, specs, false, true, false);
+ final ScriptFunctionImpl func = new ScriptFunctionImpl(name, methodHandle, null, specs, ScriptFunctionData.IS_BUILTIN);
func.setPrototype(UNDEFINED);
// Non-constructor built-in functions do not have "prototype" property
func.deleteOwnProperty(func.getMap().findProperty("prototype"));
diff --git a/src/jdk/nashorn/internal/parser/Parser.java b/src/jdk/nashorn/internal/parser/Parser.java
index 60cbaccb..f6b962a9 100644
--- a/src/jdk/nashorn/internal/parser/Parser.java
+++ b/src/jdk/nashorn/internal/parser/Parser.java
@@ -1799,6 +1799,7 @@ loop:
case THIS:
final String name = type.getName();
next();
+ lc.setFlag(lc.getCurrentFunction(), FunctionNode.USES_THIS);
return new IdentNode(primaryToken, finish, name);
case IDENT:
final IdentNode ident = getIdent();
diff --git a/src/jdk/nashorn/internal/runtime/AccessorProperty.java b/src/jdk/nashorn/internal/runtime/AccessorProperty.java
index 702ca76b..d22a9567 100644
--- a/src/jdk/nashorn/internal/runtime/AccessorProperty.java
+++ b/src/jdk/nashorn/internal/runtime/AccessorProperty.java
@@ -141,10 +141,12 @@ public final class AccessorProperty extends Property {
private Class<?> currentType;
/**
- * Delegate constructor. This is used when adding properties to the Global scope, which
- * is necessary for outermost levels in a script (the ScriptObject is represented by
- * a JO-prefixed ScriptObject class, but the properties need to be in the Global scope
- * and are thus rebound with that as receiver
+ * Delegate constructor for bound properties. This is used for properties created by
+ * {@link ScriptRuntime#mergeScope} and the Nashorn {@code Object.bindProperties} method.
+ * The former is used to add a script's defined globals to the current global scope while
+ * still storing them in a JO-prefixed ScriptObject class.
+ *
+ * <p>All properties created by this constructor have the {@link #IS_BOUND} flag set.</p>
*
* @param property accessor property to rebind
* @param delegate delegate object to rebind receiver to
@@ -157,6 +159,8 @@ public final class AccessorProperty extends Property {
this.objectGetter = bindTo(property.ensureObjectGetter(), delegate);
this.objectSetter = bindTo(property.ensureObjectSetter(), delegate);
+ // Properties created this way are bound to a delegate
+ this.flags |= IS_BOUND;
setCurrentType(property.getCurrentType());
}
diff --git a/src/jdk/nashorn/internal/runtime/Context.java b/src/jdk/nashorn/internal/runtime/Context.java
index 7df2493d..7ba60162 100644
--- a/src/jdk/nashorn/internal/runtime/Context.java
+++ b/src/jdk/nashorn/internal/runtime/Context.java
@@ -36,6 +36,8 @@ import java.io.IOException;
import java.io.PrintWriter;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
+import java.lang.ref.ReferenceQueue;
+import java.lang.ref.SoftReference;
import java.lang.reflect.Modifier;
import java.util.concurrent.atomic.AtomicLong;
import java.net.MalformedURLException;
@@ -47,6 +49,7 @@ import java.security.CodeSource;
import java.security.Permissions;
import java.security.PrivilegedAction;
import java.security.ProtectionDomain;
+import java.util.LinkedHashMap;
import java.util.Map;
import jdk.internal.org.objectweb.asm.ClassReader;
@@ -156,6 +159,9 @@ public final class Context {
private static final ThreadLocal<ScriptObject> currentGlobal = new ThreadLocal<>();
+ // class cache
+ private ClassCache classCache;
+
/**
* Get the current global scope
* @return the current global scope
@@ -349,6 +355,11 @@ public final class Context {
this.classPathLoader = null;
}
+ final int cacheSize = env._class_cache_size;
+ if (cacheSize > 0) {
+ classCache = new ClassCache(cacheSize);
+ }
+
// print version info if asked.
if (env._version) {
getErr().println("nashorn " + Version.version());
@@ -660,7 +671,7 @@ public final class Context {
public static void checkPackageAccess(final String pkgName) {
final SecurityManager sm = System.getSecurityManager();
if (sm != null) {
- checkPackageAccess(sm, pkgName.endsWith(".")? pkgName : pkgName + ".");
+ checkPackageAccess(sm, pkgName.endsWith(".") ? pkgName : pkgName + ".");
}
}
@@ -926,16 +937,10 @@ public final class Context {
// start with no errors, no warnings.
errMan.reset();
- GlobalObject global = null;
- Class<?> script;
-
- if (env._class_cache_size > 0) {
- global = (GlobalObject)Context.getGlobalTrusted();
- script = global.findCachedClass(source);
- if (script != null) {
- Compiler.LOG.fine("Code cache hit for ", source, " avoiding recompile.");
- return script;
- }
+ Class<?> script = findCachedClass(source);
+ if (script != null) {
+ Compiler.LOG.fine("Code cache hit for ", source, " avoiding recompile.");
+ return script;
}
final FunctionNode functionNode = new Parser(env, source, errMan, strict).parse();
@@ -964,10 +969,7 @@ public final class Context {
final FunctionNode newFunctionNode = compiler.compile(functionNode);
script = compiler.install(newFunctionNode);
-
- if (global != null) {
- global.cacheClass(source, script);
- }
+ cacheClass(source, script);
return script;
}
@@ -989,4 +991,60 @@ public final class Context {
private long getUniqueScriptId() {
return uniqueScriptId.getAndIncrement();
}
+
+ /**
+ * Cache for compiled script classes.
+ */
+ @SuppressWarnings("serial")
+ private static class ClassCache extends LinkedHashMap<Source, ClassReference> {
+ private final int size;
+ private final ReferenceQueue<Class<?>> queue;
+
+ ClassCache(int size) {
+ super(size, 0.75f, true);
+ this.size = size;
+ this.queue = new ReferenceQueue<>();
+ }
+
+ void cache(final Source source, final Class<?> clazz) {
+ put(source, new ClassReference(clazz, queue, source));
+ }
+
+ @Override
+ protected boolean removeEldestEntry(final Map.Entry<Source, ClassReference> eldest) {
+ return size() > size;
+ }
+
+ @Override
+ public ClassReference get(Object key) {
+ for (ClassReference ref; (ref = (ClassReference)queue.poll()) != null; ) {
+ remove(ref.source);
+ }
+ return super.get(key);
+ }
+
+ }
+
+ private static class ClassReference extends SoftReference<Class<?>> {
+ private final Source source;
+
+ ClassReference(final Class<?> clazz, final ReferenceQueue<Class<?>> queue, final Source source) {
+ super(clazz, queue);
+ this.source = source;
+ }
+ }
+
+ // Class cache management
+ private Class<?> findCachedClass(final Source source) {
+ ClassReference ref = classCache == null ? null : classCache.get(source);
+ return ref != null ? ref.get() : null;
+ }
+
+ private void cacheClass(final Source source, final Class<?> clazz) {
+ if (classCache != null) {
+ classCache.cache(source, clazz);
+ }
+ }
+
+
}
diff --git a/src/jdk/nashorn/internal/runtime/FinalScriptFunctionData.java b/src/jdk/nashorn/internal/runtime/FinalScriptFunctionData.java
index c7a410ea..f16cf41c 100644
--- a/src/jdk/nashorn/internal/runtime/FinalScriptFunctionData.java
+++ b/src/jdk/nashorn/internal/runtime/FinalScriptFunctionData.java
@@ -38,31 +38,27 @@ final class FinalScriptFunctionData extends ScriptFunctionData {
/**
* Constructor - used for bind
*
- * @param name name
- * @param arity arity
- * @param functions precompiled code
- * @param isStrict strict
- * @param isBuiltin builtin
- * @param isConstructor constructor
+ * @param name name
+ * @param arity arity
+ * @param functions precompiled code
+ * @param flags {@link ScriptFunctionData} flags
*/
- FinalScriptFunctionData(final String name, int arity, CompiledFunctions functions, final boolean isStrict, final boolean isBuiltin, final boolean isConstructor) {
- super(name, arity, isStrict, isBuiltin, isConstructor);
+ FinalScriptFunctionData(final String name, final int arity, final CompiledFunctions functions, final int flags) {
+ super(name, arity, flags);
code.addAll(functions);
}
/**
- * Constructor - used from ScriptFunction. This assumes that we have code alraedy for the
+ * Constructor - used from ScriptFunction. This assumes that we have code already for the
* method (typically a native method) and possibly specializations.
*
- * @param name name
- * @param mh method handle for generic version of method
- * @param specs specializations
- * @param isStrict strict
- * @param isBuiltin builtin
- * @param isConstructor constructor
+ * @param name name
+ * @param mh method handle for generic version of method
+ * @param specs specializations
+ * @param flags {@link ScriptFunctionData} flags
*/
- FinalScriptFunctionData(final String name, final MethodHandle mh, final MethodHandle[] specs, final boolean isStrict, final boolean isBuiltin, final boolean isConstructor) {
- super(name, arity(mh), isStrict, isBuiltin, isConstructor);
+ FinalScriptFunctionData(final String name, final MethodHandle mh, final MethodHandle[] specs, final int flags) {
+ super(name, arity(mh), flags);
addInvoker(mh);
if (specs != null) {
diff --git a/src/jdk/nashorn/internal/runtime/GlobalObject.java b/src/jdk/nashorn/internal/runtime/GlobalObject.java
index 3779f3bd..20046388 100644
--- a/src/jdk/nashorn/internal/runtime/GlobalObject.java
+++ b/src/jdk/nashorn/internal/runtime/GlobalObject.java
@@ -211,22 +211,6 @@ public interface GlobalObject {
public Object getDefaultValue(ScriptObject sobj, Class<?> typeHint);
/**
- * Find the compiled Class for the given script source, if available
- *
- * @param source Source object of the script
- * @return compiled Class object or null
- */
- public Class<?> findCachedClass(Source source);
-
- /**
- * Put the Source associated Class object in the Source-to-Class cache
- *
- * @param source Source of the script
- * @param clazz compiled Class object for the source
- */
- public void cacheClass(Source source, Class<?> clazz);
-
- /**
* Get cached InvokeByName object for the given key
* @param key key to be associated with InvokeByName object
* @param creator if InvokeByName is absent 'creator' is called to make one (lazy init)
diff --git a/src/jdk/nashorn/internal/runtime/Property.java b/src/jdk/nashorn/internal/runtime/Property.java
index d1dbe2ad..885fa712 100644
--- a/src/jdk/nashorn/internal/runtime/Property.java
+++ b/src/jdk/nashorn/internal/runtime/Property.java
@@ -84,9 +84,13 @@ public abstract class Property {
/** Can this property be undefined? */
public static final int CAN_BE_UNDEFINED = 1 << 8;
- /* Is this a function declaration property ? */
+ /** Is this a function declaration property ? */
public static final int IS_FUNCTION_DECLARATION = 1 << 9;
+ /** Is this property bound to a receiver? This means get/set operations will be delegated to
+ * a statically defined object instead of the object passed as callsite parameter. */
+ public static final int IS_BOUND = 1 << 10;
+
/** Property key. */
private final String key;
@@ -252,6 +256,16 @@ public abstract class Property {
}
/**
+ * Is this property bound to a receiver? If this method returns {@code true} get and set operations
+ * will be delegated to a statically bound object instead of the object passed as parameter.
+ *
+ * @return true if this is a bound property
+ */
+ public boolean isBound() {
+ return (flags & IS_BOUND) == IS_BOUND;
+ }
+
+ /**
* Does this property use any slots in the spill array described in
* {@link Property#isSpill}? In that case how many. Currently a property
* only uses max one spill slot, but this may change in future representations
diff --git a/src/jdk/nashorn/internal/runtime/RecompilableScriptFunctionData.java b/src/jdk/nashorn/internal/runtime/RecompilableScriptFunctionData.java
index 2b18cbc8..7e1a516a 100644
--- a/src/jdk/nashorn/internal/runtime/RecompilableScriptFunctionData.java
+++ b/src/jdk/nashorn/internal/runtime/RecompilableScriptFunctionData.java
@@ -103,9 +103,7 @@ public final class RecompilableScriptFunctionData extends ScriptFunctionData {
public RecompilableScriptFunctionData(final FunctionNode functionNode, final CodeInstaller<ScriptEnvironment> installer, final String allocatorClassName, final PropertyMap allocatorMap) {
super(functionName(functionNode),
functionNode.getParameters().size(),
- functionNode.isStrict(),
- false,
- true);
+ getFlags(functionNode));
this.functionNode = functionNode;
this.source = functionNode.getSource();
@@ -129,10 +127,11 @@ public final class RecompilableScriptFunctionData extends ScriptFunctionData {
final StringBuilder sb = new StringBuilder();
if (source != null) {
- sb.append(source.getName())
- .append(':')
- .append(functionNode.getLineNumber())
- .append(' ');
+ sb.append(source.getName());
+ if (functionNode != null) {
+ sb.append(':').append(functionNode.getLineNumber());
+ }
+ sb.append(' ');
}
return sb.toString() + super.toString();
@@ -159,6 +158,20 @@ public final class RecompilableScriptFunctionData extends ScriptFunctionData {
return Token.toDesc(TokenType.FUNCTION, position, length);
}
+ private static int getFlags(final FunctionNode functionNode) {
+ int flags = IS_CONSTRUCTOR;
+ if (functionNode.isStrict()) {
+ flags |= IS_STRICT;
+ }
+ if (functionNode.needsCallee()) {
+ flags |= NEEDS_CALLEE;
+ }
+ if (functionNode.usesThis() || functionNode.hasEval()) {
+ flags |= USES_THIS;
+ }
+ return flags;
+ }
+
@Override
ScriptObject allocate(final PropertyMap map) {
try {
@@ -182,41 +195,42 @@ public final class RecompilableScriptFunctionData extends ScriptFunctionData {
return allocatorMap;
}
+
+ @Override
+ protected void ensureCompiled() {
+ if (functionNode != null && functionNode.isLazy()) {
+ Compiler.LOG.info("Trampoline hit: need to do lazy compilation of '", functionNode.getName(), "'");
+ final Compiler compiler = new Compiler(installer);
+ functionNode = compiler.compile(functionNode);
+ assert !functionNode.isLazy();
+ compiler.install(functionNode);
+ flags = getFlags(functionNode);
+ }
+ }
+
@Override
protected synchronized void ensureCodeGenerated() {
- if (!code.isEmpty()) {
- return; // nothing to do, we have code, at least some.
- }
-
- if (functionNode.isLazy()) {
- Compiler.LOG.info("Trampoline hit: need to do lazy compilation of '", functionNode.getName(), "'");
- final Compiler compiler = new Compiler(installer);
- functionNode = compiler.compile(functionNode);
- assert !functionNode.isLazy();
- compiler.install(functionNode);
-
- /*
- * We don't need to update any flags - varArgs and needsCallee are instrincic
- * in the function world we need to get a destination node from the compile instead
- * and replace it with our function node. TODO
- */
- }
-
- /*
- * We can't get to this program point unless we have bytecode, either from
- * eager compilation or from running a lazy compile on the lines above
- */
-
- assert functionNode.hasState(CompilationState.EMITTED) : functionNode.getName() + " " + functionNode.getState() + " " + Debug.id(functionNode);
-
- // code exists - look it up and add it into the automatically sorted invoker list
- addCode(functionNode);
-
- if (! functionNode.canSpecialize()) {
- // allow GC to claim IR stuff that is not needed anymore
- functionNode = null;
- installer = null;
- }
+ if (!code.isEmpty()) {
+ return; // nothing to do, we have code, at least some.
+ }
+
+ ensureCompiled();
+
+ /*
+ * We can't get to this program point unless we have bytecode, either from
+ * eager compilation or from running a lazy compile on the lines above
+ */
+
+ assert functionNode.hasState(CompilationState.EMITTED) : functionNode.getName() + " " + functionNode.getState() + " " + Debug.id(functionNode);
+
+ // code exists - look it up and add it into the automatically sorted invoker list
+ addCode(functionNode);
+
+ if (! functionNode.canSpecialize()) {
+ // allow GC to claim IR stuff that is not needed anymore
+ functionNode = null;
+ installer = null;
+ }
}
private MethodHandle addCode(final FunctionNode fn) {
diff --git a/src/jdk/nashorn/internal/runtime/ScriptFunction.java b/src/jdk/nashorn/internal/runtime/ScriptFunction.java
index 0f5174ea..d0c6989b 100644
--- a/src/jdk/nashorn/internal/runtime/ScriptFunction.java
+++ b/src/jdk/nashorn/internal/runtime/ScriptFunction.java
@@ -66,6 +66,8 @@ public abstract class ScriptFunction extends ScriptObject {
private static final MethodHandle WRAPFILTER = findOwnMH("wrapFilter", Object.class, Object.class);
+ private static final MethodHandle GLOBALFILTER = findOwnMH("globalFilter", Object.class, Object.class);
+
/** method handle to scope getter for this ScriptFunction */
public static final Call GET_SCOPE = virtualCallNoLookup(ScriptFunction.class, "getScope", ScriptObject.class);
@@ -91,9 +93,7 @@ public abstract class ScriptFunction extends ScriptObject {
* @param map property map
* @param scope scope
* @param specs specialized version of this function - other method handles
- * @param strict is this a strict mode function?
- * @param builtin is this a built in function?
- * @param isConstructor is this a constructor?
+ * @param flags {@link ScriptFunctionData} flags
*/
protected ScriptFunction(
final String name,
@@ -101,11 +101,9 @@ public abstract class ScriptFunction extends ScriptObject {
final PropertyMap map,
final ScriptObject scope,
final MethodHandle[] specs,
- final boolean strict,
- final boolean builtin,
- final boolean isConstructor) {
+ final int flags) {
- this(new FinalScriptFunctionData(name, methodHandle, specs, strict, builtin, isConstructor), map, scope);
+ this(new FinalScriptFunctionData(name, methodHandle, specs, flags), map, scope);
}
/**
@@ -480,6 +478,13 @@ public abstract class ScriptFunction extends ScriptObject {
return ((GlobalObject)Context.getGlobalTrusted()).wrapAsObject(obj);
}
+
+ @SuppressWarnings("unused")
+ private static Object globalFilter(final Object object) {
+ // replace whatever we get with the current global object
+ return Context.getGlobalTrusted();
+ }
+
/**
* dyn:call call site signature: (callee, thiz, [args...])
* generated method signature: (callee, thiz, [args...])
@@ -495,11 +500,11 @@ public abstract class ScriptFunction extends ScriptObject {
@Override
protected GuardedInvocation findCallMethod(final CallSiteDescriptor desc, final LinkRequest request) {
final MethodType type = desc.getMethodType();
+ final boolean scopeCall = NashornCallSiteDescriptor.isScope(desc);
if (request.isCallSiteUnstable()) {
- // (this, callee, args...) => (this, callee, args[])
- final MethodHandle collector = MH.asCollector(ScriptRuntime.APPLY.methodHandle(), Object[].class,
- type.parameterCount() - 2);
+ // (callee, this, args...) => (callee, this, args[])
+ final MethodHandle collector = MH.asCollector(ScriptRuntime.APPLY.methodHandle(), Object[].class, type.parameterCount() - 2);
// If call site is statically typed to take a ScriptFunction, we don't need a guard, otherwise we need a
// generic "is this a ScriptFunction?" guard.
@@ -510,17 +515,12 @@ public abstract class ScriptFunction extends ScriptObject {
MethodHandle boundHandle;
MethodHandle guard = null;
- final boolean scopeCall = NashornCallSiteDescriptor.isScope(desc);
-
if (data.needsCallee()) {
final MethodHandle callHandle = getBestInvoker(type, request.getArguments());
- if (scopeCall) {
+ if (scopeCall && needsWrappedThis()) {
// Make a handle that drops the passed "this" argument and substitutes either Global or Undefined
- // (callee, this, args...) => (callee, args...)
- boundHandle = MH.insertArguments(callHandle, 1, needsWrappedThis() ? Context.getGlobalTrusted() : ScriptRuntime.UNDEFINED);
- // (callee, args...) => (callee, [this], args...)
- boundHandle = MH.dropArguments(boundHandle, 1, Object.class);
-
+ // (callee, this, args...) => (callee, [this], args...)
+ boundHandle = MH.filterArguments(callHandle, 1, GLOBALFILTER);
} else {
// It's already (callee, this, args...), just what we need
boundHandle = callHandle;
@@ -531,12 +531,12 @@ public abstract class ScriptFunction extends ScriptObject {
// NOTE: the only built-in named "extend" is NativeJava.extend. As a special-case we're binding the
// current lookup as its "this" so it can do security-sensitive creation of adapter classes.
boundHandle = MH.dropArguments(MH.bindTo(callHandle, desc.getLookup()), 0, Object.class, Object.class);
- } else if (scopeCall) {
+ } else if (scopeCall && needsWrappedThis()) {
// Make a handle that drops the passed "this" argument and substitutes either Global or Undefined
- // (this, args...) => (args...)
- boundHandle = MH.bindTo(callHandle, needsWrappedThis() ? Context.getGlobalTrusted() : ScriptRuntime.UNDEFINED);
- // (args...) => ([callee], [this], args...)
- boundHandle = MH.dropArguments(boundHandle, 0, Object.class, Object.class);
+ // (this, args...) => ([this], args...)
+ boundHandle = MH.filterArguments(callHandle, 0, GLOBALFILTER);
+ // ([this], args...) => ([callee], [this], args...)
+ boundHandle = MH.dropArguments(boundHandle, 0, Object.class);
} else {
// (this, args...) => ([callee], this, args...)
boundHandle = MH.dropArguments(callHandle, 0, Object.class);
diff --git a/src/jdk/nashorn/internal/runtime/ScriptFunctionData.java b/src/jdk/nashorn/internal/runtime/ScriptFunctionData.java
index f0569937..de39449b 100644
--- a/src/jdk/nashorn/internal/runtime/ScriptFunctionData.java
+++ b/src/jdk/nashorn/internal/runtime/ScriptFunctionData.java
@@ -47,33 +47,44 @@ public abstract class ScriptFunctionData {
/** All versions of this function that have been generated to code */
protected final CompiledFunctions code;
- private int arity;
-
- private final boolean isStrict;
-
- private final boolean isBuiltin;
+ /** Function flags */
+ protected int flags;
- private final boolean isConstructor;
+ private int arity;
private static final MethodHandle NEWFILTER = findOwnMH("newFilter", Object.class, Object.class, Object.class);
private static final MethodHandle BIND_VAR_ARGS = findOwnMH("bindVarArgs", Object[].class, Object[].class, Object[].class);
+ /** Is this a strict mode function? */
+ public static final int IS_STRICT = 1 << 0;
+ /** Is this a built-in function? */
+ public static final int IS_BUILTIN = 1 << 1;
+ /** Is this a constructor function? */
+ public static final int IS_CONSTRUCTOR = 1 << 2;
+ /** Does this function expect a callee argument? */
+ public static final int NEEDS_CALLEE = 1 << 3;
+ /** Does this function make use of the this-object argument? */
+ public static final int USES_THIS = 1 << 4;
+
+ /** Flag for strict or built-in functions */
+ public static final int IS_STRICT_OR_BUILTIN = IS_STRICT | IS_BUILTIN;
+ /** Flag for built-in constructors */
+ public static final int IS_BUILTIN_CONSTRUCTOR = IS_BUILTIN | IS_CONSTRUCTOR;
+ /** Flag for strict constructors */
+ public static final int IS_STRICT_CONSTRUCTOR = IS_STRICT | IS_CONSTRUCTOR;
+
/**
* Constructor
*
* @param name script function name
* @param arity arity
- * @param isStrict is the function strict
- * @param isBuiltin is the function built in
- * @param isConstructor is the function a constructor
+ * @param flags the function flags
*/
- ScriptFunctionData(final String name, final int arity, final boolean isStrict, final boolean isBuiltin, final boolean isConstructor) {
- this.name = name;
- this.arity = arity;
- this.code = new CompiledFunctions();
- this.isStrict = isStrict;
- this.isBuiltin = isBuiltin;
- this.isConstructor = isConstructor;
+ ScriptFunctionData(final String name, final int arity, final int flags) {
+ this.name = name;
+ this.arity = arity;
+ this.code = new CompiledFunctions();
+ this.flags = flags;
}
final int getArity() {
@@ -105,21 +116,21 @@ public abstract class ScriptFunctionData {
* @return true if strict, false otherwise
*/
public boolean isStrict() {
- return isStrict;
+ return (flags & IS_STRICT) != 0;
}
boolean isBuiltin() {
- return isBuiltin;
+ return (flags & IS_BUILTIN) != 0;
}
boolean isConstructor() {
- return isConstructor;
+ return (flags & IS_CONSTRUCTOR) != 0;
}
boolean needsCallee() {
- // we don't know if we need a callee or not unless we are generated
- ensureCodeGenerated();
- return code.needsCallee();
+ // we don't know if we need a callee or not unless code has been compiled
+ ensureCompiled();
+ return (flags & NEEDS_CALLEE) != 0;
}
/**
@@ -128,7 +139,7 @@ public abstract class ScriptFunctionData {
* @return true if this argument must be an object
*/
boolean needsWrappedThis() {
- return !isStrict && !isBuiltin;
+ return (flags & USES_THIS) != 0 && (flags & IS_STRICT_OR_BUILTIN) == 0;
}
String toSource() {
@@ -202,6 +213,15 @@ public abstract class ScriptFunctionData {
}
/**
+ * If we can have lazy code generation, this is a hook to ensure that the code has been compiled.
+ * This does not guarantee the code been installed in this {@code ScriptFunctionData} instance;
+ * use {@link #ensureCodeGenerated()} to install the actual method handles.
+ */
+ protected void ensureCompiled() {
+ //empty
+ }
+
+ /**
* Return a generic Object/Object invoker for this method. It will ensure code
* is generated, get the most generic of all versions of this function and adapt it
* to Objects.
@@ -259,6 +279,8 @@ public abstract class ScriptFunctionData {
final Object[] allArgs = args == null ? ScriptRuntime.EMPTY_ARRAY : args;
final int length = args == null ? 0 : args.length;
+ // Clear the callee and this flags
+ final int boundFlags = flags & ~NEEDS_CALLEE & ~USES_THIS;
CompiledFunctions boundList = new CompiledFunctions();
if (code.size() == 1) {
@@ -273,8 +295,7 @@ public abstract class ScriptFunctionData {
boundList.add(bind(inv, fn, self, allArgs));
}
- ScriptFunctionData boundData = new FinalScriptFunctionData(name, arity == -1 ? -1 : Math.max(0, arity - length), boundList, isStrict(), isBuiltin(), isConstructor());
- return boundData;
+ return new FinalScriptFunctionData(name, arity == -1 ? -1 : Math.max(0, arity - length), boundList, boundFlags);
}
/**
diff --git a/src/jdk/nashorn/internal/runtime/ScriptObject.java b/src/jdk/nashorn/internal/runtime/ScriptObject.java
index b7f9c4c1..4696bdea 100644
--- a/src/jdk/nashorn/internal/runtime/ScriptObject.java
+++ b/src/jdk/nashorn/internal/runtime/ScriptObject.java
@@ -66,6 +66,7 @@ import jdk.nashorn.internal.lookup.Lookup;
import jdk.nashorn.internal.lookup.MethodHandleFactory;
import jdk.nashorn.internal.objects.AccessorPropertyDescriptor;
import jdk.nashorn.internal.objects.DataPropertyDescriptor;
+import jdk.nashorn.internal.objects.Global;
import jdk.nashorn.internal.runtime.arrays.ArrayData;
import jdk.nashorn.internal.runtime.arrays.ArrayIndex;
import jdk.nashorn.internal.runtime.linker.Bootstrap;
@@ -131,7 +132,8 @@ public abstract class ScriptObject implements PropertyAccess {
static final MethodHandle GETPROTO = findOwnMH("getProto", ScriptObject.class);
static final MethodHandle SETPROTOCHECK = findOwnMH("setProtoCheck", void.class, Object.class);
- static final MethodHandle MEGAMORPHIC_GET = findOwnMH("megamorphicGet", Object.class, String.class, boolean.class);
+ static final MethodHandle MEGAMORPHIC_GET = findOwnMH("megamorphicGet", Object.class, String.class, boolean.class, boolean.class);
+ static final MethodHandle GLOBALFILTER = findOwnMH("globalFilter", Object.class, Object.class);
static final MethodHandle SETFIELD = findOwnMH("setField", void.class, CallSiteDescriptor.class, PropertyMap.class, PropertyMap.class, MethodHandle.class, Object.class, Object.class);
static final MethodHandle SETSPILL = findOwnMH("setSpill", void.class, CallSiteDescriptor.class, PropertyMap.class, PropertyMap.class, int.class, Object.class, Object.class);
@@ -225,6 +227,7 @@ public abstract class ScriptObject implements PropertyAccess {
final Property oldProp = newMap.findProperty(key);
if (oldProp == null) {
if (property instanceof UserAccessorProperty) {
+ // Note: we copy accessor functions to this object which is semantically different from binding.
final UserAccessorProperty prop = this.newUserAccessors(key, property.getFlags(), property.getGetterFunction(source), property.getSetterFunction(source));
newMap = newMap.addPropertyNoHistory(prop);
} else {
@@ -975,17 +978,6 @@ public abstract class ScriptObject implements PropertyAccess {
}
/**
- * Get the object value of a property
- *
- * @param find {@link FindProperty} lookup result
- *
- * @return the value of the property
- */
- protected static Object getObjectValue(final FindProperty find) {
- return find.getObjectValue();
- }
-
- /**
* Return methodHandle of value function for call.
*
* @param find data from find property.
@@ -995,7 +987,7 @@ public abstract class ScriptObject implements PropertyAccess {
* @return value of property as a MethodHandle or null.
*/
protected MethodHandle getCallMethodHandle(final FindProperty find, final MethodType type, final String bindName) {
- return getCallMethodHandle(getObjectValue(find), type, bindName);
+ return getCallMethodHandle(find.getObjectValue(), type, bindName);
}
/**
@@ -1019,7 +1011,7 @@ public abstract class ScriptObject implements PropertyAccess {
* @return Value of property.
*/
public final Object getWithProperty(final Property property) {
- return getObjectValue(new FindProperty(this, this, property));
+ return new FindProperty(this, this, property).getObjectValue();
}
/**
@@ -1740,7 +1732,7 @@ public abstract class ScriptObject implements PropertyAccess {
protected GuardedInvocation findGetMethod(final CallSiteDescriptor desc, final LinkRequest request, final String operator) {
final String name = desc.getNameToken(CallSiteDescriptor.NAME_OPERAND);
if (request.isCallSiteUnstable() || hasWithScope()) {
- return findMegaMorphicGetMethod(desc, name, "getMethod".equals(operator));
+ return findMegaMorphicGetMethod(desc, name, "getMethod".equals(operator), isScope() && NashornCallSiteDescriptor.isScope(desc));
}
final FindProperty find = findProperty(name, true);
@@ -1765,9 +1757,8 @@ public abstract class ScriptObject implements PropertyAccess {
final Property property = find.getProperty();
methodHandle = find.getGetter(returnType);
- final boolean noGuard = ObjectClassGenerator.OBJECT_FIELDS_ONLY && NashornCallSiteDescriptor.isFastScope(desc) && !property.canChangeType();
- // getMap() is fine as we have the prototype switchpoint depending on where the property was found
- final MethodHandle guard = noGuard ? null : NashornGuards.getMapGuard(getMap());
+ // Get the appropriate guard for this callsite and property.
+ final MethodHandle guard = NashornGuards.getGuard(this, property, desc);
final ScriptObject owner = find.getOwner();
if (methodHandle != null) {
@@ -1777,31 +1768,32 @@ public abstract class ScriptObject implements PropertyAccess {
}
if (!property.hasGetterFunction(owner)) {
- // If not a scope bind to actual prototype as changing prototype will change the property map.
- // For scopes we install a filter that replaces the self object with the prototype owning the property.
- methodHandle = isScope() ?
- addProtoFilter(methodHandle, find.getProtoChainLength()) :
- bindTo(methodHandle, owner);
+ // Add a filter that replaces the self object with the prototype owning the property.
+ methodHandle = addProtoFilter(methodHandle, find.getProtoChainLength());
}
- return new GuardedInvocation(methodHandle, noGuard ? null : getProtoSwitchPoint(name, owner), guard);
+ return new GuardedInvocation(methodHandle, guard == null ? null : getProtoSwitchPoint(name, owner), guard);
}
assert !NashornCallSiteDescriptor.isFastScope(desc);
return new GuardedInvocation(Lookup.emptyGetter(returnType), getProtoSwitchPoint(name, owner), guard);
}
- private static GuardedInvocation findMegaMorphicGetMethod(final CallSiteDescriptor desc, final String name, final boolean isMethod) {
- final MethodHandle invoker = MH.insertArguments(MEGAMORPHIC_GET, 1, name, isMethod);
+ private static GuardedInvocation findMegaMorphicGetMethod(final CallSiteDescriptor desc, final String name,
+ final boolean isMethod, final boolean isScope) {
+ final MethodHandle invoker = MH.insertArguments(MEGAMORPHIC_GET, 1, name, isMethod, isScope);
final MethodHandle guard = getScriptObjectGuard(desc.getMethodType());
return new GuardedInvocation(invoker, guard);
}
@SuppressWarnings("unused")
- private Object megamorphicGet(final String key, final boolean isMethod) {
+ private Object megamorphicGet(final String key, final boolean isMethod, final boolean isScope) {
final FindProperty find = findProperty(key, true);
if (find != null) {
- return getObjectValue(find);
+ return find.getObjectValue();
+ }
+ if (isScope) {
+ throw referenceError("not.defined", key);
}
return isMethod ? getNoSuchMethod(key) : invokeNoSuchProperty(key);
@@ -1996,6 +1988,15 @@ public abstract class ScriptObject implements PropertyAccess {
}
}
+ @SuppressWarnings("unused")
+ private static Object globalFilter(final Object object) {
+ ScriptObject sobj = (ScriptObject) object;
+ while (sobj != null && !(sobj instanceof Global)) {
+ sobj = sobj.getProto();
+ }
+ return sobj;
+ }
+
private static GuardedInvocation findMegaMorphicSetMethod(final CallSiteDescriptor desc, final String name) {
final MethodType type = desc.getMethodType().insertParameterTypes(1, Object.class);
final GuardedInvocation inv = findSetIndexMethod(type, NashornCallSiteDescriptor.isStrict(desc));
@@ -2041,7 +2042,7 @@ public abstract class ScriptObject implements PropertyAccess {
return noSuchProperty(desc, request);
}
- final Object value = getObjectValue(find);
+ final Object value = find.getObjectValue();
if (! (value instanceof ScriptFunction)) {
return createEmptyGetter(desc, name);
}
@@ -2067,7 +2068,7 @@ public abstract class ScriptObject implements PropertyAccess {
final boolean scopeAccess = isScope() && NashornCallSiteDescriptor.isScope(desc);
if (find != null) {
- final Object value = getObjectValue(find);
+ final Object value = find.getObjectValue();
ScriptFunction func = null;
MethodHandle methodHandle = null;
@@ -2102,7 +2103,7 @@ public abstract class ScriptObject implements PropertyAccess {
final FindProperty find = findProperty(NO_SUCH_PROPERTY_NAME, true);
if (find != null) {
- final Object func = getObjectValue(find);
+ final Object func = find.getObjectValue();
if (func instanceof ScriptFunction) {
return ScriptRuntime.apply((ScriptFunction)func, this, name);
@@ -2124,7 +2125,7 @@ public abstract class ScriptObject implements PropertyAccess {
return invokeNoSuchProperty(name);
}
- final Object value = getObjectValue(find);
+ final Object value = find.getObjectValue();
if (! (value instanceof ScriptFunction)) {
return UNDEFINED;
}
@@ -2664,7 +2665,7 @@ public abstract class ScriptObject implements PropertyAccess {
final FindProperty find = object.findProperty(key, false, false, this);
if (find != null) {
- return getObjectValue(find);
+ return find.getObjectValue();
}
}
@@ -2682,7 +2683,7 @@ public abstract class ScriptObject implements PropertyAccess {
final FindProperty find = findProperty(key, true);
if (find != null) {
- return getObjectValue(find);
+ return find.getObjectValue();
}
}
@@ -2823,7 +2824,15 @@ public abstract class ScriptObject implements PropertyAccess {
throw typeError("object.non.extensible", key, ScriptRuntime.safeToString(this));
}
} else {
- spill(key, value);
+ ScriptObject sobj = this;
+ // undefined scope properties are set in the global object.
+ if (isScope()) {
+ while (sobj != null && !(sobj instanceof Global)) {
+ sobj = sobj.getProto();
+ }
+ assert sobj != null : "no parent global object in scope";
+ }
+ sobj.spill(key, value);
}
}
diff --git a/src/jdk/nashorn/internal/runtime/SetMethodCreator.java b/src/jdk/nashorn/internal/runtime/SetMethodCreator.java
index bdbc8ec8..2c01b6d3 100644
--- a/src/jdk/nashorn/internal/runtime/SetMethodCreator.java
+++ b/src/jdk/nashorn/internal/runtime/SetMethodCreator.java
@@ -31,7 +31,6 @@ import static jdk.nashorn.internal.lookup.Lookup.MH;
import java.lang.invoke.MethodHandle;
import jdk.internal.dynalink.CallSiteDescriptor;
import jdk.internal.dynalink.linker.GuardedInvocation;
-import jdk.nashorn.internal.codegen.ObjectClassGenerator;
import jdk.nashorn.internal.lookup.Lookup;
import jdk.nashorn.internal.runtime.linker.NashornCallSiteDescriptor;
import jdk.nashorn.internal.runtime.linker.NashornGuards;
@@ -104,21 +103,9 @@ final class SetMethodCreator {
* @return the composed guarded invocation that represents the dynamic setter method for the property.
*/
GuardedInvocation createGuardedInvocation() {
- return new GuardedInvocation(methodHandle, getGuard());
+ return new GuardedInvocation(methodHandle, NashornGuards.getGuard(sobj, property, desc));
}
- private MethodHandle getGuard() {
- return needsNoGuard() ? null : NashornGuards.getMapGuard(getMap());
- }
-
- private boolean needsNoGuard() {
- return NashornCallSiteDescriptor.isFastScope(desc) &&
- (ObjectClassGenerator.OBJECT_FIELDS_ONLY || isPropertyTypeStable());
- }
-
- private boolean isPropertyTypeStable() {
- return property == null || !property.canChangeType();
- }
}
private SetMethod createSetMethod() {
@@ -153,10 +140,7 @@ final class SetMethodCreator {
final MethodHandle boundHandle;
if (!property.hasSetterFunction(find.getOwner()) && find.isInherited()) {
- // Bind or add prototype filter depending on whether this is a scope object.
- boundHandle = sobj.isScope() ?
- ScriptObject.addProtoFilter(methodHandle, find.getProtoChainLength()):
- ScriptObject.bindTo(methodHandle, find.getOwner());
+ boundHandle = ScriptObject.addProtoFilter(methodHandle, find.getProtoChainLength());
} else {
boundHandle = methodHandle;
}
@@ -165,7 +149,7 @@ final class SetMethodCreator {
private SetMethod createGlobalPropertySetter() {
final ScriptObject global = Context.getGlobalTrusted();
- return new SetMethod(ScriptObject.bindTo(global.addSpill(getName()), global), null);
+ return new SetMethod(MH.filterArguments(global.addSpill(getName()), 0, ScriptObject.GLOBALFILTER), null);
}
private SetMethod createNewPropertySetter() {
diff --git a/src/jdk/nashorn/internal/runtime/WithObject.java b/src/jdk/nashorn/internal/runtime/WithObject.java
index d4a28cd7..dc48d727 100644
--- a/src/jdk/nashorn/internal/runtime/WithObject.java
+++ b/src/jdk/nashorn/internal/runtime/WithObject.java
@@ -88,6 +88,11 @@ public final class WithObject extends ScriptObject implements Scope {
@Override
public GuardedInvocation lookup(final CallSiteDescriptor desc, final LinkRequest request) {
+ if (request.isCallSiteUnstable()) {
+ // Fall back to megamorphic invocation which performs a complete lookup each time without further relinking.
+ return super.lookup(desc, request);
+ }
+
// With scopes can never be observed outside of Nashorn code, so all call sites that can address it will of
// necessity have a Nashorn descriptor - it is safe to cast.
final NashornCallSiteDescriptor ndesc = (NashornCallSiteDescriptor)desc;
@@ -265,7 +270,7 @@ public final class WithObject extends ScriptObject implements Scope {
}
private static MethodHandle filter(final MethodHandle mh, final MethodHandle filter) {
- return MH.filterArguments(mh, 0, filter);
+ return MH.filterArguments(mh, 0, filter.asType(filter.type().changeReturnType(mh.type().parameterType(0))));
}
/**
diff --git a/src/jdk/nashorn/internal/runtime/linker/NashornGuards.java b/src/jdk/nashorn/internal/runtime/linker/NashornGuards.java
index 0b720be2..ca3e10c4 100644
--- a/src/jdk/nashorn/internal/runtime/linker/NashornGuards.java
+++ b/src/jdk/nashorn/internal/runtime/linker/NashornGuards.java
@@ -29,6 +29,11 @@ import static jdk.nashorn.internal.lookup.Lookup.MH;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
+import java.lang.ref.WeakReference;
+import jdk.internal.dynalink.CallSiteDescriptor;
+import jdk.nashorn.internal.codegen.ObjectClassGenerator;
+import jdk.nashorn.internal.objects.Global;
+import jdk.nashorn.internal.runtime.Property;
import jdk.nashorn.internal.runtime.PropertyMap;
import jdk.nashorn.internal.runtime.ScriptFunction;
import jdk.nashorn.internal.runtime.ScriptObject;
@@ -40,6 +45,7 @@ public final class NashornGuards {
private static final MethodHandle IS_SCRIPTOBJECT = findOwnMH("isScriptObject", boolean.class, Object.class);
private static final MethodHandle IS_SCRIPTFUNCTION = findOwnMH("isScriptFunction", boolean.class, Object.class);
private static final MethodHandle IS_MAP = findOwnMH("isMap", boolean.class, Object.class, PropertyMap.class);
+ private static final MethodHandle SAME_OBJECT = findOwnMH("sameObject", boolean.class, Object.class, WeakReference.class);
private static final MethodHandle IS_INSTANCEOF_2 = findOwnMH("isInstanceOf2", boolean.class, Object.class, Class.class, Class.class);
// don't create me!
@@ -75,6 +81,55 @@ public final class NashornGuards {
}
/**
+ * Determine whether the given callsite needs a guard.
+ * @param property the property, or null
+ * @param desc the callsite descriptor
+ * @return true if a guard should be used for this callsite
+ */
+ static boolean needsGuard(final Property property, final CallSiteDescriptor desc) {
+ return property == null || property.isConfigurable()
+ || property.isBound() || !ObjectClassGenerator.OBJECT_FIELDS_ONLY
+ || !NashornCallSiteDescriptor.isFastScope(desc) || property.canChangeType();
+ }
+
+ /**
+ * Get the guard for a property access. This returns an identity guard for non-configurable global properties
+ * and a map guard for everything else.
+ *
+ * @param sobj the first object in the prototype chain
+ * @param property the property
+ * @param desc the callsite descriptor
+ * @return method handle for guard
+ */
+ public static MethodHandle getGuard(final ScriptObject sobj, final Property property, final CallSiteDescriptor desc) {
+ if (!needsGuard(property, desc)) {
+ return null;
+ }
+ if (NashornCallSiteDescriptor.isScope(desc)) {
+ if (property != null && property.isBound()) {
+ // This is a declared top level variables in main script or eval, use identity guard.
+ return getIdentityGuard(sobj);
+ }
+ if (!(sobj instanceof Global) && (property == null || property.isConfigurable())) {
+ // Undeclared variables in nested evals need stronger guards
+ return combineGuards(getIdentityGuard(sobj), getMapGuard(sobj.getMap()));
+ }
+ }
+ return getMapGuard(sobj.getMap());
+ }
+
+
+ /**
+ * Get a guard that checks referential identity of the current object.
+ *
+ * @param sobj the self object
+ * @return true if same self object instance
+ */
+ public static MethodHandle getIdentityGuard(final ScriptObject sobj) {
+ return MH.insertArguments(SAME_OBJECT, 1, new WeakReference<>(sobj));
+ }
+
+ /**
* Get a guard that checks if in item is an instance of either of two classes.
*
* @param class1 the first class
@@ -112,6 +167,11 @@ public final class NashornGuards {
}
@SuppressWarnings("unused")
+ private static boolean sameObject(final Object self, final WeakReference<ScriptObject> ref) {
+ return self == ref.get();
+ }
+
+ @SuppressWarnings("unused")
private static boolean isInstanceOf2(final Object self, final Class<?> class1, final Class<?> class2) {
return class1.isInstance(self) || class2.isInstance(self);
}
diff --git a/src/jdk/nashorn/internal/runtime/linker/PrimitiveLookup.java b/src/jdk/nashorn/internal/runtime/linker/PrimitiveLookup.java
index c9b5eaa3..7665be7f 100644
--- a/src/jdk/nashorn/internal/runtime/linker/PrimitiveLookup.java
+++ b/src/jdk/nashorn/internal/runtime/linker/PrimitiveLookup.java
@@ -35,6 +35,7 @@ import jdk.internal.dynalink.linker.LinkRequest;
import jdk.internal.dynalink.support.CallSiteDescriptorFactory;
import jdk.internal.dynalink.support.Guards;
import jdk.nashorn.internal.lookup.Lookup;
+import jdk.nashorn.internal.runtime.FindProperty;
import jdk.nashorn.internal.runtime.ScriptObject;
/**
@@ -61,8 +62,9 @@ public final class PrimitiveLookup {
* type {@code receiverClass}.
*/
public static GuardedInvocation lookupPrimitive(final LinkRequest request, final Class<?> receiverClass,
- final ScriptObject wrappedReceiver, final MethodHandle wrapFilter) {
- return lookupPrimitive(request, Guards.getInstanceOfGuard(receiverClass), wrappedReceiver, wrapFilter);
+ final ScriptObject wrappedReceiver, final MethodHandle wrapFilter,
+ final MethodHandle protoFilter) {
+ return lookupPrimitive(request, Guards.getInstanceOfGuard(receiverClass), wrappedReceiver, wrapFilter, protoFilter);
}
/**
@@ -79,7 +81,8 @@ public final class PrimitiveLookup {
* type (that is implied by both {@code guard} and {@code wrappedReceiver}).
*/
public static GuardedInvocation lookupPrimitive(final LinkRequest request, final MethodHandle guard,
- final ScriptObject wrappedReceiver, final MethodHandle wrapFilter) {
+ final ScriptObject wrappedReceiver, final MethodHandle wrapFilter,
+ final MethodHandle protoFilter) {
final CallSiteDescriptor desc = request.getCallSiteDescriptor();
final String operator = CallSiteDescriptorFactory.tokenizeOperators(desc).get(0);
if ("setProp".equals(operator) || "setElem".equals(operator)) {
@@ -93,9 +96,23 @@ public final class PrimitiveLookup {
if(desc.getNameTokenCount() > 2) {
final String name = desc.getNameToken(CallSiteDescriptor.NAME_OPERAND);
- if(wrappedReceiver.findProperty(name, true) == null) {
+ final FindProperty find = wrappedReceiver.findProperty(name, true);
+ if(find == null) {
// Give up early, give chance to BeanLinker and NashornBottomLinker to deal with it.
return null;
+ } else if (find.isInherited() && !find.getProperty().hasGetterFunction(find.getOwner())) {
+ // If property is found in the prototype object bind the method handle directly to
+ // the proto filter instead of going through wrapper instantiation below.
+ final ScriptObject proto = wrappedReceiver.getProto();
+ final GuardedInvocation link = proto.lookup(desc, request);
+
+ if (link != null) {
+ final MethodHandle invocation = link.getInvocation();
+ final MethodHandle adaptedInvocation = MH.asType(invocation, invocation.type().changeParameterType(0, Object.class));
+ final MethodHandle method = MH.filterArguments(adaptedInvocation, 0, protoFilter);
+ final MethodHandle protoGuard = MH.filterArguments(link.getGuard(), 0, protoFilter);
+ return new GuardedInvocation(method, NashornGuards.combineGuards(guard, protoGuard));
+ }
}
}
final GuardedInvocation link = wrappedReceiver.lookup(desc, request);