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  * @param <T> owner class type for this code installer
  42  *
  43  */
  44 public interface CodeInstaller<T> {
  45     /**
  46      * Return the owner for the CodeInstaller, e.g. a {@link Context}
  47      * @return owner
  48      */
  49     public T getOwner();
  50 
  51     /**
  52      * Install a class.
  53      * @param className name of the class with / separation
  54      * @param bytecode  bytecode
  55      * @return the installed class
  56      */
  57     public Class<?> install(final String className, final byte[] bytecode);
  58 
  59     /**
  60      * Initialize already installed classes.
  61      * @param classes the class to initialize
  62      * @param source the source object for the classes
  63      * @param constants the runtime constants for the classes
  64      */
  65     public void initialize(final Collection<Class<?>> classes, final Source source, final Object[] constants);
  66 
  67     /**
  68      * Verify generated bytecode before emission. This is called back from the
  69      * {@link ClassEmitter} or the {@link Compiler}. If the "--verify-code" parameter
  70      * hasn't been given, this is a nop
  71      *
  72      * @param code bytecode to verify
  73      */
  74     public void verify(final byte[] code);
  75 
  76     /**
  77      * Get next unique script id
  78      * @return unique script id
  79      */
  80     public long getUniqueScriptId();
  81 
  82     /**
  83      * Store a compiled script for later reuse
  84      *
  85      * @param cacheKey key to use in cache
  86      * @param source the script source
  87      * @param mainClassName the main class name
  88      * @param classBytes map of class names to class bytes
  89      * @param initializers compilation id -&gt; FunctionInitializer map
  90      * @param constants constants array
  91      * @param compilationId compilation id
  92      */
  93     public void storeScript(final String cacheKey, final Source source, final String mainClassName, final Map<String, byte[]> classBytes,
  94             final Map<Integer, FunctionInitializer> initializers, final Object[] constants, final int compilationId);
  95 
  96     /**
  97      * Load a previously compiled script
  98      * @param source the script source
  99      * @param functionKey the function id and signature
 100      * @return compiled script data
 101      */
 102     public StoredScript loadScript(Source source, String functionKey);
 103 
 104     /**
 105      * Returns a new code installer that shares most of the functionality of this code installer, but uses a
 106      * new, independent class loader.
 107      * @return a new code installer with a new independent class loader.
 108      */
 109     public CodeInstaller<T> withNewLoader();
 110 
 111     /**
 112      * Returns true if this code installer is compatible with the other code installer. Compatibility is expected to be
 113      * an equivalence relation, and installers are supposed to be compatible with those they create using
 114      * {@link #withNewLoader()}.
 115      * @param other the other code installer tested for compatibility with this code installer.
 116      * @return true if this code installer is compatible with the other code installer.
 117      */
 118     public boolean isCompatibleWith(CodeInstaller<T> other);
 119 
 120 }