Search in sources :

Example 6 with TraceInfo

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

the class SimpleExprGenerator method processString.

protected Token processString(StringExprToken string) {
    if (string.getSegments().isEmpty()) {
        if (string.getQuote() == StringExprToken.Quote.SHELL) {
            return new ShellExecExprToken(string.getMeta(), Arrays.<Token>asList(string));
        }
        return string;
    }
    List<Token> tokens = new ArrayList<Token>();
    int i = 0;
    String value = string.getValue();
    TokenMeta meta = string.getMeta();
    for (StringExprToken.Segment segment : string.getSegments()) {
        String prev = value.substring(i, segment.from);
        if (!prev.isEmpty()) {
            StringExprToken item = new StringExprToken(new TokenMeta(prev, meta.getStartLine() + i, meta.getEndLine(), meta.getStartLine(), meta.getEndLine()), StringExprToken.Quote.SINGLE);
            tokens.add(item);
        }
        String dynamic = value.substring(segment.from, segment.to);
        if (!segment.isVariable)
            dynamic = dynamic.substring(1, dynamic.length() - 1);
        Tokenizer tokenizer = new Tokenizer(dynamic + ";", analyzer.getContext());
        try {
            SyntaxAnalyzer syntaxAnalyzer = new SyntaxAnalyzer(analyzer.getEnvironment(), tokenizer, analyzer.getFunction());
            List<Token> tree = syntaxAnalyzer.getTree();
            analyzer.getScope().addVariables(syntaxAnalyzer.getScope().getVariables());
            assert tree.size() > 0;
            Token item = tree.get(0);
            if (!(item instanceof ExprStmtToken))
                unexpectedToken(item);
            ExprStmtToken expr = (ExprStmtToken) item;
            if (expr.isSingle()) {
                tokens.add(expr.getSingle());
            } else
                tokens.add(expr);
        } catch (ParseException e) {
            TraceInfo oldTrace = e.getTraceInfo();
            e.setTraceInfo(new TraceInfo(analyzer.getContext(), meta.getStartLine() + oldTrace.getStartLine(), meta.getEndLine() + oldTrace.getEndLine(), meta.getStartLine() + oldTrace.getStartLine(), meta.getEndLine() + oldTrace.getEndLine()));
            throw e;
        }
        i = segment.to;
    }
    String prev = value.substring(i);
    if (!prev.isEmpty()) {
        StringExprToken item = new StringExprToken(new TokenMeta(prev, meta.getStartLine() + i, meta.getEndLine(), meta.getStartLine(), meta.getEndLine()), StringExprToken.Quote.SINGLE);
        tokens.add(item);
    }
    if (string.getQuote() == StringExprToken.Quote.SHELL) {
        return new ShellExecExprToken(meta, tokens);
    }
    StringBuilderExprToken result = new StringBuilderExprToken(meta, tokens);
    result.setBinary(string.isBinary());
    return result;
}
Also used : TokenMeta(org.develnext.jphp.core.tokenizer.TokenMeta) MacroToken(org.develnext.jphp.core.tokenizer.token.expr.value.macro.MacroToken) SemicolonToken(org.develnext.jphp.core.tokenizer.token.SemicolonToken) Token(org.develnext.jphp.core.tokenizer.token.Token) CastExprToken(org.develnext.jphp.core.tokenizer.token.expr.operator.cast.CastExprToken) BreakToken(org.develnext.jphp.core.tokenizer.token.BreakToken) ColonToken(org.develnext.jphp.core.tokenizer.token.ColonToken) UnsetCastExprToken(org.develnext.jphp.core.tokenizer.token.expr.operator.cast.UnsetCastExprToken) TraceInfo(php.runtime.env.TraceInfo) SyntaxAnalyzer(org.develnext.jphp.core.syntax.SyntaxAnalyzer) ParseException(php.runtime.exceptions.ParseException) Tokenizer(org.develnext.jphp.core.tokenizer.Tokenizer)

Example 7 with TraceInfo

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

the class ParameterEntity method validateTypeHinting.

