Search in sources :

Example 1 with Constant

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

the class SFunction method write.

@Override
void write(MethodWriter function, Globals globals) {
    if (reserved.getMaxLoopCounter() > 0) {
        // if there is infinite loop protection, we do this once:
        // int #loop = settings.getMaxLoopCounter()
        function.push(reserved.getMaxLoopCounter());
        function.visitVarInsn(Opcodes.ISTORE, loop.getSlot());
    }
    for (AStatement statement : statements) {
        statement.write(function, globals);
    }
    if (!methodEscape) {
        if (rtnType.sort == Sort.VOID) {
            function.returnValue();
        } else {
            throw createError(new IllegalStateException("Illegal tree structure."));
        }
    }
    String staticHandleFieldName = Def.getUserFunctionHandleFieldName(name, parameters.size());
    globals.addConstantInitializer(new Constant(location, WriterConstants.METHOD_HANDLE_TYPE, staticHandleFieldName, this::initializeConstant));
}
Also used : Constant(org.elasticsearch.painless.Constant)

Example 2 with Constant

use of org.elasticsearch.painless.Constant 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)

Example 3 with Constant

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

the class ERegex method analyze.

@Override
void analyze(Locals locals) {
    if (!read) {
        throw createError(new IllegalArgumentException("Regex constant may only be read [" + pattern + "]."));
    }
    try {
        Pattern.compile(pattern, flags);
    } catch (PatternSyntaxException exception) {
        throw createError(exception);
    }
    constant = new Constant(location, Definition.PATTERN_TYPE.type, "regexAt$" + location.getOffset(), this::initializeConstant);
    actual = Definition.PATTERN_TYPE;
}
Also used : Constant(org.elasticsearch.painless.Constant) PatternSyntaxException(java.util.regex.PatternSyntaxException)

Aggregations

Constant (org.elasticsearch.painless.Constant)3 ArrayList (java.util.ArrayList)1 PatternSyntaxException (java.util.regex.PatternSyntaxException)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