Search in sources :

Example 1 with TraceInfo

use of php.runtime.env.TraceInfo in project jphp by jphp-compiler.

the class TokenizerTest method testParseError.

@Test
public void testParseError() throws IOException {
    Throwable ex = null;
    Tokenizer tokenizer = new Tokenizer(new Context("  'foobar \n "));
    try {
        tokenizer.nextToken();
    } catch (Throwable e) {
        ex = e;
    }
    assertTrue(ex instanceof ParseException);
    TraceInfo traceInfo = ((ParseException) ex).getTraceInfo();
    assertNotNull(traceInfo);
    assertNull(traceInfo.getContext().getFileName());
    assertEquals(1, traceInfo.getStartLine());
    assertEquals(1, traceInfo.getEndLine());
    assertEquals(1, traceInfo.getStartPosition());
    assertEquals(1, traceInfo.getEndPosition());
}
Also used : Context(php.runtime.env.Context) ParseException(php.runtime.exceptions.ParseException) TraceInfo(php.runtime.env.TraceInfo) Tokenizer(org.develnext.jphp.core.tokenizer.Tokenizer) Test(org.junit.Test)

Example 2 with TraceInfo

use of php.runtime.env.TraceInfo in project jphp by jphp-compiler.

the class StackGetCommand method run.

@Override
public void run(Debugger context, CommandArguments args, Document result) {
    DebugTick tick = context.getRegisteredTick();
    Element response = createResponse(args, result);
    CallStack callStack = tick.getCallStack();
    List<CallStackItem> list = new ArrayList<>();
    Collections.addAll(list, callStack.getSnapshot());
    CallStackItem last = list.get(list.size() - 1);
    if (list.size() > 1) {
        CallStackItem prevLast = list.get(list.size() - 2);
        TraceInfo trace = prevLast.getTrace();
        prevLast.setTrace(new TraceInfo(trace.getContext(), last.getTrace().getStartLine(), trace.getEndLine(), last.getTrace().getStartPosition(), trace.getEndLine()));
    }
    last.setTrace(tick.getTrace());
    Collections.reverse(list);
    int depth = args.containsKey("d") ? Integer.parseInt(args.get("d")) : -1;
    if (depth > -1) {
        list = list.subList(0, depth + 1);
    }
    int i = 0;
    for (CallStackItem stackItem : list) {
        Element stack = result.createElement("stack");
        stack.setAttribute("level", String.valueOf(i));
        stack.setAttribute("type", "file");
        stack.setAttribute("filename", context.getFileName(stackItem.trace.getFileName()));
        stack.setAttribute("lineno", String.valueOf(stackItem.trace.getStartLine() + 1));
        stack.setAttribute("where", stackItem.getWhere());
        response.appendChild(stack);
        i++;
    }
}
Also used : DebugTick(org.develnext.jphp.debug.impl.DebugTick) CallStackItem(php.runtime.env.CallStackItem) CallStack(php.runtime.env.CallStack) Element(org.w3c.dom.Element) ArrayList(java.util.ArrayList) TraceInfo(php.runtime.env.TraceInfo)

Example 3 with TraceInfo

use of php.runtime.env.TraceInfo in project jphp by jphp-compiler.

the class FileObject method find.

@Signature(@Arg(value = "filter", optional = @Optional("NULL")))
public Memory find(final Environment env, Memory... args) {
    if (args[0].isNull()) {
        return ArrayMemory.ofStrings(file.list()).toConstant();
    } else {
        final Invoker invoker = Invoker.valueOf(env, null, args[0]);
        if (invoker == null) {
            exception(env, "Invalid filter value, must be callable");
            return Memory.NULL;
        }
        final TraceInfo trace = env.trace();
        invoker.setTrace(trace);
        String[] result = file.list(new FilenameFilter() {

            @Override
            public boolean accept(File dir, String name) {
                FileObject o = new FileObject(env, __class__, dir);
                Memory[] args = new Memory[] { new ObjectMemory(o), new StringMemory(name) };
                return invoker.callNoThrow(args).toBoolean();
            }
        });
        return ArrayMemory.ofStrings(result);
    }
}
Also used : Invoker(php.runtime.invoke.Invoker) ObjectMemory(php.runtime.memory.ObjectMemory) StringMemory(php.runtime.memory.StringMemory) TraceInfo(php.runtime.env.TraceInfo)

