use of php.runtime.reflection.helper.ClosureEntity 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.ClosureEntity in project jphp by jphp-compiler.
the class ClosureValueCompiler method write.
@Override
public void write(ClosureStmtToken closure, boolean returnValue) {
if (returnValue) {
ClosureEntity entity = compiler.getModule().findClosure(closure.getId());
boolean thisExists = closure.getFunction().isThisExists();
boolean staticExists = closure.getFunction().isStaticExists();
if (closure.getFunction().getUses().isEmpty() && !thisExists && !staticExists && closure.getFunction().getStaticLocal().isEmpty()) {
expr.writePushEnv();
expr.writePushConstString(compiler.getModule().getInternalName());
expr.writePushConstInt((int) entity.getId());
expr.writeSysDynamicCall(Environment.class, "__getSingletonClosure", Memory.class, String.class, Integer.TYPE);
} else {
add(new TypeInsnNode(NEW, entity.getInternalName()));
expr.stackPush(Memory.Type.REFERENCE);
expr.writePushDup();
expr.writePushEnv();
expr.writePushEnv();
expr.writePushConstString(compiler.getModule().getInternalName());
expr.writePushConstInt((int) entity.getId());
expr.writeSysDynamicCall(Environment.class, "__getClosure", ClassEntity.class, String.class, Integer.TYPE);
if (thisExists)
expr.writePushThis();
else
expr.writePushNull();
writeContext();
writePushUses(closure.getFunction().getUses());
add(new MethodInsnNode(INVOKESPECIAL, entity.getInternalName(), Constants.INIT_METHOD, Type.getMethodDescriptor(Type.getType(void.class), Type.getType(Environment.class), Type.getType(ClassEntity.class), Type.getType(Memory.class), Type.getType(String.class), Type.getType(Memory[].class)), false));
expr.stackPop();
expr.stackPop();
expr.stackPop();
expr.stackPop();
expr.stackPop();
expr.stackPop();
expr.writeSysStaticCall(ObjectMemory.class, "valueOf", Memory.class, IObject.class);
}
expr.setStackPeekAsImmutable();
}
}
use of php.runtime.reflection.helper.ClosureEntity 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.helper.ClosureEntity in project jphp by jphp-compiler.
the class ClosureStmtCompiler method compile.
@Override
public ClosureEntity compile() {
ClosureEntity entity = new ClosureEntity(getCompiler().getContext());
entity.setReturnReference(statement.getFunction().isReturnReference());
entity.setInternalName(compiler.getModule().getInternalName() + "_closure" + statement.getId());
entity.setId(statement.getId());
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(Closure.class.getSimpleName()));
MethodStmtToken methodToken = new MethodStmtToken(statement.getFunction());
methodToken.setClazz(classStmtToken);
methodToken.setReturnReference(entity.isReturnReference());
methodToken.setModifier(Modifier.PUBLIC);
methodToken.setName(NameToken.valueOf("__invoke"));
classStmtToken.setMethods(Arrays.asList(methodToken));
ClassStmtCompiler classStmtCompiler = new ClassStmtCompiler(this.compiler, classStmtToken);
classStmtCompiler.setSystem(true);
classStmtCompiler.setInterfaceCheck(false);
classStmtCompiler.setClassContext(statement.getOwnerClass());
classStmtCompiler.setFunctionName(null);
ClassEntity clazzEntity = classStmtCompiler.compile();
MethodEntity __invoke = clazzEntity.findMethod("__invoke");
entity.getMethods().putAll(clazzEntity.getMethods());
if (clazzEntity.getParent() != null)
entity.setParent(clazzEntity.getParent());
entity.setData(clazzEntity.getData());
entity.setParameters(__invoke.getParameters());
entity.doneDeclare();
entity.setGeneratorEntity(__invoke.getGeneratorEntity());
return entity;
}
use of php.runtime.reflection.helper.ClosureEntity in project jphp by jphp-compiler.
the class JvmCompiler method process.
public List<ExprStmtToken> process(List<Token> tokens, NamespaceStmtToken namespace) {
List<ExprStmtToken> externalCode = new ArrayList<ExprStmtToken>();
// write constants
for (ConstStmtToken constant : analyzer.getConstants()) {
List<ConstantEntity> items = compileConstant(constant);
for (ConstantEntity el : items) {
if (!constants.containsKey(el.getLowerName())) {
module.addConstant(el);
if (scope.findUserConstant(el.getName()) != null) {
environment.error(el.getTrace(), ErrorType.E_ERROR, Messages.ERR_CANNOT_REDECLARE_CONSTANT, el.getName());
}
constants.put(el.getLowerName(), el);
} else {
environment.error(el.getTrace(), ErrorType.E_ERROR, Messages.ERR_CANNOT_REDECLARE_CONSTANT, el.getName());
}
}
}
// write closures
for (ClosureStmtToken closure : analyzer.getClosures()) {
ClosureEntity closureEntity = new ClosureStmtCompiler(this, closure).compile();
module.addClosure(closureEntity);
}
for (FunctionStmtToken function : analyzer.getFunctions()) {
if (!function.isStatic()) {
FunctionEntity entity = compileFunction(function);
entity.setStatic(function.isStatic());
module.addFunction(entity);
}
}
for (Token token : tokens) {
if (token instanceof NamespaceStmtToken) {
setNamespace((NamespaceStmtToken) token);
}
if (token instanceof ClassStmtToken) {
ClassStmtCompiler cmp = new ClassStmtCompiler(this, (ClassStmtToken) token);
ClassEntity entity = cmp.compile();
entity.setStatic(true);
module.addClass(entity);
if (cmp.isInitDynamicExists()) {
externalCode.add(new ExprStmtToken(getEnvironment(), getContext(), new ClassInitEnvironment((ClassStmtToken) token, entity)));
}
} else if (token instanceof FunctionStmtToken) {
FunctionEntity entity = compileFunction((FunctionStmtToken) token);
entity.setStatic(true);
module.addFunction(entity);
functions.put(entity.getLowerName(), entity);
} else if (token instanceof ExprStmtToken) {
externalCode.add((ExprStmtToken) token);
} else
externalCode.add(new ExprStmtToken(getEnvironment(), getContext(), token));
}
return externalCode;
}
Aggregations