#include "yarv.h"

/* Array#each */

/*  
VALUE yarv_Array_each(VALUE ary){
  return rb_ary_each(ary);
  long i;
  for (i=0; i<RARRAY(ary)->len; i++) {
    yarv_yield(RARRAY(ary)->ptr[i]);
  }
  return ary;
}
  */


/* Integer#times */
VALUE yarv_Integer_times(VALUE num){
  if (FIXNUM_P(num)) {
    long i, end;
    
    end = FIX2LONG(num);

    if(IS_YARV_WORKING()){
      /* yarv specific speedup tech */
      VALUE th = yarv_get_current_running_thread();
      VALUE val;
      struct yarv_yield_data data;
      
      thread_yield_light_prepare(th, 1, &val, &data);
      for(i=0; i<end; i++){
        val = LONG2FIX(i);
        thread_yield_light_invoke(th, 1, &val, &data);
      }
    }
    else{
      for (i=0; i<end; i++) {
        rb_yield(LONG2FIX(i));
      }
    }
  }
  else {
    VALUE i = INT2FIX(0);

    for (;;) {
      if (!RTEST(rb_funcall(i, '<', 1, num))) break;
      rb_yield(i);
      i = rb_funcall(i, '+', 1, INT2FIX(1));
    }
  }
  return num;
}

