Search in sources :

Example 6 with ModuleEntity

use of php.runtime.reflection.ModuleEntity in project jphp by jphp-compiler.

the class ModuleManager method fetchCachedModule.

public ModuleEntity fetchCachedModule(String path, boolean compiled) throws Throwable {
    ModuleEntity moduleEntity = modules.get(path);
    if (moduleEntity != null) {
        return moduleEntity;
    } else {
        moduleEntity = fetchModule(path, compiled);
        if (moduleEntity == null) {
            return null;
        }
        modules.put(path, moduleEntity);
        return moduleEntity;
    }
}
Also used : ModuleEntity(php.runtime.reflection.ModuleEntity)

Example 7 with ModuleEntity

use of php.runtime.reflection.ModuleEntity in project jphp by jphp-compiler.

the class Launcher method run.

public void run(boolean mustBootstrap, boolean disableExtensions) throws Throwable {
    readConfig();
    if (!disableExtensions) {
        initExtensions();
    }
    if (isDebug()) {
        if (compileScope.getTickHandler() == null) {
            throw new LaunchException("Cannot find a debugger, please add the jphp-debugger dependency");
        }
    }
    if (Startup.isShowInitDelay()) {
        long t = System.currentTimeMillis() - startTime;
        Startup.trace("Startup time = " + t + "ms");
    }
    String file = config.getProperty("bootstrap.file", "JPHP-INF/.bootstrap.php");
    String classLoader = config.getProperty("env.classLoader", ReflectionUtils.getClassName(WrapClassLoader.WrapLauncherClassLoader.class));
    if (classLoader != null && !(classLoader.isEmpty())) {
        ClassEntity classLoaderEntity = environment.fetchClass(classLoader);
        if (classLoaderEntity == null) {
            throw new LaunchException("Class loader class is not found: " + classLoader);
        }
        WrapClassLoader loader = classLoaderEntity.newObject(environment, TraceInfo.UNKNOWN, true);
        environment.invokeMethod(loader, "register", Memory.TRUE);
    }
    if (file != null && !file.isEmpty()) {
        try {
            ModuleEntity bootstrap = loadFrom(file);
            if (bootstrap == null) {
                throw new IOException();
            }
            beforeIncludeBootstrap();
            if (new StringMemory(config.getProperty("bootstrap.showBytecode", "")).toBoolean()) {
                ModuleOpcodePrinter moduleOpcodePrinter = new ModuleOpcodePrinter(bootstrap);
                System.out.println(moduleOpcodePrinter.toString());
            }
            initModule(bootstrap);
            ArrayMemory argv = ArrayMemory.ofStrings(this.args);
            argv.unshift(Memory.NULL);
            environment.getGlobals().put("argv", argv);
            environment.getGlobals().put("argc", LongMemory.valueOf(argv.size()));
            environment.pushCall(new CallStackItem(new TraceInfo(bootstrap.getName(), -1, -1)));
            try {
                bootstrap.includeNoThrow(environment);
            } finally {
                afterIncludeBootstrap();
                environment.popCall();
                compileScope.triggerProgramShutdown(environment);
                if (StringMemory.valueOf(config.getProperty("env.doFinal", "1")).toBoolean()) {
                    environment.doFinal();
                }
            }
        } catch (IOException e) {
            throw new LaunchException("Cannot find '" + file + "' resource for `bootstrap.file` option");
        }
    } else if (mustBootstrap)
        throw new LaunchException("Please set value of the `bootstrap.file` option in the launcher.conf file");
}
Also used : ClassEntity(php.runtime.reflection.ClassEntity) WrapClassLoader(php.runtime.ext.core.classes.WrapClassLoader) ModuleEntity(php.runtime.reflection.ModuleEntity) ModuleOpcodePrinter(org.develnext.jphp.core.opcode.ModuleOpcodePrinter) ArrayMemory(php.runtime.memory.ArrayMemory) StringMemory(php.runtime.memory.StringMemory)

Example 8 with ModuleEntity

use of php.runtime.reflection.ModuleEntity 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);
        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)

Example 9 with ModuleEntity

use of php.runtime.reflection.ModuleEntity in project jphp by jphp-compiler.

the class JvmCompilerCase method includeResource.

@SuppressWarnings("unchecked")
protected Memory includeResource(String name, ArrayMemory globals) {
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    Environment environment;
    if (isConcurrent()) {
        environment = new ConcurrentEnvironment(newScope(), output);
    } else {
        environment = new Environment(newScope(), output);
    }
    File file = new File(Thread.currentThread().getContextClassLoader().getResource("resources/" + name).getFile());
    Context context = new Context(file);
    JvmCompiler compiler = new JvmCompiler(environment, context, getSyntax(context));
    ModuleEntity module = compiler.compile();
    environment.getScope().loadModule(module);
    try {
        environment.registerModule(module);
    } catch (Throwable throwable) {
        throw new RuntimeException(throwable);
    }
    if (globals != null)
        environment.getGlobals().putAll(globals);
    Memory memory = module.includeNoThrow(environment, environment.getGlobals());
    try {
        environment.doFinal();
    } catch (Throwable throwable) {
        throw new RuntimeException(throwable);
    }
    lastOutput = output.toString();
    return memory;
}
Also used : ArrayMemory(php.runtime.memory.ArrayMemory) Memory(php.runtime.Memory) ModuleEntity(php.runtime.reflection.ModuleEntity) ByteArrayOutputStream(java.io.ByteArrayOutputStream) File(java.io.File)

Example 10 with ModuleEntity

use of php.runtime.reflection.ModuleEntity in project jphp by jphp-compiler.

the class JPHPScriptEngine method compile.

@Override
public CompiledScript compile(Reader reader) throws ScriptException {
    try {
        InputStream is = new ReaderInputStream(reader);
        Context context = new Context(is);
        ModuleEntity module = environment.importModule(context);
        return new JPHPCompiledScript(module, environment);
    } catch (IOException e) {
        throw new ScriptException(e);
    } catch (Throwable e) {
        throw new ScriptException(new Exception(e));
    }
}
Also used : Context(php.runtime.env.Context) ReaderInputStream(org.develnext.jphp.scripting.util.ReaderInputStream) ReaderInputStream(org.develnext.jphp.scripting.util.ReaderInputStream) ModuleEntity(php.runtime.reflection.ModuleEntity)

Aggregations

ModuleEntity (php.runtime.reflection.ModuleEntity)12 File (java.io.File)3 Context (php.runtime.env.Context)3 ArrayMemory (php.runtime.memory.ArrayMemory)3 ClassEntity (php.runtime.reflection.ClassEntity)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 Memory (php.runtime.Memory)2 ErrorException (php.runtime.exceptions.support.ErrorException)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 JvmCompiler (org.develnext.jphp.core.compiler.jvm.JvmCompiler)1 ModuleOpcodePrinter (org.develnext.jphp.core.opcode.ModuleOpcodePrinter)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 ReaderInputStream (org.develnext.jphp.scripting.util.ReaderInputStream)1 AbstractCompiler (php.runtime.common.AbstractCompiler)1 Environment (php.runtime.env.Environment)1 CriticalException (php.runtime.exceptions.CriticalException)1 CustomErrorException (php.runtime.exceptions.CustomErrorException)1