<commit_before>#include "builtin/array.hpp"
#include "builtin/fixnum.hpp"
#include "builtin/object.hpp"
#include "builtin/proc.hpp"

#include "arguments.hpp"
#include "dispatch.hpp"

#include "capi/capi.hpp"
#include "capi/include/ruby.h"

#include <stdarg.h>

using namespace rubinius;
using namespace rubinius::capi;

namespace rubinius {
  namespace capi {

    Array* capi_get_array(NativeMethodEnvironment* env, VALUE val) {
      if(!env) env = NativeMethodEnvironment::get();

      Handle* handle = Handle::from(val);
      Array* array = c_as<Array>(handle->object());
      handle->flush(env);

      return array;
    }

    void capi_update_array(NativeMethodEnvironment* env, VALUE ary_handle) {
      if(!env) env = NativeMethodEnvironment::get();

      Handle* handle = Handle::from(ary_handle);
      handle->update(env);
    }

    void flush_cached_rarray(NativeMethodEnvironment* env, Handle* handle) {
      if(handle->is_rarray()) {
        Array* array = as<Array>(handle->object());
        RArray* rarray = handle->as_rarray(env);

        size_t size = array->size();

        if(size != rarray->len) {
          Tuple* tuple = Tuple::create(env->state(), rarray->len);
          array->tuple(env->state(), tuple);
          array->start(env->state(), Fixnum::from(0));
          array->total(env->state(), Fixnum::from(rarray->len));
        }

        for(size_t i = 0; i < size; i++) {
          array->set(env->state(), i, env->get_object(rarray->ptr[i]));
        }
      }
    }

    void update_cached_rarray(NativeMethodEnvironment* env, Handle* handle) {
      if(handle->is_rarray()) {
        Array* array = c_as<Array>(handle->object());
        RArray* rarray = handle->as_rarray(env);

        size_t size = array->size();

        if(rarray->len != size) {
          delete[] rarray->dmwmb;
          rarray->dmwmb = rarray->ptr = new VALUE[size];
          rarray->aux.capa = rarray->len = size;
        }

        for(size_t i = 0; i < size; i++) {
          rarray->ptr[i] = env->get_handle(array->get(env->state(), i));
        }
      }
    }

    RArray* Handle::as_rarray(NativeMethodEnvironment* env) {
      if(type_ != cRArray) {
        Array* array = c_as<Array>(object());
        size_t size = array->size();

        RArray* ary = new RArray;
        VALUE* ptr = new VALUE[size];
        for(size_t i = 0; i < size; i++) {
          ptr[i] = env->get_handle(array->get(env->state(), i));
        }

        ary->dmwmb = ary->ptr = ptr;
        ary->aux.capa = ary->len = size;
        ary->aux.shared = Qfalse;

        type_ = cRArray;
        as_.rarray = ary;

        flush_ = flush_cached_rarray;
        update_ = update_cached_rarray;

        env->state()->shared.global_handles()->move(this,
            env->state()->shared.cached_handles());
      }

      return as_.rarray;
    }
  }
}