public static void validateTypeHinting(Environment env, int index, Memory[] args, HintType type, boolean nullable) {
    Memory value = args[index - 1];
    if (!checkTypeHinting(env, value, type, nullable)) {
        String given;
        if (value == null) {
            given = "none";
        } else if (value.isObject()) {
            given = "instance of " + value.toValue(ObjectMemory.class).getReflection().getName();
        } else {
            given = value.getRealType().toString();
        }
        StackTraceElement[] stack = Thread.currentThread().getStackTrace();
        StackTraceElement e = stack[2];
        StackTraceElement where = stack[3];
        TraceInfo trace;
        if (where.getLineNumber() <= 0)
            trace = env.trace();
        else
            trace = new TraceInfo(where);
        JVMStackTracer.Item item = new JVMStackTracer.Item(env.scope.getClassLoader(), e);
        env.error(trace, ErrorType.E_RECOVERABLE_ERROR, "Argument %s passed to %s() must be of the type %s, %s given", index, item.getSignature(), type.toString(), given);
    }
}
Also used : JVMStackTracer(php.runtime.util.JVMStackTracer) ObjectMemory(php.runtime.memory.ObjectMemory) Memory(php.runtime.Memory) ObjectMemory(php.runtime.memory.ObjectMemory) TraceInfo(php.runtime.env.TraceInfo)

Example 8 with TraceInfo

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

the class FileObject method findFiles.

@Signature(@Arg(value = "filter", optional = @Optional("NULL")))
public Memory findFiles(final Environment env, Memory... args) {
    File[] result;
    if (args[0].isNull()) {
        result = file.listFiles();
    } 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);
        result = file.listFiles(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();
            }
        });
    }
    ArrayMemory arr = new ArrayMemory();
    if (result != null) {
        for (File e : result) {
            arr.add(new ObjectMemory(new FileObject(env, __class__, e)));
        }
    }
    return arr.toConstant();
}
Also used : ArrayMemory(php.runtime.memory.ArrayMemory) Invoker(php.runtime.invoke.Invoker) ObjectMemory(php.runtime.memory.ObjectMemory) Memory(php.runtime.Memory) ArrayMemory(php.runtime.memory.ArrayMemory) LongMemory(php.runtime.memory.LongMemory) ObjectMemory(php.runtime.memory.ObjectMemory) StringMemory(php.runtime.memory.StringMemory) StringMemory(php.runtime.memory.StringMemory) TraceInfo(php.runtime.env.TraceInfo)

Example 9 with TraceInfo

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

the class ErrorHandler method onError.

public boolean onError(Environment env, SystemMessage message) {
    if (ErrorType.check(errorHandlerFlags, message.getType())) {
        TraceInfo trace = message.getTrace().trace;
        int argCount = invoker.getArgumentCount();
        if (argCount < 4)
            argCount = 4;
        else if (argCount > 5)
            argCount = 5;
        Memory[] args = new Memory[argCount];
        args[0] = LongMemory.valueOf(message.getType().value);
        args[1] = new StringMemory(message.getMessage());
        args[2] = new StringMemory(trace.getFileName());
        args[3] = LongMemory.valueOf(trace.getStartLine() + 1);
        if (argCount > 4)
            args[4] = new ArrayMemory(false, message.getTrace().args);
        try {
            invoker.setTrace(null);
            return (invoker.call(args).toValue() != Memory.FALSE);
        } catch (ErrorException e) {
            throw e;
        } catch (BaseException e) {
            throw e;
        } catch (DieException e) {
            throw e;
        } catch (Throwable throwable) {
            throw new RuntimeException(throwable);
        }
    }
    return false;
}
Also used : ArrayMemory(php.runtime.memory.ArrayMemory) BaseException(php.runtime.lang.BaseException) Memory(php.runtime.Memory) ArrayMemory(php.runtime.memory.ArrayMemory) LongMemory(php.runtime.memory.LongMemory) StringMemory(php.runtime.memory.StringMemory) DieException(php.runtime.env.DieException) StringMemory(php.runtime.memory.StringMemory) TraceInfo(php.runtime.env.TraceInfo) ErrorException(php.runtime.exceptions.support.ErrorException)

Example 10 with TraceInfo

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

the class ErrorException method setTraceInfo.

@Override
public void setTraceInfo(Environment env, TraceInfo trace) {
    super.setTraceInfo(env, trace);
    Memory line = clazz.refOfProperty(props, "line");
    Memory file = clazz.refOfProperty(props, "file");
    if (!line.isNull() || !file.isNull()) {
        this.trace = new TraceInfo(file.isNull() ? trace.getFileName() : file.toString(), line.isNull() ? trace.getStartLine() : line.toInteger() - 1, trace.getStartPosition());
    }
}
Also used : Memory(php.runtime.Memory) 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