1 /*
   2  * Copyright (c) 2014, 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  */
  25 package jdk.nashorn.internal.runtime;
  26 
  27 import static jdk.nashorn.internal.lookup.Lookup.MH;
  28 
  29 import java.io.Serializable;
  30 import java.lang.invoke.MethodHandle;
  31 import java.lang.invoke.MethodHandles;
  32 import jdk.nashorn.internal.codegen.CompilerConstants;
  33 
  34 /**
  35  * Encapsulates the allocation strategy for a function when used as a constructor.
  36  */
  37 final public class AllocationStrategy implements Serializable {
  38     private static final long serialVersionUID = 1L;
  39 
  40     private static final MethodHandles.Lookup LOOKUP = MethodHandles.lookup();
  41 
  42     /** Allocator map from allocator descriptor */
  43     private final PropertyMap allocatorMap;
  44 
  45     /** Name of class where allocator function resides */
  46     private final String allocatorClassName;
  47 
  48     /** lazily generated allocator */
  49     private transient MethodHandle allocator;
  50 
  51     /**
  52      * Construct an allocation strategy with the given map and class name.
  53      * @param allocatorMap the property map
  54      * @param className the class name
  55      */
  56     public AllocationStrategy(final PropertyMap allocatorMap, final String className) {
  57         this.allocatorMap = allocatorMap;
  58         // These classes get loaded, so an interned variant of their name is most likely around anyway.
  59         this.allocatorClassName = className.intern();
  60     }
  61 
  62     PropertyMap getAllocatorMap() {
  63         return allocatorMap;
  64     }
  65 
  66     ScriptObject allocate(final PropertyMap map) {
  67         try {
  68             if (allocator == null) {
  69                 allocator = MH.findStatic(LOOKUP, Context.forStructureClass(allocatorClassName),
  70                         CompilerConstants.ALLOCATE.symbolName(), MH.type(ScriptObject.class, PropertyMap.class));
  71             }
  72             return (ScriptObject)allocator.invokeExact(map);
  73         } catch (final RuntimeException | Error e) {
  74             throw e;
  75         } catch (final Throwable t) {
  76             throw new RuntimeException(t);
  77         }
  78     }
  79 
  80     @Override
  81     public String toString() {
  82         return "AllocationStrategy[allocatorClassName=" + allocatorClassName + ", allocatorMap.size=" +
  83                 allocatorMap.size() + "]";
  84     }
  85 }