Exemplo n.º 1
0
static VALUE Buffer_initialize(int argc, VALUE* argv, VALUE self)
{
    VALUE io = Qnil;
    VALUE options = Qnil;

    if(argc == 0 || (argc == 1 && argv[0] == Qnil)) {
        /* Qnil */

    } else if(argc == 1) {
        VALUE v = argv[0];
        if(rb_type(v) == T_HASH) {
            options = v;
        } else {
            io = v;
        }

    } else if(argc == 2) {
        io = argv[0];
        options = argv[1];
        if(rb_type(options) != T_HASH) {
            rb_raise(rb_eArgError, "expected Hash but found %s.", rb_obj_classname(io));
        }

    } else {
        rb_raise(rb_eArgError, "wrong number of arguments (%d for 0..1)", argc);
    }

    BUFFER(self, b);

    MessagePack_Buffer_initialize(b, io, options);

    return self;
}
Exemplo n.º 2
0
VALUE MessagePack_unpack(int argc, VALUE* argv)
{
    VALUE src;

    switch(argc) {
    case 1:
        src = argv[0];
        break;
    default:
        rb_raise(rb_eArgError, "wrong number of arguments (%d for 1)", argc);
    }

    VALUE io = Qnil;
    if(rb_type(src) != T_STRING) {
        io = src;
        src = Qnil;
    }

    VALUE self = Unpacker_alloc(cMessagePack_Unpacker);
    UNPACKER(self, uk);
    //msgpack_unpacker_reset(s_unpacker);
    //msgpack_buffer_reset_io(UNPACKER_BUFFER_(s_unpacker));

    /* prefer reference than copying */
    msgpack_buffer_set_write_reference_threshold(UNPACKER_BUFFER_(uk), 0);

    if(io != Qnil) {
        MessagePack_Buffer_initialize(UNPACKER_BUFFER_(uk), io, Qnil);
    }

    if(src != Qnil) {
        /* prefer reference than copying; see MessagePack_Unpacker_module_init */
        msgpack_buffer_append_string(UNPACKER_BUFFER_(uk), src);
    }

    int r = msgpack_unpacker_read(uk, 0);
    if(r < 0) {
        raise_unpacker_error(r);
    }

    /* raise if extra bytes follow */
    if(msgpack_buffer_top_readable_size(UNPACKER_BUFFER_(uk)) > 0) {
        rb_raise(eMalformedFormatError, "extra bytes follow after a deserialized object");
    }

#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 msgpack_unpacker_get_last_object(uk);
}
Exemplo n.º 3
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;
}
Exemplo n.º 4
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;
}
Exemplo n.º 5
0
VALUE MessagePack_unpack(int argc, VALUE* argv)
{
    VALUE src;

    switch(argc) {
    case 1:
        src = argv[0];
        break;
    default:
        rb_raise(rb_eArgError, "wrong number of arguments (%d for 1)", argc);
    }

    VALUE io = Qnil;
    if(rb_type(src) != T_STRING) {
        io = src;
        src = Qnil;
    }

    // TODO create an instance if io is set for thread safety?
    //VALUE self = Unpacker_alloc(cMessagePack_Unpacker);
    //UNPACKER(self, uk);
    msgpack_unpacker_reset(s_unpacker);
    msgpack_buffer_reset_io(UNPACKER_BUFFER_(s_unpacker));

    if(io != Qnil) {
        MessagePack_Buffer_initialize(UNPACKER_BUFFER_(s_unpacker), io, Qnil);
    }

    if(src != Qnil) {
        /* prefer reference than copying; see MessagePack_Unpacker_module_init */
        msgpack_buffer_append_string(UNPACKER_BUFFER_(s_unpacker), src);
    }

    int r = msgpack_unpacker_read(s_unpacker, 0);
    if(r < 0) {
        raise_unpacker_error(r);
    }

    /* raise if extra bytes follow */
    if(msgpack_buffer_top_readable_size(UNPACKER_BUFFER_(s_unpacker)) > 0) {
        rb_raise(eMalformedFormatError, "extra bytes follow after a deserialized object");
    }

    return msgpack_unpacker_get_last_object(s_unpacker);
}
Exemplo 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 */
    return retval;
}
Exemplo n.º 7
0
static mrb_value
Packer_initialize(mrb_state* mrb, mrb_value self)
{
  mrb_value io = mrb_nil_value();
  mrb_value options = mrb_nil_value();
  mrb_value *argv;
  int argc; /* Number of arguments */

  mrb_get_args(mrb, "*", &argv, &argc);
  if (argc == 0 || (argc == 1 && mrb_obj_eq(mrb, argv[0], mrb_nil_value()))) {
    /* mrb_nil_value() */
  } else if (argc == 1) {
    puts("hash");
    mrb_value v = argv[0];
    if (mrb_type(v) == MRB_TT_HASH) {
      options = v;
    } else {
      io = v;
    }
  } else if (argc == 2) {
    io = argv[0];
    options = argv[1];
    if (mrb_type(options) != MRB_TT_HASH) {
      mrb_raisef(mrb, E_ARGUMENT_ERROR, "expected Hash but found %S.", mrb_str_new_cstr(mrb, "classname"));
    }
  } else {
    mrb_raisef(mrb, E_ARGUMENT_ERROR, "wrong number of arguments (%S for 0..1)", mrb_fixnum_value(argc));
  }

  self = Packer_alloc(mrb, mrb_class_ptr(self));

  PACKER(mrb, self, pk);
  if (!mrb_nil_p(io) || !mrb_nil_p(options)) {
    MessagePack_Buffer_initialize(mrb, PACKER_BUFFER_(pk), io, options);
  }

  // TODO options

  return self;
}