/* :nodoc: */
static mrb_value Cache_init(mrb_state *mrb, mrb_value self)
{
  mrb_value o;
  mrb_get_args(mrb, "o", &o);
  lmc_check_dict(mrb, o);
  lmc_error_t e;
  rb_lmc_handle_t *h;

  local_memcache_t *l = local_memcache_create(rstring_ptr_null(mrb_hash_get(mrb, o, lmc_rb_sym_namespace(mrb))),
                                              rstring_ptr_null(mrb_hash_get(mrb, o, lmc_rb_sym_filename(mrb))),
                                              double_value(mrb, mrb_hash_get(mrb, o, lmc_rb_sym_size_mb(mrb))),
                                              size_t_value(mrb_hash_get(mrb, o, lmc_rb_sym_min_alloc_size(mrb))), &e);

  if (!l)
    rb_lmc_raise_exception(mrb, &e);

  h = (rb_lmc_handle_t *)DATA_PTR(self);
  if (h) {
    mrb_free(mrb, h);
  }
  DATA_TYPE(self) = &lmc_cache_type;
  DATA_PTR(self) = NULL;

  h = (rb_lmc_handle_t *)mrb_malloc(mrb, sizeof(rb_lmc_handle_t));
  if (!h) {
    mrb_raise(mrb, E_RUNTIME_ERROR, "memory allocation error");
  }

  h->lmc = l;
  h->open = 1;

  DATA_PTR(self) = h;

  return self;
}
/*
 * call-seq: Cache.drop(*args)
 *
 * Deletes a memory pool.  If the :force option is set, locked semaphores are
 * removed as well.
 *
 * WARNING: Do only call this method with the :force option if you are sure
 * that you really want to remove this memory pool and no more processes are
 * still using it.
 *
 * If you delete a pool and other processes still have handles open on it, the
 * status of these handles becomes undefined.  There's no way for a process to
 * know when a handle is not valid anymore, so only delete a memory pool if
 * you are sure that all handles are closed.
 *
 * valid options for drop are
 * [:namespace]
 * [:filename]
 * [:force]
 *
 * The memory pool must be specified by either setting the :filename or
 * :namespace option.  The default for :force is false.
 */
static mrb_value Cache__drop(mrb_state *mrb, mrb_value self)
{
  mrb_value o;
  mrb_get_args(mrb, "o", &o);
  lmc_check_dict(mrb, o);
  lmc_error_t e;
  if (!local_memcache_drop_namespace(rstring_ptr_null(mrb_hash_get(mrb, o, lmc_rb_sym_namespace(mrb))),
                                     rstring_ptr_null(mrb_hash_get(mrb, o, lmc_rb_sym_filename(mrb))),
                                     bool_value(mrb_hash_get(mrb, o, lmc_rb_sym_force(mrb))), &e)) {
    rb_lmc_raise_exception(mrb, &e);
  }
  return mrb_nil_value();
}
bool rubyval_to_ccvaluemapintkey(mrb_state* mrb, mrb_value arg, cocos2d::ValueMapIntKey* ret, const char* funcName)
{
    if (! mrb_hash_p(arg)) {
        return false;
    }
    
    mrb_value key_arr = mrb_hash_keys(mrb, arg);
    mrb_int len = mrb_ary_len(mrb, key_arr);
    ValueMapIntKey& dict = *ret;
    for (mrb_int i = 0; i < len; i++) {
        mrb_value hk = mrb_ary_ref(mrb, key_arr, i);
        mrb_value hv = mrb_hash_get(mrb, arg, hk);
        int int_key = 0;
        
        if (mrb_string_p(hk)) {
            char *kstr = mrb_str_to_cstr(mrb, hk);
            int_key = atoi(kstr);
        } else if (mrb_symbol_p(hk)) {
            mrb_sym sym = mrb_symbol(hk);
            const char* kstr = mrb_sym2name(mrb, sym);
            int_key = atoi(kstr);
        } else {
            return false;
        }
        Value val;
        if (! rubyval_to_ccvalue(mrb, hv, &val)) {
            return false;
        }
        
        dict[int_key] = val;
    }
    return true;
}
示例#4
0
/*
 *  call-seq:
 *     SerialPort.new(port)  # => SerialPort
 *
 *  Open serial port.
 *
 *  Parameters:
 *    +port+    font size.
 *       :default   Default serial port
 *       :uart      UART
 *       :bt        Bluetooth
 *
 *  Returns SerialPort object.
 */
static mrb_value
mrb_serial_init(mrb_state *mrb, mrb_value self)
{
  struct RClass *ser = mrb_obj_class(mrb, self);
  mrb_value pmap = mrb_const_get(mrb, mrb_obj_value(ser), mrb_intern_lit(mrb, "PORT"));
  mrb_sym port;
  mrb_value portv;
  FILE *fp;

  mrb_get_args(mrb, "n", &port);

  portv = mrb_hash_get(mrb, pmap, mrb_symbol_value(port));
  if (mrb_nil_p(portv)) {
    mrb_raisef(mrb, E_ARGUMENT_ERROR, "invalid port :%S", mrb_sym2str(mrb, port));
  }

  fp = ev3_serial_open_file((serial_port_t)mrb_fixnum(portv));
  if (fp == NULL) {
    mrb_raisef(mrb, E_IO_ERROR, "Serial port cannot open :%S", mrb_sym2str(mrb, port));
  }

  mrb_file_attach(self, fp);

  return self;
}
示例#5
0
/*
 *  call-seq:
 *     lcd.clear(col)  # => nil
 *
 *  Clear window.
 *
 *  Parameters:
 *    +col+     LCD foreground color.
 *       :black   black (default)
 *       :white   while
 *
 *  Returns nil.
 */
static mrb_value
mrb_lcd_clear(mrb_state *mrb, mrb_value self)
{
  mrb_lcd_t *plcd = (mrb_lcd_t*)DATA_PTR(self);
  struct RClass *lcd = mrb_obj_class(mrb, self);
  mrb_value cmap = mrb_const_get(mrb, mrb_obj_value(lcd), mrb_intern_lit(mrb, "COLOR"));
  mrb_sym col = mrb_intern_lit(mrb, "black");
  mrb_value colv;

  mrb_get_args(mrb, "|n", &col);

  colv = mrb_hash_get(mrb, cmap, mrb_symbol_value(col));
  if (mrb_nil_p(colv)) {
    mrb_raisef(mrb, E_ARGUMENT_ERROR, "invalid foreground color :%S", mrb_sym2str(mrb, col));
  }

  ev3_lcd_fill_rect(
    plcd->left,
    plcd->top,
    plcd->width,
    plcd->height,
    mrb_fixnum(colv) ? 0 : 1
  );
  plcd->cx = plcd->cy = 0;

  return mrb_nil_value();
}
bool rubyval_to_dictionary(mrb_state* mrb, mrb_value arg, __Dictionary** outValue, const char* funcName)
{
    if (! mrb_hash_p(arg)) {
        return false;
    }
    
    mrb_value key_arr = mrb_hash_keys(mrb, arg);
    mrb_int len = mrb_ary_len(mrb, key_arr);
    __Dictionary* dic = __Dictionary::create();
    for (mrb_int i = 0; i < len; i++) {
        mrb_value hk = mrb_ary_ref(mrb, key_arr, i);
        mrb_value hv = mrb_hash_get(mrb, arg, hk);
        if (mrb_string_p(hk)) {
            char *kstr = mrb_str_to_cstr(mrb, hk);
            Ref* ref = to_ref_value(mrb, hv);
            dic->setObject(ref, std::string(kstr));
        } else if (mrb_symbol_p(hk)) {
            mrb_sym sym = mrb_symbol(hk);
            const char* kstr = mrb_sym2name(mrb, sym);
            Ref* ref = to_ref_value(mrb, hv);
            dic->setObject(ref, std::string(kstr));
        } else {
            CCASSERT(false, "not supported key value type");
        }
    }
    *outValue = dic;
    
    return true;
}
示例#7
0
文件: hash.c 项目: denji/mruby
/*
 *  call-seq:
 *     hsh[key]    ->  value
 *
 *  Element Reference---Retrieves the <i>value</i> object corresponding
 *  to the <i>key</i> object. If not found, returns the default value (see
 *  <code>Hash::new</code> for details).
 *
 *     h = { "a" => 100, "b" => 200 }
 *     h["a"]   #=> 100
 *     h["c"]   #=> nil
 *
 */
