8080090: -d option should dump script source as well
Reviewed-by: hannesw, lagergren
This commit is contained in:
parent
fd0591c105
commit
ee03c0e102
@ -1239,6 +1239,10 @@ public final class Context {
|
||||
}
|
||||
|
||||
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()) {
|
||||
|
@ -73,7 +73,7 @@ public final class ScriptEnvironment {
|
||||
/** 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 */
|
||||
|
@ -28,9 +28,11 @@ package jdk.nashorn.internal.runtime;
|
||||
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.Path;
|
||||
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 final class Source implements Loggable {
|
||||
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()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -114,7 +114,7 @@ nashorn.option.d = { \
|
||||
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 \
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user