27 |
27 |
28 import static jdk.nashorn.internal.codegen.CompilerConstants.CONSTANTS; |
28 import static jdk.nashorn.internal.codegen.CompilerConstants.CONSTANTS; |
29 import static jdk.nashorn.internal.codegen.CompilerConstants.CREATE_PROGRAM_FUNCTION; |
29 import static jdk.nashorn.internal.codegen.CompilerConstants.CREATE_PROGRAM_FUNCTION; |
30 import static jdk.nashorn.internal.codegen.CompilerConstants.SOURCE; |
30 import static jdk.nashorn.internal.codegen.CompilerConstants.SOURCE; |
31 import static jdk.nashorn.internal.codegen.CompilerConstants.STRICT_MODE; |
31 import static jdk.nashorn.internal.codegen.CompilerConstants.STRICT_MODE; |
|
32 import static jdk.nashorn.internal.runtime.CodeStore.newCodeStore; |
32 import static jdk.nashorn.internal.runtime.ECMAErrors.typeError; |
33 import static jdk.nashorn.internal.runtime.ECMAErrors.typeError; |
33 import static jdk.nashorn.internal.runtime.ScriptRuntime.UNDEFINED; |
34 import static jdk.nashorn.internal.runtime.ScriptRuntime.UNDEFINED; |
34 import static jdk.nashorn.internal.runtime.Source.sourceFor; |
35 import static jdk.nashorn.internal.runtime.Source.sourceFor; |
35 |
36 |
36 import java.io.File; |
37 import java.io.File; |
198 @Override |
199 @Override |
199 public void storeScript(final String cacheKey, final Source source, final String mainClassName, |
200 public void storeScript(final String cacheKey, final Source source, final String mainClassName, |
200 final Map<String,byte[]> classBytes, final Map<Integer, FunctionInitializer> initializers, |
201 final Map<String,byte[]> classBytes, final Map<Integer, FunctionInitializer> initializers, |
201 final Object[] constants, final int compilationId) { |
202 final Object[] constants, final int compilationId) { |
202 if (context.codeStore != null) { |
203 if (context.codeStore != null) { |
203 context.codeStore.storeScript(cacheKey, source, mainClassName, classBytes, initializers, constants, compilationId); |
204 context.codeStore.store(cacheKey, source, mainClassName, classBytes, initializers, constants, compilationId); |
204 } |
205 } |
205 } |
206 } |
206 |
207 |
207 @Override |
208 @Override |
208 public StoredScript loadScript(final Source source, final String functionKey) { |
209 public StoredScript loadScript(final Source source, final String functionKey) { |
209 if (context.codeStore != null) { |
210 if (context.codeStore != null) { |
210 return context.codeStore.loadScript(source, functionKey); |
211 return context.codeStore.load(source, functionKey); |
211 } |
212 } |
212 return null; |
213 return null; |
213 } |
214 } |
214 } |
215 } |
215 |
216 |
461 classCache = new ClassCache(cacheSize); |
462 classCache = new ClassCache(cacheSize); |
462 } |
463 } |
463 |
464 |
464 if (env._persistent_cache) { |
465 if (env._persistent_cache) { |
465 try { |
466 try { |
466 final String cacheDir = Options.getStringProperty("nashorn.persistent.code.cache", "nashorn_code_cache"); |
467 codeStore = newCodeStore(this); |
467 codeStore = new CodeStore(this, cacheDir); |
|
468 } catch (final IOException e) { |
468 } catch (final IOException e) { |
469 throw new RuntimeException("Error initializing code cache", e); |
469 throw new RuntimeException("Error initializing code cache", e); |
470 } |
470 } |
471 } |
471 } |
472 |
472 |
1115 FunctionNode functionNode = null; |
1115 FunctionNode functionNode = null; |
1116 final boolean useCodeStore = env._persistent_cache && !env._parse_only && !env._optimistic_types; |
1116 final boolean useCodeStore = env._persistent_cache && !env._parse_only && !env._optimistic_types; |
1117 final String cacheKey = useCodeStore ? CodeStore.getCacheKey(0, null) : null; |
1117 final String cacheKey = useCodeStore ? CodeStore.getCacheKey(0, null) : null; |
1118 |
1118 |
1119 if (useCodeStore) { |
1119 if (useCodeStore) { |
1120 storedScript = codeStore.loadScript(source, cacheKey); |
1120 storedScript = codeStore.load(source, cacheKey); |
1121 } |
1121 } |
1122 |
1122 |
1123 if (storedScript == null) { |
1123 if (storedScript == null) { |
1124 functionNode = new Parser(env, source, errMan, strict, getLogger(Parser.class)).parse(); |
1124 functionNode = new Parser(env, source, errMan, strict, getLogger(Parser.class)).parse(); |
1125 |
1125 |
1192 * @return main script class |
1192 * @return main script class |
1193 */ |
1193 */ |
1194 private static Class<?> install(final StoredScript storedScript, final Source source, final CodeInstaller<ScriptEnvironment> installer) { |
1194 private static Class<?> install(final StoredScript storedScript, final Source source, final CodeInstaller<ScriptEnvironment> installer) { |
1195 |
1195 |
1196 final Map<String, Class<?>> installedClasses = new HashMap<>(); |
1196 final Map<String, Class<?>> installedClasses = new HashMap<>(); |
|
1197 final Map<String, byte[]> classBytes = storedScript.getClassBytes(); |
1197 final Object[] constants = storedScript.getConstants(); |
1198 final Object[] constants = storedScript.getConstants(); |
1198 final String mainClassName = storedScript.getMainClassName(); |
1199 final String mainClassName = storedScript.getMainClassName(); |
1199 final byte[] mainClassBytes = storedScript.getClassBytes().get(mainClassName); |
1200 final byte[] mainClassBytes = classBytes.get(mainClassName); |
1200 final Class<?> mainClass = installer.install(mainClassName, mainClassBytes); |
1201 final Class<?> mainClass = installer.install(mainClassName, mainClassBytes); |
1201 final Map<Integer, FunctionInitializer> initialzers = storedScript.getInitializers(); |
1202 final Map<Integer, FunctionInitializer> initialzers = storedScript.getInitializers(); |
1202 |
1203 |
1203 installedClasses.put(mainClassName, mainClass); |
1204 installedClasses.put(mainClassName, mainClass); |
1204 |
1205 |
1205 for (final Map.Entry<String, byte[]> entry : storedScript.getClassBytes().entrySet()) { |
1206 for (final Map.Entry<String, byte[]> entry : classBytes.entrySet()) { |
1206 final String className = entry.getKey(); |
1207 final String className = entry.getKey(); |
1207 if (className.equals(mainClassName)) { |
1208 if (className.equals(mainClassName)) { |
1208 continue; |
1209 continue; |
1209 } |
1210 } |
1210 final byte[] code = entry.getValue(); |
1211 final byte[] code = entry.getValue(); |