src/jdk/nashorn/internal/objects/ScriptFunctionImpl.java

Thu, 25 Sep 2014 15:53:47 +0200

author
lagergren
date
Thu, 25 Sep 2014 15:53:47 +0200
changeset 1028
d79265f2fa92
parent 963
e2497b11a021
child 1110
a56051d3cdf5
permissions
-rw-r--r--

8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
Reviewed-by: hannesw, attila, sundar

     1 /*
     2  * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     8  * particular file as subject to the "Classpath" exception as provided
     9  * by Oracle in the LICENSE file that accompanied this code.
    10  *
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    14  * version 2 for more details (a copy is included in the LICENSE file that
    15  * accompanied this code).
    16  *
    17  * You should have received a copy of the GNU General Public License version
    18  * 2 along with this work; if not, write to the Free Software Foundation,
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    20  *
    21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    22  * or visit www.oracle.com if you need additional information or have any
    23  * questions.
    24  */
    26 package jdk.nashorn.internal.objects;
    28 import static jdk.nashorn.internal.lookup.Lookup.MH;
    29 import static jdk.nashorn.internal.runtime.ScriptRuntime.UNDEFINED;
    31 import java.lang.invoke.MethodHandle;
    32 import java.util.ArrayList;
    34 import jdk.nashorn.internal.runtime.AccessorProperty;
    35 import jdk.nashorn.internal.runtime.GlobalFunctions;
    36 import jdk.nashorn.internal.runtime.Property;
    37 import jdk.nashorn.internal.runtime.PropertyMap;
    38 import jdk.nashorn.internal.runtime.RecompilableScriptFunctionData;
    39 import jdk.nashorn.internal.runtime.ScriptFunction;
    40 import jdk.nashorn.internal.runtime.ScriptFunctionData;
    41 import jdk.nashorn.internal.runtime.ScriptObject;
    42 import jdk.nashorn.internal.runtime.Specialization;
    44 /**
    45  * Concrete implementation of ScriptFunction. This sets correct map for the
    46  * function objects -- to expose properties like "prototype", "length" etc.
    47  */
    48 public class ScriptFunctionImpl extends ScriptFunction {
    50     /** Reference to constructor prototype. */
    51     private Object prototype;
    53     // property map for strict mode functions
    54     private static final PropertyMap strictmodemap$;
    55     // property map for bound functions
    56     private static final PropertyMap boundfunctionmap$;
    57     // property map for non-strict, non-bound functions.
    58     private static final PropertyMap map$;
    60     // Marker object for lazily initialized prototype object
    61     private static final Object LAZY_PROTOTYPE = new Object();
    63     private ScriptFunctionImpl(final String name, final MethodHandle invokeHandle, final Specialization[] specs, final Global global) {
    64         super(name, invokeHandle, map$, null, specs, ScriptFunctionData.IS_BUILTIN_CONSTRUCTOR);
    65         init(global);
    66     }
    68     /**
    69      * Constructor called by Nasgen generated code, no membercount, use the default map.
    70      * Creates builtin functions only.
    71      *
    72      * @param name name of function
    73      * @param invokeHandle handle for invocation
    74      * @param specs specialized versions of this method, if available, null otherwise
    75      */
    76     ScriptFunctionImpl(final String name, final MethodHandle invokeHandle, final Specialization[] specs) {
    77         this(name, invokeHandle, specs, Global.instance());
    78     }
    80     private ScriptFunctionImpl(final String name, final MethodHandle invokeHandle, final PropertyMap map, final Specialization[] specs, final Global global) {
    81         super(name, invokeHandle, map.addAll(map$), null, specs, ScriptFunctionData.IS_BUILTIN_CONSTRUCTOR);
    82         init(global);
    83     }
    85     /**
    86      * Constructor called by Nasgen generated code, no membercount, use the map passed as argument.
    87      * Creates builtin functions only.
    88      *
    89      * @param name name of function
    90      * @param invokeHandle handle for invocation
    91      * @param map initial property map
    92      * @param specs specialized versions of this method, if available, null otherwise
    93      */
    94     ScriptFunctionImpl(final String name, final MethodHandle invokeHandle, final PropertyMap map, final Specialization[] specs) {
    95         this(name, invokeHandle, map, specs, Global.instance());
    96     }
    98     private ScriptFunctionImpl(final String name, final MethodHandle methodHandle, final ScriptObject scope, final Specialization[] specs, final int flags, final Global global) {
    99         super(name, methodHandle, getMap(isStrict(flags)), scope, specs, flags);
   100         init(global);
   101     }
   103     /**
   104      * Constructor called by Global.newScriptFunction (runtime).
   105      *
   106      * @param name name of function
   107      * @param methodHandle handle for invocation
   108      * @param scope scope object
   109      * @param specs specialized versions of this method, if available, null otherwise
   110      * @param flags {@link ScriptFunctionData} flags
   111      */
   112     ScriptFunctionImpl(final String name, final MethodHandle methodHandle, final ScriptObject scope, final Specialization[] specs, final int flags) {
   113         this(name, methodHandle, scope, specs, flags, Global.instance());
   114     }
   116     private ScriptFunctionImpl(final RecompilableScriptFunctionData data, final ScriptObject scope, final Global global) {
   117         super(data, getMap(data.isStrict()), scope);
   118         init(global);
   119     }
   121     /**
   122      * Constructor called by (compiler) generated code for {@link ScriptObject}s.
   123      *
   124      * @param data static function data
   125      * @param scope scope object
   126      */
   127     public ScriptFunctionImpl(final RecompilableScriptFunctionData data, final ScriptObject scope) {
   128         this(data, scope, Global.instance());
   129     }
   131     /**
   132      * Only invoked internally from {@link BoundScriptFunctionImpl} constructor.
   133      * @param data the script function data for the bound function.
   134      * @param global the global object
   135      */
   136     ScriptFunctionImpl(final ScriptFunctionData data, final Global global) {
   137         super(data, boundfunctionmap$, null);
   138         init(global);
   139     }
   141     static {
   142         final ArrayList<Property> properties = new ArrayList<>(3);
   143         properties.add(AccessorProperty.create("prototype", Property.NOT_ENUMERABLE | Property.NOT_CONFIGURABLE, G$PROTOTYPE, S$PROTOTYPE));
   144         properties.add(AccessorProperty.create("length",  Property.NOT_ENUMERABLE | Property.NOT_CONFIGURABLE | Property.NOT_WRITABLE, G$LENGTH, null));
   145         properties.add(AccessorProperty.create("name", Property.NOT_ENUMERABLE | Property.NOT_CONFIGURABLE | Property.NOT_WRITABLE, G$NAME, null));
   146         map$ = PropertyMap.newMap(properties);
   147         strictmodemap$ = createStrictModeMap(map$);
   148         boundfunctionmap$ = createBoundFunctionMap(strictmodemap$);
   149     }
   151     private static PropertyMap createStrictModeMap(final PropertyMap map) {
   152         final int flags = Property.NOT_ENUMERABLE | Property.NOT_CONFIGURABLE;
   153         PropertyMap newMap = map;
   154         // Need to add properties directly to map since slots are assigned speculatively by newUserAccessors.
   155         newMap = newMap.addPropertyNoHistory(map.newUserAccessors("arguments", flags));
   156         newMap = newMap.addPropertyNoHistory(map.newUserAccessors("caller", flags));
   157         return newMap;
   158     }
   160     private static boolean isStrict(final int flags) {
   161         return (flags & ScriptFunctionData.IS_STRICT) != 0;
   162     }
   164     // Choose the map based on strict mode!
   165     private static PropertyMap getMap(final boolean strict) {
   166         return strict ? strictmodemap$ : map$;
   167     }
   169     private static PropertyMap createBoundFunctionMap(final PropertyMap strictModeMap) {
   170         // Bound function map is same as strict function map, but additionally lacks the "prototype" property, see
   171         // ECMAScript 5.1 section 15.3.4.5
   172         return strictModeMap.deleteProperty(strictModeMap.findProperty("prototype"));
   173     }
   175     // Instance of this class is used as global anonymous function which
   176     // serves as Function.prototype object.
   177     private static class AnonymousFunction extends ScriptFunctionImpl {
   178         private static final PropertyMap anonmap$ = PropertyMap.newMap();
   180         AnonymousFunction() {
   181             super("", GlobalFunctions.ANONYMOUS, anonmap$, null);
   182         }
   183     }
   185     static ScriptFunctionImpl newAnonymousFunction() {
   186         return new AnonymousFunction();
   187     }
   189     private static ScriptFunction makeFunction(final String name, final MethodHandle methodHandle, final Specialization[] specs, final int flags) {
   190         final ScriptFunctionImpl func = new ScriptFunctionImpl(name, methodHandle, null, specs, flags);
   191         func.setPrototype(UNDEFINED);
   192         // Non-constructor built-in functions do not have "prototype" property
   193         func.deleteOwnProperty(func.getMap().findProperty("prototype"));
   195         return func;
   196     }
   198     /**
   199      * Factory method for non-constructor built-in functions
   200      *
   201      * @param name   function name
   202      * @param methodHandle handle for invocation
   203      * @param specs  specialized versions of function if available, null otherwise
   204      * @return new ScriptFunction
   205      */
   206     static ScriptFunction makeFunction(final String name, final MethodHandle methodHandle, final Specialization[] specs) {
   207         return makeFunction(name, methodHandle, specs, ScriptFunctionData.IS_BUILTIN);
   208     }
   210     /**
   211      * Factory method for non-constructor built-in, strict functions
   212      *
   213      * @param name   function name
   214      * @param methodHandle handle for invocation
   215      * @return new ScriptFunction
   216      */
   217     static ScriptFunction makeStrictFunction(final String name, final MethodHandle methodHandle) {
   218         return makeFunction(name, methodHandle, null, ScriptFunctionData.IS_BUILTIN | ScriptFunctionData.IS_STRICT );
   219     }
   221     /**
   222      * Factory method for non-constructor built-in functions
   223      *
   224      * @param name   function name
   225      * @param methodHandle handle for invocation
   226      * @return new ScriptFunction
   227      */
   228     static ScriptFunction makeFunction(final String name, final MethodHandle methodHandle) {
   229         return makeFunction(name, methodHandle, null);
   230     }
   232     @Override
   233     public ScriptFunction makeSynchronizedFunction(final Object sync) {
   234         final MethodHandle mh = MH.insertArguments(ScriptFunction.INVOKE_SYNC, 0, this, sync);
   235         return makeFunction(getName(), mh);
   236     }
   238     /**
   239      * Same as {@link ScriptFunction#makeBoundFunction(Object, Object[])}. The only reason we override it is so that we
   240      * can expose it to methods in this package.
   241      * @param self the self to bind to this function. Can be null (in which case, null is bound as this).
   242      * @param args additional arguments to bind to this function. Can be null or empty to not bind additional arguments.
   243      * @return a function with the specified self and parameters bound.
   244      */
   245     @Override
   246     protected ScriptFunction makeBoundFunction(final Object self, final Object[] args) {
   247         return super.makeBoundFunction(self, args);
   248     }
   250     /**
   251      * This method is used to create a bound function based on this function.
   252      *
   253      * @param data the {@code ScriptFunctionData} specifying the functions immutable portion.
   254      * @return a function initialized from the specified data. Its parent scope will be set to null, therefore the
   255      * passed in data should not expect a callee.
   256      */
   257     @Override
   258     protected ScriptFunction makeBoundFunction(final ScriptFunctionData data) {
   259         return new BoundScriptFunctionImpl(data, getTargetFunction());
   260     }
   262     // return Object.prototype - used by "allocate"
   263     @Override
   264     protected final ScriptObject getObjectPrototype() {
   265         return Global.objectPrototype();
   266     }
   268     @Override
   269     public final Object getPrototype() {
   270         if (prototype == LAZY_PROTOTYPE) {
   271             prototype = new PrototypeObject(this);
   272         }
   273         return prototype;
   274     }
   276     @Override
   277     public final void setPrototype(final Object newProto) {
   278         if (newProto instanceof ScriptObject && newProto != this.prototype && allocatorMap != null) {
   279             // Replace our current allocator map with one that is associated with the new prototype.
   280             allocatorMap = allocatorMap.changeProto((ScriptObject)newProto);
   281         }
   282         this.prototype = newProto;
   283     }
   285     // Internals below..
   286     private void init(final Global global) {
   287         this.setInitialProto(global.getFunctionPrototype());
   288         this.prototype = LAZY_PROTOTYPE;
   290         // We have to fill user accessor functions late as these are stored
   291         // in this object rather than in the PropertyMap of this object.
   292         assert objectSpill == null;
   293         final ScriptFunction typeErrorThrower = global.getTypeErrorThrower();
   294         if (findProperty("arguments", true) != null) {
   295             initUserAccessors("arguments", Property.NOT_CONFIGURABLE | Property.NOT_ENUMERABLE, typeErrorThrower, typeErrorThrower);
   296        }
   297         if (findProperty("caller", true) != null) {
   298             initUserAccessors("caller", Property.NOT_CONFIGURABLE | Property.NOT_ENUMERABLE, typeErrorThrower, typeErrorThrower);
   299        }
   300     }
   301 }

mercurial