summaryrefslogtreecommitdiff
path: root/ext/json/generator
diff options
context:
space:
mode:
authorakr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2016-05-17 06:53:48 +0000
committerakr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2016-05-17 06:53:48 +0000
commitf9727c12cc8fbc5f752f5983be1f14bb976e5a13 (patch)
treebdf433b181c6d6e00bd33581136ab2970bc9509d /ext/json/generator
parent565241f11f5d217631ed1e8bbafec7a088bed654 (diff)
[Feature #12005] Unify Fixnum and Bignum into Integer
* [Feature #12005] Unify Fixnum and Bignum into Integer * include/ruby/ruby.h (rb_class_of): Return rb_cInteger for fixnums. * insns.def (INTEGER_REDEFINED_OP_FLAG): Unified from FIXNUM_REDEFINED_OP_FLAG and BIGNUM_REDEFINED_OP_FLAG. * vm_core.h: Ditto. * vm_insnhelper.c (opt_eq_func): Use INTEGER_REDEFINED_OP_FLAG instead of FIXNUM_REDEFINED_OP_FLAG. * vm.c (vm_redefinition_check_flag): Use rb_cInteger instead of rb_cFixnum and rb_cBignum. (C): Use Integer instead of Fixnum and Bignum. * numeric.c (fix_succ): Removed. (Init_Numeric): Define Fixnum as Integer. * bignum.c (bignew): Use rb_cInteger instead of Rb_cBignum. (rb_int_coerce): replaced from rb_big_coerce and return fixnums as-is. (Init_Bignum): Define Bignum as Integer. Don't define ===. * error.c (builtin_class_name): Return "Integer" for fixnums. * sprintf.c (ruby__sfvextra): Use rb_cInteger instead of rb_cFixnum. * ext/-test-/testutil: New directory to test. Currently it provides utilities for fixnum and bignum. * ext/json/generator/generator.c: Define mInteger_to_json. * lib/mathn.rb (Fixnum#/): Redefinition removed. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@55024 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'ext/json/generator')
-rw-r--r--ext/json/generator/generator.c38
-rw-r--r--ext/json/generator/generator.h1
2 files changed, 32 insertions, 7 deletions
diff --git a/ext/json/generator/generator.c b/ext/json/generator/generator.c
index a135e283482..99130ea4b2c 100644
--- a/ext/json/generator/generator.c
+++ b/ext/json/generator/generator.c
@@ -7,7 +7,7 @@ static ID i_encoding, i_encode;
#endif
static VALUE mJSON, mExt, mGenerator, cState, mGeneratorMethods, mObject,
- mHash, mArray, mFixnum, mBignum, mFloat, mString, mString_Extend,
+ mHash, mArray, mInteger, mFixnum, mBignum, mFloat, mString, mString_Extend,
mTrueClass, mFalseClass, mNilClass, eGeneratorError,
eNestingError, CRegexp_MULTILINE, CJSON_SAFE_STATE_PROTOTYPE,
i_SAFE_STATE_PROTOTYPE;
@@ -347,6 +347,16 @@ static VALUE mArray_to_json(int argc, VALUE *argv, VALUE self) {
*
* Returns a JSON string representation for this Integer number.
*/
+static VALUE mInteger_to_json(int argc, VALUE *argv, VALUE self)
+{
+ GENERATE_JSON(integer);
+}
+
+/*
+ * call-seq: to_json(*)
+ *
+ * Returns a JSON string representation for this Integer number.
+ */
static VALUE mFixnum_to_json(int argc, VALUE *argv, VALUE self)
{
GENERATE_JSON(fixnum);
@@ -825,6 +835,14 @@ static void generate_json_bignum(FBuffer *buffer, VALUE Vstate, JSON_Generator_S
fbuffer_append_str(buffer, tmp);
}
+static void generate_json_integer(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj)
+{
+ if (FIXNUM_P(obj))
+ generate_json_fixnum(buffer, Vstate, state, obj);
+ else
+ generate_json_bignum(buffer, Vstate, state, obj);
+}
+
static void generate_json_float(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj)
{
double value = RFLOAT_VALUE(obj);
@@ -858,9 +876,9 @@ static void generate_json(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *s
generate_json_false(buffer, Vstate, state, obj);
} else if (obj == Qtrue) {
generate_json_true(buffer, Vstate, state, obj);
- } else if (klass == rb_cFixnum) {
+ } else if (FIXNUM_P(obj)) {
generate_json_fixnum(buffer, Vstate, state, obj);
- } else if (klass == rb_cBignum) {
+ } else if (RB_TYPE_P(obj, T_BIGNUM)) {
generate_json_bignum(buffer, Vstate, state, obj);
} else if (klass == rb_cFloat) {
generate_json_float(buffer, Vstate, state, obj);
@@ -1402,10 +1420,16 @@ void Init_generator(void)
rb_define_method(mHash, "to_json", mHash_to_json, -1);
mArray = rb_define_module_under(mGeneratorMethods, "Array");
rb_define_method(mArray, "to_json", mArray_to_json, -1);
- mFixnum = rb_define_module_under(mGeneratorMethods, "Fixnum");
- rb_define_method(mFixnum, "to_json", mFixnum_to_json, -1);
- mBignum = rb_define_module_under(mGeneratorMethods, "Bignum");
- rb_define_method(mBignum, "to_json", mBignum_to_json, -1);
+ if (rb_cInteger == rb_cFixnum) {
+ mInteger = rb_define_module_under(mGeneratorMethods, "Integer");
+ rb_define_method(mInteger, "to_json", mInteger_to_json, -1);
+ }
+ else {
+ mFixnum = rb_define_module_under(mGeneratorMethods, "Fixnum");
+ rb_define_method(mFixnum, "to_json", mFixnum_to_json, -1);
+ mBignum = rb_define_module_under(mGeneratorMethods, "Bignum");
+ rb_define_method(mBignum, "to_json", mBignum_to_json, -1);
+ }
mFloat = rb_define_module_under(mGeneratorMethods, "Float");
rb_define_method(mFloat, "to_json", mFloat_to_json, -1);
mString = rb_define_module_under(mGeneratorMethods, "String");
diff --git a/ext/json/generator/generator.h b/ext/json/generator/generator.h
index 298c0a4965a..ffd55e416bb 100644
--- a/ext/json/generator/generator.h
+++ b/ext/json/generator/generator.h
@@ -122,6 +122,7 @@ static void generate_json_string(FBuffer *buffer, VALUE Vstate, JSON_Generator_S
static void generate_json_null(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj);
static void generate_json_false(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj);
static void generate_json_true(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj);
+static void generate_json_integer(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj);
static void generate_json_fixnum(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj);
static void generate_json_bignum(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj);
static void generate_json_float(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj);