Search in sources :

Example 1 with SimpleChecksAdapter

use of org.elasticsearch.painless.SimpleChecksAdapter in project elasticsearch by elastic.

the class SSource method write.

public void write() {
    // Create the ClassWriter.
    int classFrames = ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS;
    int classAccess = Opcodes.ACC_PUBLIC | Opcodes.ACC_SUPER | Opcodes.ACC_FINAL;
    String classBase = BASE_CLASS_TYPE.getInternalName();
    String className = CLASS_TYPE.getInternalName();
    String[] classInterfaces = new String[] { Type.getType(scriptInterface.getInterface()).getInternalName() };
    ClassWriter writer = new ClassWriter(classFrames);
    ClassVisitor visitor = writer;
    // if picky is enabled, turn on some checks. instead of VerifyError at the end, you get a helpful stacktrace.
    if (settings.isPicky()) {
        visitor = new SimpleChecksAdapter(visitor);
    }
    if (debugStream != null) {
        visitor = new TraceClassVisitor(visitor, debugStream, null);
    }
    visitor.visit(WriterConstants.CLASS_VERSION, classAccess, className, null, classBase, classInterfaces);
    visitor.visitSource(Location.computeSourceName(name, source), null);
    // Write the constructor:
    MethodWriter constructor = new MethodWriter(Opcodes.ACC_PUBLIC, CONSTRUCTOR, visitor, globals.getStatements(), settings);
    constructor.visitCode();
    constructor.loadThis();
    constructor.loadArgs();
    constructor.invokeConstructor(BASE_CLASS_TYPE, CONSTRUCTOR);
    constructor.returnValue();
    constructor.endMethod();
    // Write the method defined in the interface:
    MethodWriter executeMethod = new MethodWriter(Opcodes.ACC_PUBLIC, scriptInterface.getExecuteMethod(), visitor, globals.getStatements(), settings);
    executeMethod.visitCode();
    write(executeMethod, globals);
    executeMethod.endMethod();
    // Write all functions:
    for (SFunction function : functions) {
        function.write(visitor, settings, globals);
    }
    // Write all synthetic functions. Note that this process may add more :)
    while (!globals.getSyntheticMethods().isEmpty()) {
        List<SFunction> current = new ArrayList<>(globals.getSyntheticMethods().values());
        globals.getSyntheticMethods().clear();
        for (SFunction function : current) {
            function.write(visitor, settings, globals);
        }
    }
    // Write the constants
    if (false == globals.getConstantInitializers().isEmpty()) {
        Collection<Constant> inits = globals.getConstantInitializers().values();
        // Fields
        for (Constant constant : inits) {
            visitor.visitField(Opcodes.ACC_FINAL | Opcodes.ACC_PRIVATE | Opcodes.ACC_STATIC, constant.name, constant.type.getDescriptor(), null, null).visitEnd();
        }
        // Initialize the constants in a static initializer
        final MethodWriter clinit = new MethodWriter(Opcodes.ACC_STATIC, WriterConstants.CLINIT, visitor, globals.getStatements(), settings);
        clinit.visitCode();
        for (Constant constant : inits) {
            constant.initializer.accept(clinit);
            clinit.putStatic(CLASS_TYPE, constant.name, constant.type);
        }
        clinit.returnValue();
        clinit.endMethod();
    }
    // Write any uses$varName methods for used variables
    for (org.objectweb.asm.commons.Method usesMethod : scriptInterface.getUsesMethods()) {
        MethodWriter ifaceMethod = new MethodWriter(Opcodes.ACC_PUBLIC, usesMethod, visitor, globals.getStatements(), settings);
        ifaceMethod.visitCode();
        ifaceMethod.push(reserved.getUsedVariables().contains(usesMethod.getName().substring("uses$".length())));
        ifaceMethod.returnValue();
        ifaceMethod.endMethod();
    }
    // End writing the class and store the generated bytes.
    visitor.visitEnd();
    bytes = writer.toByteArray();
}
Also used : Constant(org.elasticsearch.painless.Constant) MethodWriter(org.elasticsearch.painless.MethodWriter) ArrayList(java.util.ArrayList) ClassVisitor(org.objectweb.asm.ClassVisitor) TraceClassVisitor(org.objectweb.asm.util.TraceClassVisitor) ClassWriter(org.objectweb.asm.ClassWriter) TraceClassVisitor(org.objectweb.asm.util.TraceClassVisitor) SimpleChecksAdapter(org.elasticsearch.painless.SimpleChecksAdapter)

Aggregations

ArrayList (java.util.ArrayList)1 Constant (org.elasticsearch.painless.Constant)1 MethodWriter (org.elasticsearch.painless.MethodWriter)1 SimpleChecksAdapter (org.elasticsearch.painless.SimpleChecksAdapter)1 ClassVisitor (org.objectweb.asm.ClassVisitor)1 ClassWriter (org.objectweb.asm.ClassWriter)1 TraceClassVisitor (org.objectweb.asm.util.TraceClassVisitor)1