| 1 |
/* |
| 2 |
* If placed under the "mruby/examples/mrbgems/mruby-YOUR-bigint" directory, |
| 3 |
* this file is available under the Creative Commons Zero License (CC0). |
| 4 |
* Note that file is incomplete. |
| 5 |
* |
| 6 |
* TODO: If this file is copied and another implementation is written, |
| 7 |
* remove this comment block from the copied file. |
| 8 |
*/ |
| 9 |
|
| 10 |
#include <mruby.h> |
| 11 |
#include <mruby/numeric.h> |
| 12 |
|
| 13 |
/* |
| 14 |
* The "mruby/internal.h" file should be placed after the other mruby header files. |
| 15 |
*/ |
| 16 |
#include <mruby/internal.h> |
| 17 |
|
| 18 |
/* |
| 19 |
* The "mruby/presym.h" file is placed at the end of the mruby header file. |
| 20 |
*/ |
| 21 |
#include <mruby/presym.h> |
| 22 |
|
| 23 |
/* |
| 24 |
* Define your own struct RBigint. |
| 25 |
* |
| 26 |
* - Object type must be MRB_TT_BIGINT. |
| 27 |
* - If the structure is named RBigint, MRB_OBJ_ALLOC() can be used as is. |
| 28 |
*/ |
| 29 |
struct RBigint { |
| 30 |
/* |
| 31 |
* Put MRB_OBJECT_HEADER before the first member of the structure. |
| 32 |
*/ |
| 33 |
MRB_OBJECT_HEADER; |
| 34 |
|
| 35 |
/* |
| 36 |
* Up to 3 words can be freely configured. |
| 37 |
*/ |
| 38 |
size_t len; |
| 39 |
size_t capa; |
| 40 |
uintptr_t *num; |
| 41 |
}; |
| 42 |
|
| 43 |
/* |
| 44 |
* Assert with mrb_static_assert_object_size() that the entire structure is within 6 words. |
| 45 |
*/ |
| 46 |
mrb_static_assert_object_size(struct RBigint); |
| 47 |
|
| 48 |
/* |
| 49 |
* The lower 16 bits of the object flags (`obj->flags`) can be used freely by the GEM author. |
| 50 |
*/ |
| 51 |
#define MY_BIGINT_NEGATIVE_FLAG 1 |
| 52 |
#define MY_BIGINT_NEGATIVE_P(obj) ((obj)->flags & MY_BIGINT_NEGATIVE_FLAG) |
| 53 |
|
| 54 |
/* |
| 55 |
* Implement the functions declared in `#ifdef MRUBY_USE_BIGINT ... #endif` in the "mruby/internal.h" file. |
| 56 |
*/ |
| 57 |
|
| 58 |
mrb_value |
| 59 |
mrb_bint_new_int(mrb_state *mrb, mrb_int x) |
| 60 |
{ |
| 61 |
struct RBigint *obj = MRB_OBJ_ALLOC(mrb, MRB_TT_BIGINT, mrb->integer_class); |
| 62 |
|
| 63 |
... |
| 64 |
|
| 65 |
return mrb_obj_value(obj); |
| 66 |
} |
| 67 |
|
| 68 |
/* |
| 69 |
* The implementation function continues... |
| 70 |
*/ |