Painless: Remove extraneous INLINE constant. (#29340)

This commit is contained in:
Jack Conradson 2018-04-02 21:34:01 -07:00 committed by GitHub
parent 1df43a09b7
commit 782e41a67e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 28 additions and 52 deletions

View File

@ -70,41 +70,29 @@ public final class Location {
private static final int MAX_NAME_LENGTH = 256; private static final int MAX_NAME_LENGTH = 256;
/** Computes the file name (mostly important for stacktraces) */ /** Computes the file name (mostly important for stacktraces) */
public static String computeSourceName(String scriptName, String source) { public static String computeSourceName(String scriptName) {
StringBuilder fileName = new StringBuilder(); StringBuilder fileName = new StringBuilder();
if (scriptName.equals(PainlessScriptEngine.INLINE_NAME)) { // its an anonymous script, include at least a portion of the source to help identify which one it is
// its an anonymous script, include at least a portion of the source to help identify which one it is // but don't create stacktraces with filenames that contain newlines or huge names.
// but don't create stacktraces with filenames that contain newlines or huge names.
// truncate to the first newline // truncate to the first newline
int limit = source.indexOf('\n'); int limit = scriptName.indexOf('\n');
if (limit >= 0) { if (limit >= 0) {
int limit2 = source.indexOf('\r'); int limit2 = scriptName.indexOf('\r');
if (limit2 >= 0) { if (limit2 >= 0) {
limit = Math.min(limit, limit2); limit = Math.min(limit, limit2);
}
} else {
limit = source.length();
} }
// truncate to our limit
limit = Math.min(limit, MAX_NAME_LENGTH);
fileName.append(source, 0, limit);
// if we truncated, make it obvious
if (limit != source.length()) {
fileName.append(" ...");
}
fileName.append(" @ <inline script>");
} else { } else {
// its a named script, just use the name limit = scriptName.length();
// but don't trust this has a reasonable length! }
if (scriptName.length() > MAX_NAME_LENGTH) {
fileName.append(scriptName, 0, MAX_NAME_LENGTH); // truncate to our limit
fileName.append(" ..."); limit = Math.min(limit, MAX_NAME_LENGTH);
} else { fileName.append(scriptName, 0, limit);
fileName.append(scriptName);
} // if we truncated, make it obvious
if (limit != scriptName.length()) {
fileName.append(" ...");
} }
return fileName.toString(); return fileName.toString();
} }

View File

@ -91,14 +91,7 @@ public interface PainlessScript {
scriptStack.add(element.toString()); scriptStack.add(element.toString());
} }
} }
// build a name for the script: ScriptException scriptException = new ScriptException("runtime error", t, scriptStack, getName(), PainlessScriptEngine.NAME);
final String name;
if (PainlessScriptEngine.INLINE_NAME.equals(getName())) {
name = getSource();
} else {
name = getName();
}
ScriptException scriptException = new ScriptException("runtime error", t, scriptStack, name, PainlessScriptEngine.NAME);
for (Map.Entry<String, List<String>> entry : extraMetadata.entrySet()) { for (Map.Entry<String, List<String>> entry : extraMetadata.entrySet()) {
scriptException.addMetadata(entry.getKey(), entry.getValue()); scriptException.addMetadata(entry.getKey(), entry.getValue());
} }

View File

@ -119,11 +119,6 @@ public final class PainlessScriptEngine extends AbstractComponent implements Scr
return NAME; return NAME;
} }
/**
* When a script is anonymous (inline), we give it this name.
*/
static final String INLINE_NAME = "<inline>";
@Override @Override
public <T> T compile(String scriptName, String scriptSource, ScriptContext<T> context, Map<String, String> params) { public <T> T compile(String scriptName, String scriptSource, ScriptContext<T> context, Map<String, String> params) {
Compiler compiler = contextsToCompilers.get(context); Compiler compiler = contextsToCompilers.get(context);
@ -425,7 +420,7 @@ public final class PainlessScriptEngine extends AbstractComponent implements Scr
return AccessController.doPrivileged(new PrivilegedAction<Object>() { return AccessController.doPrivileged(new PrivilegedAction<Object>() {
@Override @Override
public Object run() { public Object run() {
String name = scriptName == null ? INLINE_NAME : scriptName; String name = scriptName == null ? source : scriptName;
Constructor<?> constructor = compiler.compile(loader, new MainMethodReserved(), name, source, compilerSettings); Constructor<?> constructor = compiler.compile(loader, new MainMethodReserved(), name, source, compilerSettings);
try { try {
@ -488,7 +483,7 @@ public final class PainlessScriptEngine extends AbstractComponent implements Scr
AccessController.doPrivileged(new PrivilegedAction<Void>() { AccessController.doPrivileged(new PrivilegedAction<Void>() {
@Override @Override
public Void run() { public Void run() {
String name = scriptName == null ? INLINE_NAME : scriptName; String name = scriptName == null ? source : scriptName;
compiler.compile(loader, reserved, name, source, compilerSettings); compiler.compile(loader, reserved, name, source, compilerSettings);
return null; return null;

View File

@ -198,7 +198,7 @@ public final class Walker extends PainlessParserBaseVisitor<ANode> {
this.reserved.push(reserved); this.reserved.push(reserved);
this.debugStream = debugStream; this.debugStream = debugStream;
this.settings = settings; this.settings = settings;
this.sourceName = Location.computeSourceName(sourceName, sourceText); this.sourceName = Location.computeSourceName(sourceName);
this.sourceText = sourceText; this.sourceText = sourceText;
this.globals = new Globals(new BitSet(sourceText.length())); this.globals = new Globals(new BitSet(sourceText.length()));
this.definition = definition; this.definition = definition;

View File

@ -249,7 +249,7 @@ public final class SSource extends AStatement {
} }
visitor.visit(WriterConstants.CLASS_VERSION, classAccess, className, null, visitor.visit(WriterConstants.CLASS_VERSION, classAccess, className, null,
Type.getType(scriptClassInfo.getBaseClass()).getInternalName(), classInterfaces); Type.getType(scriptClassInfo.getBaseClass()).getInternalName(), classInterfaces);
visitor.visitSource(Location.computeSourceName(name, source), null); visitor.visitSource(Location.computeSourceName(name), null);
// Write the a method to bootstrap def calls // Write the a method to bootstrap def calls
MethodWriter bootstrapDef = new MethodWriter(Opcodes.ACC_STATIC | Opcodes.ACC_VARARGS, DEF_BOOTSTRAP_METHOD, visitor, MethodWriter bootstrapDef = new MethodWriter(Opcodes.ACC_STATIC | Opcodes.ACC_VARARGS, DEF_BOOTSTRAP_METHOD, visitor,