Search in sources :

Example 1 with ClosureEntity

use of php.runtime.reflection.helper.ClosureEntity in project jphp by jphp-compiler.

the class ModuleOpcodePrinter method toOutput.

public void toOutput(Writer writer, int flags) {
    try {
        OpcodePrinter opcodePrinter = new OpcodePrinter(module);
        writer.write("#### Module class: " + module.getContext().getModuleName() + "\n");
        opcodePrinter.toWriter(writer, flags);
        writer.write("#### /Module class \n\n\n");
        for (ClosureEntity closure : module.getClosures()) {
            opcodePrinter = new OpcodePrinter(closure);
            writer.write("#### Closure: " + closure.getInternalName() + "\n");
            opcodePrinter.toWriter(writer);
            writer.write("#### /Closure \n\n\n");
        }
        for (GeneratorEntity generator : module.getGenerators()) {
            opcodePrinter = new OpcodePrinter(generator);
            writer.write("#### Generator: " + generator.getInternalName() + "\n");
            opcodePrinter.toWriter(writer);
            writer.write("#### /Generator \n\n\n");
        }
        for (ClassEntity clazz : module.getClasses()) {
            if (clazz.isClass() || clazz.isTrait()) {
                opcodePrinter = new OpcodePrinter(clazz);
                writer.write("#### " + (clazz.isTrait() ? "Trait" : "Class") + ": " + clazz.getName() + "\n");
                opcodePrinter.toWriter(writer);
                writer.write("#### /" + (clazz.isTrait() ? "Trait" : "Class") + " \n\n\n");
            }
        }
        for (FunctionEntity function : module.getFunctions()) {
            opcodePrinter = new OpcodePrinter(function);
            writer.write("#### Function: " + function.getName() + "\n");
            opcodePrinter.toWriter(writer);
            writer.write("#### /Function \n\n\n");
        }
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
Also used : ClassEntity(php.runtime.reflection.ClassEntity) GeneratorEntity(php.runtime.reflection.helper.GeneratorEntity) FunctionEntity(php.runtime.reflection.FunctionEntity) ClosureEntity(php.runtime.reflection.helper.ClosureEntity)

Example 2 with ClosureEntity

use of php.runtime.reflection.helper.ClosureEntity in project jphp by jphp-compiler.

the class ClosureValueCompiler method write.

@Override
public void write(ClosureStmtToken closure, boolean returnValue) {
    if (returnValue) {
        ClosureEntity entity = compiler.getModule().findClosure(closure.getId());
        boolean thisExists = closure.getFunction().isThisExists();
        boolean staticExists = closure.getFunction().isStaticExists();
        if (closure.getFunction().getUses().isEmpty() && !thisExists && !staticExists && closure.getFunction().getStaticLocal().isEmpty()) {
            expr.writePushEnv();
            expr.writePushConstString(compiler.getModule().getInternalName());
            expr.writePushConstInt((int) entity.getId());
            expr.writeSysDynamicCall(Environment.class, "__getSingletonClosure", Memory.class, String.class, Integer.TYPE);
        } else {
            add(new TypeInsnNode(NEW, entity.getInternalName()));
            expr.stackPush(Memory.Type.REFERENCE);
            expr.writePushDup();
            expr.writePushEnv();
            expr.writePushEnv();
            expr.writePushConstString(compiler.getModule().getInternalName());
            expr.writePushConstInt((int) entity.getId());
            expr.writeSysDynamicCall(Environment.class, "__getClosure", ClassEntity.class, String.class, Integer.TYPE);
            if (thisExists)
                expr.writePushThis();
            else
                expr.writePushNull();
            writeContext();
            writePushUses(closure.getFunction().getUses());
            add(new MethodInsnNode(INVOKESPECIAL, entity.getInternalName(), Constants.INIT_METHOD, Type.getMethodDescriptor(Type.getType(void.class), Type.getType(Environment.class), Type.getType(ClassEntity.class), Type.getType(Memory.class), Type.getType(String.class), Type.getType(Memory[].class)), false));
            expr.stackPop();
            expr.stackPop();
            expr.stackPop();
            expr.stackPop();
            expr.stackPop();
            expr.stackPop();
            expr.writeSysStaticCall(ObjectMemory.class, "valueOf", Memory.class, IObject.class);
        }
        expr.setStackPeekAsImmutable();
    }
}
Also used : ClassEntity(php.runtime.reflection.ClassEntity) ClosureEntity(php.runtime.reflection.helper.ClosureEntity) MethodInsnNode(org.objectweb.asm.tree.MethodInsnNode) Memory(php.runtime.Memory) ObjectMemory(php.runtime.memory.ObjectMemory) StringMemory(php.runtime.memory.StringMemory) Environment(php.runtime.env.Environment) TypeInsnNode(org.objectweb.asm.tree.TypeInsnNode)

Example 3 with ClosureEntity

use of php.runtime.reflection.helper.ClosureEntity in project jphp by jphp-compiler.

the class ReflectionParameter method __construct.

@Signature({ @Arg("function"), @Arg("parameter") })
public Memory __construct(Environment env, Memory... args) {
    ParameterEntity[] parameters = null;
    if (args[0].isClosure()) {
        ClosureEntity tmp = (ClosureEntity) args[0].toValue(ObjectMemory.class).getReflection();
        parameters = tmp.parameters;
    } else if (args[0].isArray()) {
        Invoker invoker = Invoker.valueOf(env, null, args[0]);
        if (invoker == null) {
            exception(env, "%s does not exists", args[0].toString());
            return Memory.NULL;
        }
        parameters = invoker.getParameters();
    } else {
        String name = args[0].toString();
        if (name.contains("::")) {
            Invoker invoker = Invoker.valueOf(env, null, args[0]);
            if (invoker == null) {
                exception(env, "%s does not exists", args[0].toString());
                return Memory.NULL;
            }
            parameters = invoker.getParameters();
        } else {
            FunctionEntity tmp = env.fetchFunction(name);
            functionEntity = tmp;
            if (tmp == null) {
                exception(env, "Function %s does not exist", args[0].toString());
                return Memory.NULL;
            }
            if (tmp.isInternal()) {
                exception(env, "%s(): ReflectionParameter does not support internal functions", tmp.getName());
                return Memory.NULL;
            }
            parameters = tmp.getParameters();
        }
    }
    entity = null;
    String name = args[1].toString();
    if (parameters != null) {
        if (args[1].isNumber()) {
            int index = args[1].toInteger();
            if (index >= 0 && index < parameters.length) {
                entity = parameters[index];
                position = index;
            }
        } else {
            int i = 0;
            for (ParameterEntity e : parameters) {
                if (e.getName().equals(name)) {
                    entity = e;
                    position = i;
                    break;
                }
                i++;
            }
        }
    }
    if (entity == null)
        exception(env, "Parameter %s does not exist", name);
    setEntity(entity);
    return Memory.NULL;
}
Also used : ParameterEntity(php.runtime.reflection.ParameterEntity) AbstractFunctionEntity(php.runtime.reflection.support.AbstractFunctionEntity) FunctionEntity(php.runtime.reflection.FunctionEntity) Invoker(php.runtime.invoke.Invoker) ClosureEntity(php.runtime.reflection.helper.ClosureEntity)

Example 4 with ClosureEntity

use of php.runtime.reflection.helper.ClosureEntity in project jphp by jphp-compiler.

the class ClosureStmtCompiler method compile.

@Override
public ClosureEntity compile() {
    ClosureEntity entity = new ClosureEntity(getCompiler().getContext());
    entity.setReturnReference(statement.getFunction().isReturnReference());
    entity.setInternalName(compiler.getModule().getInternalName() + "_closure" + statement.getId());
    entity.setId(statement.getId());
    entity.setTrace(statement.toTraceInfo(compiler.getContext()));
    ClassStmtToken classStmtToken = new ClassStmtToken(statement.getMeta());
    classStmtToken.setNamespace(NamespaceStmtToken.getDefault());
    classStmtToken.setName(NameToken.valueOf(entity.getInternalName()));
    classStmtToken.setExtend(ExtendsStmtToken.valueOf(Closure.class.getSimpleName()));
    MethodStmtToken methodToken = new MethodStmtToken(statement.getFunction());
    methodToken.setClazz(classStmtToken);
    methodToken.setReturnReference(entity.isReturnReference());
    methodToken.setModifier(Modifier.PUBLIC);
    methodToken.setName(NameToken.valueOf("__invoke"));
    classStmtToken.setMethods(Arrays.asList(methodToken));
    ClassStmtCompiler classStmtCompiler = new ClassStmtCompiler(this.compiler, classStmtToken);
    classStmtCompiler.setSystem(true);
    classStmtCompiler.setInterfaceCheck(false);
    classStmtCompiler.setClassContext(statement.getOwnerClass());
    classStmtCompiler.setFunctionName(null);
    ClassEntity clazzEntity = classStmtCompiler.compile();
    MethodEntity __invoke = clazzEntity.findMethod("__invoke");
    entity.getMethods().putAll(clazzEntity.getMethods());
    if (clazzEntity.getParent() != null)
        entity.setParent(clazzEntity.getParent());
    entity.setData(clazzEntity.getData());
    entity.setParameters(__invoke.getParameters());
    entity.doneDeclare();
    entity.setGeneratorEntity(__invoke.getGeneratorEntity());
    return entity;
}
Also used : ClassEntity(php.runtime.reflection.ClassEntity) ClosureEntity(php.runtime.reflection.helper.ClosureEntity) MethodEntity(php.runtime.reflection.MethodEntity) ClassStmtToken(org.develnext.jphp.core.tokenizer.token.stmt.ClassStmtToken) MethodStmtToken(org.develnext.jphp.core.tokenizer.token.stmt.MethodStmtToken)

Example 5 with ClosureEntity

use of php.runtime.reflection.helper.ClosureEntity in project jphp by jphp-compiler.

the class JvmCompiler method process.

public List<ExprStmtToken> process(List<Token> tokens, NamespaceStmtToken namespace) {
    List<ExprStmtToken> externalCode = new ArrayList<ExprStmtToken>();
    // write constants
    for (ConstStmtToken constant : analyzer.getConstants()) {
        List<ConstantEntity> items = compileConstant(constant);
        for (ConstantEntity el : items) {
            if (!constants.containsKey(el.getLowerName())) {
                module.addConstant(el);
                if (scope.findUserConstant(el.getName()) != null) {
                    environment.error(el.getTrace(), ErrorType.E_ERROR, Messages.ERR_CANNOT_REDECLARE_CONSTANT, el.getName());
                }
                constants.put(el.getLowerName(), el);
            } else {
                environment.error(el.getTrace(), ErrorType.E_ERROR, Messages.ERR_CANNOT_REDECLARE_CONSTANT, el.getName());
            }
        }
    }
    // write closures
    for (ClosureStmtToken closure : analyzer.getClosures()) {
        ClosureEntity closureEntity = new ClosureStmtCompiler(this, closure).compile();
        module.addClosure(closureEntity);
    }
    for (FunctionStmtToken function : analyzer.getFunctions()) {
        if (!function.isStatic()) {
            FunctionEntity entity = compileFunction(function);
            entity.setStatic(function.isStatic());
            module.addFunction(entity);
        }
    }
    for (Token token : tokens) {
        if (token instanceof NamespaceStmtToken) {
            setNamespace((NamespaceStmtToken) token);
        }
        if (token instanceof ClassStmtToken) {
            ClassStmtCompiler cmp = new ClassStmtCompiler(this, (ClassStmtToken) token);
            ClassEntity entity = cmp.compile();
            entity.setStatic(true);
            module.addClass(entity);
            if (cmp.isInitDynamicExists()) {
                externalCode.add(new ExprStmtToken(getEnvironment(), getContext(), new ClassInitEnvironment((ClassStmtToken) token, entity)));
            }
        } else if (token instanceof FunctionStmtToken) {
            FunctionEntity entity = compileFunction((FunctionStmtToken) token);
            entity.setStatic(true);
            module.addFunction(entity);
            functions.put(entity.getLowerName(), entity);
        } else if (token instanceof ExprStmtToken) {
            externalCode.add((ExprStmtToken) token);
        } else
            externalCode.add(new ExprStmtToken(getEnvironment(), getContext(), token));
    }
    return externalCode;
}
Also used : Token(org.develnext.jphp.core.tokenizer.token.Token) YieldExprToken(org.develnext.jphp.core.tokenizer.token.expr.value.YieldExprToken) NameToken(org.develnext.jphp.core.tokenizer.token.expr.value.NameToken) ClosureStmtToken(org.develnext.jphp.core.tokenizer.token.expr.value.ClosureStmtToken) ClosureEntity(php.runtime.reflection.helper.ClosureEntity) ClosureStmtToken(org.develnext.jphp.core.tokenizer.token.expr.value.ClosureStmtToken)

Aggregations

ClosureEntity (php.runtime.reflection.helper.ClosureEntity)11 ClassEntity (php.runtime.reflection.ClassEntity)5 Memory (php.runtime.Memory)4 FunctionEntity (php.runtime.reflection.FunctionEntity)4 GeneratorEntity (php.runtime.reflection.helper.GeneratorEntity)4 Environment (php.runtime.env.Environment)3 Closure (php.runtime.lang.Closure)3 DumpException (php.runtime.loader.dump.io.DumpException)3 ObjectMemory (php.runtime.memory.ObjectMemory)3 DumpInputStream (php.runtime.loader.dump.io.DumpInputStream)2 ConstantEntity (php.runtime.reflection.ConstantEntity)2 ParameterEntity (php.runtime.reflection.ParameterEntity)2 AbstractFunctionEntity (php.runtime.reflection.support.AbstractFunctionEntity)2 Method (java.lang.reflect.Method)1 Token (org.develnext.jphp.core.tokenizer.token.Token)1 ClosureStmtToken (org.develnext.jphp.core.tokenizer.token.expr.value.ClosureStmtToken)1 NameToken (org.develnext.jphp.core.tokenizer.token.expr.value.NameToken)1 YieldExprToken (org.develnext.jphp.core.tokenizer.token.expr.value.YieldExprToken)1 ClassStmtToken (org.develnext.jphp.core.tokenizer.token.stmt.ClassStmtToken)1 MethodStmtToken (org.develnext.jphp.core.tokenizer.token.stmt.MethodStmtToken)1