Example 4 with TraceInfo

use of php.runtime.env.TraceInfo in project jphp by jphp-compiler.

the class MemoryOperation method get.

@SuppressWarnings("unchecked")
public static MemoryOperation get(final Class<?> type, Type genericTypes, boolean includeParents) {
    MemoryOperation operation = null;
    if (genericTypes instanceof ParameterizedType) {
        operation = genericOperations.get(new ParametrizedClass(type, ((ParameterizedType) genericTypes).getActualTypeArguments()));
    }
    if (operation == null) {
        operation = operations.get(type);
        if (operation == null) {
            if (type.isArray()) {
                MemoryOperation arrayMemoryOperation = new ArrayMemoryOperation(type);
                register(arrayMemoryOperation);
                return arrayMemoryOperation;
            }
            if (Enum.class.isAssignableFrom(type)) {
                return new MemoryOperation() {

                    @Override
                    public Class<?>[] getOperationClasses() {
                        return new Class<?>[] { Enum.class };
                    }

                    @Override
                    @SuppressWarnings("unchecked")
                    public Object convert(Environment env, TraceInfo trace, Memory arg) throws Throwable {
                        return arg.isNull() ? null : Enum.valueOf((Class<? extends Enum>) type, arg.toString());
                    }

                    @Override
                    public Memory unconvert(Environment env, TraceInfo trace, Object arg) throws Throwable {
                        return arg == null ? Memory.NULL : StringMemory.valueOf(((Enum) arg).name());
                    }

                    @Override
                    public void applyTypeHinting(ParameterEntity parameter) {
                        parameter.setTypeEnum((Class<? extends Enum>) type);
                    }
                };
            }
            final Class<? extends BaseWrapper> wrapperClass = wrappers.get(type);
            if (wrapperClass != null) {
                Constructor<BaseWrapper> constructor;
                try {
                    constructor = (Constructor<BaseWrapper>) wrapperClass.getConstructor(Environment.class, type);
                } catch (NoSuchMethodException e) {
                    try {
                        constructor = (Constructor<BaseWrapper>) wrapperClass.getConstructor(Environment.class, Object.class);
                    } catch (NoSuchMethodException e1) {
                        throw new CriticalException(e);
                    }
                }
                final Constructor<BaseWrapper> finalConstructor = constructor;
                return new MemoryOperation() {

                    @Override
                    public Class<?>[] getOperationClasses() {
                        return new Class<?>[0];
                    }

                    @Override
                    public Object convert(Environment env, TraceInfo trace, Memory arg) throws Throwable {
                        if (arg.isNull()) {
                            return null;
                        }
                        return arg.toObject(BaseWrapper.class).getWrappedObject();
                    }

                    @Override
                    public Memory unconvert(Environment env, TraceInfo trace, Object arg) throws Throwable {
                        if (arg == null) {
                            return Memory.NULL;
                        }
                        Constructor<BaseWrapper> constructorContext = finalConstructor;
                        Class<? extends BaseWrapper> wrapperClassContext = wrapperClass;
                        if (arg.getClass() != type) {
                            wrapperClassContext = wrappers.get(arg.getClass());
                        }
                        if (wrapperClassContext != null && wrapperClassContext != wrapperClass) {
                            constructorContext = (Constructor<BaseWrapper>) wrapperClassContext.getConstructor(Environment.class, arg.getClass());
                        }
                        try {
                            BaseWrapper instance = constructorContext.newInstance(env, arg);
                            return ObjectMemory.valueOf(instance.__getOriginInstance());
                        } catch (InstantiationException | IllegalAccessException | InvocationTargetException e) {
                            throw new CriticalException(e);
                        }
                    }

                    @Override
                    public void applyTypeHinting(ParameterEntity parameter) {
                        parameter.setTypeNativeClass(type);
                    }
                };
            } else if (IObject.class.isAssignableFrom(type)) {
                return new MemoryOperation() {

                    @Override
                    public Class<?>[] getOperationClasses() {
                        return new Class<?>[] { IObject.class };
                    }

                    @Override
                    @SuppressWarnings("unchecked")
                    public Object convert(Environment env, TraceInfo trace, Memory arg) throws Throwable {
                        if (arg.isNull()) {
                            return null;
                        }
                        return arg.toObject((Class<? extends IObject>) type);
                    }

                    @Override
                    public Memory unconvert(Environment env, TraceInfo trace, Object arg) throws Throwable {
                        if (arg == null) {
                            return Memory.NULL;
                        }
                        return ObjectMemory.valueOf((IObject) arg);
                    }

                    @Override
                    public void applyTypeHinting(ParameterEntity parameter) {
                        parameter.setType(ReflectionUtils.getClassName(type));
                    }
                };
            } else {
                Class<?> superType = type.getSuperclass();
                if (Object.class != superType && (includeParents || type.isAnonymousClass())) {
                    return get(superType, type.getGenericSuperclass(), includeParents);
                }
            }
        }
    }
    if (operation == null) {
        return null;
    }
    if (genericTypes instanceof ParameterizedType) {
        return operation.instance(((ParameterizedType) genericTypes).getActualTypeArguments());
    }
    return operation;
}
Also used : Memory(php.runtime.Memory) ObjectMemory(php.runtime.memory.ObjectMemory) StringMemory(php.runtime.memory.StringMemory) ParameterizedType(java.lang.reflect.ParameterizedType) BaseWrapper(php.runtime.lang.BaseWrapper) IObject(php.runtime.lang.IObject) Constructor(java.lang.reflect.Constructor) TraceInfo(php.runtime.env.TraceInfo) IterableMemoryOperation(php.runtime.memory.support.operation.iterator.IterableMemoryOperation) PropertiesMemoryOperation(php.runtime.memory.support.operation.map.PropertiesMemoryOperation) HashMapMemoryOperation(php.runtime.memory.support.operation.map.HashMapMemoryOperation) LinkedHashMapMemoryOperation(php.runtime.memory.support.operation.map.LinkedHashMapMemoryOperation) SetMemoryOperation(php.runtime.memory.support.operation.collection.SetMemoryOperation) MapMemoryOperation(php.runtime.memory.support.operation.map.MapMemoryOperation) HashSetMemoryOperation(php.runtime.memory.support.operation.collection.HashSetMemoryOperation) EnumerationMemoryOperation(php.runtime.memory.support.operation.collection.EnumerationMemoryOperation) ListMemoryOperation(php.runtime.memory.support.operation.collection.ListMemoryOperation) CriticalException(php.runtime.exceptions.CriticalException) InvocationTargetException(java.lang.reflect.InvocationTargetException) ParameterEntity(php.runtime.reflection.ParameterEntity) Environment(php.runtime.env.Environment) IObject(php.runtime.lang.IObject)

