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