private void jitThresholdReached(
      final DefaultMethod method,
      final RubyInstanceConfig config,
      ThreadContext context,
      final String className,
      final String methodName) {
    // Disable any other jit tasks from entering queue
    method.setCallCount(-1);

    final Ruby runtime = context.runtime;

    Runnable jitTask = new JITTask(className, method, methodName);

    // if background JIT is enabled and threshold is > 0 and we have an executor...
    if (config.getJitBackground() && config.getJitThreshold() > 0 && executor != null) {
      // JIT in background
      try {
        executor.submit(jitTask);
      } catch (RejectedExecutionException ree) {
        // failed to submit, just run it directly
        jitTask.run();
      }
    } else {
      // just run directly
      jitTask.run();
    }
  }
  public void tryJIT(
      DefaultMethod method, ThreadContext context, String className, String methodName) {
    if (!config.getCompileMode().shouldJIT()) return;

    if (method.incrementCallCount() < config.getJitThreshold()) return;

    jitThresholdReached(method, config, context, className, methodName);
  }
示例#3
0
  /** Tests the {@link org.jruby.runtime.profile.ProfilingServiceLookup} too */
  public void testNoProfilingServerAvailableIfProfilingIsDisabled() {
    RubyInstanceConfig config = Ruby.getGlobalRuntime().getInstanceConfig();

    RubyInstanceConfig configOne = new RubyInstanceConfig(config);
    configOne.setProfilingMode(RubyInstanceConfig.ProfilingMode.OFF);

    Ruby ruby = Ruby.newInstance(configOne);

    assertNull(ruby.getProfilingService());
  }
示例#4
0
  private void setupJRuby(Activity startActivity) {
    System.setProperty("jruby.bytecode.version", "1.5");

    // enable proxy classes
    System.setProperty(
        "jruby.ji.proxyClassFactory", "com.rickbutton.rubydroid.DalvikProxyClassFactory");
    System.setProperty("jruby.ji.upper.case.package.name.allowed", "true");
    System.setProperty("jruby.class.cache.path", appContext.getDir("dex", 0).getAbsolutePath());

    // setup jruby home
    String apkName = getApkName();
    String jrubyHome = "file:" + apkName + "!/jruby.home";
    System.setProperty("jruby.home", jrubyHome);

    // configure jruby
    System.setProperty("jruby.compile.mode", "OFF"); // OFF OFFIR JITIR? FORCE FORCEIR
    // System.setProperty("jruby.compile.backend", "DALVIK");
    System.setProperty("jruby.bytecode.version", "1.6");
    System.setProperty("jruby.interfaces.useProxy", "true");
    System.setProperty("jruby.management.enabled", "false");
    System.setProperty("jruby.objectspace.enabled", "false");
    System.setProperty("jruby.thread.pooling", "true");
    System.setProperty("jruby.native.enabled", "false");
    System.setProperty("jruby.ir.passes", "LocalOptimizationPass,DeadCodeElimination");
    System.setProperty("jruby.backtrace.style", "normal"); // normal raw full mri

    ClassLoader loader = new PathClassLoader(apkName, RubySystem.class.getClassLoader());

    // disable gems
    RubyInstanceConfig config = new RubyInstanceConfig();
    config.setDisableGems(true);
    config.setLoader(loader);
    Ruby.newInstance(config);

    container =
        new ScriptingContainer(LocalContextScope.SINGLETON, LocalVariableBehavior.PERSISTENT);
    container.setClassLoader(loader);

    Thread.currentThread().setContextClassLoader(loader);

    if (appContext.getFilesDir() != null) {
      container.setCurrentDirectory(appContext.getFilesDir().getPath());
    }

    container.put("$package_name", appContext.getPackageName());

    container.put("app_context", appContext);
    container.put("system", this);
    rubyContext = container.runScriptlet(PathType.CLASSPATH, "ruby/droid/init.rb");

    Object mainActivity = container.get("MainActivity");
    container.callMethod(rubyContext, "start_ruby_activity", mainActivity, startActivity);
  }
