Search in sources :

Example 1 with ConstantEntity

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

the class ConstantDumperTest method testBasic.

@Test
public void testBasic() throws IOException {
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    ConstantEntity constantEntity = new ConstantEntity("foobar", Memory.CONST_INT_5, false);
    constantDumper.save(constantEntity, output);
    ConstantEntity copyEntity = constantDumper.load(new ByteArrayInputStream(output.toByteArray()));
    Assert.assertEquals("foobar", copyEntity.getName());
    Assert.assertEquals(Memory.CONST_INT_5, copyEntity.getValue());
    Assert.assertFalse(copyEntity.isCaseSensitise());
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ConstantEntity(php.runtime.reflection.ConstantEntity) Test(org.junit.Test)

Example 2 with ConstantEntity

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

the class ClassConstantMemory method toImmutable.

@Override
public Memory toImmutable(Environment env, TraceInfo trace) {
    ClassEntity classEntity = env.fetchClass(className, classLowerName, true);
    if (classEntity == null) {
        env.error(trace, ErrorType.E_ERROR, Messages.ERR_CLASS_NOT_FOUND, className);
        return NULL;
    }
    ConstantEntity entity = classEntity.findConstant(name);
    if (entity == null) {
        env.error(trace, ErrorType.E_ERROR, Messages.ERR_UNDEFINED_CLASS_CONSTANT, className + "::" + name);
        return NULL;
    }
    return entity.getValue();
}
Also used : ClassEntity(php.runtime.reflection.ClassEntity) ConstantEntity(php.runtime.reflection.ConstantEntity)

Example 3 with ConstantEntity

use of php.runtime.reflection.ConstantEntity 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 4 with ConstantEntity

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

the class ClassDumperTest method testComplex.

@Test
public void testComplex() throws IOException {
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    final ClassEntity entity = new ClassEntity(context);
    entity.setId(1);
    entity.setName("Foobar");
    entity.setData(new byte[] { 1 });
    entity.addConstant(new ConstantEntity("const1", Memory.TRUE, true));
    entity.addConstant(new ConstantEntity("const2", Memory.FALSE, true));
    entity.addConstant(new ConstantEntity("const3", Memory.NULL, true));
    entity.addProperty(new PropertyEntity(context) {

        {
            setName("prop1");
            setStatic(true);
            setDefaultValue(Memory.CONST_INT_3);
        }
    });
    entity.addProperty(new PropertyEntity(context) {

        {
            setName("prop2");
            setStatic(false);
            setDefaultValue(Memory.CONST_INT_5);
        }
    });
    entity.addMethod(new MethodEntity(context) {

        {
            setName("method1");
            setModifier(Modifier.PUBLIC);
            setClazz(entity);
        }
    }, null);
    dumper.save(entity, output);
    ClassEntity copyEntity = dumper.load(new ByteArrayInputStream(output.toByteArray()));
    Assert.assertEquals("Foobar", copyEntity.getName());
    Assert.assertArrayEquals(new byte[] { 1 }, copyEntity.getData());
    Assert.assertEquals(3, copyEntity.constants.size());
    Assert.assertEquals(1, copyEntity.staticProperties.size());
    Assert.assertEquals(1, copyEntity.properties.size());
    Assert.assertEquals(1, copyEntity.getMethods().size());
    Assert.assertEquals("const1", copyEntity.findConstant("const1").getName());
    Assert.assertEquals("const2", copyEntity.findConstant("const2").getName());
    Assert.assertEquals("const3", copyEntity.findConstant("const3").getName());
    Assert.assertEquals("prop1", copyEntity.staticProperties.get("prop1").getName());
    Assert.assertEquals("prop2", copyEntity.properties.get("prop2").getName());
    Assert.assertEquals("method1", copyEntity.findMethod("method1").getName());
}
Also used : ClassEntity(php.runtime.reflection.ClassEntity) PropertyEntity(php.runtime.reflection.PropertyEntity) ByteArrayInputStream(java.io.ByteArrayInputStream) MethodEntity(php.runtime.reflection.MethodEntity) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ConstantEntity(php.runtime.reflection.ConstantEntity) Test(org.junit.Test)

Example 5 with ConstantEntity

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

the class ObjectInvokeHelper method getConstant.

public static Memory getConstant(String className, String lowerClassName, String constant, Environment env, TraceInfo trace, ConstantCallCache callCache, int cacheIndex) {
    ConstantEntity constantEntity = null;
    if (callCache != null) {
        constantEntity = callCache.get(env, cacheIndex);
    }
    if (constantEntity == null) {
        ClassEntity entity = env.fetchClass(className, lowerClassName, true);
        if (entity == null) {
            env.error(trace, Messages.ERR_CLASS_NOT_FOUND.fetch(className));
            return Memory.NULL;
        }
        constantEntity = entity.findConstant(constant);
        if (constantEntity == null) {
            env.error(trace, Messages.ERR_UNDEFINED_CLASS_CONSTANT.fetch(constant));
            return Memory.NULL;
        }
        if (callCache != null) {
            callCache.put(env, cacheIndex, constantEntity);
        }
    }
    Memory value = constantEntity.getValue(env);
    if (value == null) {
        return Memory.NULL;
    }
    return value;
}
Also used : ClassEntity(php.runtime.reflection.ClassEntity) Memory(php.runtime.Memory) ArrayMemory(php.runtime.memory.ArrayMemory) ReferenceMemory(php.runtime.memory.ReferenceMemory) ObjectMemory(php.runtime.memory.ObjectMemory) StringMemory(php.runtime.memory.StringMemory) ConstantEntity(php.runtime.reflection.ConstantEntity)

Aggregations

ConstantEntity (php.runtime.reflection.ConstantEntity)8 ClassEntity (php.runtime.reflection.ClassEntity)5 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 Test (org.junit.Test)2 Memory (php.runtime.Memory)2 DumpException (php.runtime.loader.dump.io.DumpException)2 DumpInputStream (php.runtime.loader.dump.io.DumpInputStream)2 ArrayMemory (php.runtime.memory.ArrayMemory)2 FunctionEntity (php.runtime.reflection.FunctionEntity)2 ClosureEntity (php.runtime.reflection.helper.ClosureEntity)2 GeneratorEntity (php.runtime.reflection.helper.GeneratorEntity)2 HashSet (java.util.HashSet)1 Modifier (php.runtime.common.Modifier)1 TraceInfo (php.runtime.env.TraceInfo)1 Extension (php.runtime.ext.support.Extension)1 CompileConstant (php.runtime.ext.support.compile.CompileConstant)1 DumpOutputStream (php.runtime.loader.dump.io.DumpOutputStream)1 ObjectMemory (php.runtime.memory.ObjectMemory)1 ReferenceMemory (php.runtime.memory.ReferenceMemory)1