package jdk.nashorn.internal.runtime;
import java.io.Serializable;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
public final class StoredScript implements Serializable {
private final int compilationId;
private final String mainClassName;
private final Map<String, byte[]> classBytes;
private final Object[] constants;
private final Map<Integer, FunctionInitializer> initializers;
private static final long serialVersionUID = 2958227232195298340L;
public StoredScript(final int compilationId, final String mainClassName, final Map<String, byte[]> classBytes, final Map<Integer, FunctionInitializer> initializers, final Object[] constants) {
this.compilationId = compilationId;
this.mainClassName = mainClassName;
this.classBytes = classBytes;
this.constants = constants;
this.initializers = initializers;
}
public int getCompilationId() {
return compilationId;
}
private Map<String, Class<?>> installClasses(final Source source, final CodeInstaller installer) {
final Map<String, Class<?>> installedClasses = new HashMap<>();
final byte[] mainClassBytes = classBytes.get(mainClassName);
final Class<?> mainClass = installer.install(mainClassName, mainClassBytes);
installedClasses.put(mainClassName, mainClass);
for (final Map.Entry<String, byte[]> entry : classBytes.entrySet()) {
final String className = entry.getKey();
if (!className.equals(mainClassName)) {
installedClasses.put(className, installer.install(className, entry.getValue()));
}
}
installer.initialize(installedClasses.values(), source, constants);
return installedClasses;
}
FunctionInitializer installFunction(final RecompilableScriptFunctionData data, final CodeInstaller installer) {
final Map<String, Class<?>> installedClasses = installClasses(data.getSource(), installer);
assert initializers != null;
assert initializers.size() == 1;
final FunctionInitializer initializer = initializers.values().iterator().next();
for (int i = 0; i < constants.length; i++) {
if (constants[i] instanceof RecompilableScriptFunctionData) {
final RecompilableScriptFunctionData newData = data.getScriptFunctionData(((RecompilableScriptFunctionData) constants[i]).getFunctionNodeId());
assert newData != null;
newData.initTransients(data.getSource(), installer);
constants[i] = newData;
}
}
initializer.setCode(installedClasses.get(initializer.getClassName()));
return initializer;
}
Class<?> installScript(final Source source, final CodeInstaller installer) {
final Map<String, Class<?>> installedClasses = installClasses(source, installer);
for (final Object constant : constants) {
if (constant instanceof RecompilableScriptFunctionData) {
final RecompilableScriptFunctionData data = (RecompilableScriptFunctionData) constant;
data.initTransients(source, installer);
final FunctionInitializer initializer = initializers.get(data.getFunctionNodeId());
if (initializer != null) {
initializer.setCode(installedClasses.get(initializer.getClassName()));
data.initializeCode(initializer);
}
}
}
return installedClasses.get(mainClassName);
}
@Override
public int hashCode() {
int hash = mainClassName.hashCode();
hash = 31 * hash + classBytes.hashCode();
hash = 31 * hash + Arrays.hashCode(constants);
return hash;
}
@Override
public boolean equals(final Object obj) {
if (obj == this) {
return true;
}
if (!(obj instanceof StoredScript)) {
return false;
}
final StoredScript cs = (StoredScript) obj;
return mainClassName.equals(cs.mainClassName)
&& classBytes.equals(cs.classBytes)
&& Arrays.equals(constants, cs.constants);
}
}