8020224: LinkageError: attempted duplicate class definition when --loader-per-compiler=false

Wed, 10 Jul 2013 13:25:07 +0530

author
sundar
date
Wed, 10 Jul 2013 13:25:07 +0530
changeset 425
997a3215744a
parent 424
d480015ab732
child 426
a9b74daed4f9

8020224: LinkageError: attempted duplicate class definition when --loader-per-compiler=false
Reviewed-by: hannesw

src/jdk/nashorn/internal/codegen/Compiler.java file | annotate | diff | comparison | revisions
src/jdk/nashorn/internal/runtime/CodeInstaller.java file | annotate | diff | comparison | revisions
src/jdk/nashorn/internal/runtime/Context.java file | annotate | diff | comparison | revisions
test/src/jdk/nashorn/internal/runtime/TrustedScriptEngineTest.java file | annotate | diff | comparison | revisions
     1.1 --- a/src/jdk/nashorn/internal/codegen/Compiler.java	Tue Jul 09 15:56:59 2013 +0200
     1.2 +++ b/src/jdk/nashorn/internal/codegen/Compiler.java	Wed Jul 10 13:25:07 2013 +0530
     1.3 @@ -528,7 +528,7 @@
     1.4          return this.env;
     1.5      }
     1.6  
     1.7 -    private static String safeSourceName(final Source source) {
     1.8 +    private String safeSourceName(final Source source) {
     1.9          String baseName = new File(source.getName()).getName();
    1.10  
    1.11          final int index = baseName.lastIndexOf(".js");
    1.12 @@ -537,6 +537,9 @@
    1.13          }
    1.14  
    1.15          baseName = baseName.replace('.', '_').replace('-', '_');
    1.16 +        if (! env._loader_per_compile) {
    1.17 +            baseName = baseName + installer.getUniqueScriptId();
    1.18 +        }
    1.19          final String mangled = NameCodec.encode(baseName);
    1.20  
    1.21          return mangled != null ? mangled : baseName;
     2.1 --- a/src/jdk/nashorn/internal/runtime/CodeInstaller.java	Tue Jul 09 15:56:59 2013 +0200
     2.2 +++ b/src/jdk/nashorn/internal/runtime/CodeInstaller.java	Wed Jul 10 13:25:07 2013 +0530
     2.3 @@ -62,4 +62,10 @@
     2.4       * @param code bytecode to verify
     2.5       */
     2.6      public void verify(final byte[] code);
     2.7 +
     2.8 +    /**
     2.9 +     * Get next unique script id
    2.10 +     * @return unique script id
    2.11 +     */
    2.12 +    public long getUniqueScriptId();
    2.13  }
     3.1 --- a/src/jdk/nashorn/internal/runtime/Context.java	Tue Jul 09 15:56:59 2013 +0200
     3.2 +++ b/src/jdk/nashorn/internal/runtime/Context.java	Wed Jul 10 13:25:07 2013 +0530
     3.3 @@ -96,6 +96,11 @@
     3.4          public void verify(final byte[] code) {
     3.5              context.verify(code);
     3.6          }
     3.7 +
     3.8 +        @Override
     3.9 +        public long getUniqueScriptId() {
    3.10 +            return context.getUniqueScriptId();
    3.11 +        }
    3.12      }
    3.13  
    3.14      /** Is Context global debug mode enabled ? */
    3.15 @@ -197,6 +202,9 @@
    3.16      /** Current error manager. */
    3.17      private final ErrorManager errors;
    3.18  
    3.19 +    /** Unique id for script. Used only when --loader-per-compile=false */
    3.20 +    private long uniqueScriptId;
    3.21 +
    3.22      private static final ClassLoader myLoader = Context.class.getClassLoader();
    3.23      private static final StructureLoader sharedLoader;
    3.24      private static final AccessControlContext NO_PERMISSIONS_CONTEXT;
    3.25 @@ -816,4 +824,8 @@
    3.26      private ScriptObject newGlobalTrusted() {
    3.27          return new Global(this);
    3.28      }
    3.29 +
    3.30 +    private synchronized long getUniqueScriptId() {
    3.31 +        return uniqueScriptId++;
    3.32 +    }
    3.33  }
     4.1 --- a/test/src/jdk/nashorn/internal/runtime/TrustedScriptEngineTest.java	Tue Jul 09 15:56:59 2013 +0200
     4.2 +++ b/test/src/jdk/nashorn/internal/runtime/TrustedScriptEngineTest.java	Wed Jul 10 13:25:07 2013 +0530
     4.3 @@ -145,4 +145,55 @@
     4.4  
     4.5          fail("Cannot find nashorn factory!");
     4.6      }
     4.7 +
     4.8 +    @Test
     4.9 +    /**
    4.10 +     * Test repeated evals with --loader-per-compile=false
    4.11 +     * We used to get "class redefinition error".
    4.12 +     */
    4.13 +    public void noLoaderPerCompilerTest() {
    4.14 +        final ScriptEngineManager sm = new ScriptEngineManager();
    4.15 +        for (ScriptEngineFactory fac : sm.getEngineFactories()) {
    4.16 +            if (fac instanceof NashornScriptEngineFactory) {
    4.17 +                final NashornScriptEngineFactory nfac = (NashornScriptEngineFactory)fac;
    4.18 +                final String[] options = new String[] { "--loader-per-compile=false" };
    4.19 +                final ScriptEngine e = nfac.getScriptEngine(options);
    4.20 +                try {
    4.21 +                    e.eval("2 + 3");
    4.22 +                    e.eval("4 + 4");
    4.23 +                } catch (final ScriptException se) {
    4.24 +                    se.printStackTrace();
    4.25 +                    fail(se.getMessage());
    4.26 +                }
    4.27 +                return;
    4.28 +            }
    4.29 +        }
    4.30 +        fail("Cannot find nashorn factory!");
    4.31 +    }
    4.32 +
    4.33 +    @Test
    4.34 +    /**
    4.35 +     * Test that we can use same script name in repeated evals with --loader-per-compile=false
    4.36 +     * We used to get "class redefinition error" as name was derived from script name.
    4.37 +     */
    4.38 +    public void noLoaderPerCompilerWithSameNameTest() {
    4.39 +        final ScriptEngineManager sm = new ScriptEngineManager();
    4.40 +        for (ScriptEngineFactory fac : sm.getEngineFactories()) {
    4.41 +            if (fac instanceof NashornScriptEngineFactory) {
    4.42 +                final NashornScriptEngineFactory nfac = (NashornScriptEngineFactory)fac;
    4.43 +                final String[] options = new String[] { "--loader-per-compile=false" };
    4.44 +                final ScriptEngine e = nfac.getScriptEngine(options);
    4.45 +                e.put(ScriptEngine.FILENAME, "test.js");
    4.46 +                try {
    4.47 +                    e.eval("2 + 3");
    4.48 +                    e.eval("4 + 4");
    4.49 +                } catch (final ScriptException se) {
    4.50 +                    se.printStackTrace();
    4.51 +                    fail(se.getMessage());
    4.52 +                }
    4.53 +                return;
    4.54 +            }
    4.55 +        }
    4.56 +        fail("Cannot find nashorn factory!");
    4.57 +    }
    4.58  }

mercurial