use of php.runtime.reflection.helper.ClosureEntity 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.ClosureEntity in project jphp by jphp-compiler.
the class ClosureDumper method load.
@Override
public ClosureEntity load(InputStream input) throws IOException {
DumpInputStream data = new DumpInputStream(input);
ClosureEntity entity = new ClosureEntity(context);
entity.setParent(env.scope.fetchUserClass(Closure.class));
entity.setInternalName(data.readName());
entity.setReturnReference(data.readBoolean());
entity.methodMagicInvoke = methodDumper.load(input);
entity.methodMagicInvoke.setClazz(entity);
entity.addMethod(entity.methodMagicInvoke, null);
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;
}
paramCount = data.readInt();
if (paramCount < 0)
throw new DumpException("Invalid param count");
entity.uses = new ParameterEntity[paramCount];
for (int i = 0; i < paramCount; i++) {
ParameterEntity param = parameterDumper.load(input);
param.setTrace(entity.getTrace());
entity.uses[i] = param;
}
entity.setData(data.readRawData(Integer.MAX_VALUE));
return entity;
}
use of php.runtime.reflection.helper.ClosureEntity in project jphp by jphp-compiler.
the class ModuleDumper method load.
@Override
public ModuleEntity load(InputStream input) throws IOException {
DumpInputStream data = new DumpInputStream(input);
int STAMP = data.readInt();
if (STAMP != DUMP_STAMP)
throw new DumpException("Invalid file format");
int VERSION = data.readInt();
if (VERSION != DUMP_VERSION)
throw new DumpException("Invalid dump version - " + VERSION + ", only " + DUMP_VERSION);
// legacy
data.readLangMode();
ModuleEntity entity = new ModuleEntity(context);
entity.setName(data.readName());
entity.setInternalName(data.readName());
entity.setTrace(data.readTrace(context));
// constants
int count = data.readInt();
for (int i = 0; i < count; i++) {
ConstantEntity el = constantDumper.load(input);
el.setModule(entity);
entity.addConstant(el);
}
// closures
count = data.readInt();
for (int i = 0; i < count; i++) {
ClosureEntity el = closureDumper.load(input);
el.setModule(entity);
entity.addClosure(el);
}
count = data.readInt();
for (int i = 0; i < count; i++) {
GeneratorEntity el = generatorDumper.load(input);
el.setModule(entity);
entity.addGenerator(el);
}
// functions
count = data.readInt();
for (int i = 0; i < count; i++) {
FunctionEntity el = functionDumper.load(input);
el.setModule(entity);
entity.addFunction(el);
}
// classes
count = data.readInt();
ClassDumper classDumper = new ClassDumper(context, entity, env, debugInformation);
for (int i = 0; i < count; i++) {
ClassEntity el = classDumper.load(input);
el.setModule(entity);
entity.addClass(el);
}
// byte code
entity.setData(data.readRawData(Integer.MAX_VALUE));
data.readRawData(1024 * 1024 * 5);
return entity;
}
use of php.runtime.reflection.helper.ClosureEntity 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;
}
use of php.runtime.reflection.helper.ClosureEntity in project jphp by jphp-compiler.
the class FunctionEntity method getClosure.
@Override
public Closure getClosure(Environment env) {
if (cachedClosure != null)
return cachedClosure;
final FunctionEntity bind = this;
final ClosureEntity closureEntity1 = new ClosureEntity(this.getContext());
closureEntity1.setParent(env.scope.fetchUserClass(Closure.class));
closureEntity1.parameters = this.parameters;
closureEntity1.setReturnReference(this.isReturnReference());
MethodEntity m = new MethodEntity(this);
m.setClazz(closureEntity1);
m.setName("__invoke");
closureEntity1.addMethod(m, null);
closureEntity1.doneDeclare();
Closure tmp = new Closure(env, closureEntity1, new ObjectMemory(env.getLateObject()), null, new Memory[0]) {
@Override
public Memory __invoke(Environment e, Memory... args) {
try {
return bind.invoke(e, e.peekCall(0).trace, args);
} catch (RuntimeException e1) {
throw e1;
} catch (Throwable throwable) {
throw new RuntimeException(throwable);
}
}
@Override
public Memory getOrCreateStatic(String name) {
return Memory.NULL;
}
@Override
public ClassEntity getReflection() {
return closureEntity1;
}
};
try {
m.setNativeMethod(tmp.getClass().getDeclaredMethod("__invoke", Environment.class, Memory[].class));
} catch (NoSuchMethodException e) {
throw new RuntimeException(e);
}
return cachedClosure = tmp;
}
Aggregations