From: Martin Bosslet <Martin.Bosslet@...>
Date: 2012-02-25T05:34:08+09:00
Subject: [ruby-core:42869] [ruby-trunk - Feature #6065] Allow Bignum marshalling/unmarshalling from C API


Issue #6065 has been updated by Martin Bosslet.


Kenta Murata wrote:
> I also believe it is useful that the feature to dump a Bignum to C array.
> 
> I made a patch for realizing the feature.
> Please check this gist https://gist.github.com/1892968
> 
> If Matz approve the patch, I will commit that.

Great, I would really appreciate that, thank you!

One comment, which is also in reply to why I believe it
could be dangerous to simply promote rb_big_(un)pack to
public API: rhey are tightly coupled to our internal
representation of Bignums. We should probably explicitly
define the format to be expected of the long array. This
way we'll separate our internal representation from the 
format that is actually exchanged. This would allow us to
change the representation internally without breaking
compatibility in the API layer each time we do so.

So even if for now it would suffice to simply memcpy
our internal representation to the array, we should
already fix the format to avoid problems in the
future?


----------------------------------------
Feature #6065: Allow Bignum marshalling/unmarshalling from C API
https://bugs.ruby-lang.org/issues/6065

Author: Martin Bosslet
Status: Assigned
Priority: Normal
Assignee: Kenta Murata
Category: core
Target version: 2.0.0


Currently, there's no public C API to create a Bignum. 
There is rb_big_pack and rb_big_unpack that will do the
job, but they are not portable.

Could we offer public functionality that is independent 
of the internal representation for the task of 
marshaling/unmarshalling a Bignum to raw C data types?

I'd like to propose something like

- creating a bignum:

  VALUE rb_big_from_ary(unsigned long *longs, size_t num_longs, int signed)

- retrieving a representation of a Bignum (longs are allocated):

  size_t rb_big_to_ary(VALUE big, unsigned long **longs, int *signed)

For getting a representation, rb_big2str could also be used,
but the above would simplify things when developing an extension
that is in need of Bignum support.

Names and signatures are of course open for discussion,
the example should just serve as an indication of what 
I'm aiming at.

To avoid ambiguity, it would have to be defined how 
the longs are ordered and how the signed flag is to be
interpreted - I would suggest a very simple
representation: Let "longs" be the representation of
the absolute value of the bignum in little- or big-endian
order, where each of the longs themselves should probably
be in the same order, in order to eliminate ambivalence.
Signed is either 0 or 1, so no two's complement or anything
involved. 

I would volunteer to provide a patch for this if we would
agree on something.


-- 
http://bugs.ruby-lang.org/