<commit_before>#include "mrb_ae.h"

static mrb_value
mrb_aeCreateEventLoop(mrb_state *mrb, mrb_value self)
{
  mrb_int setsize;

  int argc = mrb_get_args(mrb, "|i", &setsize);
  if (argc == 0) {
    struct rlimit limit;
    int rc = getrlimit(RLIMIT_NOFILE, &limit);
    assert (rc == 0);
    setsize = limit.rlim_cur;
  }

  if (setsize < INT_MIN||setsize > INT_MAX) {
    mrb_raise(mrb, E_RUNTIME_ERROR, "setsize doesn't fit into int");
  }

  errno = 0;
  aeEventLoop *loop = aeCreateEventLoop(setsize);
  if (loop) {
    loop->mrb = mrb;
    mrb_data_init(self, loop, &mrb_aeEventLoop_type);
  }
  else {
    mrb_sys_fail(mrb, "aeCreateEventLoop");
  }


  return self;
}

static mrb_value
mrb_aeStop(mrb_state *mrb, mrb_value self)
{
  aeStop((aeEventLoop *) DATA_PTR(self));

  return mrb_nil_value();
}

static inline void
mrb_aeFileProc(aeEventLoop *eventLoop, int fd, void *clientData, int mask)
{
  mrb_state *mrb = eventLoop->mrb;
  int arena_index = mrb_gc_arena_save(mrb);
  mrb_ae_callback_data *callback_data = (mrb_ae_callback_data *) clientData;

  mrb_value args[2];
  args[0] = callback_data->client_data;
  args[1] = mrb_fixnum_value(mask);
  mrb_yield_argv(mrb, callback_data->block, 2, args);
  mrb_gc_arena_restore(mrb, arena_index);
}

static inline mrb_value
mrb_ae_create_file_callback_data(mrb_state *mrb, mrb_value self, mrb_value sock, mrb_value block, int mask)
{
  struct RBasic *callback_data_obj = mrb_obj_alloc(mrb, MRB_TT_DATA, mrb_class_get_under(mrb, mrb_class(mrb, self), "CallbackData"));
  mrb_value mrb_ae_callback_data = mrb_obj_value((struct RObject*)callback_data_obj);
  mrb_ae_callback_data = mrb_funcall_with_block(mrb, mrb_ae_callback_data, mrb_intern_lit(mrb, "initialize"), 1, &sock, block);
  mrb_iv_set(mrb, mrb_ae_callback_data, mrb_intern_lit(mrb, "mask"), mrb_fixnum_value(mask));

  return mrb_ae_callback_data;
}

static mrb_value
mrb_aeCreateFileEvent(mrb_state *mrb, mrb_value self)
{
  mrb_value sock = mrb_nil_value();
  mrb_int mask = AE_READABLE;
  mrb_value block = mrb_nil_value();

  mrb_get_args(mrb, "|oi&", &sock, &mask, &block);

  if (mrb_nil_p(block)) {
    mrb_raise(mrb, E_ARGUMENT_ERROR, "no block given");
  }

  int fd = 0;

  switch(mrb_type(sock)) {
    case MRB_TT_DATA: {
      mrb_value fd_val = mrb_Integer(mrb, sock);
      fd = mrb_fixnum(fd_val);
    } break;
    case MRB_TT_FIXNUM: {
      fd = mrb_fixnum(sock);
    } break;
    case MRB_TT_FALSE:
      break;
    default:
        mrb_raise(mrb, E_ARGUMENT_ERROR, "sock type not supported");
  }

  mrb_value mrb_ae_callback_data = mrb_ae_create_file_callback_data(mrb, self, sock, block, mask);

  errno = 0;
  int rc = aeCreateFileEvent((aeEventLoop *) DATA_PTR(self), fd, mask, mrb_aeFileProc, DATA_PTR(mrb_ae_callback_data));
  if (rc != AE_OK) {
    mrb_sys_fail(mrb, "aeCreateFileEvent");
  }

  return mrb_ae_callback_data;
}