static mrb_value
mrb_hash_aget(mrb_state *mrb, mrb_value self)
{
  mrb_value key;

  mrb_get_args(mrb, "o", &key);
  return mrb_hash_get(mrb, self, key);
}
示例#8
0
static mrb_value
mrb_value_to_string(mrb_state* mrb, mrb_value value) {
  mrb_value str;
  ARENA_SAVE;
  switch (mrb_type(value)) {
  case MRB_TT_FIXNUM:
  case MRB_TT_FLOAT:
  case MRB_TT_TRUE:
  case MRB_TT_FALSE:
  case MRB_TT_UNDEF:
    str = mrb_funcall(mrb, value, "to_s", 0, NULL);
    break;
  case MRB_TT_STRING:
    str = mrb_funcall(mrb, value, "inspect", 0, NULL);
    break;
  case MRB_TT_HASH:
    {
      str = mrb_str_new_cstr(mrb, "{");
      mrb_value keys = mrb_hash_keys(mrb, value);
      int n, l = RARRAY_LEN(keys);
      for (n = 0; n < l; n++) {
        mrb_value key = mrb_ary_entry(keys, n);
        mrb_value enckey = mrb_funcall(mrb, key, "to_s", 0, NULL);
        enckey = mrb_funcall(mrb, enckey, "inspect", 0, NULL);
        mrb_str_concat(mrb, str, enckey);
        mrb_str_cat2(mrb, str, ":");
        mrb_value obj = mrb_hash_get(mrb, value, key);
        mrb_str_concat(mrb, str, mrb_value_to_string(mrb, obj));
        if (n != l - 1) {
          mrb_str_cat2(mrb, str, ",");
        }
        ARENA_RESTORE;
      }
      mrb_str_cat2(mrb, str, "}");
      break;
    }
  case MRB_TT_ARRAY:
    {
      mrb_value str = mrb_str_new_cstr(mrb, "[");
      int n, l = RARRAY_LEN(value);
      for (n = 0; n < l; n++) {
        mrb_value obj = mrb_ary_entry(value, n);
        mrb_str_concat(mrb, str, mrb_value_to_string(mrb, obj));
        if (n != l - 1) {
          mrb_str_cat2(mrb, str, ",");
        }
        ARENA_RESTORE;
      }
      mrb_str_cat2(mrb, str, "]");
      break;
    }
  default:
    mrb_raise(mrb, E_ARGUMENT_ERROR, "invalid argument");
  }
  ARENA_RESTORE;
  return str;
}
示例#9
0
static mrb_value
mrb_ipvs_dest_init(mrb_state *mrb, mrb_value self){
  int parse;
  mrb_value arg_opt = mrb_nil_value(),
            addr = mrb_nil_value(),
            obj = mrb_nil_value();
  mrb_int port,
          weight;
  struct mrb_ipvs_entry *ie;

  ie = (struct mrb_ipvs_entry*)mrb_malloc(mrb, sizeof(*ie));
  memset(ie, 0, sizeof(struct mrb_ipvs_entry));

  mrb_get_args(mrb, "H", &arg_opt);
  if (mrb_nil_p(arg_opt)) mrb_raise(mrb, E_ARGUMENT_ERROR, "invalid argument");
  addr = mrb_hash_get(mrb, arg_opt, mrb_str_new_cstr(mrb, "addr"));
  if (mrb_nil_p(addr)) mrb_raise(mrb, E_ARGUMENT_ERROR, "invalid argument");
  obj = mrb_hash_get(mrb, arg_opt, mrb_str_new_cstr(mrb, "port"));
  port = mrb_nil_p(obj) ? 0 : mrb_fixnum(obj);
  if (port < 0 || port > 65535) mrb_raise(mrb, E_ARGUMENT_ERROR, "invalid port value specified"); 
  weight = mrb_fixnum(mrb_hash_get(mrb, arg_opt, mrb_str_new_cstr(mrb, "weight")));
  obj = mrb_hash_get(mrb, arg_opt, mrb_str_new_cstr(mrb, "weight"));
  weight = mrb_nil_p(obj) ? DEF_WEIGHT : mrb_fixnum(obj);
  if (weight < 0 || weight > INT_MAX) mrb_raise(mrb, E_ARGUMENT_ERROR, "invalid weight value specified"); 

  if (strrchr((char *) RSTRING_PTR(addr), ':') == NULL) ie->svc.port = htons(port);
  parse = parse_service((char *) RSTRING_PTR(addr), &ie->svc);
  if (!(parse & SERVICE_ADDR))
    mrb_raise(mrb, E_ARGUMENT_ERROR, "invalid argument");

  mrb_iv_set(mrb, self, mrb_intern_lit(mrb, "@service"), mrb_nil_value());

  ie->dest.af = ie->svc.af;
  ie->dest.addr = ie->svc.addr;
  ie->dest.port = ie->svc.port;
  ie->dest.weight = weight;

  DATA_TYPE(self) = &mrb_ipvs_dest_type;
  DATA_PTR(self) = ie;

  return self;
}
示例#10
0
文件: hash.c 项目: gaffo/mruby
mrb_value
mrb_hash_values_at(mrb_state *mrb, int argc, mrb_value *argv, mrb_value hash)
{
    mrb_value result = mrb_ary_new_capa(mrb, argc);
    long i;

    for (i=0; i<argc; i++) {
        mrb_ary_push(mrb, result, mrb_hash_get(mrb, hash, argv[i]));
    }
    return result;
}
示例#11
0
/*
 *  call-seq:
 *     LCD.new(font, x, y, width, height, color)  # => LCD
 *
 *  Creates LCD object.
 *
 *  Parameters:
 *    +font+    font size.
 *       :small   small font (6x8, default)
 *       :medium  medium font (8x16)
 *    +x+       Window origin (left) X coordinate. (default: 0)
 *    +y+       Window origin (top) Y coordinate. (default: 0)
 *    +width+   Width of window. (default: LCD::WIDTH)
 *    +height+  Height of window. (default: LCD::HEIGHT)
 *    +color+   LCD foreground color.
 *       :black   black (default)
 *       :white   while
 *
 *  Returns LCD object.
 */
