Search in sources :

Example 6 with Memory

use of php.runtime.Memory in project jphp by jphp-compiler.

the class ArraysTest method testArrayPushRefAssign.

@Test
public void testArrayPushRefAssign() {
    Memory memory = includeResource("arrays/array_push_ref_assign.php");
    Assert.assertEquals("success", memory.toString());
}
Also used : Memory(php.runtime.Memory) ArrayMemory(php.runtime.memory.ArrayMemory) Test(org.junit.Test)

Example 7 with Memory

use of php.runtime.Memory 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();
    }
}
Also used : ClassEntity(php.runtime.reflection.ClassEntity) ClosureEntity(php.runtime.reflection.helper.ClosureEntity) MethodInsnNode(org.objectweb.asm.tree.MethodInsnNode) Memory(php.runtime.Memory) ObjectMemory(php.runtime.memory.ObjectMemory) StringMemory(php.runtime.memory.StringMemory) Environment(php.runtime.env.Environment) TypeInsnNode(org.objectweb.asm.tree.TypeInsnNode)

Example 8 with Memory

use of php.runtime.Memory in project jphp by jphp-compiler.

the class GlobalDefinitionCompiler method write.

@Override
public void write(GlobalStmtToken token) {
    for (ValueExprToken variable : token.getVariables()) {
        if (variable instanceof VariableExprToken) {
            LocalVariable local = method.getLocalVariable(((VariableExprToken) variable).getName());
            assert local != null;
            expr.writePushEnv();
            expr.writePushConstString(local.name);
            expr.writeSysDynamicCall(Environment.class, "getOrCreateGlobal", Memory.class, String.class);
            expr.writeVarStore(local, false, false);
        } else if (variable instanceof GetVarExprToken) {
            BaseExprCompiler<GetVarExprToken> compiler = (BaseExprCompiler<GetVarExprToken>) expr.getCompiler(GetVarExprToken.class);
            if (compiler == null)
                throw new CriticalException("Cannot find a valid compiler for " + GetVarExprToken.class.getName());
            compiler.write((GetVarExprToken) variable, true);
            expr.writePushEnv();
            Memory name = expr.writeExpression(((GetVarExprToken) variable).getName(), true, true, true);
            if (name != null) {
                expr.writePushConstString(name.toString());
            } else {
                expr.writePopString();
            }
            expr.writeSysDynamicCall(Environment.class, "getOrCreateGlobal", Memory.class, String.class);
            expr.writeSysDynamicCall(Memory.class, "assign", Memory.class, Memory.class);
            expr.writePopAll(1);
        }
    }
}
Also used : Memory(php.runtime.Memory) LocalVariable(org.develnext.jphp.core.compiler.jvm.misc.LocalVariable) Environment(php.runtime.env.Environment) VariableExprToken(org.develnext.jphp.core.tokenizer.token.expr.value.VariableExprToken) GetVarExprToken(org.develnext.jphp.core.tokenizer.token.expr.value.GetVarExprToken) CriticalException(php.runtime.exceptions.CriticalException) ValueExprToken(org.develnext.jphp.core.tokenizer.token.expr.ValueExprToken)

Example 9 with Memory

use of php.runtime.Memory in project jphp by jphp-compiler.

the class ReturnCompiler method write.

@Override
public void write(ReturnStmtToken token) {
    if (token.getValue() != null && method.getGeneratorEntity() != null) {
        env.error(token.toTraceInfo(compiler.getContext()), ErrorType.E_ERROR, "Generators cannot return values using \"return\"");
    }
    Memory result = Memory.NULL;
    boolean isImmutable = method.getEntity().isImmutable();
    if (token.getValue() != null)
        result = expr.writeExpression(token.getValue(), true, true);
    if (result != null) {
        if (isImmutable) {
            if (method.getEntity().getResult() == null)
                method.getEntity().setResult(result);
        }
        expr.writePushMemory(result);
    } else {
        method.getEntity().setImmutable(false);
    }
    if (expr.stackEmpty(false))
        expr.writePushNull();
    else
        expr.writePopBoxing(false);
    if (method.getEntity().isReturnReference()) {
        expr.writePushDup();
        expr.writePushEnv();
        expr.writePushTraceInfo(token);
        expr.writeSysStaticCall(InvokeHelper.class, "checkReturnReference", void.class, Memory.class, Environment.class, TraceInfo.class);
    } else
        expr.writePopImmutable();
    if (!method.getTryStack().empty()) {
        LocalVariable variable = method.getLocalVariable("~result~");
        if (variable == null)
            variable = method.addLocalVariable("~result~", null, Memory.class);
        expr.writeVarStore(variable, false, false);
        add(new JumpInsnNode(GOTO, method.getTryStack().peek().getReturnLabel()));
    } else {
        add(new InsnNode(ARETURN));
        //removeStackFrame();
        expr.stackPop();
    }
}
Also used : JumpInsnNode(org.objectweb.asm.tree.JumpInsnNode) InsnNode(org.objectweb.asm.tree.InsnNode) Memory(php.runtime.Memory) LocalVariable(org.develnext.jphp.core.compiler.jvm.misc.LocalVariable) JumpInsnNode(org.objectweb.asm.tree.JumpInsnNode)

Example 10 with Memory

use of php.runtime.Memory in project jphp by jphp-compiler.

the class CastTest method testObject.

@Test
public void testObject() {
    Memory memory = includeResource("cast/object.php");
    Assert.assertEquals("success", memory.toString());
}
Also used : Memory(php.runtime.Memory) Test(org.junit.Test)

Aggregations

Memory (php.runtime.Memory)346 Test (org.junit.Test)124 ArrayMemory (php.runtime.memory.ArrayMemory)122 ObjectMemory (php.runtime.memory.ObjectMemory)75 LongMemory (php.runtime.memory.LongMemory)58 StringMemory (php.runtime.memory.StringMemory)58 ForeachIterator (php.runtime.lang.ForeachIterator)47 ReferenceMemory (php.runtime.memory.ReferenceMemory)40 KeyValueMemory (php.runtime.memory.KeyValueMemory)25 Invoker (php.runtime.invoke.Invoker)21 ArrayKeyMemory (php.runtime.memory.helper.ArrayKeyMemory)21 ArrayValueMemory (php.runtime.memory.helper.ArrayValueMemory)21 ShortcutMemory (php.runtime.memory.helper.ShortcutMemory)21 Environment (php.runtime.env.Environment)20 IObject (php.runtime.lang.IObject)19 UndefinedMemory (php.runtime.memory.helper.UndefinedMemory)16 ClassEntity (php.runtime.reflection.ClassEntity)16 TraceInfo (php.runtime.env.TraceInfo)12 File (java.io.File)7 LocalVariable (org.develnext.jphp.core.compiler.jvm.misc.LocalVariable)7