示例#5
0
  /** Tests the {@link org.jruby.runtime.profile.ProfilingServiceLookup} too */
  public void testProfilingServiceLookupWorks() {
    try {
      RubyInstanceConfig config = Ruby.getGlobalRuntime().getInstanceConfig();

      RubyInstanceConfig configOne = new RubyInstanceConfig(config);

      configOne.setProfilingService(TestProfilingService.class.getName());
      configOne.setProfilingMode(RubyInstanceConfig.ProfilingMode.SERVICE);
      Ruby ruby = Ruby.newInstance(configOne);

      assertNotNull(ruby.getProfilingService());
      assertTrue(ruby.getProfilingService() instanceof TestProfilingService);
    } catch (RaiseException e) {
      // e.printStackTrace();
      // TODO hwo to mock org.jruby.exceptions.RaiseException: (LoadError) no such file to load --
      // jruby/profiler/shutdown_hook
    }
  }
  /**
   * {@inheritDoc}
   *
   * <p>Latest method of invoking jruby script have been adapted from <a
   * href="http://wiki.jruby.org/wiki/Java_Integration" title="Click to visit JRuby Wiki"> JRuby
   * wiki:</a>
   *
   * @todo create a way to prevent initialization and shutdown with each script invocation.
   */
  protected PicoContainer createContainerFromScript(
      PicoContainer parentContainer, Object assemblyScope) {
    if (parentContainer == null) {
      parentContainer = new EmptyPicoContainer();
    }
    parentContainer =
        new DefaultClassLoadingPicoContainer(
            getClassLoader(), new DefaultPicoContainer(new Caching(), parentContainer));

    RubyInstanceConfig rubyConfig = new RubyInstanceConfig();
    rubyConfig.setLoader(this.getClassLoader());
    Ruby ruby = JavaEmbedUtils.initialize(Collections.EMPTY_LIST, rubyConfig);
    ruby.getLoadService().require("org/picocontainer/script/jruby/scriptedbuilder");
    ruby.defineReadonlyVariable("$parent", JavaEmbedUtils.javaToRuby(ruby, parentContainer));
    ruby.defineReadonlyVariable("$assembly_scope", JavaEmbedUtils.javaToRuby(ruby, assemblyScope));

    try {

      // IRubyObject result = ruby.executeScript(script);
      IRubyObject result = JavaEmbedUtils.newRuntimeAdapter().eval(ruby, script);
      return (PicoContainer) JavaEmbedUtils.rubyToJava(ruby, result, PicoContainer.class);
    } catch (RaiseException re) {
      if (re.getCause() instanceof ScriptedPicoContainerMarkupException) {
        throw (ScriptedPicoContainerMarkupException) re.getCause();
      }
      String message =
          (String) JavaEmbedUtils.rubyToJava(ruby, re.getException().message, String.class);
      if (message.startsWith(MARKUP_EXCEPTION_PREFIX)) {
        throw new ScriptedPicoContainerMarkupException(
            message.substring(MARKUP_EXCEPTION_PREFIX.length()));
      } else {
        throw new PicoCompositionException(message, re);
      }
    } finally {
      JavaEmbedUtils.terminate(ruby);
    }
  }
  void inject(Map<String, Object> environmentVars) {
    if (!environmentVars.isEmpty()) {
      Map<String, Object> replacementEnv = new HashMap<String, Object>(System.getenv());
      for (Map.Entry<String, Object> envVar : environmentVars.entrySet()) {
        String key = envVar.getKey();
        Object val = envVar.getValue();
        if (val == null || "".equals(val)) {
          replacementEnv.remove(envVar.getKey());
          if ("GEM_PATH".equals(key) && !environmentVars.containsKey("GEM_HOME")) {
            replacementEnv.remove("GEM_HOME");
          }
        } else {
          replacementEnv.put(key, val);
          if ("GEM_PATH".equals(key) && !environmentVars.containsKey("GEM_HOME")) {
            String gemHome = val.toString().split(File.pathSeparator)[0];
            replacementEnv.put("GEM_HOME", gemHome);
          }
        }
      }

      config.setEnvironment(replacementEnv);
    }
  }