static mrb_value
mrb_lcd_init(mrb_state *mrb, mrb_value self)
{
  struct RClass *lcd = mrb_obj_class(mrb, self);
  mrb_value fmap = mrb_const_get(mrb, mrb_obj_value(lcd), mrb_intern_lit(mrb, "FONT"));
  mrb_value cmap = mrb_const_get(mrb, mrb_obj_value(lcd), mrb_intern_lit(mrb, "COLOR"));
  mrb_sym font = mrb_intern_lit(mrb, "small");
  mrb_sym col = mrb_intern_lit(mrb, "black");
  mrb_value fontv;
  mrb_value colv;
  mrb_lcd_t *plcd;

  DATA_TYPE(self) = &mrb_lcd_type;
  plcd = (mrb_lcd_t*)mrb_malloc(mrb, sizeof(mrb_lcd_t));
  DATA_PTR(self) = plcd;
  memset(plcd, 0, sizeof(mrb_lcd_t));
  plcd->width  = EV3_LCD_WIDTH;
  plcd->height = EV3_LCD_HEIGHT;

  mrb_get_args(mrb, "|niiiin", &font, &plcd->left, &plcd->top, &plcd->width, &plcd->height, &col);

  fontv = mrb_hash_get(mrb, fmap, mrb_symbol_value(font));
  if (mrb_nil_p(fontv)) {
    mrb_raisef(mrb, E_ARGUMENT_ERROR, "invalid font size :%S", mrb_sym2str(mrb, font));
  }
  plcd->font = mrb_fixnum(fontv);
  if (_font_size[plcd->font].w == 0) {
    /* initialize font size at 1st time */
    ev3_font_get_size(plcd->font, &_font_size[plcd->font].w, &_font_size[plcd->font].h);
  }

  colv = mrb_hash_get(mrb, cmap, mrb_symbol_value(col));
  if (mrb_nil_p(colv)) {
    mrb_raisef(mrb, E_ARGUMENT_ERROR, "invalid foreground color :%S", mrb_sym2str(mrb, col));
  }
  plcd->color = mrb_fixnum(colv);

  return self;
}
示例#12
0
static ERL_NIF_TERM make_hash(ErlNifEnv* env, mrb_state* mrb, mrb_value o) {
  mrb_value keys = mrb_hash_keys(mrb, o);
  size_t len = (int) RARRAY(keys)->len;
  ERL_NIF_TERM list = enif_make_list_from_array(env, NULL, 0);

  for(int i = len; i>0; --i) {
    mrb_value k = mrb_ary_ref(mrb, keys, (mrb_int)i - 1);
    ERL_NIF_TERM key = mruby2erl(env, mrb, k);
    ERL_NIF_TERM value = mruby2erl(env, mrb, mrb_hash_get(mrb, o, k));
    list = enif_make_list_cell(env, enif_make_tuple2(env, key, value), list);
  }

  return enif_make_tuple1(env, list);
}
示例#13
0
static mrb_value
mrb_ipvs_service_init(mrb_state *mrb, mrb_value self){
  int parse;
  mrb_value arg_opt = mrb_nil_value(),
            addr = mrb_nil_value(),
            proto = mrb_nil_value(), 
            sched_name = mrb_nil_value(),
            ops = mrb_nil_value(),
            obj = mrb_nil_value();
  mrb_int port,
          timeout,
          netmask;
  struct mrb_ipvs_entry *ie;

  ie = (struct mrb_ipvs_entry*)mrb_malloc(mrb, sizeof(*ie));
  memset(ie, 0, sizeof(struct mrb_ipvs_entry));

  mrb_get_args(mrb, "H", &arg_opt);
  if (mrb_nil_p(arg_opt)) mrb_raise(mrb, E_ARGUMENT_ERROR, "invalid argument");

  addr = mrb_hash_get(mrb, arg_opt, mrb_str_new_cstr(mrb, "addr"));
  if (mrb_nil_p(addr)) mrb_raise(mrb, E_ARGUMENT_ERROR, "invalid argument");

  proto = mrb_hash_get(mrb, arg_opt, mrb_str_new_cstr(mrb, "protocol"));
  if (mrb_nil_p(proto)) proto = mrb_str_new_cstr(mrb, DEF_PROTO);

  sched_name = mrb_hash_get(mrb, arg_opt, mrb_str_new_cstr(mrb, "sched_name"));
  if (mrb_nil_p(sched_name)) sched_name = mrb_str_new_cstr(mrb, DEF_SCHED);

  obj = mrb_hash_get(mrb, arg_opt, mrb_str_new_cstr(mrb, "port"));
  port = mrb_nil_p(obj) ? 0 : mrb_fixnum(obj);
  if (port < 0 || port > 65535) mrb_raise(mrb, E_ARGUMENT_ERROR, "invalid argument"); 

  ops = mrb_hash_get(mrb, arg_opt, mrb_str_new_cstr(mrb, "ops"));
  timeout = mrb_fixnum(mrb_hash_get(mrb, arg_opt, mrb_str_new_cstr(mrb, "timeout")));
  netmask = mrb_fixnum(mrb_hash_get(mrb, arg_opt, mrb_str_new_cstr(mrb, "netmask")));

  ie->svc.protocol = parse_proto((char *) RSTRING_PTR(proto));

  parse = parse_service((char *) RSTRING_PTR(addr), &ie->svc);
  if (strrchr((char *) RSTRING_PTR(addr), ':') == NULL) ie->svc.port = htons(port);

  if (!(parse & SERVICE_ADDR))
    mrb_raise(mrb, E_ARGUMENT_ERROR, "invalid argument");

  strncpy(ie->svc.sched_name, (char *) RSTRING_PTR(sched_name), IP_VS_SCHEDNAME_MAXLEN);

  DATA_TYPE(self) = &mrb_ipvs_service_type;
  DATA_PTR(self) = ie;

  return self;
}
示例#14
0
文件: hash.c 项目: lemonhall/mruby
mrb_value
mrb_hash_rassoc(mrb_state *mrb, mrb_value hash)
{
  mrb_value key, value, has_key;

  mrb_get_args(mrb, "o", &key);
  has_key = mrb_hash_has_keyWithKey(mrb, hash, key);
  if (mrb_test(has_key)) {
    value = mrb_hash_get(mrb, hash, key);
    return mrb_assoc_new(mrb, value, key);
  }
  else {
    return mrb_nil_value();
  }
}
示例#15
0
/*
 *  call-seq:
 *     Button.new(key)  # => Button
 *
 *  Creates Button object.
 *
 *  Parameters:
 *    +key+     Button name
 *       :left    Left button
 *       :right   Right button
 *       :up      Up button
 *       :down    Down button
 *       :enter   Enter button
 *       :back    Back button
 *
 *  Returns Butotn object.
 */
