use of org.elasticsearch.painless.node.SSource in project elasticsearch by elastic.
the class Walker method visitSource.
@Override
public ANode visitSource(SourceContext ctx) {
reserved.push(new MainMethodReserved());
List<SFunction> functions = new ArrayList<>();
for (FunctionContext function : ctx.function()) {
functions.add((SFunction) visit(function));
}
List<AStatement> statements = new ArrayList<>();
for (StatementContext statement : ctx.statement()) {
statements.add((AStatement) visit(statement));
}
return new SSource(scriptInterface, settings, sourceName, sourceText, debugStream, (MainMethodReserved) reserved.pop(), location(ctx), functions, globals, statements);
}
use of org.elasticsearch.painless.node.SSource in project elasticsearch by elastic.
the class Compiler method compile.
/**
* Runs the two-pass compiler to generate a Painless script.
* @param <T> the type of the script
* @param loader The ClassLoader used to define the script.
* @param iface Interface the compiled script should implement
* @param name The name of the script.
* @param source The source code for the script.
* @param settings The CompilerSettings to be used during the compilation.
* @return An executable script that implements both {@code <T>} and is a subclass of {@link PainlessScript}
*/
static <T> T compile(Loader loader, Class<T> iface, String name, String source, CompilerSettings settings) {
if (source.length() > MAXIMUM_SOURCE_LENGTH) {
throw new IllegalArgumentException("Scripts may be no longer than " + MAXIMUM_SOURCE_LENGTH + " characters. The passed in script is " + source.length() + " characters. Consider using a" + " plugin if a script longer than this length is a requirement.");
}
ScriptInterface scriptInterface = new ScriptInterface(iface);
SSource root = Walker.buildPainlessTree(scriptInterface, name, source, settings, null);
root.analyze();
root.write();
try {
Class<? extends PainlessScript> clazz = loader.define(CLASS_NAME, root.getBytes());
java.lang.reflect.Constructor<? extends PainlessScript> constructor = clazz.getConstructor(String.class, String.class, BitSet.class);
return iface.cast(constructor.newInstance(name, source, root.getStatements()));
} catch (Exception exception) {
// Catch everything to let the user know this is something caused internally.
throw new IllegalStateException("An internal error occurred attempting to define the script [" + name + "].", exception);
}
}
use of org.elasticsearch.painless.node.SSource in project elasticsearch by elastic.
the class Compiler method compile.
/**
* Runs the two-pass compiler to generate a Painless script. (Used by the debugger.)
* @param iface Interface the compiled script should implement
* @param source The source code for the script.
* @param settings The CompilerSettings to be used during the compilation.
* @return The bytes for compilation.
*/
static byte[] compile(Class<?> iface, String name, String source, CompilerSettings settings, Printer debugStream) {
if (source.length() > MAXIMUM_SOURCE_LENGTH) {
throw new IllegalArgumentException("Scripts may be no longer than " + MAXIMUM_SOURCE_LENGTH + " characters. The passed in script is " + source.length() + " characters. Consider using a" + " plugin if a script longer than this length is a requirement.");
}
ScriptInterface scriptInterface = new ScriptInterface(iface);
SSource root = Walker.buildPainlessTree(scriptInterface, name, source, settings, debugStream);
root.analyze();
root.write();
return root.getBytes();
}
Aggregations