Search in sources :

Example 1 with ModuleDumper

use of php.runtime.loader.dump.ModuleDumper in project jphp by jphp-compiler.

the class Environment method importCompiledModule.

public ModuleEntity importCompiledModule(Context context, boolean debugInformation) throws Throwable {
    String moduleName = context.getModuleName();
    ModuleEntity module = moduleName == null ? null : scope.findUserModule(moduleName);
    if (module == null) {
        ModuleDumper moduleDumper = new ModuleDumper(context, this, debugInformation);
        module = moduleDumper.load(context.getInputStream(getDefaultCharset()));
        synchronized (scope) {
            scope.loadModule(module);
        }
    }
    registerModule(module);
    scope.addUserModule(module);
    return module;
}
Also used : ModuleDumper(php.runtime.loader.dump.ModuleDumper)

Example 2 with ModuleDumper

use of php.runtime.loader.dump.ModuleDumper in project jphp by jphp-compiler.

the class WrapModule method __construct.

@Signature({ @Arg("source"), @Arg(value = "compiled", optional = @Optional("false")), @Arg(value = "debugInformation", optional = @Optional("true")) })
public Memory __construct(Environment env, Memory... args) throws Throwable {
    InputStream is = Stream.getInputStream(env, args[0]);
    try {
        Context context = new Context(is, Stream.getPath(args[0]), env.getDefaultCharset());
        if (args[1].toBoolean()) {
            ModuleDumper moduleDumper = new ModuleDumper(context, env, args[2].toBoolean());
            module = moduleDumper.load(context.getInputStream(env.getDefaultCharset()));
        } else {
            AbstractCompiler compiler = env.scope.createCompiler(env, context);
            module = compiler.compile(false);
        }
        register(env);
    } finally {
        Stream.closeStream(env, is);
    }
    return Memory.NULL;
}
Also used : AbstractCompiler(php.runtime.common.AbstractCompiler) InputStream(java.io.InputStream) ModuleDumper(php.runtime.loader.dump.ModuleDumper)

Example 3 with ModuleDumper

use of php.runtime.loader.dump.ModuleDumper in project jphp by jphp-compiler.

the class Launcher method loadFromCompiled.

public ModuleEntity loadFromCompiled(String file) throws IOException {
    InputStream inputStream = getResource(file);
    if (inputStream == null)
        return null;
    Context context = new Context(inputStream, file, environment.getDefaultCharset());
    ModuleDumper moduleDumper = new ModuleDumper(context, environment, true);
    return moduleDumper.load(inputStream);
}
Also used : ModuleDumper(php.runtime.loader.dump.ModuleDumper)

Example 4 with ModuleDumper

use of php.runtime.loader.dump.ModuleDumper 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 5 with ModuleDumper

use of php.runtime.loader.dump.ModuleDumper in project jphp by jphp-compiler.

the class WrapModule method dump.

@Signature({ @Arg("target"), @Arg(value = "saveDebugInfo", optional = @Optional("true")) })
public Memory dump(Environment env, Memory... args) throws IOException {
    ModuleDumper moduleDumper = new ModuleDumper(module.getContext(), env, args[1].toBoolean());
    OutputStream os = Stream.getOutputStream(env, args[0]);
    try {
        moduleDumper.save(module, os);
    } finally {
        Stream.closeStream(env, os);
    }
    return Memory.NULL;
}
Also used : OutputStream(java.io.OutputStream) ModuleDumper(php.runtime.loader.dump.ModuleDumper)

Aggregations

ModuleDumper (php.runtime.loader.dump.ModuleDumper)6 File (java.io.File)2 InputStream (java.io.InputStream)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 DataInputStream (java.io.DataInputStream)1 IOException (java.io.IOException)1 OutputStream (java.io.OutputStream)1 Test (org.develnext.jphp.core.tester.Test)1 Memory (php.runtime.Memory)1 AbstractCompiler (php.runtime.common.AbstractCompiler)1 Context (php.runtime.env.Context)1 CustomErrorException (php.runtime.exceptions.CustomErrorException)1 ErrorException (php.runtime.exceptions.support.ErrorException)1 UncaughtException (php.runtime.lang.UncaughtException)1 BaseBaseException (php.runtime.lang.exception.BaseBaseException)1 ArrayMemory (php.runtime.memory.ArrayMemory)1 ModuleEntity (php.runtime.reflection.ModuleEntity)1 PrintF (php.runtime.util.PrintF)1