Search in sources :

Example 6 with ClosureEntity

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

the class ModuleDumper method save.

@Override
public void save(ModuleEntity entity, OutputStream output) throws IOException {
    if (entity.getData() == null)
        throw new DumpException("Module '" + entity.getName() + "' not compiled");
    DumpOutputStream data = new DumpOutputStream(output);
    // version
    data.writeInt(DUMP_STAMP);
    data.writeInt(DUMP_VERSION);
    // legacy code.
    data.writeEnum(LangMode.DEFAULT);
    // module name
    data.writeName(entity.getContext().getModuleName());
    data.writeName(entity.getInternalName());
    // trace
    data.writeTrace(entity.getTrace());
    // constants
    data.writeInt(entity.getConstants().size());
    for (ConstantEntity e : entity.getConstants()) {
        constantDumper.save(e, output);
    }
    // closures
    data.writeInt(entity.getClosures().size());
    for (ClosureEntity e : entity.getClosures()) {
        closureDumper.save(e, output);
    }
    // generators
    data.writeInt(entity.getGenerators().size());
    for (GeneratorEntity e : entity.getGenerators()) {
        generatorDumper.save(e, output);
    }
    // functions
    data.writeInt(entity.getFunctions().size());
    for (FunctionEntity e : entity.getFunctions()) {
        functionDumper.save(e, output);
    }
    // classes
    data.writeInt(entity.getClasses().size());
    for (ClassEntity e : entity.getClasses()) {
        classDumper.save(e, output);
    }
    if (includeData) {
        // byte code
        data.writeInt(entity.getData().length);
        data.write(entity.getData());
    } else {
        data.writeInt(0);
    }
    // 5 mb
    data.writeRawData(null, 1024 * 1024 * 5);
}
Also used : ClassEntity(php.runtime.reflection.ClassEntity) GeneratorEntity(php.runtime.reflection.helper.GeneratorEntity) FunctionEntity(php.runtime.reflection.FunctionEntity) ClosureEntity(php.runtime.reflection.helper.ClosureEntity) DumpException(php.runtime.loader.dump.io.DumpException) DumpOutputStream(php.runtime.loader.dump.io.DumpOutputStream) ConstantEntity(php.runtime.reflection.ConstantEntity)

Example 7 with ClosureEntity

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

the class ClosureDumper method load.

@Override
public ClosureEntity load(InputStream input) throws IOException {
    DumpInputStream data = new DumpInputStream(input);
    ClosureEntity entity = new ClosureEntity(context);
    entity.setParent(env.scope.fetchUserClass(Closure.class));
    entity.setInternalName(data.readName());
    entity.setReturnReference(data.readBoolean());
    entity.methodMagicInvoke = methodDumper.load(input);
    entity.methodMagicInvoke.setClazz(entity);
    entity.addMethod(entity.methodMagicInvoke, null);
    int paramCount = data.readInt();
    if (paramCount < 0)
        throw new DumpException("Invalid param count");
    entity.parameters = new ParameterEntity[paramCount];
    for (int i = 0; i < paramCount; i++) {
        ParameterEntity param = parameterDumper.load(input);
        param.setTrace(entity.getTrace());
        entity.parameters[i] = param;
    }
    paramCount = data.readInt();
    if (paramCount < 0)
        throw new DumpException("Invalid param count");
    entity.uses = new ParameterEntity[paramCount];
    for (int i = 0; i < paramCount; i++) {
        ParameterEntity param = parameterDumper.load(input);
        param.setTrace(entity.getTrace());
        entity.uses[i] = param;
    }
    entity.setData(data.readRawData(Integer.MAX_VALUE));
    return entity;
}
Also used : DumpInputStream(php.runtime.loader.dump.io.DumpInputStream) ParameterEntity(php.runtime.reflection.ParameterEntity) Closure(php.runtime.lang.Closure) ClosureEntity(php.runtime.reflection.helper.ClosureEntity) DumpException(php.runtime.loader.dump.io.DumpException)

