Esempio n. 1
0
static int write_hash_foreach(VALUE key, VALUE value, VALUE pk_value)
{
    if (key == Qundef) {
        return ST_CONTINUE;
    }
    msgpack_packer_t* pk = (msgpack_packer_t*) pk_value;
    msgpack_packer_write_value(pk, key);
    msgpack_packer_write_value(pk, value);
    return ST_CONTINUE;
}
Esempio n. 2
0
VALUE MessagePack_pack(int argc, VALUE* argv)
{
    VALUE v;

    if (argc < 0 || argc > 3) {
        rb_raise(rb_eArgError, "wrong number of arguments (%d for 1..3)", argc);
    }
    v = argv[0];

    VALUE self = MessagePack_Factory_packer(argc - 1, argv + 1, cMessagePack_DefaultFactory);
    PACKER(self, pk);

    msgpack_packer_write_value(pk, v);

    VALUE retval;
    if(msgpack_buffer_has_io(PACKER_BUFFER_(pk))) {
        msgpack_buffer_flush(PACKER_BUFFER_(pk));
        retval = Qnil;
    } else {
        retval = msgpack_buffer_all_as_string(PACKER_BUFFER_(pk));
    }

    msgpack_buffer_clear(PACKER_BUFFER_(pk)); /* to free rmem before GC */

#ifdef RB_GC_GUARD
    /* This prevents compilers from optimizing out the `self` variable
     * from stack. Otherwise GC free()s it. */
    RB_GC_GUARD(self);
#endif

    return retval;
}
Esempio n. 3
0
static mrb_value Packer_write(mrb_state* mrb, mrb_value self)
{
  mrb_value obj;

  mrb_get_args(mrb, "o", &obj);
  PACKER(mrb, self, pk);
  msgpack_packer_write_value(mrb, pk, obj);
  return self;
}
Esempio n. 4
0
mrb_value
MessagePack_pack_v(mrb_state *mrb, int argc, mrb_value* argv)
{
  //mrb_value *argv;
  //int argc; /* Number of arguments */
  /* TODO options */
  mrb_value v;
  mrb_value io = mrb_nil_value();

  //mrb_get_args(mrb, "*", &argv, &argc);
  switch (argc) {
  case 2:
    io = argv[1];
    /* Pass-through */
  case 1:
    v = argv[0];
    break;
  default:
    mrb_raisef(mrb, E_ARGUMENT_ERROR, "wrong number of arguments (%S for 1..2)", mrb_fixnum_value(argc));
  }

  mrb_value self = Packer_alloc(mrb, cMessagePack_Packer);

  PACKER(mrb, self, pk);
  //msgpack_packer_reset(s_packer);
  //msgpack_buffer_reset_io(PACKER_BUFFER_(s_packer));

  if (!mrb_nil_p(io)) {
    MessagePack_Buffer_initialize(mrb, PACKER_BUFFER_(pk), io, mrb_nil_value());
  }

  msgpack_packer_write_value(mrb, pk, v);

  mrb_value retval;
  if (!mrb_nil_p(io)) {
    msgpack_buffer_flush(mrb, PACKER_BUFFER_(pk));
    retval = mrb_nil_value();
  } else {
    retval = msgpack_buffer_all_as_string(mrb, PACKER_BUFFER_(pk));
  }

  //msgpack_buffer_clear(mrb, PACKER_BUFFER_(pk)); /* to free rmem before GC */

#ifdef RB_GC_GUARD
  /* This prevents compilers from optimizing out the `self` variable
   * from stack. Otherwise GC free()s it. */
  RB_GC_GUARD(self);
#endif

  return retval;
}
Esempio n. 5
0
void msgpack_packer_write_array_value(msgpack_packer_t* pk, VALUE v)
{
    /* actual return type of RARRAY_LEN is long */
    unsigned long len = RARRAY_LEN(v);
    if(len > 0xffffffffUL) {
        rb_raise(rb_eArgError, "size of array is too long to pack: %lu bytes should be <= %lu", len, 0xffffffffUL);
    }
    unsigned int len32 = (unsigned int)len;
    msgpack_packer_write_array_header(pk, len32);

    unsigned int i;
    for(i=0; i < len32; ++i) {
        VALUE e = rb_ary_entry(v, i);
        msgpack_packer_write_value(pk, e);
    }
}
Esempio n. 6
0
VALUE MessagePack_pack(int argc, VALUE* argv)
{
    // TODO options

    VALUE v;
    VALUE io = Qnil;

    switch(argc) {
    case 2:
        io = argv[1];
        /* pass-through */
    case 1:
        v = argv[0];
        break;
    default:
        rb_raise(rb_eArgError, "wrong number of arguments (%d for 1..2)", argc);
    }

    VALUE self = Packer_alloc(cMessagePack_Packer);
    PACKER(self, pk);
    //msgpack_packer_reset(s_packer);
    //msgpack_buffer_reset_io(PACKER_BUFFER_(s_packer));

    if(io != Qnil) {
        MessagePack_Buffer_initialize(PACKER_BUFFER_(pk), io, Qnil);
    }

    msgpack_packer_write_value(pk, v);

    VALUE retval;
    if(io != Qnil) {
        msgpack_buffer_flush(PACKER_BUFFER_(pk));
        retval = Qnil;
    } else {
        retval = msgpack_buffer_all_as_string(PACKER_BUFFER_(pk));
    }

    msgpack_buffer_clear(PACKER_BUFFER_(pk)); /* to free rmem before GC */

#ifdef RB_GC_GUARD
    /* This prevents compilers from optimizing out the `self` variable
     * from stack. Otherwise GC free()s it. */
    RB_GC_GUARD(self);
#endif

    return retval;
}
Esempio n. 7
0
VALUE MessagePack_pack(int argc, VALUE* argv)
{
    // TODO options

    VALUE v;
    VALUE io = Qnil;

    switch(argc) {
    case 2:
        io = argv[1];
        /* pass-through */
    case 1:
        v = argv[0];
        break;
    default:
        rb_raise(rb_eArgError, "wrong number of arguments (%d for 1..2)", argc);
    }

    VALUE self = Packer_alloc(cMessagePack_Packer);
    PACKER(self, pk);
    //msgpack_packer_reset(s_packer);
    //msgpack_buffer_reset_io(PACKER_BUFFER_(s_packer));

    if(io != Qnil) {
        MessagePack_Buffer_initialize(PACKER_BUFFER_(pk), io, Qnil);
    }

    msgpack_packer_write_value(pk, v);

    VALUE retval;
    if(io != Qnil) {
        msgpack_buffer_flush(PACKER_BUFFER_(pk));
        retval = Qnil;
    } else {
        retval = msgpack_buffer_all_as_string(PACKER_BUFFER_(pk));
    }

    msgpack_buffer_clear(PACKER_BUFFER_(pk)); /* to free rmem before GC */
    return retval;
}
Esempio n. 8
0
static VALUE Packer_write(VALUE self, VALUE v)
{
    PACKER(self, pk);
    msgpack_packer_write_value(pk, v);
    return self;
}