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  */
  25 
  26 package jdk.nashorn.internal.runtime;
  27 
  28 import java.util.Collection;
  29 import java.util.Map;
  30 import jdk.nashorn.internal.codegen.ClassEmitter;
  31 
  32 /**
  33  * Interface for installing classes passed to the compiler.
  34  * As only the code generating package (i.e. Context) knows about
  35  * the ScriptLoader and it would be a security hazard otherwise
  36  * the Compiler is given an installation interface for its code.
  37  * <p>
  38  * The compiler still retains most of the state around code emission
  39  * and management internally, so this is to avoid passing around any
  40  * logic that isn't directly related to installing a class
  41  *
  42  */
  43 public interface CodeInstaller {
  44     /**
  45      * Return the {@link Context} associated with this code installer.
  46      * @return the context.
  47      */
  48     public Context getContext();
  49 
  50     /**
  51      * Install a class.
  52      * @param className name of the class with / separation
  53      * @param bytecode  bytecode
  54      * @return the installed class
  55      */
  56     public Class<?> install(final String className, final byte[] bytecode);
  57 
  58     /**
  59      * Initialize already installed classes.
  60      * @param classes the class to initialize
  61      * @param source the source object for the classes
  62      * @param constants the runtime constants for the classes
  63      */
  64     public void initialize(final Collection<Class<?>> classes, final Source source, final Object[] constants);
  65 
  66     /**
  67      * Verify generated bytecode before emission. This is called back from the
  68      * {@link ClassEmitter} or the {@link Compiler}. If the "--verify-code" parameter
  69      * hasn't been given, this is a nop
  70      *
  71      * @param code bytecode to verify
  72      */
  73     public void verify(final byte[] code);
  74 
  75     /**
  76      * Get next unique script id
  77      * @return unique script id
  78      */
  79     public long getUniqueScriptId();
  80 
  81     /**
  82      * Store a compiled script for later reuse
  83      *
  84      * @param cacheKey key to use in cache
  85      * @param source the script source
  86      * @param mainClassName the main class name
  87      * @param classBytes map of class names to class bytes
  88      * @param initializers compilation id -&gt; FunctionInitializer map
  89      * @param constants constants array
  90      * @param compilationId compilation id
  91      */
  92     public void storeScript(final String cacheKey, final Source source, final String mainClassName, final Map<String, byte[]> classBytes,
  93             final Map<Integer, FunctionInitializer> initializers, final Object[] constants, final int compilationId);
  94 
  95     /**
  96      * Load a previously compiled script
  97      * @param source the script source
  98      * @param functionKey the function id and signature
  99      * @return compiled script data
 100      */
 101     public StoredScript loadScript(Source source, String functionKey);
 102 
 103     /**
 104      * Returns a code installer {@code #isCompatibleWith(CodeInstaller) compatible with} this installer, but
 105      * is suitable for on-demand compilations. Can return itself if it is itself suitable.
 106      * @return a compatible code installer suitable for on-demand compilations.
 107      */
 108     public CodeInstaller getOnDemandCompilationInstaller();
 109 
 110     /**
 111      * Returns a code installer {@code #isCompatibleWith(CodeInstaller) compatible with} this installer, but
 112      * is suitable for installation of multiple classes that reference each other by name. Should be used when
 113      * a compilation job produces multiple compilation units. Can return itself if it is itself suitable.
 114      * @return a compatible code installer suitable for installation of multiple classes.
 115      */
 116     public CodeInstaller getMultiClassCodeInstaller();
 117 
 118     /**
 119      * Returns true if this code installer is compatible with the other code installer. Compatibility is expected to be
 120      * an equivalence relation, and installers are supposed to be compatible with those they create using
 121      * {@link #getOnDemandCompilationInstaller()}.
 122      * @param other the other code installer tested for compatibility with this code installer.
 123      * @return true if this code installer is compatible with the other code installer.
 124      */
 125     public boolean isCompatibleWith(CodeInstaller other);
 126 
 127 }