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());
}
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();
}
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);
}
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());
}
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;
}
Aggregations