Search in sources :

Example 1 with ErrorException

use of php.runtime.exceptions.support.ErrorException in project jphp by jphp-compiler.

the class EvalFunctions method eval.

public static Memory eval(Environment env, TraceInfo trace, @Runtime.GetLocals ArrayMemory locals, String code) throws Throwable {
    Context context = new Context(code);
    try {
        Tokenizer tokenizer = new Tokenizer(context);
        SyntaxAnalyzer analyzer = syntaxAnalyzer.get();
        analyzer.reset(env, tokenizer);
        AbstractCompiler compiler = new JvmCompiler(env, context, analyzer);
        ModuleEntity module = compiler.compile();
        env.scope.loadModule(module);
        env.registerModule(module);
        return module.include(env, locals);
    } catch (ErrorException e) {
        if (e.getType() == ErrorType.E_PARSE) {
            if (env.isHandleErrors(ErrorType.E_PARSE))
                throw new ParseException(evalErrorMessage(e), trace);
        } else
            env.error(trace, e.getType(), evalErrorMessage(e));
    }
    return Memory.FALSE;
}
Also used : Context(php.runtime.env.Context) AbstractCompiler(php.runtime.common.AbstractCompiler) SyntaxAnalyzer(org.develnext.jphp.core.syntax.SyntaxAnalyzer) JvmCompiler(org.develnext.jphp.core.compiler.jvm.JvmCompiler) ModuleEntity(php.runtime.reflection.ModuleEntity) ParseException(php.runtime.exceptions.ParseException) ErrorException(php.runtime.exceptions.support.ErrorException) Tokenizer(org.develnext.jphp.core.tokenizer.Tokenizer)

Example 2 with ErrorException

use of php.runtime.exceptions.support.ErrorException in project jphp by jphp-compiler.

the class Environment method catchUncaught.

public boolean catchUncaught(Exception e, boolean retry) {
    if (e instanceof UncaughtException)
        return catchUncaught((UncaughtException) e);
    else if (e instanceof DieException) {
        System.exit(((DieException) e).getExitCode());
        return true;
    } else if (e instanceof ErrorException) {
        ErrorException er = (ErrorException) e;
        getErrorReportHandler().onFatal(er);
        JVMStackTracer tracer = scope.getStackTracer(e);
        int i = 0;
        for (JVMStackTracer.Item el : tracer) {
            if (!el.isInternal()) {
                echo("\n\t #" + (i++) + " " + el);
            }
        }
        echo("\n");
        for (JVMStackTracer.Item el : tracer) {
            if (!el.isSystem()) {
                echo("\n\t " + (el.isInternal() ? "" : "->") + " " + el);
            }
        }
        return true;
    } else if (e instanceof FinallyException) {
        // nop
        return true;
    } else if (e instanceof BaseBaseException) {
        BaseBaseException be = (BaseBaseException) e;
        if (exceptionHandler != null) {
            try {
                exceptionHandler.onException(this, be);
            } catch (BaseBaseException _e) {
                if (retry) {
                    throw new RuntimeException(_e);
                } else {
                    catchUncaught(_e, true);
                }
            } catch (Throwable throwable) {
                throw new RuntimeException(throwable);
            }
            return true;
        } else {
            try {
                ExceptionHandler.DEFAULT.onException(this, be);
                return true;
            } catch (Throwable throwable) {
                throw new RuntimeException(throwable);
            }
        }
    } else {
        throw new RuntimeException(e);
    }
}
Also used : JVMStackTracer(php.runtime.util.JVMStackTracer) ErrorException(php.runtime.exceptions.support.ErrorException) BaseBaseException(php.runtime.lang.exception.BaseBaseException)

Example 3 with ErrorException

use of php.runtime.exceptions.support.ErrorException 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 4 with ErrorException

use of php.runtime.exceptions.support.ErrorException in project jphp by jphp-compiler.

the class JvmCompilerCase method check.

