Search in sources :

Example 26 with ClassEntity

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

the class Stream method of.

@Signature({ @Arg("path"), @Arg(value = "mode", optional = @Optional("r")) })
public static Memory of(Environment env, Memory... args) throws Throwable {
    String path = args[0].toString();
    String protocol = "file";
    int pos = path.indexOf("://");
    if (pos > -1) {
        protocol = path.substring(0, pos);
        path = path.substring(pos + 3);
    }
    String className = env.getUserValue(Stream.class.getName() + "#" + protocol, String.class);
    if (className == null) {
        throw new IOException("Unregistered protocol - " + protocol + "://");
    }
    ClassEntity classEntity = env.fetchClass(className);
    if (classEntity.getNativeClass().getAnnotation(UsePathWithProtocols.class) != null) {
        path = protocol + "://" + path;
    }
    return new ObjectMemory(classEntity.newObject(env, env.trace(), true, new StringMemory(path), args[1]));
}
Also used : ClassEntity(php.runtime.reflection.ClassEntity) ObjectMemory(php.runtime.memory.ObjectMemory) StringMemory(php.runtime.memory.StringMemory)

Example 27 with ClassEntity

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

the class WrapEnvironment method exportClass.

@Signature(@Arg("className"))
public Memory exportClass(final Environment env, Memory... args) throws Throwable {
    ClassEntity classEntity = environment.fetchClass(args[0].toString());
    if (classEntity == null) {
        env.exception(Messages.ERR_CLASS_NOT_FOUND.fetch(args[0]));
        return Memory.NULL;
    }
    env.registerClass(classEntity);
    return Memory.NULL;
}
Also used : ClassEntity(php.runtime.reflection.ClassEntity)

Example 28 with ClassEntity

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

the class WrapAndroid method startActivity.

@Signature
public static void startActivity(Environment env, String clazz) throws ClassNotFoundException {
    ClassEntity entity = env.fetchClass(clazz);
    if (entity == null) {
        env.exception(Messages.ERR_CLASS_NOT_FOUND.fetch(clazz));
        return;
    }
    Intent intent = new Intent(AndroidStandaloneLoader.getContext(), entity.getNativeClass());
    AndroidStandaloneLoader.getMainActivity().startActivity(intent);
}
Also used : ClassEntity(php.runtime.reflection.ClassEntity) Intent(android.content.Intent) Signature(php.runtime.annotation.Reflection.Signature)

Example 29 with ClassEntity

use of php.runtime.reflection.ClassEntity 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 30 with ClassEntity

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

the class ClassDumperTest method testBasic.

@Test
public void testBasic() throws IOException {
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    ClassEntity entity = new ClassEntity(context);
    entity.setAbstract(true);
    entity.setFinal(true);
    entity.setId(1);
    entity.setName("Foobar");
    entity.setData(new byte[] { 1, 2, 3, 4 });
    dumper.save(entity, output);
    ClassEntity copyEntity = dumper.load(new ByteArrayInputStream(output.toByteArray()));
    Assert.assertEquals("Foobar", copyEntity.getName());
    Assert.assertTrue(copyEntity.isAbstract());
    Assert.assertTrue(copyEntity.isFinal());
    Assert.assertArrayEquals(new byte[] { 1, 2, 3, 4 }, copyEntity.getData());
}
Also used : ClassEntity(php.runtime.reflection.ClassEntity) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Aggregations

ClassEntity (php.runtime.reflection.ClassEntity)54 ObjectMemory (php.runtime.memory.ObjectMemory)21 Memory (php.runtime.Memory)14 ArrayMemory (php.runtime.memory.ArrayMemory)14 PropertyEntity (php.runtime.reflection.PropertyEntity)10 MethodEntity (php.runtime.reflection.MethodEntity)9 StringMemory (php.runtime.memory.StringMemory)8 ConstantEntity (php.runtime.reflection.ConstantEntity)5 FunctionEntity (php.runtime.reflection.FunctionEntity)5 CriticalException (php.runtime.exceptions.CriticalException)4 Closure (php.runtime.lang.Closure)4 IObject (php.runtime.lang.IObject)4 ReferenceMemory (php.runtime.memory.ReferenceMemory)4 ClosureEntity (php.runtime.reflection.helper.ClosureEntity)4 GeneratorEntity (php.runtime.reflection.helper.GeneratorEntity)4 ForeachIterator (php.runtime.lang.ForeachIterator)3 ModuleEntity (php.runtime.reflection.ModuleEntity)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2