static mrb_value
mrb_btn_init(mrb_state *mrb, mrb_value self)
{
  struct RClass *btn = mrb_obj_class(mrb, self);
  mrb_value map = mrb_const_get(mrb, mrb_obj_value(btn), mrb_intern_lit(mrb, "KEY"));
  mrb_sym key;
  mrb_value keyv;

  mrb_get_args(mrb, "n", &key);
  keyv = mrb_hash_get(mrb, map, mrb_symbol_value(key));

  mrb_iv_set(mrb, self, mrb_intern_lit(mrb, "@key"), keyv);

  return self;
}
示例#16
0
文件: hash-ext.c 项目: nobu/mruby
static mrb_value
hash_values_at(mrb_state *mrb, mrb_value hash)
{
  mrb_value *argv, result;
  mrb_int argc, i;
  int ai;

  mrb_get_args(mrb, "*!", &argv, &argc);
  result = mrb_ary_new_capa(mrb, argc);
  ai = mrb_gc_arena_save(mrb);
  for (i = 0; i < argc; i++) {
    mrb_ary_push(mrb, result, mrb_hash_get(mrb, hash, argv[i]));
    mrb_gc_arena_restore(mrb, ai);
  }
  return result;
}
示例#17
0
std::map<std::string, std::string> MRuby::invert(const std::map<std::string, std::string> &lis) const {
    std::map<std::string, std::string> res;

    mrb_value a = mrb_hash_new_capa(mMrb, lis.size());
    for (auto it = lis.begin(); it != lis.end(); ++it) {
        mrb_hash_set(mMrb, a, mrb_str_new_cstr(mMrb, it->first.c_str()), mrb_str_new_cstr(mMrb, it->second.c_str()));
    }
    mrb_value r = mrb_funcall(mMrb, mrb_top_self(mMrb), "invert", 1, a);
    mrb_value keys = mrb_hash_keys(mMrb, r);
    for (int i = 0; i < RARRAY_LEN(keys); ++i) {
        mrb_value k = mrb_ary_ref(mMrb, keys, i);
        mrb_value v = mrb_hash_get(mMrb, r, k);
        res[mrb_str_to_cstr(mMrb, k)] = mrb_str_to_cstr(mMrb, v);
    }
    return res;
}
示例#18
0
/*
 *  call-seq:
 *     LED.color = col  # => nil
 *
 *  Light LED.
 *
 *  Parameters:
 *    +col+     LED color
 *       LED::OFF
 *       LED::RED
 *       LED::GREEN
 *       LED::ORANGE
 *
 *  Returns nil.
 */
static mrb_value
mrb_led_color(mrb_state *mrb, mrb_value self)
{
  mrb_value cmap = mrb_const_get(mrb, self, mrb_intern_lit(mrb, "COLOR"));
  mrb_sym col;
  mrb_value colv;

  mrb_get_args(mrb, "n", &col);
  colv = mrb_hash_get(mrb, cmap, mrb_symbol_value(col));
  if (mrb_nil_p(colv)) {
    mrb_raisef(mrb, E_ARGUMENT_ERROR, "invalid color :%S", mrb_sym2str(mrb, col));
  }

  ev3_led_set_color(mrb_fixnum(colv));
  return mrb_nil_value();
}
示例#19
0
static mrb_value mrb_vedis_append_hash(mrb_state *mrb, mrb_value self)
{
    int ai;
    vedis *vstore = DATA_PTR(self);
    mrb_value hash, keys, key, val;

    mrb_get_args(mrb, "H", &hash);
    keys = mrb_hash_keys(mrb, hash);
    ai = mrb_gc_arena_save(mrb);
    while (!mrb_nil_p(key = mrb_ary_pop(mrb, keys))) {
        val = mrb_hash_get(mrb, hash, key);
        mrb_vedis_append_s(mrb, key, val, vstore);
        mrb_gc_arena_restore(mrb, ai);
    }
    return mrb_true_value();
}
示例#20
0
/*
 *  call-seq:
 *     LCD.font = font  # => nil
 *
 *  Set the current font.
 *
 *  Parameters:
 *    +font+    font size.
 *       :small     small font
 *       :medium    medium font (8x16)
 *
 *  Returns nil.
 */
