CodeInstaller.java revision 1036:f0b5e3900a10
150472Speter/*
21664Sphk * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
369040Sben * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
469040Sben *
569040Sben * This code is free software; you can redistribute it and/or modify it
669040Sben * under the terms of the GNU General Public License version 2 only, as
782604Salex * published by the Free Software Foundation.  Oracle designates this
882604Salex * particular file as subject to the "Classpath" exception as provided
982604Salex * by Oracle in the LICENSE file that accompanied this code.
1082604Salex *
111664Sphk * This code is distributed in the hope that it will be useful, but WITHOUT
123023Srgrimes * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
133023Srgrimes * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
1482604Salex * version 2 for more details (a copy is included in the LICENSE file that
1582604Salex * accompanied this code).
1682604Salex *
171664Sphk * You should have received a copy of the GNU General Public License version
18133476Sharti * 2 along with this work; if not, write to the Free Software Foundation,
19133476Sharti * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20133476Sharti *
2172679Skris * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22133476Sharti * or visit www.oracle.com if you need additional information or have any
23133476Sharti * questions.
24133476Sharti */
2599260Sjohan
2672878Skrispackage jdk.nashorn.internal.runtime;
2772878Skris
2872878Skrisimport java.util.Collection;
2972878Skrisimport java.util.Map;
3072878Skrisimport jdk.nashorn.internal.codegen.ClassEmitter;
3199260Sjohan
3273145Skris/**
33136608Sobrien * Interface for installing classes passed to the compiler.
34136608Sobrien * As only the code generating package (i.e. Context) knows about
35136608Sobrien * the ScriptLoader and it would be a security hazard otherwise
36136608Sobrien * the Compiler is given an installation interface for its code.
37103048Skris * <p>
38136608Sobrien * The compiler still retains most of the state around code emission
3972878Skris * and management internally, so this is to avoid passing around any
4072679Skris * logic that isn't directly related to installing a class
41101232Sru * @param <T> owner class type for this code installer
42101232Sru *
43136608Sobrien */
44136914Srupublic interface CodeInstaller<T> {
45136914Sru    /**
4672878Skris     * Return the owner for the CodeInstaller, e.g. a {@link Context}
4758648Skris     * @return owner
48131467Sdes     */
4958648Skris    public T getOwner();
5068917Sdougb
51124694Sobrien    /**
52131467Sdes     * Install a class.
53131522Sdes     * @param className name of the class with / separation
541664Sphk     * @param bytecode  bytecode
5529281Sjkh     * @return the installed class
561664Sphk     */
5759006Sobrien    public Class<?> install(final String className, final byte[] bytecode);
5859006Sobrien
5959006Sobrien    /**
6059006Sobrien     * Initialize already installed classes.
611664Sphk     * @param classes the class to initialize
6259006Sobrien     * @param source the source object for the classes
6359006Sobrien     * @param constants the runtime constants for the classes
64104124Sjmallett     */
65104124Sjmallett    public void initialize(final Collection<Class<?>> classes, final Source source, final Object[] constants);
66104124Sjmallett
67104124Sjmallett    /**
68104124Sjmallett     * Verify generated bytecode before emission. This is called back from the
69104124Sjmallett     * {@link ClassEmitter} or the {@link Compiler}. If the "--verify-code" parameter
70104124Sjmallett     * hasn't been given, this is a nop
7162136Sobrien     *
7262136Sobrien     * @param code bytecode to verify
7380452Speter     */
7499260Sjohan    public void verify(final byte[] code);
7562136Sobrien
7682604Salex    /**
7782604Salex     * Get next unique script id
7882604Salex     * @return unique script id
7982604Salex     */
8062136Sobrien    public long getUniqueScriptId();
8168917Sdougb
8268263Sobrien    /**
8368263Sobrien     * Store a compiled script for later reuse
8468263Sobrien     *
8565380Sobrien     * @param cacheKey key to use in cache
8665380Sobrien     * @param source the script source
8765380Sobrien     * @param mainClassName the main class name
8881749Sobrien     * @param classBytes map of class names to class bytes
8981749Sobrien     * @param initializers compilation id -> FunctionInitializer map
9081749Sobrien     * @param constants constants array
9181749Sobrien     * @param compilationId compilation id
9281749Sobrien     */
9381749Sobrien    public void storeScript(final String cacheKey, final Source source, final String mainClassName, final Map<String, byte[]> classBytes,
9442325Sobrien            final Map<Integer, FunctionInitializer> initializers, final Object[] constants, final int compilationId);
95100870Sru
9635222Sache    /**
9765884Sache     * Load a previously compiled script
9865957Sache     * @param source the script source
9965884Sache     * @param functionKey the function id and signature
10068705Sgreen     * @return compiled script data
101136914Sru     */
10268705Sgreen    public StoredScript loadScript(Source source, String functionKey);
10397387Stjr}
10497387Stjr