Example 8 with ClosureEntity

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

the class ModuleDumper method load.

@Override
public ModuleEntity load(InputStream input) throws IOException {
    DumpInputStream data = new DumpInputStream(input);
    int STAMP = data.readInt();
    if (STAMP != DUMP_STAMP)
        throw new DumpException("Invalid file format");
    int VERSION = data.readInt();
    if (VERSION != DUMP_VERSION)
        throw new DumpException("Invalid dump version - " + VERSION + ", only " + DUMP_VERSION);
    // legacy
    data.readLangMode();
    ModuleEntity entity = new ModuleEntity(context);
    entity.setName(data.readName());
    entity.setInternalName(data.readName());
    entity.setTrace(data.readTrace(context));
    // constants
    int count = data.readInt();
    for (int i = 0; i < count; i++) {
        ConstantEntity el = constantDumper.load(input);
        el.setModule(entity);
        entity.addConstant(el);
    }
    // closures
    count = data.readInt();
    for (int i = 0; i < count; i++) {
        ClosureEntity el = closureDumper.load(input);
        el.setModule(entity);
        entity.addClosure(el);
    }
    count = data.readInt();
    for (int i = 0; i < count; i++) {
        GeneratorEntity el = generatorDumper.load(input);
        el.setModule(entity);
        entity.addGenerator(el);
    }
    // functions
    count = data.readInt();
    for (int i = 0; i < count; i++) {
        FunctionEntity el = functionDumper.load(input);
        el.setModule(entity);
        entity.addFunction(el);
    }
    // classes
    count = data.readInt();
    ClassDumper classDumper = new ClassDumper(context, entity, env, debugInformation);
    for (int i = 0; i < count; i++) {
        ClassEntity el = classDumper.load(input);
        el.setModule(entity);
        entity.addClass(el);
    }
    // byte code
    entity.setData(data.readRawData(Integer.MAX_VALUE));
    data.readRawData(1024 * 1024 * 5);
    return entity;
}
Also used : ClassEntity(php.runtime.reflection.ClassEntity) GeneratorEntity(php.runtime.reflection.helper.GeneratorEntity) DumpInputStream(php.runtime.loader.dump.io.DumpInputStream) FunctionEntity(php.runtime.reflection.FunctionEntity) ClosureEntity(php.runtime.reflection.helper.ClosureEntity) ModuleEntity(php.runtime.reflection.ModuleEntity) DumpException(php.runtime.loader.dump.io.DumpException) ConstantEntity(php.runtime.reflection.ConstantEntity)

Example 9 with ClosureEntity

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

the class RuntimeClassLoader method loadModule.

public boolean loadModule(ModuleEntity module, boolean withBytecode) {
    String internal = module.getInternalName();
    boolean ret = false;
    if (!module.isLoaded()) {
        internalModules.put(internal, module);
        try {
            for (ClosureEntity closure : module.getClosures()) loadClosure(closure, withBytecode);
            for (GeneratorEntity generator : module.getGenerators()) loadGenerator(generator, withBytecode);
            for (ClassEntity clazz : module.getClasses()) {
                if (clazz.getType() != ClassEntity.Type.INTERFACE)
                    loadClass(clazz, withBytecode);
            }
            for (FunctionEntity function : module.getFunctions()) {
                loadFunction(function, withBytecode);
            }
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        } catch (NoSuchFieldException e) {
            throw new RuntimeException(e);
        }
        module.setLoaded(true);
        ret = true;
    }
    if (withBytecode) {
        byte[] data = translateData(module.getData());
        Class<?> result = defineClass(null, data, 0, module.getData().length);
        module.setNativeClazz(result);
    }
    try {
        Method method = module.getNativeClazz().getDeclaredMethod("__include", Environment.class, Memory[].class, ArrayMemory.class);
        module.setNativeMethod(method);
    } catch (NoSuchMethodException e) {
        throw new RuntimeException(e);
    }
    return ret;
}
Also used : GeneratorEntity(php.runtime.reflection.helper.GeneratorEntity) Memory(php.runtime.Memory) ArrayMemory(php.runtime.memory.ArrayMemory) Method(java.lang.reflect.Method) ClosureEntity(php.runtime.reflection.helper.ClosureEntity)

