FunctionInitializer.java revision 1032:52752e15fe18
1/* 2 * Copyright (c) 2010, 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 26package jdk.nashorn.internal.runtime; 27 28import java.io.IOException; 29import java.io.ObjectInputStream; 30import java.io.ObjectOutputStream; 31import java.io.Serializable; 32import java.lang.invoke.MethodType; 33import java.util.Map; 34import jdk.nashorn.internal.codegen.CompileUnit; 35import jdk.nashorn.internal.codegen.FunctionSignature; 36import jdk.nashorn.internal.codegen.types.Type; 37import jdk.nashorn.internal.ir.FunctionNode; 38 39/** 40 * Class that contains information allowing us to look up a method handle implementing a JavaScript function 41 * from a generated class. This is used both for code coming from codegen and for persistent serialized code. 42 */ 43public final class FunctionInitializer implements Serializable { 44 45 private final String className; 46 private final MethodType methodType; 47 private final int flags; 48 private transient Map<Integer, Type> invalidatedProgramPoints; 49 private transient Class<?> code; 50 51 private static final long serialVersionUID = -5420835725902966692L; 52 53 /** 54 * Constructor. 55 * 56 * @param functionNode the function node 57 */ 58 public FunctionInitializer(final FunctionNode functionNode) { 59 this(functionNode, null); 60 } 61 62 /** 63 * Copy constructor. 64 * 65 * @param init original initializer 66 */ 67 FunctionInitializer(final FunctionInitializer init) { 68 this.className = init.getClassName(); 69 this.methodType = init.getMethodType(); 70 this.flags = init.getFlags(); 71 } 72 73 /** 74 * Constructor. 75 * 76 * @param functionNode the function node 77 * @param invalidatedProgramPoints invalidated program points 78 */ 79 public FunctionInitializer(final FunctionNode functionNode, final Map<Integer, Type> invalidatedProgramPoints) { 80 this.className = functionNode.getCompileUnit().getUnitClassName(); 81 this.methodType = new FunctionSignature(functionNode).getMethodType(); 82 this.flags = functionNode.getFlags(); 83 this.invalidatedProgramPoints = invalidatedProgramPoints; 84 85 final CompileUnit cu = functionNode.getCompileUnit(); 86 if (cu != null) { 87 this.code = cu.getCode(); 88 } 89 90 assert className != null; 91 } 92 93 /** 94 * Returns the name of the class implementing the function. 95 * 96 * @return the class name 97 */ 98 public String getClassName() { 99 return className; 100 } 101 102 /** 103 * Returns the type of the method implementing the function. 104 * 105 * @return the method type 106 */ 107 public MethodType getMethodType() { 108 return methodType; 109 } 110 111 /** 112 * Returns the function flags. 113 * 114 * @return function flags 115 */ 116 public int getFlags() { 117 return flags; 118 } 119 120 /** 121 * Returns the class implementing the function. 122 * 123 * @return the class 124 */ 125 public Class<?> getCode() { 126 return code; 127 } 128 129 /** 130 * Set the class implementing the function 131 * @param code the class 132 */ 133 public void setCode(final Class<?> code) { 134 // Make sure code has not been set and has expected class name 135 if (this.code != null) { 136 throw new IllegalStateException("code already set"); 137 } 138 assert className.equals(code.getTypeName().replace('.', '/')) : "unexpected class name"; 139 this.code = code; 140 } 141 142 /** 143 * Returns the map of invalidated program points. 144 * 145 * @return invalidated program points 146 */ 147 public Map<Integer, Type> getInvalidatedProgramPoints() { 148 return invalidatedProgramPoints; 149 } 150 151 private void writeObject(final ObjectOutputStream out) throws IOException { 152 out.defaultWriteObject(); 153 Type.writeTypeMap(invalidatedProgramPoints, out); 154 } 155 156 private void readObject(final ObjectInputStream in) throws IOException, ClassNotFoundException { 157 in.defaultReadObject(); 158 invalidatedProgramPoints = Type.readTypeMap(in); 159 } 160} 161