use of php.runtime.reflection.helper.GeneratorEntity 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.helper.GeneratorEntity in project jphp by jphp-compiler.
the class GeneratorStmtCompiler method compile.
@Override
public GeneratorEntity compile() {
GeneratorEntity entity = new GeneratorEntity(getCompiler().getContext());
entity.setReturnReference(statement.isReturnReference());
entity.setInternalName(compiler.getModule().getInternalName() + "_generator" + statement.getId());
entity.setId(statement.getGeneratorId());
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(Generator.class.getSimpleName()));
MethodStmtToken methodToken = new MethodStmtToken(statement);
methodToken.setClazz(classStmtToken);
methodToken.setGenerator(false);
methodToken.setReturnReference(false);
methodToken.setModifier(Modifier.PROTECTED);
methodToken.setName(NameToken.valueOf("_run"));
methodToken.setUses(statement.getArguments());
methodToken.setArguments(Collections.<ArgumentStmtToken>emptyList());
classStmtToken.setMethods(Arrays.asList(methodToken));
ClassStmtCompiler classStmtCompiler = new ClassStmtCompiler(this.compiler, classStmtToken);
classStmtCompiler.setSystem(true);
classStmtCompiler.setInterfaceCheck(false);
classStmtCompiler.setGeneratorEntity(entity);
classStmtCompiler.setFunctionName(statement.getFulledName());
ClassEntity clazzEntity = classStmtCompiler.compile();
entity.getMethods().putAll(clazzEntity.getMethods());
if (clazzEntity.getParent() != null)
entity.setParent(clazzEntity.getParent());
entity.setData(clazzEntity.getData());
entity.doneDeclare();
return entity;
}
use of php.runtime.reflection.helper.GeneratorEntity in project jphp by jphp-compiler.
the class GeneratorDumper method load.
@Override
public GeneratorEntity load(InputStream input) throws IOException {
GeneratorEntity entity = classDumper.load(input, GeneratorEntity.class);
DumpInputStream data = new DumpInputStream(input);
entity.setReturnReference(data.readBoolean());
int paramCount = data.readInt();
if (paramCount < 0)
throw new DumpException("Invalid param count");
entity.parameters = new ParameterEntity[paramCount];
for (int i = 0; i < paramCount; i++) {
ParameterEntity param = parameterDumper.load(input);
param.setTrace(entity.getTrace());
entity.parameters[i] = param;
}
return entity;
}
use of php.runtime.reflection.helper.GeneratorEntity 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.helper.GeneratorEntity in project jphp by jphp-compiler.
the class RuntimeClassLoader method loadModule.
public boolean loadModule(ModuleEntity module, boolean withBytecode) {
String internal = module.getInternalName();
boolean ret = false;
if (!module.isLoaded()) {
internalModules.put(internal, module);
try {
for (ClosureEntity closure : module.getClosures()) loadClosure(closure, withBytecode);
for (GeneratorEntity generator : module.getGenerators()) loadGenerator(generator, withBytecode);
for (ClassEntity clazz : module.getClasses()) {
if (clazz.getType() != ClassEntity.Type.INTERFACE)
loadClass(clazz, withBytecode);
}
for (FunctionEntity function : module.getFunctions()) {
loadFunction(function, withBytecode);
}
} catch (NoSuchMethodException e) {
throw new RuntimeException(e);
} catch (NoSuchFieldException e) {
throw new RuntimeException(e);
}
module.setLoaded(true);
ret = true;
}
if (withBytecode) {
byte[] data = translateData(module.getData());
Class<?> result = defineClass(null, data, 0, module.getData().length);
module.setNativeClazz(result);
}
try {
Method method = module.getNativeClazz().getDeclaredMethod("__include", Environment.class, Memory[].class, ArrayMemory.class);
module.setNativeMethod(method);
} catch (NoSuchMethodException e) {
throw new RuntimeException(e);
}
return ret;
}
Aggregations