Example 5 with TraceInfo

use of php.runtime.env.TraceInfo in project jphp by jphp-compiler.

the class ClassStmtCompiler method writeInitStatic.

@SuppressWarnings("unchecked")
protected void writeInitStatic() {
    MethodNode node = new MethodNodeImpl();
    node.access = ACC_STATIC;
    node.name = Constants.STATIC_INIT_METHOD;
    node.desc = Type.getMethodDescriptor(Type.getType(void.class));
    MethodStmtCompiler methodCompiler = new MethodStmtCompiler(this, node);
    ExpressionStmtCompiler expressionCompiler = new ExpressionStmtCompiler(methodCompiler, null);
    methodCompiler.writeHeader();
    // trace list
    expressionCompiler.writePushSmallInt(traceList.size());
    node.instructions.add(new TypeInsnNode(ANEWARRAY, Type.getInternalName(TraceInfo.class)));
    expressionCompiler.stackPush(Memory.Type.REFERENCE);
    int i = 0;
    for (TraceInfo traceInfo : traceList) {
        expressionCompiler.writePushDup();
        expressionCompiler.writePushSmallInt(i);
        expressionCompiler.writePushCreateTraceInfo(traceInfo.getStartLine(), traceInfo.getStartPosition());
        node.instructions.add(new InsnNode(AASTORE));
        expressionCompiler.stackPop();
        expressionCompiler.stackPop();
        i++;
    }
    expressionCompiler.writePutStatic("$TRC", TraceInfo[].class);
    // memory constants
    expressionCompiler.writePushSmallInt(memoryConstants.size());
    node.instructions.add(new TypeInsnNode(ANEWARRAY, Type.getInternalName(Memory.class)));
    expressionCompiler.stackPush(Memory.Type.REFERENCE);
    i = 0;
    for (Memory memory : memoryConstants) {
        expressionCompiler.writePushDup();
        expressionCompiler.writePushSmallInt(i);
        expressionCompiler.writePushMemory(memory);
        expressionCompiler.writePopBoxing(true, false);
        node.instructions.add(new InsnNode(AASTORE));
        expressionCompiler.stackPop();
        expressionCompiler.stackPop();
        i++;
    }
    expressionCompiler.writePutStatic("$MEM", Memory[].class);
    // memory array constants
    expressionCompiler.writePushSmallInt(memoryArrayConstants.size());
    node.instructions.add(new TypeInsnNode(ANEWARRAY, Type.getInternalName(Memory[].class)));
    expressionCompiler.stackPush(Memory.Type.REFERENCE);
    i = 0;
    for (Collection<Memory> memories : memoryArrayConstants) {
        expressionCompiler.writePushDup();
        expressionCompiler.writePushSmallInt(i);
        expressionCompiler.writePushParameters(memories);
        node.instructions.add(new InsnNode(AASTORE));
        expressionCompiler.stackPop();
        expressionCompiler.stackPop();
        i++;
    }
    expressionCompiler.writePutStatic("$AMEM", Memory[][].class);
    // cached calls
    expressionCompiler.writePushNewObject(FunctionCallCache.class);
    expressionCompiler.writePutStatic("$CALL_FUNC_CACHE", FunctionCallCache.class);
    expressionCompiler.writePushNewObject(MethodCallCache.class);
    expressionCompiler.writePutStatic("$CALL_METH_CACHE", MethodCallCache.class);
    expressionCompiler.writePushNewObject(ConstantCallCache.class);
    expressionCompiler.writePutStatic("$CALL_CONST_CACHE", ConstantCallCache.class);
    expressionCompiler.writePushNewObject(PropertyCallCache.class);
    expressionCompiler.writePutStatic("$CALL_PROP_CACHE", PropertyCallCache.class);
    expressionCompiler.writePushNewObject(ClassCallCache.class);
    expressionCompiler.writePutStatic("$CALL_CLASS_CACHE", ClassCallCache.class);
    node.instructions.add(new InsnNode(RETURN));
    methodCompiler.writeFooter();
    this.node.methods.add(node);
}
Also used : Memory(php.runtime.Memory) MethodNodeImpl(org.develnext.jphp.core.compiler.jvm.node.MethodNodeImpl) TraceInfo(php.runtime.env.TraceInfo)