static mrb_value
mrb_lcd_set_font(mrb_state *mrb, mrb_value self)
{
  mrb_value fmap = mrb_const_get(mrb, self, mrb_intern_lit(mrb, "FONT"));
  mrb_sym font;
  mrb_value fontv;

  mrb_get_args(mrb, "n", &font);
  fontv = mrb_hash_get(mrb, fmap, mrb_symbol_value(font));
  if (mrb_nil_p(fontv)) {
    mrb_raisef(mrb, E_ARGUMENT_ERROR, "invalid font size :%S", mrb_sym2str(mrb, font));
  }

  ev3_lcd_set_font(mrb_fixnum(fontv));
  mrb_cv_set(mrb, self, mrb_intern_lit(mrb, "@@font"), fontv);

  return mrb_nil_value();
}
示例#21
0
文件: hash.c 项目: lemonhall/mruby
mrb_value
mrb_hash_assoc(mrb_state *mrb, mrb_value hash)
{
  mrb_value key, value, has_key;

  mrb_get_args(mrb, "o", &key);
  if (mrb_nil_p(key))
    mrb_raise(mrb, E_ARGUMENT_ERROR, "wrong number of arguments");

  has_key = mrb_hash_has_keyWithKey(mrb, hash, key);
  if (mrb_test(has_key)) {
    value = mrb_hash_get(mrb, hash, key);
    return mrb_assoc_new(mrb, key, value);
  }
  else {
    return mrb_nil_value();
  }
}
示例#22
0
static mrb_value mrb_ipvs_service_init(mrb_state *mrb, mrb_value self) {
  int parse;
  mrb_value arg_opt = mrb_nil_value(), addr = mrb_nil_value(),
            proto = mrb_nil_value(), sched_name = mrb_nil_value(),
            obj = mrb_nil_value();
  // mrb_value arg_opt = mrb_nil_value(), addr = mrb_nil_value(),
  //           proto = mrb_nil_value(), sched_name = mrb_nil_value(),
  //           ops = mrb_nil_value(), obj = mrb_nil_value();
  mrb_int port;
  // mrb_int port, timeout, netmask;
  struct mrb_ipvs_service *ie;

  ie = (struct mrb_ipvs_service *)mrb_malloc(mrb, sizeof(*ie));
  memset(ie, 0, sizeof(struct mrb_ipvs_service));

  mrb_get_args(mrb, "H", &arg_opt);

  if (mrb_nil_p(arg_opt)) {
    mrb_raise(mrb, E_ARGUMENT_ERROR, "invalid argument");
  }

  addr = mrb_hash_get(mrb, arg_opt, mrb_str_new_cstr(mrb, "addr"));
  if (mrb_nil_p(addr)) {
    mrb_raise(mrb, E_ARGUMENT_ERROR, "invalid argument");
  }

  obj = mrb_hash_get(mrb, arg_opt, mrb_str_new_cstr(mrb, "port"));
  port = mrb_nil_p(obj) ? 0 : mrb_fixnum(obj);
  if (port < 0 || port > 65535) {
    mrb_raise(mrb, E_ARGUMENT_ERROR, "invalid port value specified");
  }

  proto = mrb_hash_get(mrb, arg_opt, mrb_str_new_cstr(mrb, "protocol"));
  if (mrb_nil_p(proto)) {
    proto = mrb_str_new_cstr(mrb, DEF_PROTO);
  }

  sched_name = mrb_hash_get(mrb, arg_opt, mrb_str_new_cstr(mrb, "sched_name"));
  if (mrb_nil_p(sched_name)) {
    sched_name = mrb_str_new_cstr(mrb, DEF_SCHED);
  }

  // ops = mrb_hash_get(mrb, arg_opt, mrb_str_new_cstr(mrb, "ops"));
  // timeout =
  //     mrb_fixnum(mrb_hash_get(mrb, arg_opt, mrb_str_new_cstr(mrb,
  //     "timeout")));
  // netmask =
  //     mrb_fixnum(mrb_hash_get(mrb, arg_opt, mrb_str_new_cstr(mrb,
  //     "netmask")));

  ie->svc.protocol = parse_proto((char *)RSTRING_PTR(proto));

  parse = parse_service((char *)RSTRING_PTR(addr), &ie->svc);
  if (strrchr((char *)RSTRING_PTR(addr), ':') == NULL) {
    ie->svc.port = htons(port);
  }

  if (!(parse & SERVICE_ADDR)) {
    mrb_raise(mrb, E_ARGUMENT_ERROR, "invalid argument");
  }

  strncpy(ie->svc.sched_name, (char *)RSTRING_PTR(sched_name),
          IP_VS_SCHEDNAME_MAXLEN);

  mrb_data_init(self, ie, &mrb_ipvs_service_type);

  return self;
}
示例#23
0
static mrb_value sysctl_net_list(mrb_state *mrb, mrb_value self)
{
  const char *ifname = NULL;
  struct ifaddrs *addrs, *curr;
  mrb_value r_ret;
  struct RClass *if_class = mrb_class_get(mrb, "NetworkAddress");
  
  mrb_get_args(mrb, "|z", &ifname);
  
  if( getifaddrs(&addrs) == -1 ){
    mrb_raisef(mrb, E_RUNTIME_ERROR, "getifaddrs: %S", mrb_str_new_cstr(mrb, strerror(errno)));
    goto ret;
  }
  
  if( ifname ){
    r_ret = mrb_ary_new(mrb);
  }
  else {
    r_ret = mrb_hash_new(mrb);
  }
  
  curr = addrs;
  
  while( curr != NULL ){
    char addr[INET6_ADDRSTRLEN];
    char netmask[INET6_ADDRSTRLEN];
    
    if( curr->ifa_netmask != NULL ){
      mrb_value r_key, r_addr, r_list;
      
      // only return wanted infos
      if( !ifname || !strcmp(ifname, curr->ifa_name) ){
        if( sockaddr_to_string(curr->ifa_addr, addr, sizeof(addr)) == -1)
          continue;
        
        if( sockaddr_to_string(curr->ifa_netmask, netmask, sizeof(netmask)) == -1)
          continue;
        
        r_key = mrb_str_new_cstr(mrb, curr->ifa_name);
        r_addr = mrb_funcall(mrb, mrb_obj_value(if_class), "new", 2,
            mrb_str_new_cstr(mrb, addr),
            mrb_str_new_cstr(mrb, netmask)
          );
        
        if( ifname ){
          mrb_ary_push(mrb, r_ret, r_addr);
        }
        else {
          // check if key exists
          r_list = mrb_hash_get(mrb, r_ret, r_key);
          if( mrb_nil_p(r_list) ){
            r_list = mrb_ary_new(mrb);
          }
          
          mrb_ary_push(mrb, r_list, r_addr);
          
          mrb_hash_set(mrb, r_ret, r_key, r_list);
        }
      }
    }
    
    curr = curr->ifa_next;
  }

ret:
  return r_ret;
}
示例#24
0
文件: nhash.c 项目: christiank/libn
char *
nstringhash_get(const nstringhash *hash, const char *key)
{
  return mrb_str_to_cstr(__LIBN_R,
    mrb_hash_get(__LIBN_R, hash->_hash, mrb_str_new_cstr(__LIBN_R, key)));
}
示例#25
0
文件: nhash.c 项目: christiank/libn
void *
nhash_get(const nhash *hash, const char *key)
{
  return mrb_hash_get(__LIBN_R,
    hash->_hash, mrb_str_new_cstr(__LIBN_R, key)).value.p;
}
示例#26
0
static mrb_value
mrb_curl_get(mrb_state *mrb, mrb_value self)
{
  char error[CURL_ERROR_SIZE] = {0};
  CURL* curl;
  CURLcode res = CURLE_OK;
  MEMFILE* mf;
  struct RClass* _class_curl;
  int ssl_verifypeer;
  struct curl_slist* headerlist = NULL;
  mrb_value str;
  struct RClass* _class_http;
  struct RClass* _class_http_parser;
  mrb_value parser;
  mrb_value args[1];

  mrb_value url = mrb_nil_value();
  mrb_value headers = mrb_nil_value();
  mrb_value b = mrb_nil_value();
  mrb_get_args(mrb, "S|H&", &url, &headers, &b);

  if (!mrb_nil_p(headers) && mrb_type(headers) != MRB_TT_HASH) {
    mrb_raise(mrb, E_ARGUMENT_ERROR, "invalid argument");
  }
  mf = memfopen();
  curl = curl_easy_init();
  _class_curl = mrb_class_get(mrb, "Curl");
  ssl_verifypeer = mrb_fixnum(mrb_const_get(mrb, mrb_obj_value(_class_curl), mrb_intern_cstr(mrb, "SSL_VERIFYPEER")));
  curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, ssl_verifypeer);
  curl_easy_setopt(curl, CURLOPT_URL, RSTRING_PTR(url));
  curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, error);
  curl_easy_setopt(curl, CURLOPT_WRITEDATA, mf);
  if (mrb_nil_p(b)) {
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, memfwrite);
  } else {
    mf->mrb = mrb;
    mf->proc = b;
    mf->header = mrb_nil_value();
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, memfwrite_callback);
  }
  curl_easy_setopt(curl, CURLOPT_HEADERDATA, mf);
  curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, memfwrite);
  curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 0);

  if (!mrb_nil_p(headers)) {
    mrb_value keys = mrb_hash_keys(mrb, headers);
    int i, l = RARRAY_LEN(keys);
    for (i = 0; i < l; i++) {
      mrb_value key = mrb_ary_entry(keys, i);
      mrb_value header = mrb_str_dup(mrb, key);
      mrb_str_cat2(mrb, header, ": ");
      mrb_str_concat(mrb, header, mrb_hash_get(mrb, headers, key));
      headerlist = curl_slist_append(headerlist, RSTRING_PTR(header));
    }
    curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerlist);
  }

  res = curl_easy_perform(curl);

  if (headerlist)
    curl_slist_free_all(headerlist);

  curl_easy_cleanup(curl);
  if (res != CURLE_OK) {
    mrb_raise(mrb, E_RUNTIME_ERROR, error);
  }
  if (!mrb_nil_p(b)) {
    return mrb_nil_value();
  }

  str = mrb_str_new(mrb, mf->data, mf->size);
  memfclose(mf);

  _class_http = mrb_class_get(mrb, "HTTP");
  _class_http_parser = mrb_class_ptr(mrb_const_get(mrb, mrb_obj_value(_class_http), mrb_intern_cstr(mrb, "Parser")));
  parser = mrb_obj_new(mrb, _class_http_parser, 0, NULL);
  args[0] = str;
  return mrb_funcall_argv(mrb, parser, mrb_intern_cstr(mrb, "parse_response"), 1, args);
}
示例#27
0
文件: hash.c 项目: gaffo/mruby
mrb_value
mrb_hash_lookup(mrb_state *mrb, mrb_value hash, mrb_value key)
{
  return mrb_hash_get(mrb, hash, key);
}
示例#28
0
文件: yaml.c 项目: hone/mruby-yaml
int value_to_node(mrb_state *mrb,
  yaml_document_t *document, mrb_value value)
{
  int node;

  switch (mrb_type(value))
  {
    case MRB_TT_ARRAY:
    {
      mrb_int len = mrb_ary_len(mrb, value);
      mrb_int i;
      int ai = mrb_gc_arena_save(mrb);

      node = yaml_document_add_sequence(document, NULL,
        YAML_ANY_SEQUENCE_STYLE);

      for (i = 0; i < len; i++)
      {
        mrb_value child = mrb_ary_ref(mrb, value, i);
        int child_node = value_to_node(mrb, document, child);

        /* Add the child to the sequence */
        yaml_document_append_sequence_item(document, node, child_node);
        mrb_gc_arena_restore(mrb, ai);
      }

      break;
    }

    case MRB_TT_HASH:
    {
      /* Iterating a list of keys is slow, but it only
       * requires use of the interface defined in `hash.h`.
       */

      mrb_value keys = mrb_hash_keys(mrb, value);
      mrb_int len = mrb_ary_len(mrb, keys);
      mrb_int i;
      int ai = mrb_gc_arena_save(mrb);

      node = yaml_document_add_mapping(document, NULL,
        YAML_ANY_MAPPING_STYLE);

      for (i = 0; i < len; i++)
      {
        mrb_value key = mrb_ary_ref(mrb, keys, i);
        mrb_value child = mrb_hash_get(mrb, value, key);

        int key_node = value_to_node(mrb, document, key);
        int child_node = value_to_node(mrb, document, child);

        /* Add the key/value pair to the mapping */
        yaml_document_append_mapping_pair(document, node,
          key_node, child_node);
        mrb_gc_arena_restore(mrb, ai);
      }

      break;
    }

    default:
    {
      if (mrb_nil_p(value)) {
        /* http://yaml.org/type/null.html
           Canonical form */
        value = mrb_str_new_lit(mrb, "~");
      } else {
        /* Equivalent to `obj = obj#to_s` */
        value = mrb_obj_as_string(mrb, value);
      }
      /* Fallthrough */
    }

    case MRB_TT_STRING:
    {
      yaml_scalar_style_t style = YAML_ANY_SCALAR_STYLE;
      if (RSTRING_LEN(value) == 0) {
        /* If the String is empty, it may be reloaded as a nil instead of an
         * empty string, to avoid that place a quoted string instead */
        style = YAML_SINGLE_QUOTED_SCALAR_STYLE;
      }
      yaml_char_t *value_chars = (unsigned char *) RSTRING_PTR(value);
      node = yaml_document_add_scalar(document, NULL,
        value_chars, RSTRING_LEN(value), style);
      break;
    }
  }

  return node;
}
示例#29
0
static mrb_value http_request_method(mrb_state *mrb, mrb_value self)
{
    h2o_mruby_generator_t *generator;
    struct st_h2o_mruby_http_request_context_t *ctx;
    const char *arg_url;
    mrb_int arg_url_len;
    mrb_value arg_hash;
    h2o_iovec_t method;
    h2o_url_t url;

    /* parse args */
    arg_hash = mrb_nil_value();
    mrb_get_args(mrb, "s|H", &arg_url, &arg_url_len, &arg_hash);

    /* precond check */
    if ((generator = h2o_mruby_current_generator) == NULL || generator->req == NULL)
        mrb_exc_raise(mrb, create_downstream_closed_exception(mrb));

    /* allocate context and initialize */
    ctx = h2o_mem_alloc_shared(&generator->req->pool, sizeof(*ctx), on_dispose);
    memset(ctx, 0, sizeof(*ctx));
    ctx->generator = generator;
    ctx->receiver = mrb_nil_value();
    h2o_buffer_init(&ctx->req.buf, &h2o_socket_buffer_prototype);
    h2o_buffer_init(&ctx->resp.after_closed, &h2o_socket_buffer_prototype);
    ctx->refs.request = mrb_nil_value();
    ctx->refs.input_stream = mrb_nil_value();

    /* uri */
    if (h2o_url_parse(arg_url, arg_url_len, &url) != 0)
        mrb_raise(mrb, E_ARGUMENT_ERROR, "invaild URL");

    /* method */
    method = h2o_iovec_init(H2O_STRLIT("GET"));
    if (mrb_hash_p(arg_hash)) {
        mrb_value t = mrb_hash_get(mrb, arg_hash, mrb_symbol_value(generator->ctx->symbols.sym_method));
        if (!mrb_nil_p(t)) {
            t = mrb_str_to_str(mrb, t);
            method = h2o_iovec_init(RSTRING_PTR(t), RSTRING_LEN(t));
        }
    }

    /* start building the request */
    h2o_buffer_reserve(&ctx->req.buf, method.len + 1);
    append_to_buffer(&ctx->req.buf, method.base, method.len);
    append_to_buffer(&ctx->req.buf, H2O_STRLIT(" "));
    h2o_buffer_reserve(&ctx->req.buf,
                       url.path.len + url.authority.len + sizeof(" HTTP/1.1\r\nConnection: close\r\nHost: \r\n") - 1);
    append_to_buffer(&ctx->req.buf, url.path.base, url.path.len);
    append_to_buffer(&ctx->req.buf, H2O_STRLIT(" HTTP/1.1\r\nConnection: close\r\nHost: "));
    append_to_buffer(&ctx->req.buf, url.authority.base, url.authority.len);
    append_to_buffer(&ctx->req.buf, H2O_STRLIT("\r\n"));

    /* headers */
    if (mrb_hash_p(arg_hash)) {
        mrb_value headers = mrb_hash_get(mrb, arg_hash, mrb_symbol_value(generator->ctx->symbols.sym_headers));
        if (!mrb_nil_p(headers)) {
            if (h2o_mruby_iterate_headers(generator->ctx, headers, flatten_request_header, ctx) != 0) {
                mrb_value exc = mrb_obj_value(mrb->exc);
                mrb->exc = NULL;
                mrb_exc_raise(mrb, exc);
            }
        }
    }
    /* body */
    if (mrb_hash_p(arg_hash)) {
        mrb_value body = mrb_hash_get(mrb, arg_hash, mrb_symbol_value(generator->ctx->symbols.sym_body));
        if (!mrb_nil_p(body)) {
            if (mrb_obj_eq(mrb, body, generator->rack_input)) {
                /* fast path */
                mrb_int pos;
                mrb_input_stream_get_data(mrb, body, NULL, NULL, &pos, NULL, NULL);
                ctx->req.body = generator->req->entity;
                ctx->req.body.base += pos;
                ctx->req.body.len -= pos;
            } else {
                if (!mrb_string_p(body)) {
                    body = mrb_funcall(mrb, body, "read", 0);
                    if (!mrb_string_p(body))
                        mrb_raise(mrb, E_ARGUMENT_ERROR, "body.read did not return string");
                }
                ctx->req.body = h2o_strdup(&ctx->generator->req->pool, RSTRING_PTR(body), RSTRING_LEN(body));
            }
            if (!ctx->req.has_transfer_encoding) {
                char buf[64];
                size_t l = (size_t)sprintf(buf, "content-length: %zu\r\n", ctx->req.body.len);
                h2o_buffer_reserve(&ctx->req.buf, l);
                append_to_buffer(&ctx->req.buf, buf, l);
            }
        }
    }

    h2o_buffer_reserve(&ctx->req.buf, 2);
    append_to_buffer(&ctx->req.buf, H2O_STRLIT("\r\n"));

    /* build request and connect */
    h2o_http1client_connect(&ctx->client, ctx, &generator->req->conn->ctx->proxy.client_ctx, url.host, h2o_url_get_port(&url),
                            on_connect);

    ctx->refs.request = h2o_mruby_create_data_instance(mrb, mrb_ary_entry(generator->ctx->constants, H2O_MRUBY_HTTP_REQUEST_CLASS),
                                                       ctx, &request_type);
    return ctx->refs.request;
}
示例#30
0
void
mrb_mruby_wslay_gem_init(mrb_state* mrb) {
  struct RClass *wslay_mod, *wslay_error_cl, *wslay_event_mod,
  *wslay_event_context_cl, *wslay_event_context_server_cl, *wslay_event_context_client_cl;

  wslay_mod = mrb_define_module(mrb, "Wslay");
  mrb_define_module_function(mrb, wslay_mod, "get_rsv1", mrb_wslay_get_rsv1, MRB_ARGS_REQ(1));
  mrb_define_module_function(mrb, wslay_mod, "get_rsv2", mrb_wslay_get_rsv2, MRB_ARGS_REQ(1));
  mrb_define_module_function(mrb, wslay_mod, "get_rsv3", mrb_wslay_get_rsv3, MRB_ARGS_REQ(1));
  wslay_error_cl = mrb_define_class_under(mrb, wslay_mod, "Err", E_RUNTIME_ERROR);
  mrb_value wslay_error_hash = mrb_hash_new_capa(mrb, 9 * 2);
  mrb_define_const(mrb, wslay_mod, "Error", wslay_error_hash);
  mrb_hash_set(mrb, wslay_error_hash, mrb_fixnum_value(WSLAY_ERR_WANT_READ), mrb_symbol_value(mrb_intern_lit(mrb, "want_read")));
  mrb_hash_set(mrb, wslay_error_hash, mrb_fixnum_value(WSLAY_ERR_WANT_WRITE), mrb_symbol_value(mrb_intern_lit(mrb, "want_write")));
  mrb_hash_set(mrb, wslay_error_hash, mrb_fixnum_value(WSLAY_ERR_PROTO), mrb_symbol_value(mrb_intern_lit(mrb, "proto")));
  mrb_hash_set(mrb, wslay_error_hash, mrb_fixnum_value(WSLAY_ERR_INVALID_ARGUMENT), mrb_symbol_value(mrb_intern_lit(mrb, "invalid_argument")));
  mrb_hash_set(mrb, wslay_error_hash, mrb_fixnum_value(WSLAY_ERR_INVALID_CALLBACK), mrb_symbol_value(mrb_intern_lit(mrb, "invalid_callback")));
  mrb_hash_set(mrb, wslay_error_hash, mrb_fixnum_value(WSLAY_ERR_NO_MORE_MSG), mrb_symbol_value(mrb_intern_lit(mrb, "no_more_msg")));
  mrb_hash_set(mrb, wslay_error_hash, mrb_fixnum_value(WSLAY_ERR_CALLBACK_FAILURE), mrb_symbol_value(mrb_intern_lit(mrb, "callback_failure")));
  mrb_hash_set(mrb, wslay_error_hash, mrb_fixnum_value(WSLAY_ERR_WOULDBLOCK), mrb_symbol_value(mrb_intern_lit(mrb, "wouldblock")));
  mrb_hash_set(mrb, wslay_error_hash, mrb_fixnum_value(WSLAY_ERR_NOMEM), mrb_symbol_value(mrb_intern_lit(mrb, "nomem")));
  mrb_value wslay_error_hash_keys = mrb_hash_keys(mrb, wslay_error_hash);
  for (mrb_int i = 0; i < RARRAY_LEN(wslay_error_hash_keys); i++) {
    mrb_value key = mrb_ary_ref(mrb, wslay_error_hash_keys, i);
    mrb_hash_set(mrb, wslay_error_hash,
      mrb_hash_get(mrb, wslay_error_hash, key), key);
  }

  mrb_value wslay_status_code_hash = mrb_hash_new_capa(mrb, 12 * 2);
  mrb_define_const(mrb, wslay_mod, "StatusCode", wslay_status_code_hash);
  mrb_hash_set(mrb, wslay_status_code_hash, mrb_fixnum_value(WSLAY_CODE_NORMAL_CLOSURE), mrb_symbol_value(mrb_intern_lit(mrb, "normal_closure")));
  mrb_hash_set(mrb, wslay_status_code_hash, mrb_fixnum_value(WSLAY_CODE_GOING_AWAY), mrb_symbol_value(mrb_intern_lit(mrb, "going_away")));
  mrb_hash_set(mrb, wslay_status_code_hash, mrb_fixnum_value(WSLAY_CODE_PROTOCOL_ERROR), mrb_symbol_value(mrb_intern_lit(mrb, "protocol_error")));
  mrb_hash_set(mrb, wslay_status_code_hash, mrb_fixnum_value(WSLAY_CODE_UNSUPPORTED_DATA), mrb_symbol_value(mrb_intern_lit(mrb, "unsupported_data")));
  mrb_hash_set(mrb, wslay_status_code_hash, mrb_fixnum_value(WSLAY_CODE_NO_STATUS_RCVD), mrb_symbol_value(mrb_intern_lit(mrb, "no_status_rcvd")));
  mrb_hash_set(mrb, wslay_status_code_hash, mrb_fixnum_value(WSLAY_CODE_ABNORMAL_CLOSURE), mrb_symbol_value(mrb_intern_lit(mrb, "abnormal_closure")));
  mrb_hash_set(mrb, wslay_status_code_hash, mrb_fixnum_value(WSLAY_CODE_INVALID_FRAME_PAYLOAD_DATA), mrb_symbol_value(mrb_intern_lit(mrb, "invalid_frame_payload_data")));
  mrb_hash_set(mrb, wslay_status_code_hash, mrb_fixnum_value(WSLAY_CODE_POLICY_VIOLATION), mrb_symbol_value(mrb_intern_lit(mrb, "policy_violation")));
  mrb_hash_set(mrb, wslay_status_code_hash, mrb_fixnum_value(WSLAY_CODE_MESSAGE_TOO_BIG), mrb_symbol_value(mrb_intern_lit(mrb, "message_too_big")));
  mrb_hash_set(mrb, wslay_status_code_hash, mrb_fixnum_value(WSLAY_CODE_MANDATORY_EXT), mrb_symbol_value(mrb_intern_lit(mrb, "mandatory_ext")));
  mrb_hash_set(mrb, wslay_status_code_hash, mrb_fixnum_value(WSLAY_CODE_INTERNAL_SERVER_ERROR), mrb_symbol_value(mrb_intern_lit(mrb, "internal_server_error")));
  mrb_hash_set(mrb, wslay_status_code_hash, mrb_fixnum_value(WSLAY_CODE_TLS_HANDSHAKE), mrb_symbol_value(mrb_intern_lit(mrb, "tls_handshake")));
  mrb_value wslay_status_code_hash_keys = mrb_hash_keys(mrb, wslay_status_code_hash);
  for (mrb_int i = 0; i < RARRAY_LEN(wslay_status_code_hash_keys); i++) {
    mrb_value key = mrb_ary_ref(mrb, wslay_status_code_hash_keys, i);
    mrb_hash_set(mrb, wslay_status_code_hash,
      mrb_hash_get(mrb, wslay_status_code_hash, key), key);
  }

  mrb_value io_flags_hash = mrb_hash_new_capa(mrb, 2);
  mrb_define_const(mrb, wslay_mod, "IoFlags", io_flags_hash);
  mrb_hash_set(mrb, io_flags_hash, mrb_fixnum_value(WSLAY_MSG_MORE), mrb_symbol_value(mrb_intern_lit(mrb, "msg_more")));
  mrb_hash_set(mrb, io_flags_hash, mrb_symbol_value(mrb_intern_lit(mrb, "msg_more")), mrb_fixnum_value(WSLAY_MSG_MORE));

  mrb_value wslay_opcode_hash = mrb_hash_new_capa(mrb, 6 * 2);
  mrb_define_const(mrb, wslay_mod, "OpCode", wslay_opcode_hash);
  mrb_hash_set(mrb, wslay_opcode_hash, mrb_fixnum_value(WSLAY_CONTINUATION_FRAME), mrb_symbol_value(mrb_intern_lit(mrb, "continuation_frame")));
  mrb_hash_set(mrb, wslay_opcode_hash, mrb_fixnum_value(WSLAY_TEXT_FRAME), mrb_symbol_value(mrb_intern_lit(mrb, "text_frame")));
  mrb_hash_set(mrb, wslay_opcode_hash, mrb_fixnum_value(WSLAY_BINARY_FRAME), mrb_symbol_value(mrb_intern_lit(mrb, "binary_frame")));
  mrb_hash_set(mrb, wslay_opcode_hash, mrb_fixnum_value(WSLAY_CONNECTION_CLOSE), mrb_symbol_value(mrb_intern_lit(mrb, "connection_close")));
  mrb_hash_set(mrb, wslay_opcode_hash, mrb_fixnum_value(WSLAY_PING), mrb_symbol_value(mrb_intern_lit(mrb, "ping")));
  mrb_hash_set(mrb, wslay_opcode_hash, mrb_fixnum_value(WSLAY_PONG), mrb_symbol_value(mrb_intern_lit(mrb, "pong")));
  mrb_value wslay_opcode_hash_keys = mrb_hash_keys(mrb, wslay_opcode_hash);
  for (mrb_int i = 0; i < RARRAY_LEN(wslay_opcode_hash_keys); i++) {
    mrb_value key = mrb_ary_ref(mrb, wslay_opcode_hash_keys, i);
    mrb_hash_set(mrb, wslay_opcode_hash,
      mrb_hash_get(mrb, wslay_opcode_hash, key), key);
  }

  wslay_event_mod = mrb_define_module_under(mrb, wslay_mod, "Event");
  wslay_event_context_cl = mrb_define_class_under(mrb, wslay_event_mod, "Context", mrb->object_class);
  MRB_SET_INSTANCE_TT(wslay_event_context_cl, MRB_TT_DATA);
  mrb_define_method(mrb, wslay_event_context_cl, "no_buffering=",         mrb_wslay_event_config_set_no_buffering,        MRB_ARGS_REQ(1));
  mrb_define_method(mrb, wslay_event_context_cl, "max_recv_msg_length=",  mrb_wslay_event_config_set_max_recv_msg_length, MRB_ARGS_REQ(1));
  mrb_define_method(mrb, wslay_event_context_cl, "recv",                  mrb_wslay_event_recv,                           MRB_ARGS_NONE());
  mrb_define_method(mrb, wslay_event_context_cl, "send",                  mrb_wslay_event_send,                           MRB_ARGS_NONE());
  mrb_define_method(mrb, wslay_event_context_cl, "queue_msg",             mrb_wslay_event_queue_msg,                      MRB_ARGS_REQ(2));
  mrb_define_method(mrb, wslay_event_context_cl, "queue_close",           mrb_wslay_event_queue_close,                    MRB_ARGS_ARG(1, 1));
  mrb_define_method(mrb, wslay_event_context_cl, "want_read?",            mrb_wslay_event_want_read,                      MRB_ARGS_NONE());
  mrb_define_method(mrb, wslay_event_context_cl, "want_write?",           mrb_wslay_event_want_write,                     MRB_ARGS_NONE());
  mrb_define_method(mrb, wslay_event_context_cl, "close_received?",       mrb_wslay_event_get_close_received,             MRB_ARGS_NONE());
  mrb_define_method(mrb, wslay_event_context_cl, "close_sent?",           mrb_wslay_event_get_close_sent,                 MRB_ARGS_NONE());
  mrb_define_method(mrb, wslay_event_context_cl, "status_code_received",  mrb_wslay_event_get_status_code_received,       MRB_ARGS_NONE());
  mrb_define_method(mrb, wslay_event_context_cl, "status_code_sent",      mrb_wslay_event_get_status_code_sent,           MRB_ARGS_NONE());
  mrb_define_method(mrb, wslay_event_context_cl, "queued_msg_count",      mrb_wslay_event_get_queued_msg_count,           MRB_ARGS_NONE());
  mrb_define_method(mrb, wslay_event_context_cl, "queued_msg_length",     mrb_wslay_event_get_queued_msg_length,          MRB_ARGS_NONE());

  wslay_event_context_server_cl = mrb_define_class_under(mrb, wslay_event_context_cl, "Server", wslay_event_context_cl);
  mrb_define_method(mrb, wslay_event_context_server_cl, "initialize", mrb_wslay_event_context_server_init, MRB_ARGS_REQ(1));

  wslay_event_context_client_cl = mrb_define_class_under(mrb, wslay_event_context_cl, "Client", wslay_event_context_cl);
  mrb_define_method(mrb, wslay_event_context_client_cl, "initialize", mrb_wslay_event_context_client_init, MRB_ARGS_REQ(1));
}