use of php.runtime.reflection.ParameterEntity 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.ParameterEntity in project jphp by jphp-compiler.
the class MethodDumper method load.
@Override
public MethodEntity load(InputStream input) throws IOException {
DumpInputStream data = new DumpInputStream(input);
MethodEntity entity = new MethodEntity(context);
String docComment = data.readUTF();
if (!docComment.isEmpty()) {
entity.setDocComment(new DocumentComment(docComment));
}
// static
entity.setStatic(data.readBoolean());
// final
entity.setFinal(data.readBoolean());
// abstract
entity.setAbstract(data.readBoolean());
entity.setAbstractable(data.readBoolean());
entity.setImmutable(data.readBoolean());
entity.setResult(data.readMemory());
entity.setEmpty(data.readBoolean());
// ref
entity.setReturnReference(data.readBoolean());
// uses stack trace
entity.setUsesStackTrace(data.readBoolean());
// modifier
entity.setModifier(data.readModifier());
// name
entity.setName(data.readName());
entity.setInternalName(data.readName());
// trace
entity.setTrace(data.readTrace(context));
int paramCount = data.readInt();
if (paramCount < 0)
throw new DumpException("Invalid param count");
entity.setParameters(new ParameterEntity[paramCount]);
for (int i = 0; i < paramCount; i++) {
ParameterEntity param = parameterDumper.load(input);
param.setTrace(entity.getTrace());
entity.getParameters()[i] = param;
}
data.readRawData();
return entity;
}
use of php.runtime.reflection.ParameterEntity in project jphp by jphp-compiler.
the class MethodStmtCompiler method compile.
@Override
public MethodEntity compile() {
if (statement != null) {
if (external)
statement.setDynamicLocal(true);
if (statement.getDocComment() != null)
entity.setDocComment(new DocumentComment(statement.getDocComment().getComment()));
entity.setAbstract(statement.isAbstract());
entity.setAbstractable(statement.getBody() == null);
entity.setFinal(statement.isFinal());
entity.setStatic(statement.isStatic());
entity.setModifier(statement.getModifier());
entity.setReturnReference(statement.isReturnReference());
entity.setTrace(statement.toTraceInfo(compiler.getContext()));
entity.setImmutable(statement.getArguments().isEmpty());
entity.setGeneratorEntity(generatorEntity);
if (clazz.isSystem())
entity.setInternalName(entity.getName());
else
entity.setInternalName(entity.getName() + "$" + clazz.entity.nextMethodIndex());
ParameterEntity[] parameters = new ParameterEntity[statement.getArguments().size()];
int i = 0;
for (ArgumentStmtToken argument : statement.getArguments()) {
parameters[i] = new ParameterEntity(compiler.getContext());
ParameterEntity parameter = parameters[i];
parameter.setReference(argument.isReference());
parameter.setName(argument.getName().getName());
parameter.setTrace(argument.toTraceInfo(compiler.getContext()));
parameter.setMutable(statement.isDynamicLocal() || statement.variable(argument.getName()).isMutable());
parameter.setUsed(!statement.isUnusedVariable(argument.getName()));
parameter.setVariadic(argument.isVariadic());
parameter.setType(argument.getHintType());
if (argument.getHintTypeClass() != null) {
parameter.setTypeClass(argument.getHintTypeClass().getName());
}
ExpressionStmtCompiler expressionStmtCompiler = new ExpressionStmtCompiler(compiler);
ExprStmtToken value = argument.getValue();
if (value != null) {
Memory defaultValue = expressionStmtCompiler.writeExpression(value, true, true, false);
// try detect constant
if (value.isSingle()) {
if (value.getSingle() instanceof NameToken) {
parameter.setDefaultValueConstName(((NameToken) value.getSingle()).getName());
if (defaultValue == null) {
defaultValue = (new ConstantMemory(((NameToken) value.getSingle()).getName()));
parameter.setMutable(true);
}
} else if (value.getSingle() instanceof StaticAccessExprToken) {
StaticAccessExprToken access = (StaticAccessExprToken) value.getSingle();
if (access.getClazz() instanceof NameToken && access.getField() instanceof NameToken) {
if (defaultValue == null)
defaultValue = (new ClassConstantMemory(((NameToken) access.getClazz()).getName(), ((NameToken) access.getField()).getName()));
parameter.setDefaultValueConstName(((NameToken) access.getClazz()).getName() + "::" + ((NameToken) access.getField()).getName());
parameter.setMutable(true);
}
}
}
if (defaultValue == null)
compiler.getEnvironment().error(argument.toTraceInfo(compiler.getContext()), ErrorType.E_COMPILE_ERROR, Messages.ERR_EXPECTED_CONST_VALUE, "$" + argument.getName().getName());
parameter.setDefaultValue(defaultValue);
}
i++;
}
entity.setParameters(parameters);
}
if (statement != null && clazz.statement.isInterface()) {
if (!statement.isInterfacable()) {
compiler.getEnvironment().error(entity.getTrace(), Messages.ERR_INTERFACE_FUNCTION_CANNOT_CONTAIN_BODY.fetch(entity.getSignatureString(false)));
}
if (statement.isAbstract() || statement.isFinal()) {
compiler.getEnvironment().error(entity.getTrace(), Messages.ERR_ACCESS_TYPE_FOR_INTERFACE_METHOD.fetch(entity.getSignatureString(false)));
}
} else {
writeHeader();
if (statement.isGenerator()) {
entity.setEmpty(false);
entity.setImmutable(false);
GeneratorStmtCompiler generatorStmtCompiler = new GeneratorStmtCompiler(compiler, statement);
entity.setGeneratorEntity(generatorStmtCompiler.compile());
ExpressionStmtCompiler expr = new ExpressionStmtCompiler(this, null);
expr.makeUnknown(new TypeInsnNode(NEW, entity.getGeneratorEntity().getInternalName()));
expr.stackPush(Memory.Type.REFERENCE);
expr.writePushDup();
// env
expr.writePushEnv();
// classEntity
expr.writePushDup();
expr.writePushConstString(compiler.getModule().getInternalName());
expr.writePushConstInt((int) entity.getGeneratorEntity().getId());
expr.writeSysDynamicCall(Environment.class, "__getGenerator", ClassEntity.class, String.class, Integer.TYPE);
// self
expr.writePushThis();
// uses
expr.writeVarLoad("~args");
expr.writeSysCall(entity.getGeneratorEntity().getInternalName(), INVOKESPECIAL, Constants.INIT_METHOD, void.class, Environment.class, ClassEntity.class, Memory.class, Memory[].class);
expr.writeSysStaticCall(ObjectMemory.class, "valueOf", Memory.class, IObject.class);
expr.makeUnknown(new InsnNode(Opcodes.ARETURN));
expr.stackPop();
} else {
ExpressionStmtCompiler expr = new ExpressionStmtCompiler(this, null);
entity.setEmpty(true);
if (statement != null && statement.getBody() != null) {
expr.writeDefineVariables(statement.getLocal());
expr.write(statement.getBody());
if (!statement.getBody().getInstructions().isEmpty()) {
entity.setEmpty(false);
}
}
if (generatorEntity != null) {
expr.writeVarLoad("~this");
expr.writePushConstBoolean(false);
expr.writeSysDynamicCall(null, "_setValid", void.class, Boolean.TYPE);
}
ReturnStmtToken token = new ReturnStmtToken(new TokenMeta("", 0, 0, 0, 0));
token.setValue(null);
expr.getCompiler(ReturnStmtToken.class).write(token);
}
writeFooter();
}
return entity;
}
use of php.runtime.reflection.ParameterEntity in project jphp by jphp-compiler.
the class MethodDumperTest method testWithArgs.
@Test
public void testWithArgs() throws IOException {
ByteArrayOutputStream output = new ByteArrayOutputStream();
MethodEntity entity = new MethodEntity(context);
entity.setName("foobar");
entity.setModifier(Modifier.PUBLIC);
entity.setParameters(new ParameterEntity[] { new ParameterEntity(context) {
{
setName("param1");
setType(HintType.ARRAY);
}
}, new ParameterEntity(context) {
{
setName("param2");
}
} });
dumper.save(entity, output);
MethodEntity copyEntity = dumper.load(new ByteArrayInputStream(output.toByteArray()));
Assert.assertEquals("foobar", copyEntity.getName());
Assert.assertEquals(2, copyEntity.getParameters().length);
Assert.assertEquals("param1", copyEntity.getParameters()[0].getName());
Assert.assertEquals(HintType.ARRAY, copyEntity.getParameters()[0].getType());
Assert.assertEquals("param2", copyEntity.getParameters()[1].getName());
}
use of php.runtime.reflection.ParameterEntity in project jphp by jphp-compiler.
the class ParameterDumperTest method testBasic.
@Test
public void testBasic() throws IOException {
ByteArrayOutputStream output = new ByteArrayOutputStream();
ParameterEntity entity = new ParameterEntity(context);
entity.setTypeClass("FooBar");
entity.setType(HintType.CALLABLE);
entity.setReference(true);
entity.setDefaultValue(Memory.CONST_INT_5);
entity.setName("Foobar");
dumper.save(entity, output);
ParameterEntity copyEntity = dumper.load(new ByteArrayInputStream(output.toByteArray()));
Assert.assertEquals("Foobar", copyEntity.getName());
Assert.assertEquals(Memory.CONST_INT_5, copyEntity.getDefaultValue());
Assert.assertTrue(copyEntity.isReference());
Assert.assertEquals(HintType.CALLABLE, copyEntity.getType());
Assert.assertNull(copyEntity.getTypeClass());
}
Aggregations