Aggregations

TraceInfo (php.runtime.env.TraceInfo)15 Memory (php.runtime.Memory)10 ObjectMemory (php.runtime.memory.ObjectMemory)4 StringMemory (php.runtime.memory.StringMemory)4 InvocationTargetException (java.lang.reflect.InvocationTargetException)3 Environment (php.runtime.env.Environment)3 ParseException (php.runtime.exceptions.ParseException)3 ArrayList (java.util.ArrayList)2 Tokenizer (org.develnext.jphp.core.tokenizer.Tokenizer)2 CompileFunction (php.runtime.ext.support.compile.CompileFunction)2 Invoker (php.runtime.invoke.Invoker)2 IObject (php.runtime.lang.IObject)2 ArrayMemory (php.runtime.memory.ArrayMemory)2 LongMemory (php.runtime.memory.LongMemory)2 Constructor (java.lang.reflect.Constructor)1 ParameterizedType (java.lang.reflect.ParameterizedType)1 MethodNodeImpl (org.develnext.jphp.core.compiler.jvm.node.MethodNodeImpl)1 SyntaxAnalyzer (org.develnext.jphp.core.syntax.SyntaxAnalyzer)1 TokenMeta (org.develnext.jphp.core.tokenizer.TokenMeta)1 BreakToken (org.develnext.jphp.core.tokenizer.token.BreakToken)1