public void check(String name, boolean withErrors, int errorFlags) {
    File file;
    ByteArrayOutputStream outputR = new ByteArrayOutputStream();
    Environment environment;
    if (isConcurrent()) {
        environment = new ConcurrentEnvironment(newScope(), outputR);
    } else {
        environment = new Environment(newScope(), outputR);
    }
    // environment.setErrorFlags(ErrorType.E_ALL.value);
    Test test = new Test(file = new File(Thread.currentThread().getContextClassLoader().getResource("resources/" + name).getFile()));
    Context context = new Context(test.getFile(), file);
    try {
        JvmCompiler compiler = new JvmCompiler(environment, context, getSyntax(context));
        environment.setErrorFlags(0);
        if (errorFlags != -1)
            environment.setErrorFlags(errorFlags);
        if (!isCompiled()) {
            environment.setErrorFlags(ErrorType.E_ALL.value);
        }
        ModuleEntity module = compiler.compile(false);
        if (isCompiled()) {
            ByteArrayOutputStream output = new ByteArrayOutputStream();
            ModuleDumper dumper = new ModuleDumper(context, environment, true);
            dumper.save(module, output);
            environment.setErrorFlags(ErrorType.E_ALL.value);
            module = dumper.load(new ByteArrayInputStream(output.toByteArray()));
        }
        environment.getScope().loadModule(module);
        environment.getScope().addUserModule(module);
        environment.registerModule(module);
        environment.getModuleManager().addModule(context.getFileName(), module);
        Memory memory = module.includeNoThrow(environment, environment.getGlobals());
    } catch (ErrorException e) {
        if (withErrors) {
            environment.getErrorReportHandler().onFatal(e);
        } else {
            throw new CustomErrorException(e.getType(), e.getMessage() + " line: " + (e.getTraceInfo().getStartLine() + test.getSectionLine("FILE") + 2) + ", pos: " + (e.getTraceInfo().getStartPosition() + 1), e.getTraceInfo());
        }
    } catch (UncaughtException | BaseBaseException e) {
        environment.catchUncaught(e);
    } catch (Throwable throwable) {
        throw new RuntimeException(throwable);
    }
    try {
        environment.doFinal();
    } catch (ErrorException e) {
        if (withErrors) {
            environment.getErrorReportHandler().onFatal(e);
            try {
                environment.getDefaultBuffer().flush();
            } catch (Throwable throwable) {
                throw new RuntimeException(throwable);
            }
        } else
            throw e;
    } catch (RuntimeException e) {
        throw e;
    } catch (Throwable throwable) {
        throw new RuntimeException(throwable);
    }
    lastOutput = outputR.toString();
    if (test.getExpect() != null)
        Assert.assertEquals(test.getTest() + " (" + name + ")", test.getExpect(), rtrim(lastOutput));
    if (test.getExpectF() != null) {
        Memory result = StringFunctions.sscanf(environment, TraceInfo.valueOf(file.getName(), 0, 0), rtrim(lastOutput), test.getExpectF());
        if (result.isNull())
            result = new ArrayMemory();
        PrintF printF = new PrintF(environment.getLocale(), test.getExpectF(), ((ArrayMemory) result).values());
        String out = printF.toString();
        Assert.assertEquals(out, rtrim(lastOutput));
    }
}
Also used : PrintF(php.runtime.util.PrintF) ArrayMemory(php.runtime.memory.ArrayMemory) Memory(php.runtime.Memory) ModuleEntity(php.runtime.reflection.ModuleEntity) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ArrayMemory(php.runtime.memory.ArrayMemory) Test(org.develnext.jphp.core.tester.Test) ByteArrayInputStream(java.io.ByteArrayInputStream) CustomErrorException(php.runtime.exceptions.CustomErrorException) ErrorException(php.runtime.exceptions.support.ErrorException) CustomErrorException(php.runtime.exceptions.CustomErrorException) File(java.io.File) ModuleDumper(php.runtime.loader.dump.ModuleDumper) BaseBaseException(php.runtime.lang.exception.BaseBaseException) UncaughtException(php.runtime.lang.UncaughtException)

Aggregations

ErrorException (php.runtime.exceptions.support.ErrorException)4 Memory (php.runtime.Memory)2 BaseBaseException (php.runtime.lang.exception.BaseBaseException)2 ArrayMemory (php.runtime.memory.ArrayMemory)2 ModuleEntity (php.runtime.reflection.ModuleEntity)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 File (java.io.File)1 JvmCompiler (org.develnext.jphp.core.compiler.jvm.JvmCompiler)1 SyntaxAnalyzer (org.develnext.jphp.core.syntax.SyntaxAnalyzer)1 Test (org.develnext.jphp.core.tester.Test)1 Tokenizer (org.develnext.jphp.core.tokenizer.Tokenizer)1 AbstractCompiler (php.runtime.common.AbstractCompiler)1 Context (php.runtime.env.Context)1 DieException (php.runtime.env.DieException)1 TraceInfo (php.runtime.env.TraceInfo)1 CustomErrorException (php.runtime.exceptions.CustomErrorException)1 ParseException (php.runtime.exceptions.ParseException)1 BaseException (php.runtime.lang.BaseException)1 UncaughtException (php.runtime.lang.UncaughtException)1