Example 10 with ClosureEntity

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

the class FunctionEntity method getClosure.

@Override
public Closure getClosure(Environment env) {
    if (cachedClosure != null)
        return cachedClosure;
    final FunctionEntity bind = this;
    final ClosureEntity closureEntity1 = new ClosureEntity(this.getContext());
    closureEntity1.setParent(env.scope.fetchUserClass(Closure.class));
    closureEntity1.parameters = this.parameters;
    closureEntity1.setReturnReference(this.isReturnReference());
    MethodEntity m = new MethodEntity(this);
    m.setClazz(closureEntity1);
    m.setName("__invoke");
    closureEntity1.addMethod(m, null);
    closureEntity1.doneDeclare();
    Closure tmp = new Closure(env, closureEntity1, new ObjectMemory(env.getLateObject()), null, new Memory[0]) {

        @Override
        public Memory __invoke(Environment e, Memory... args) {
            try {
                return bind.invoke(e, e.peekCall(0).trace, args);
            } catch (RuntimeException e1) {
                throw e1;
            } catch (Throwable throwable) {
                throw new RuntimeException(throwable);
            }
        }

        @Override
        public Memory getOrCreateStatic(String name) {
            return Memory.NULL;
        }

        @Override
        public ClassEntity getReflection() {
            return closureEntity1;
        }
    };
    try {
        m.setNativeMethod(tmp.getClass().getDeclaredMethod("__invoke", Environment.class, Memory[].class));
    } catch (NoSuchMethodException e) {
        throw new RuntimeException(e);
    }
    return cachedClosure = tmp;
}
Also used : AbstractFunctionEntity(php.runtime.reflection.support.AbstractFunctionEntity) Closure(php.runtime.lang.Closure) ObjectMemory(php.runtime.memory.ObjectMemory) ClosureEntity(php.runtime.reflection.helper.ClosureEntity) Memory(php.runtime.Memory) ObjectMemory(php.runtime.memory.ObjectMemory) Environment(php.runtime.env.Environment)

Aggregations

ClosureEntity (php.runtime.reflection.helper.ClosureEntity)11 ClassEntity (php.runtime.reflection.ClassEntity)5 Memory (php.runtime.Memory)4 FunctionEntity (php.runtime.reflection.FunctionEntity)4 GeneratorEntity (php.runtime.reflection.helper.GeneratorEntity)4 Environment (php.runtime.env.Environment)3 Closure (php.runtime.lang.Closure)3 DumpException (php.runtime.loader.dump.io.DumpException)3 ObjectMemory (php.runtime.memory.ObjectMemory)3 DumpInputStream (php.runtime.loader.dump.io.DumpInputStream)2 ConstantEntity (php.runtime.reflection.ConstantEntity)2 ParameterEntity (php.runtime.reflection.ParameterEntity)2 AbstractFunctionEntity (php.runtime.reflection.support.AbstractFunctionEntity)2 Method (java.lang.reflect.Method)1 Token (org.develnext.jphp.core.tokenizer.token.Token)1 ClosureStmtToken (org.develnext.jphp.core.tokenizer.token.expr.value.ClosureStmtToken)1 NameToken (org.develnext.jphp.core.tokenizer.token.expr.value.NameToken)1 YieldExprToken (org.develnext.jphp.core.tokenizer.token.expr.value.YieldExprToken)1 ClassStmtToken (org.develnext.jphp.core.tokenizer.token.stmt.ClassStmtToken)1 MethodStmtToken (org.develnext.jphp.core.tokenizer.token.stmt.MethodStmtToken)1