8098808: Convert Scope from interface to class
authorhannesw
Wed, 17 Jun 2015 13:56:53 +0200
changeset 31199 17932ffc49b5
parent 31198 f807ce35a661
child 31200 1212849c3200
8098808: Convert Scope from interface to class Reviewed-by: sundar, attila
nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/objects/Global.java
nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/objects/NativeDebug.java
nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/Context.java
nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/FunctionScope.java
nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/Scope.java
nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/ScriptObject.java
nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/WithObject.java
--- a/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/objects/Global.java	Wed Jun 17 14:21:20 2015 +0530
+++ b/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/objects/Global.java	Wed Jun 17 13:56:53 2015 +0200
@@ -87,7 +87,7 @@
  * Representation of global scope.
  */
 @ScriptClass("Global")
-public final class Global extends ScriptObject implements Scope {
+public final class Global extends Scope {
     // Placeholder value used in place of a location property (__FILE__, __DIR__, __LINE__)
     private static final Object LOCATION_PROPERTY_PLACEHOLDER = new Object();
     private final InvokeByName TO_STRING = new InvokeByName("toString", ScriptObject.class);
@@ -906,9 +906,6 @@
      */
     private ScriptFunction typeErrorThrower;
 
-    // Flag to indicate that a split method issued a return statement
-    private int splitState = -1;
-
     // Used to store the last RegExp result to support deprecated RegExp constructor properties
     private RegExpResult lastRegExpResult;
 
@@ -995,7 +992,6 @@
     public Global(final Context context) {
         super(checkAndGetMap(context));
         this.context = context;
-        this.setIsScope();
         this.lexicalScope = context.getEnv()._es6 ? new LexicalScope(this) : null;
     }
 
@@ -2355,26 +2351,6 @@
     }
 
     /**
-     * Get the current split state.
-     *
-     * @return current split state
-     */
-    @Override
-    public int getSplitState() {
-        return splitState;
-    }
-
-    /**
-     * Set the current split state.
-     *
-     * @param state current split state
-     */
-    @Override
-    public void setSplitState(final int state) {
-        splitState = state;
-    }
-
-    /**
      * Return the ES6 global scope for lexically declared bindings.
      * @return the ES6 lexical global scope.
      */
--- a/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/objects/NativeDebug.java	Wed Jun 17 14:21:20 2015 +0530
+++ b/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/objects/NativeDebug.java	Wed Jun 17 13:56:53 2015 +0200
@@ -37,6 +37,7 @@
 import jdk.nashorn.internal.runtime.JSType;
 import jdk.nashorn.internal.runtime.PropertyListeners;
 import jdk.nashorn.internal.runtime.PropertyMap;
+import jdk.nashorn.internal.runtime.Scope;
 import jdk.nashorn.internal.runtime.ScriptFunction;
 import jdk.nashorn.internal.runtime.ScriptObject;
 import jdk.nashorn.internal.runtime.ScriptRuntime;
@@ -245,7 +246,7 @@
         final PrintWriter out = Context.getCurrentErr();
 
         out.println("ScriptObject count " + ScriptObject.getCount());
-        out.println("Scope count " + ScriptObject.getScopeCount());
+        out.println("Scope count " + Scope.getCount());
         out.println("ScriptObject listeners added " + PropertyListeners.getListenersAdded());
         out.println("ScriptObject listeners removed " + PropertyListeners.getListenersRemoved());
         out.println("ScriptFunction constructor calls " + ScriptFunction.getConstructorCount());
--- a/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/Context.java	Wed Jun 17 14:21:20 2015 +0530
+++ b/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/Context.java	Wed Jun 17 13:56:53 2015 +0200
@@ -741,7 +741,7 @@
     }
 
     private static ScriptObject newScope(final ScriptObject callerScope) {
-        return new FunctionScope(PropertyMap.newMap(FunctionScope.class), callerScope);
+        return new Scope(callerScope, PropertyMap.newMap(Scope.class));
     }
 
     private static Source loadInternal(final String srcStr, final String prefix, final String resourcePath) {
--- a/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/FunctionScope.java	Wed Jun 17 14:21:20 2015 +0530
+++ b/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/FunctionScope.java	Wed Jun 17 13:56:53 2015 +0200
@@ -35,17 +35,12 @@
  *
  * The constructor of this class is responsible for any function prologue
  * involving the scope.
- *
- * TODO see NASHORN-715.
  */
-public class FunctionScope extends ScriptObject implements Scope {
+public class FunctionScope extends Scope {
 
     /** Area to store scope arguments. (public for access from scripts.) */
     public final ScriptObject arguments;
 
-    /** Flag to indicate that a split method issued a return statement */
-    private int splitState = -1;
-
     /**
      * Constructor
      *
@@ -56,7 +51,6 @@
     public FunctionScope(final PropertyMap map, final ScriptObject callerScope, final ScriptObject arguments) {
         super(callerScope, map);
         this.arguments = arguments;
-        setIsScope();
     }
 
     /**
@@ -68,7 +62,6 @@
     public FunctionScope(final PropertyMap map, final ScriptObject callerScope) {
         super(callerScope, map);
         this.arguments = null;
-        setIsScope();
     }
 
     /**
@@ -82,23 +75,4 @@
         super(map, primitiveSpill, objectSpill);
         this.arguments = null;
     }
-
-
-    /**
-     * Get the current split state.
-     * @return current split state
-     */
-    @Override
-    public int getSplitState() {
-        return splitState;
-    }
-
-    /**
-     * Set the current split state.
-     * @param state current split state
-     */
-    @Override
-    public void setSplitState(final int state) {
-        splitState = state;
-    }
 }
--- a/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/Scope.java	Wed Jun 17 14:21:20 2015 +0530
+++ b/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/Scope.java	Wed Jun 17 13:56:53 2015 +0200
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2010, 2015, 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
@@ -25,30 +25,105 @@
 
 package jdk.nashorn.internal.runtime;
 
-import static jdk.nashorn.internal.codegen.CompilerConstants.interfaceCallNoLookup;
+import static jdk.nashorn.internal.codegen.CompilerConstants.virtualCallNoLookup;
 
 import jdk.nashorn.internal.codegen.CompilerConstants;
 
 /**
- * Interface implemented by {@link ScriptObject}s that act as scope.
+ * A {@link ScriptObject} subclass for objects that act as scope.
  */
-public interface Scope {
+public class Scope extends ScriptObject {
+
+    /* This is used to store return state of split functions. */
+    private int splitState = -1;
+
+    /** This is updated only in debug mode - counts number of {@code ScriptObject} instances created that are scope */
+    private static int count;
 
     /** Method handle that points to {@link Scope#getSplitState}. */
-    public static final CompilerConstants.Call GET_SPLIT_STATE = interfaceCallNoLookup(Scope.class, "getSplitState", int.class);
+    public static final CompilerConstants.Call GET_SPLIT_STATE = virtualCallNoLookup(Scope.class, "getSplitState", int.class);
+    /** Method handle that points to {@link Scope#setSplitState(int)}. */
+    public static final CompilerConstants.Call SET_SPLIT_STATE = virtualCallNoLookup(Scope.class, "setSplitState", void.class, int.class);
+
+    /**
+     * Constructor
+     *
+     * @param map initial property map
+     */
+    public Scope(final PropertyMap map) {
+        super(map);
+        if (Context.DEBUG) {
+            count++;
+        }
+    }
+
+    /**
+     * Constructor
+     *
+     * @param proto parent scope
+     * @param map   initial property map
+     */
+    public Scope(final ScriptObject proto, final PropertyMap map) {
+        super(proto, map);
+        if (Context.DEBUG) {
+            count++;
+        }
+    }
 
-    /** Method handle that points to {@link Scope#setSplitState(int)}. */
-    public static final CompilerConstants.Call SET_SPLIT_STATE = interfaceCallNoLookup(Scope.class, "setSplitState", void.class, int.class);
+    /**
+     * Constructor
+     *
+     * @param map            property map
+     * @param primitiveSpill primitive spill array
+     * @param objectSpill    reference spill array
+     */
+    public Scope(final PropertyMap map, final long[] primitiveSpill, final Object[] objectSpill) {
+        super(map, primitiveSpill, objectSpill);
+        if (Context.DEBUG) {
+            count++;
+        }
+    }
+
+    @Override
+    public boolean isScope() {
+        return true;
+    }
+
+    @Override
+    boolean hasWithScope() {
+        for (ScriptObject obj = this; obj != null; obj = obj.getProto()) {
+            if (obj instanceof WithObject) {
+                return true;
+            }
+        }
+        return false;
+    }
 
     /**
      * Get the scope's split method state.
-     * @return the current state
+     *
+     * @return current split state
      */
-    public int getSplitState();
+    public int getSplitState() {
+        return splitState;
+    }
 
     /**
      * Set the scope's split method state.
-     * @param state the new state.
+     *
+     * @param state current split state
      */
-    public void setSplitState(int state);
+    public void setSplitState(final int state) {
+        splitState = state;
+    }
+
+    /**
+     * Get number of {@code Scope} instances created. If not running in debug
+     * mode this is always 0.
+     *
+     * @return number of scope ScriptObjects created
+     */
+    public static int getScopeCount() {
+        return count;
+    }
 }
--- a/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/ScriptObject.java	Wed Jun 17 14:21:20 2015 +0530
+++ b/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/ScriptObject.java	Wed Jun 17 13:56:53 2015 +0200
@@ -109,20 +109,17 @@
     /** Search fall back routine name for "no such property" */
     public static final String NO_SUCH_PROPERTY_NAME = "__noSuchProperty__";
 
-    /** Per ScriptObject flag - is this a scope object? */
-    public static final int IS_SCOPE       = 1 << 0;
-
     /** Per ScriptObject flag - is this an array object? */
-    public static final int IS_ARRAY       = 1 << 1;
+    public static final int IS_ARRAY               = 1 << 0;
 
     /** Per ScriptObject flag - is this an arguments object? */
-    public static final int IS_ARGUMENTS   = 1 << 2;
+    public static final int IS_ARGUMENTS           = 1 << 1;
 
     /** Is length property not-writable? */
-    public static final int IS_LENGTH_NOT_WRITABLE = 1 << 3;
+    public static final int IS_LENGTH_NOT_WRITABLE = 1 << 2;
 
     /** Is this a builtin object? */
-    public static final int IS_BUILTIN = 1 << 4;
+    public static final int IS_BUILTIN             = 1 << 3;
 
     /**
      * Spill growth rate - by how many elements does {@link ScriptObject#primitiveSpill} and
@@ -1630,23 +1627,12 @@
         return getMap().isFrozen();
     }
 
-
-    /**
-     * Flag this ScriptObject as scope
-     */
-    public final void setIsScope() {
-        if (Context.DEBUG) {
-            scopeCount++;
-        }
-        flags |= IS_SCOPE;
-    }
-
     /**
      * Check whether this ScriptObject is scope
      * @return true if scope
      */
-    public final boolean isScope() {
-        return (flags & IS_SCOPE) != 0;
+    public boolean isScope() {
+        return false;
     }
 
     /**
@@ -1921,14 +1907,7 @@
      * Test whether this object contains in its prototype chain or is itself a with-object.
      * @return true if a with-object was found
      */
-    final boolean hasWithScope() {
-        if (isScope()) {
-            for (ScriptObject obj = this; obj != null; obj = obj.getProto()) {
-                if (obj instanceof WithObject) {
-                    return true;
-                }
-            }
-        }
+    boolean hasWithScope() {
         return false;
     }
 
@@ -3817,9 +3796,6 @@
     /** This is updated only in debug mode - counts number of {@code ScriptObject} instances created */
     private static int count;
 
-    /** This is updated only in debug mode - counts number of {@code ScriptObject} instances created that are scope */
-    private static int scopeCount;
-
     /**
      * Get number of {@code ScriptObject} instances created. If not running in debug
      * mode this is always 0
@@ -3829,15 +3805,4 @@
     public static int getCount() {
         return count;
     }
-
-    /**
-     * Get number of scope {@code ScriptObject} instances created. If not running in debug
-     * mode this is always 0
-     *
-     * @return number of scope ScriptObjects created
-     */
-    public static int getScopeCount() {
-        return scopeCount;
-    }
-
 }
--- a/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/WithObject.java	Wed Jun 17 14:21:20 2015 +0530
+++ b/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/WithObject.java	Wed Jun 17 13:56:53 2015 +0200
@@ -44,7 +44,7 @@
  * This class supports the handling of scope in a with body.
  *
  */
-public final class WithObject extends ScriptObject implements Scope {
+public final class WithObject extends Scope {
     private static final MethodHandle WITHEXPRESSIONGUARD    = findOwnMH("withExpressionGuard",  boolean.class, Object.class, PropertyMap.class, SwitchPoint.class);
     private static final MethodHandle WITHEXPRESSIONFILTER   = findOwnMH("withFilterExpression", Object.class, Object.class);
     private static final MethodHandle WITHSCOPEFILTER        = findOwnMH("withFilterScope",      Object.class, Object.class);
@@ -62,7 +62,6 @@
      */
     WithObject(final ScriptObject scope, final ScriptObject expression) {
         super(scope, null);
-        setIsScope();
         this.expression = expression;
     }