extern "C" {
  struct RArray* capi_rarray_struct(VALUE val) {
    NativeMethodEnvironment* env = NativeMethodEnvironment::get();
    Handle* h = Handle::from(val);

    env->check_tracked_handle(h);
    return h->as_rarray(env);
  }

  VALUE rb_Array(VALUE obj_handle) {
    NativeMethodEnvironment* env = NativeMethodEnvironment::get();

    Object* obj = env->get_object(obj_handle);

    if (kind_of<Array>(obj)) {
      return obj_handle;
    }

    Array* array = Array::create(env->state(), 1);
    array->set(env->state(), 0, obj);

    return env->get_handle(array);
  }

  VALUE rb_ary_clear(VALUE self_handle) {
    return rb_funcall2(self_handle, rb_intern("clear"), 0, NULL);
  }

  VALUE rb_ary_delete(VALUE self_handle, VALUE item) {
    return rb_funcall(self_handle, rb_intern("delete"), 1, item);
  }

  VALUE rb_ary_dup(VALUE self_handle) {
    return rb_funcall2(self_handle, rb_intern("dup"), 0, NULL);
  }

  /* @todo Check 64-bit? */
  VALUE rb_ary_entry(VALUE self_handle, int index) {
    NativeMethodEnvironment* env = NativeMethodEnvironment::get();

    Array* self = capi_get_array(env, self_handle);
    return env->get_handle(self->aref(env->state(), Fixnum::from(index)));
  }

  VALUE rb_ary_each(VALUE self_handle) {
    NativeMethodEnvironment* env = NativeMethodEnvironment::get();

    Array* self = capi_get_array(env, self_handle);
    for(size_t i = 0; i < self->size(); i++) {
      rb_yield(env->get_handle(self->get(env->state(), i)));
    }

    return self_handle;
  }

  VALUE rb_ary_includes(VALUE self, VALUE obj) {
    return rb_funcall(self, rb_intern("include?"), 1, obj);
  }

  VALUE rb_ary_join(VALUE self_handle, VALUE separator_handle) {
    return rb_funcall(self_handle, rb_intern("join"), 1, separator_handle);
  }

  VALUE rb_ary_to_s(VALUE self_handle) {
    return rb_ary_join(self_handle, Qnil);
  }

  /** By default, Arrays have space for 16 elements. */
  static const unsigned long cCApiArrayDefaultCapacity = 16;

  VALUE rb_ary_new() {
    NativeMethodEnvironment* env = NativeMethodEnvironment::get();

    Array* array = Array::create(env->state(), cCApiArrayDefaultCapacity);
    return env->get_handle(array);
  }

  VALUE rb_ary_new2(unsigned long length) {
    NativeMethodEnvironment* env = NativeMethodEnvironment::get();

    Array* array = Array::create(env->state(), length);

    return env->get_handle(array);
  }

  VALUE rb_ary_new3(unsigned long length, ...) {
    va_list args;
    va_start(args, length);

    NativeMethodEnvironment* env = NativeMethodEnvironment::get();<commit_msg>Upcall to #to_s for rb_ary_to_s<commit_after>#include "builtin/array.hpp"
#include "builtin/fixnum.hpp"
#include "builtin/object.hpp"
#include "builtin/proc.hpp"

#include "arguments.hpp"
#include "dispatch.hpp"

#include "capi/capi.hpp"
#include "capi/include/ruby.h"

#include <stdarg.h>

using namespace rubinius;
using namespace rubinius::capi;

namespace rubinius {
  namespace capi {

    Array* capi_get_array(NativeMethodEnvironment* env, VALUE val) {
      if(!env) env = NativeMethodEnvironment::get();

      Handle* handle = Handle::from(val);
      Array* array = c_as<Array>(handle->object());
      handle->flush(env);

      return array;
    }

    void capi_update_array(NativeMethodEnvironment* env, VALUE ary_handle) {
      if(!env) env = NativeMethodEnvironment::get();

      Handle* handle = Handle::from(ary_handle);
      handle->update(env);
    }

    void flush_cached_rarray(NativeMethodEnvironment* env, Handle* handle) {
      if(handle->is_rarray()) {
        Array* array = as<Array>(handle->object());
        RArray* rarray = handle->as_rarray(env);

        size_t size = array->size();

        if(size != rarray->len) {
          Tuple* tuple = Tuple::create(env->state(), rarray->len);
          array->tuple(env->state(), tuple);
          array->start(env->state(), Fixnum::from(0));
          array->total(env->state(), Fixnum::from(rarray->len));
        }

        for(size_t i = 0; i < size; i++) {
          array->set(env->state(), i, env->get_object(rarray->ptr[i]));
        }
      }
    }

    void update_cached_rarray(NativeMethodEnvironment* env, Handle* handle) {
      if(handle->is_rarray()) {
        Array* array = c_as<Array>(handle->object());
        RArray* rarray = handle->as_rarray(env);

        size_t size = array->size();

        if(rarray->len != size) {
          delete[] rarray->dmwmb;
          rarray->dmwmb = rarray->ptr = new VALUE[size];
          rarray->aux.capa = rarray->len = size;
        }

        for(size_t i = 0; i < size; i++) {
          rarray->ptr[i] = env->get_handle(array->get(env->state(), i));
        }
      }
    }

    RArray* Handle::as_rarray(NativeMethodEnvironment* env) {
      if(type_ != cRArray) {
        Array* array = c_as<Array>(object());
        size_t size = array->size();

        RArray* ary = new RArray;
        VALUE* ptr = new VALUE[size];
        for(size_t i = 0; i < size; i++) {
          ptr[i] = env->get_handle(array->get(env->state(), i));
        }

        ary->dmwmb = ary->ptr = ptr;
        ary->aux.capa = ary->len = size;
        ary->aux.shared = Qfalse;

        type_ = cRArray;
        as_.rarray = ary;

        flush_ = flush_cached_rarray;
        update_ = update_cached_rarray;

        env->state()->shared.global_handles()->move(this,
            env->state()->shared.cached_handles());
      }

      return as_.rarray;
    }
  }
}

extern "C" {
  struct RArray* capi_rarray_struct(VALUE val) {
    NativeMethodEnvironment* env = NativeMethodEnvironment::get();
    Handle* h = Handle::from(val);

    env->check_tracked_handle(h);
    return h->as_rarray(env);
  }

  VALUE rb_Array(VALUE obj_handle) {
    NativeMethodEnvironment* env = NativeMethodEnvironment::get();

    Object* obj = env->get_object(obj_handle);

    if (kind_of<Array>(obj)) {
      return obj_handle;
    }

    Array* array = Array::create(env->state(), 1);
    array->set(env->state(), 0, obj);

    return env->get_handle(array);
  }

  VALUE rb_ary_clear(VALUE self_handle) {
    return rb_funcall2(self_handle, rb_intern("clear"), 0, NULL);
  }

  VALUE rb_ary_delete(VALUE self_handle, VALUE item) {
    return rb_funcall(self_handle, rb_intern("delete"), 1, item);
  }

  VALUE rb_ary_dup(VALUE self_handle) {
    return rb_funcall2(self_handle, rb_intern("dup"), 0, NULL);
  }

  /* @todo Check 64-bit? */
  VALUE rb_ary_entry(VALUE self_handle, int index) {
    NativeMethodEnvironment* env = NativeMethodEnvironment::get();

    Array* self = capi_get_array(env, self_handle);
    return env->get_handle(self->aref(env->state(), Fixnum::from(index)));
  }

  VALUE rb_ary_each(VALUE self_handle) {
    NativeMethodEnvironment* env = NativeMethodEnvironment::get();

    Array* self = capi_get_array(env, self_handle);
    for(size_t i = 0; i < self->size(); i++) {
      rb_yield(env->get_handle(self->get(env->state(), i)));
    }

    return self_handle;
  }

  VALUE rb_ary_includes(VALUE self, VALUE obj) {
    return rb_funcall(self, rb_intern("include?"), 1, obj);
  }

  VALUE rb_ary_join(VALUE self_handle, VALUE separator_handle) {
    return rb_funcall(self_handle, rb_intern("join"), 1, separator_handle);
  }

  VALUE rb_ary_to_s(VALUE self_handle) {
    return rb_funcall(self_handle, rb_intern("to_s"), 0);
  }

  /** By default, Arrays have space for 16 elements. */
  static const unsigned long cCApiArrayDefaultCapacity = 16;

  VALUE rb_ary_new() {
    NativeMethodEnvironment* env = NativeMethodEnvironment::get();

    Array* array = Array::create(env->state(), cCApiArrayDefaultCapacity);
    return env->get_handle(array);
  }

  VALUE rb_ary_new2(unsigned long length) {
    NativeMethodEnvironment* env = NativeMethodEnvironment::get();

    Array* array = Array::create(env->state(), length);

    return env->get_handle(array);
  }

  VALUE rb_ary_new3(unsigned long length, ...) {
    va_list args;
    va_start(args, length);

    NativeMethodEnvironment* env = NativeMethodEnvironment::get();
{'fraction_non_alphanumeric': 0.14971984936162397, 'fraction_numerical': 0.004133370074400662, 'mean_word_length': 3.187692307692308, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 149, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '186160', 'n_tokens_mistral': 3724, 'n_tokens_neox': 3560, 'n_words': 708}