8080090: -d option should dump script source as well
Reviewed-by: hannesw, lagergren
--- a/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/Context.java Mon May 11 22:57:24 2015 +0530
+++ b/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/Context.java Tue May 12 12:40:33 2015 +0530
@@ -1239,6 +1239,10 @@
}
if (storedScript == null) {
+ if (env._dest_dir != null) {
+ source.dump(env._dest_dir);
+ }
+
functionNode = new Parser(env, source, errMan, strict, getLogger(Parser.class)).parse();
if (errMan.hasErrors()) {
--- a/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/ScriptEnvironment.java Mon May 11 22:57:24 2015 +0530
+++ b/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/ScriptEnvironment.java Tue May 12 12:40:33 2015 +0530
@@ -73,7 +73,7 @@
/** Generate line number table in class files */
public final boolean _debug_lines;
- /** Package to which generated class files are added */
+ /** Directory in which source files and generated class files are dumped */
public final String _dest_dir;
/** Display stack trace upon error, default is false */
--- a/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/Source.java Mon May 11 22:57:24 2015 +0530
+++ b/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/Source.java Tue May 12 12:40:33 2015 +0530
@@ -28,9 +28,11 @@
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
import java.io.IOError;
import java.io.IOException;
import java.io.InputStream;
+import java.io.PrintWriter;
import java.io.Reader;
import java.lang.ref.WeakReference;
import java.net.MalformedURLException;
@@ -44,6 +46,7 @@
import java.nio.file.Paths;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
+import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.Base64;
import java.util.Objects;
@@ -989,4 +992,39 @@
public DebugLogger getLogger() {
return initLogger(Context.getContextTrusted());
}
+
+ private File dumpFile(final String dir) {
+ final URL u = getURL();
+ final StringBuilder buf = new StringBuilder();
+ // make it unique by prefixing current date & time
+ buf.append(LocalDateTime.now().toString());
+ buf.append('_');
+ if (u != null) {
+ // make it a safe file name
+ buf.append(u.toString()
+ .replace('/', '_')
+ .replace('\\', '_'));
+ } else {
+ buf.append(getName());
+ }
+
+ return new File(dir, buf.toString());
+ }
+
+ void dump(final String dir) {
+ final File file = dumpFile(dir);
+ try (final FileOutputStream fos = new FileOutputStream(file)) {
+ final PrintWriter pw = new PrintWriter(fos);
+ pw.print(data.toString());
+ pw.flush();
+ } catch (final IOException ioExp) {
+ debug("Skipping source dump for " +
+ name +
+ ": " +
+ ECMAErrors.getMessage(
+ "io.error.cant.write",
+ dir.toString() +
+ " : " + ioExp.toString()));
+ }
+ }
}
--- a/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/resources/Options.properties Mon May 11 22:57:24 2015 +0530
+++ b/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/resources/Options.properties Tue May 12 12:40:33 2015 +0530
@@ -114,7 +114,7 @@
short_name="-d", \
is_undocumented=true, \
params="<path>", \
- desc="specify a destination directory to dump class files.", \
+ desc="specify a destination directory to dump source and class files.", \
type=String \
}