static inline void
mrb_ae_delete_file_callback_data(mrb_state *mrb, mrb_value mrb_ae_callback_data, mrb_value self)
{
  mrb_value client_data = mrb_iv_remove(mrb, mrb_ae_callback_data, mrb_intern_lit(mrb, "client_data"));
  int fd = 0;
  if (!mrb_undef_p(client_data)) {
    mrb_value fd_val = mrb_Integer(mrb, client_data);
    fd = mrb_fixnum(fd_val);
  }
  int mask = mrb_fixnum(mrb_iv_remove(mrb, mrb_ae_callback_data, mrb_intern_lit(mrb, "mask")));
  mrb_iv_remove(mrb, mrb_ae_callback_data, mrb_intern_lit(mrb, "block"));<commit_msg>fix typos<commit_after>#include "mrb_ae.h"

static mrb_value
mrb_aeCreateEventLoop(mrb_state *mrb, mrb_value self)
{
  mrb_int setsize;

  int argc = mrb_get_args(mrb, "|i", &setsize);
  if (argc == 0) {
    struct rlimit limit;
    int rc = getrlimit(RLIMIT_NOFILE, &limit);
    assert (rc == 0);
    setsize = limit.rlim_cur;
  }

  if (setsize < INT_MIN||setsize > INT_MAX) {
    mrb_raise(mrb, E_RUNTIME_ERROR, "setsize doesn't fit into int");
  }

  errno = 0;
  aeEventLoop *loop = aeCreateEventLoop(setsize);
  if (loop) {
    loop->mrb = mrb;
    mrb_data_init(self, loop, &mrb_aeEventLoop_type);
  }
  else {
    mrb_sys_fail(mrb, "aeCreateEventLoop");
  }


  return self;
}

static mrb_value
mrb_aeStop(mrb_state *mrb, mrb_value self)
{
  aeStop((aeEventLoop *) DATA_PTR(self));

  return mrb_nil_value();
}

static inline void
mrb_aeFileProc(aeEventLoop *eventLoop, int fd, void *clientData, int mask)
{
  mrb_state *mrb = eventLoop->mrb;
  int arena_index = mrb_gc_arena_save(mrb);
  mrb_ae_callback_data *callback_data = (mrb_ae_callback_data *) clientData;

  mrb_value args[2];
  args[0] = callback_data->client_data;
  args[1] = mrb_fixnum_value(mask);
  mrb_yield_argv(mrb, callback_data->block, 2, args);
  mrb_gc_arena_restore(mrb, arena_index);
}

static inline mrb_value
mrb_ae_create_file_callback_data(mrb_state *mrb, mrb_value self, mrb_value client_data, int mask, mrb_value block)
{
  struct RBasic *callback_data_obj = mrb_obj_alloc(mrb, MRB_TT_DATA, mrb_class_get_under(mrb, mrb_class(mrb, self), "CallbackData"));
  mrb_value mrb_ae_callback_data = mrb_obj_value((struct RObject*)callback_data_obj);
  mrb_ae_callback_data = mrb_funcall_with_block(mrb, mrb_ae_callback_data, mrb_intern_lit(mrb, "initialize"), 1, &client_data, block);
  mrb_iv_set(mrb, mrb_ae_callback_data, mrb_intern_lit(mrb, "mask"), mrb_fixnum_value(mask));

  return mrb_ae_callback_data;
}

static mrb_value
mrb_aeCreateFileEvent(mrb_state *mrb, mrb_value self)
{
  mrb_value sock = mrb_nil_value();
  mrb_int mask = AE_READABLE;
  mrb_value block = mrb_nil_value();

  mrb_get_args(mrb, "|oi&", &sock, &mask, &block);

  if (mrb_nil_p(block)) {
    mrb_raise(mrb, E_ARGUMENT_ERROR, "no block given");
  }

  int fd = 0;

  switch(mrb_type(sock)) {
    case MRB_TT_DATA: {
      mrb_value fd_val = mrb_Integer(mrb, sock);
      fd = mrb_fixnum(fd_val);
    } break;
    case MRB_TT_FIXNUM: {
      fd = mrb_fixnum(sock);
    } break;
    case MRB_TT_FALSE:
      break;
    default:
        mrb_raise(mrb, E_ARGUMENT_ERROR, "sock type not supported");
  }

  mrb_value mrb_ae_callback_data = mrb_ae_create_file_callback_data(mrb, self, sock, mask, block);

  errno = 0;
  int rc = aeCreateFileEvent((aeEventLoop *) DATA_PTR(self), fd, mask, mrb_aeFileProc, DATA_PTR(mrb_ae_callback_data));
  if (rc != AE_OK) {
    mrb_sys_fail(mrb, "aeCreateFileEvent");
  }

  return mrb_ae_callback_data;
}

static inline void
mrb_ae_delete_file_callback_data(mrb_state *mrb, mrb_value mrb_ae_callback_data, mrb_value self)
{
  mrb_value client_data = mrb_iv_remove(mrb, mrb_ae_callback_data, mrb_intern_lit(mrb, "@client_data"));
  int fd = 0;
  if (!mrb_undef_p(client_data)) {
    mrb_value fd_val = mrb_Integer(mrb, client_data);
    fd = mrb_fixnum(fd_val);
  }
  int mask = mrb_fixnum(mrb_iv_remove(mrb, mrb_ae_callback_data, mrb_intern_lit(mrb, "mask")));
  mrb_iv_remove(mrb, mrb_ae_callback_data, mrb_intern_lit(mrb, "@block"));
{'fraction_non_alphanumeric': 0.11666666666666667, 'fraction_numerical': 0.003216374269005848, 'mean_word_length': 5.174187725631769, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 13, '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': '290595', 'n_tokens_mistral': 2917, 'n_tokens_neox': 2860, 'n_words': 514}