Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions src/main/java/org/codelibs/sai/internal/runtime/CodeStore.java
Original file line number Diff line number Diff line change
Expand Up @@ -260,8 +260,21 @@ public File run() throws IOException {

private static String getVersionDir(final ScriptEnvironment env) throws IOException {
try {
final String versionDir = OptimisticTypesPersistence.getVersionDirName();
return env._optimistic_types ? versionDir + "_opt" : versionDir;
final StringBuilder versionDir = new StringBuilder(OptimisticTypesPersistence.getVersionDirName());

if (env._optimistic_types) {
versionDir.append("_opt");
}

// The language level decides what the source even parses as, so a script
// stored under one must never be loaded under the other: the store is
// consulted before parsing, so an es6 entry would answer an es5 run and
// hand it code the es5 parser would have rejected.
if (env._es6) {
versionDir.append("_es6");
}

return versionDir.toString();
} catch (final Exception e) {
throw new IOException(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertNotEquals;
import java.io.File;
import java.io.IOException;
import java.nio.file.DirectoryStream;
Expand Down Expand Up @@ -85,6 +86,22 @@ public class CodeStoreAndPathTest {

private static final String[] ENGINE_OPTIONS_OPT = new String[] { "--persistent-code-cache", "--optimistic-types=true" };
private static final String[] ENGINE_OPTIONS_NOOPT = new String[] { "--persistent-code-cache", "--optimistic-types=false" };
private static final String[] ENGINE_OPTIONS_ES6 =
new String[] { "--persistent-code-cache", "--optimistic-types=false", "--language=es6" };

@Test
public void es6CodeCacheIsSeparateTest() throws ScriptException {
System.setProperty("sai.persistent.code.cache", codeCache);
final SaiScriptEngineFactory fac = new SaiScriptEngineFactory();

fac.getScriptEngine(ENGINE_OPTIONS_NOOPT).eval(code1);
fac.getScriptEngine(ENGINE_OPTIONS_ES6).eval(code1);

// The store is consulted before the source is parsed, so an entry compiled under
// one language level must never answer a run at the other: es5 would be handed
// code its own parser would have rejected.
assertNotEquals(getCodeCachePath(false, true), getCodeCachePath(false, false));
}

@Test
public void pathHandlingTest() {
Expand Down Expand Up @@ -153,11 +170,18 @@ public void codeCacheTestOpt() throws ScriptException, IOException {
}

private static Path getCodeCachePath(final boolean optimistic) {
return getCodeCachePath(optimistic, false);
}

private static Path getCodeCachePath(final boolean optimistic, final boolean es6) {
final String codeCache = System.getProperty("sai.persistent.code.cache");
final Path codeCachePath = FileSystems.getDefault().getPath(codeCache).toAbsolutePath();
final String[] files = codeCachePath.toFile().list();
for (final String file : files) {
if (file.endsWith("_opt") == optimistic) {
final boolean isEs6 = file.endsWith("_es6");
final String withoutLanguage = isEs6 ? file.substring(0, file.length() - "_es6".length()) : file;

if (isEs6 == es6 && withoutLanguage.endsWith("_opt") == optimistic) {
return codeCachePath.resolve(file);
}
}
Expand Down
Loading