Search in sources :

Example 6 with MethodOutput

use of stanhebben.zenscript.util.MethodOutput in project ZenScript by CraftTweaker.

the class ExpressionArrayListAdd method compile.

@Override
public void compile(boolean result, IEnvironmentMethod environment) {
    MethodOutput output = environment.getOutput();
    array.compile(true, environment);
    output.dup();
    value.compile(true, environment);
    output.invokeInterface(List.class, "add", boolean.class, Object.class);
    output.pop();
}
Also used : MethodOutput(stanhebben.zenscript.util.MethodOutput)

Example 7 with MethodOutput

use of stanhebben.zenscript.util.MethodOutput in project ZenScript by CraftTweaker.

the class ExpressionJavaLambdaSimpleGeneric method compile.

@Override
public void compile(boolean result, IEnvironmentMethod environment) {
    if (!result)
        return;
    Method method = interfaceClass.getMethods()[0];
    // generate class
    String clsName = environment.makeClassName();
    ClassWriter cw = new ZenClassWriter(ClassWriter.COMPUTE_FRAMES);
    cw.visit(Opcodes.V1_6, Opcodes.ACC_PUBLIC, clsName, createMethodSignature(), "java/lang/Object", new String[] { internal(interfaceClass) });
    MethodOutput constructor = new MethodOutput(cw, Opcodes.ACC_PUBLIC, "<init>", "()V", null, null);
    constructor.start();
    constructor.loadObject(0);
    constructor.invokeSpecial("java/lang/Object", "<init>", "()V");
    constructor.ret();
    constructor.end();
    MethodOutput output = new MethodOutput(cw, Opcodes.ACC_PUBLIC, method.getName(), descriptor, null, null);
    IEnvironmentClass environmentClass = new EnvironmentClass(cw, environment);
    IEnvironmentMethod environmentMethod = new EnvironmentMethod(output, environmentClass);
    for (int i = 0; i < arguments.size(); i++) {
        ZenType typeToPut = arguments.get(i).getType();
        if (typeToPut.equals(ZenType.ANY))
            typeToPut = environment.getType(method.getGenericParameterTypes()[i]);
        if (typeToPut == null)
            typeToPut = environment.getType(method.getParameterTypes()[i]);
        environmentMethod.putValue(arguments.get(i).getName(), new SymbolArgument(i + 1, typeToPut), getPosition());
    }
    output.start();
    for (Statement statement : statements) {
        statement.compile(environmentMethod);
    }
    output.ret();
    output.end();
    if (!Objects.equals(genericClass, Object.class)) {
        MethodOutput bridge = new MethodOutput(cw, Opcodes.ACC_PUBLIC | Opcodes.ACC_SYNTHETIC | Opcodes.ACC_BRIDGE, method.getName(), ZenTypeUtil.descriptor(method), null, null);
        bridge.loadObject(0);
        bridge.loadObject(1);
        bridge.checkCast(internal(genericClass));
        if (arguments.size() > 1) {
            for (int i = 1; i < arguments.size(); ) {
                bridge.load(org.objectweb.asm.Type.getType(method.getParameterTypes()[i]), ++i);
            }
        }
        bridge.invokeVirtual(clsName, method.getName(), descriptor);
        bridge.returnType(org.objectweb.asm.Type.getReturnType(method));
        bridge.end();
    }
    environment.putClass(clsName, cw.toByteArray());
    // make class instance
    environment.getOutput().newObject(clsName);
    environment.getOutput().dup();
    environment.getOutput().construct(clsName);
}
Also used : SymbolArgument(stanhebben.zenscript.symbols.SymbolArgument) Statement(stanhebben.zenscript.statements.Statement) MethodOutput(stanhebben.zenscript.util.MethodOutput) Method(java.lang.reflect.Method) ClassWriter(org.objectweb.asm.ClassWriter) ZenType(stanhebben.zenscript.type.ZenType)

Example 8 with MethodOutput

use of stanhebben.zenscript.util.MethodOutput in project ZenScript by CraftTweaker.

the class ExpressionFloat method compile.

@Override
public void compile(boolean result, IEnvironmentMethod environment) {
    if (!result)
        return;
    MethodOutput output = environment.getOutput();
    if (type == ZenTypeFloat.INSTANCE) {
        output.constant((float) value);
    } else if (type == ZenTypeDouble.INSTANCE) {
        output.constant(value);
    } else if (type == ZenTypeFloatObject.INSTANCE) {
        output.constant((float) value);
        output.invokeStatic(ZenTypeUtil.internal(Float.class), "valueOf", "(F)Ljava/lang/Float;");
    } else if (type == ZenTypeDoubleObject.INSTANCE) {
        output.constant(value);
        output.invokeSpecial(ZenTypeUtil.internal(Double.class), "valueOf", "(D)Ljava/lang/Double;");
    } else {
        throw new RuntimeException("Internal compiler error: source type is not a floating point type");
    }
}
Also used : MethodOutput(stanhebben.zenscript.util.MethodOutput)

Example 9 with MethodOutput

use of stanhebben.zenscript.util.MethodOutput in project ZenScript by CraftTweaker.

the class ExpressionArrayAdd method compile.

@Override
public void compile(boolean result, IEnvironmentMethod environment) {
    MethodOutput output = environment.getOutput();
    array.compile(true, environment);
    value.cast(getPosition(), environment, type.getBaseType()).compile(true, environment);
    if (type.getBaseType().toJavaClass().isPrimitive()) {
        Class<?> arrayType = getType().toJavaClass();
        output.invokeStatic(ArrayUtil.class, "add", arrayType, arrayType, type.getBaseType().toJavaClass());
    } else {
        output.invokeStatic(ArrayUtil.class, "add", Object[].class, Object[].class, Object.class);
        output.checkCast(type.getSignature());
    }
}
Also used : MethodOutput(stanhebben.zenscript.util.MethodOutput)

Aggregations

MethodOutput (stanhebben.zenscript.util.MethodOutput)9 Label (org.objectweb.asm.Label)4 Method (java.lang.reflect.Method)1 ClassWriter (org.objectweb.asm.ClassWriter)1 Statement (stanhebben.zenscript.statements.Statement)1 SymbolArgument (stanhebben.zenscript.symbols.SymbolArgument)1 ZenType (stanhebben.zenscript.type.ZenType)1