Beispiel #1
0
  public void eventHandler(
      ThreadContext context, String event, String file, int line, String name, IRubyObject type) {
    // Line numbers are 1s based.  Arrays are zero based.  We need to compensate for that.
    line -= 1;

    // Make sure that we have SCRIPT_LINES__ and it's a hash
    RubyHash scriptLines = getScriptLines(context.getRuntime());
    if (scriptLines == null || !scriptLines.containsKey(file)) {
      return;
    }

    // make sure the file's source lines are in SCRIPT_LINES__
    cover = getCover(context.getRuntime());
    RubyArray lines = (RubyArray) scriptLines.get(file);
    if (lines == null || cover == null) {
      return;
    }

    // make sure file's counts are in COVER and set to zero
    RubyArray counts = (RubyArray) cover.get(file);
    if (counts == null) {
      counts = context.getRuntime().newArray();
      for (int i = 0; i < lines.size(); i++) {
        counts.add(Long.valueOf(0));
      }
      cover.put(file, counts);
    }

    // in the case of code generation (one example is instance_eval for routes optimization)
    // We could get here and see that we are not matched up with what we expect
    if (counts.size() <= line) {
      for (int i = counts.size(); i <= line; i++) {
        counts.add(Long.valueOf(0));
      }
    }

    if (!context.isWithinTrace()) {
      try {
        context.setWithinTrace(true);
        // update counts in COVER
        Long count = (Long) counts.get(line);
        if (count == null) {
          count = Long.valueOf(0);
        }
        count = Long.valueOf(count.longValue() + 1);
        counts.set(line, count);
      } finally {
        context.setWithinTrace(false);
      }
    }
  }
Beispiel #2
0
 public static RubyArray safeArrayToRubyArray(ThreadContext context, SafeArray sa) {
   Variant[] variants = sa.toVariantArray();
   RubyArray ary = RubyArray.newArray(context.getRuntime(), variants.length);
   for (int i = 0; i < variants.length; i++) {
     ary.add(jacobToRuby(context, variants[i]));
   }
   return ary;
 }
Beispiel #3
0
    @JRubyMethod
    public synchronized IRubyObject select(
        ThreadContext context, IRubyObject timeout, Block block) {
      Ruby runtime = context.getRuntime();

      if (!this.selector.isOpen()) {
        throw context.getRuntime().newIOError("selector is closed");
      }

      int ready = doSelect(runtime, context, timeout);

      /* Timeout or wakeup */
      if (ready <= 0) return context.nil;

      RubyArray array = null;
      if (!block.isGiven()) {
        array = runtime.newArray(this.selector.selectedKeys().size());
      }

      Iterator selectedKeys = this.selector.selectedKeys().iterator();
      while (selectedKeys.hasNext()) {
        SelectionKey key = (SelectionKey) selectedKeys.next();
        processKey(key);
        selectedKeys.remove();

        if (block.isGiven()) {
          block.call(context, (IRubyObject) key.attachment());
        } else {
          array.add(key.attachment());
        }
      }

      if (block.isGiven()) {
        return RubyNumeric.int2fix(runtime, ready);
      } else {
        return array;
      }
    }