Search in sources :

Example 1 with FunctionEntity

use of php.runtime.reflection.FunctionEntity 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 FunctionEntity

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

the class FunctionDumperTest method testBasic.

@Test
public void testBasic() throws IOException {
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    FunctionEntity entity = new FunctionEntity(context);
    entity.setStatic(true);
    entity.setImmutable(true);
    entity.setResult(Memory.TRUE);
    entity.setEmpty(true);
    entity.setName("Foobar");
    entity.setData(new byte[] { 1, 2, 3, 4 });
    dumper.save(entity, output);
    FunctionEntity copyEntity = dumper.load(new ByteArrayInputStream(output.toByteArray()));
    Assert.assertEquals("Foobar", copyEntity.getName());
    Assert.assertTrue(copyEntity.isStatic());
    Assert.assertTrue(copyEntity.isImmutable());
    Assert.assertTrue(copyEntity.isEmpty());
    Assert.assertArrayEquals(new byte[] { 1, 2, 3, 4 }, copyEntity.getData());
}
Also used : FunctionEntity(php.runtime.reflection.FunctionEntity) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Example 3 with FunctionEntity

use of php.runtime.reflection.FunctionEntity 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 FunctionEntity

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

the class ModuleDumper method save.

@Override
public void save(ModuleEntity entity, OutputStream output) throws IOException {
    if (entity.getData() == null)
        throw new DumpException("Module '" + entity.getName() + "' not compiled");
    DumpOutputStream data = new DumpOutputStream(output);
    // version
    data.writeInt(DUMP_STAMP);
    data.writeInt(DUMP_VERSION);
    // legacy code.
    data.writeEnum(LangMode.DEFAULT);
    // module name
    data.writeName(entity.getContext().getModuleName());
    data.writeName(entity.getInternalName());
    // trace
    data.writeTrace(entity.getTrace());
    // constants
    data.writeInt(entity.getConstants().size());
    for (ConstantEntity e : entity.getConstants()) {
        constantDumper.save(e, output);
    }
    // closures
    data.writeInt(entity.getClosures().size());
    for (ClosureEntity e : entity.getClosures()) {
        closureDumper.save(e, output);
    }
    // generators
    data.writeInt(entity.getGenerators().size());
    for (GeneratorEntity e : entity.getGenerators()) {
        generatorDumper.save(e, output);
    }
    // functions
    data.writeInt(entity.getFunctions().size());
    for (FunctionEntity e : entity.getFunctions()) {
        functionDumper.save(e, output);
    }
    // classes
    data.writeInt(entity.getClasses().size());
    for (ClassEntity e : entity.getClasses()) {
        classDumper.save(e, output);
    }
    if (includeData) {
        // byte code
        data.writeInt(entity.getData().length);
        data.write(entity.getData());
    } else {
        data.writeInt(0);
    }
    // 5 mb
    data.writeRawData(null, 1024 * 1024 * 5);
}
Also used : ClassEntity(php.runtime.reflection.ClassEntity) GeneratorEntity(php.runtime.reflection.helper.GeneratorEntity) FunctionEntity(php.runtime.reflection.FunctionEntity) ClosureEntity(php.runtime.reflection.helper.ClosureEntity) DumpException(php.runtime.loader.dump.io.DumpException) DumpOutputStream(php.runtime.loader.dump.io.DumpOutputStream) ConstantEntity(php.runtime.reflection.ConstantEntity)

Example 5 with FunctionEntity

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

the class InfoFunctions method get_defined_functions.

public static Memory get_defined_functions(Environment env) {
    ArrayMemory array = new ArrayMemory();
    ArrayMemory item = (ArrayMemory) array.refOfIndex("internal").assign(new ArrayMemory());
    for (FunctionEntity entity : env.getFunctions()) {
        if (entity.isInternal())
            item.add(new StringMemory(entity.getName()));
    }
    item = (ArrayMemory) array.refOfIndex("user").assign(new ArrayMemory());
    for (FunctionEntity entity : env.getLoadedFunctions().values()) {
        if (!entity.isInternal())
            item.add(new StringMemory(entity.getName()));
    }
    return array.toConstant();
}
Also used : ArrayMemory(php.runtime.memory.ArrayMemory) FunctionEntity(php.runtime.reflection.FunctionEntity) StringMemory(php.runtime.memory.StringMemory)

Aggregations

FunctionEntity (php.runtime.reflection.FunctionEntity)11 ClassEntity (php.runtime.reflection.ClassEntity)5 ClosureEntity (php.runtime.reflection.helper.ClosureEntity)4 DumpException (php.runtime.loader.dump.io.DumpException)3 GeneratorEntity (php.runtime.reflection.helper.GeneratorEntity)3 AbstractFunctionEntity (php.runtime.reflection.support.AbstractFunctionEntity)3 DumpInputStream (php.runtime.loader.dump.io.DumpInputStream)2 ObjectMemory (php.runtime.memory.ObjectMemory)2 ConstantEntity (php.runtime.reflection.ConstantEntity)2 ParameterEntity (php.runtime.reflection.ParameterEntity)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 Test (org.junit.Test)1 Invoker (php.runtime.invoke.Invoker)1 DumpOutputStream (php.runtime.loader.dump.io.DumpOutputStream)1 ArrayMemory (php.runtime.memory.ArrayMemory)1 StringMemory (php.runtime.memory.StringMemory)1 MethodEntity (php.runtime.reflection.MethodEntity)1 ModuleEntity (php.runtime.reflection.ModuleEntity)1