[前][次][番号順一覧][スレッド一覧]

ruby-changes:53908

From: mrkn <ko1@a...>
Date: Sun, 2 Dec 2018 14:29:20 +0900 (JST)
Subject: [ruby-changes:53908] mrkn:r66127 (trunk): Fix JSON::Parser against bigdecimal updates

mrkn	2018-12-02 14:21:57 +0900 (Sun, 02 Dec 2018)

  New Revision: 66127

  https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=66127

  Log:
    Fix JSON::Parser against bigdecimal updates

  Modified files:
    trunk/ext/json/parser/parser.c
    trunk/ext/json/parser/parser.rl
    trunk/test/json/json_parser_test.rb
Index: test/json/json_parser_test.rb
===================================================================
--- test/json/json_parser_test.rb	(revision 66126)
+++ test/json/json_parser_test.rb	(revision 66127)
@@ -110,8 +110,8 @@ class JSONParserTest < Test::Unit::TestC https://github.com/ruby/ruby/blob/trunk/test/json/json_parser_test.rb#L110
   end
 
   def test_parse_bigdecimals
-    assert_equal(BigDecimal,                                 JSON.parse('{"foo": 9.01234567890123456789}', decimal_class: BigDecimal)["foo"].class)
-    assert_equal(BigDecimal.new("0.901234567890123456789E1"),JSON.parse('{"foo": 9.01234567890123456789}', decimal_class: BigDecimal)["foo"]      )
+    assert_equal(BigDecimal,                             JSON.parse('{"foo": 9.01234567890123456789}', decimal_class: BigDecimal)["foo"].class)
+    assert_equal(BigDecimal("0.901234567890123456789E1"),JSON.parse('{"foo": 9.01234567890123456789}', decimal_class: BigDecimal)["foo"]      )
   end
 
   if Array.method_defined?(:permutation)
Index: ext/json/parser/parser.rl
===================================================================
--- ext/json/parser/parser.rl	(revision 66126)
+++ ext/json/parser/parser.rl	(revision 66127)
@@ -89,12 +89,13 @@ static int convert_UTF32_to_UTF8(char *b https://github.com/ruby/ruby/blob/trunk/ext/json/parser/parser.rl#L89
 
 static VALUE mJSON, mExt, cParser, eParserError, eNestingError;
 static VALUE CNaN, CInfinity, CMinusInfinity;
+static VALUE cBigDecimal = Qundef;
 
 static ID i_json_creatable_p, i_json_create, i_create_id, i_create_additions,
           i_chr, i_max_nesting, i_allow_nan, i_symbolize_names,
           i_object_class, i_array_class, i_decimal_class, i_key_p,
           i_deep_const_get, i_match, i_match_string, i_aset, i_aref,
-          i_leftshift, i_new;
+          i_leftshift, i_new, i_BigDecimal;
 
 %%{
     machine JSON_common;
@@ -339,6 +340,19 @@ static char *JSON_parse_integer(JSON_Par https://github.com/ruby/ruby/blob/trunk/ext/json/parser/parser.rl#L340
              )  (^[0-9Ee.\-]? @exit );
 }%%
 
+static int is_bigdecimal_class(VALUE obj)
+{
+  if (cBigDecimal == Qundef) {
+    if (rb_const_defined(rb_cObject, i_BigDecimal)) {
+      cBigDecimal = rb_const_get_at(rb_cObject, i_BigDecimal);
+    }
+    else {
+      return 0;
+    }
+  }
+  return obj == cBigDecimal;
+}
+
 static char *JSON_parse_float(JSON_Parser *json, char *p, char *pe, VALUE *result)
 {
     int cs = EVIL;
@@ -357,7 +371,11 @@ static char *JSON_parse_float(JSON_Parse https://github.com/ruby/ruby/blob/trunk/ext/json/parser/parser.rl#L371
         } else {
           VALUE text;
           text = rb_str_new2(FBUFFER_PTR(json->fbuffer));
-          *result = rb_funcall(json->decimal_class, i_new, 1, text);
+          if (is_bigdecimal_class(json->decimal_class)) {
+            *result = rb_funcall(Qnil, i_BigDecimal, 1, text);
+          } else {
+            *result = rb_funcall(json->decimal_class, i_new, 1, text);
+          }
         }
         return p + 1;
     } else {
@@ -861,6 +879,7 @@ void Init_parser(void) https://github.com/ruby/ruby/blob/trunk/ext/json/parser/parser.rl#L879
     i_aref = rb_intern("[]");
     i_leftshift = rb_intern("<<");
     i_new = rb_intern("new");
+    i_BigDecimal = rb_intern("BigDecimal");
 }
 
 /*
Index: ext/json/parser/parser.c
===================================================================
--- ext/json/parser/parser.c	(revision 66126)
+++ ext/json/parser/parser.c	(revision 66127)
@@ -91,12 +91,13 @@ static int convert_UTF32_to_UTF8(char *b https://github.com/ruby/ruby/blob/trunk/ext/json/parser/parser.c#L91
 
 static VALUE mJSON, mExt, cParser, eParserError, eNestingError;
 static VALUE CNaN, CInfinity, CMinusInfinity;
+static VALUE cBigDecimal = Qundef;
 
 static ID i_json_creatable_p, i_json_create, i_create_id, i_create_additions,
           i_chr, i_max_nesting, i_allow_nan, i_symbolize_names,
           i_object_class, i_array_class, i_decimal_class, i_key_p,
           i_deep_const_get, i_match, i_match_string, i_aset, i_aref,
-          i_leftshift, i_new;
+          i_leftshift, i_new, i_BigDecimal;
 
 
 #line 125 "parser.rl"
@@ -985,6 +986,19 @@ enum {JSON_float_en_main = 1}; https://github.com/ruby/ruby/blob/trunk/ext/json/parser/parser.c#L986
 #line 340 "parser.rl"
 
 
+static int is_bigdecimal_class(VALUE obj)
+{
+  if (cBigDecimal == Qundef) {
+    if (rb_const_defined(rb_cObject, i_BigDecimal)) {
+      cBigDecimal = rb_const_get_at(rb_cObject, i_BigDecimal);
+    }
+    else {
+      return 0;
+    }
+  }
+  return obj == cBigDecimal;
+}
+
 static char *JSON_parse_float(JSON_Parser *json, char *p, char *pe, VALUE *result)
 {
     int cs = EVIL;
@@ -1136,7 +1150,11 @@ case 7: https://github.com/ruby/ruby/blob/trunk/ext/json/parser/parser.c#L1150
         } else {
           VALUE text;
           text = rb_str_new2(FBUFFER_PTR(json->fbuffer));
-          *result = rb_funcall(json->decimal_class, i_new, 1, text);
+          if (is_bigdecimal_class(json->decimal_class)) {
+            *result = rb_funcall(Qnil, i_BigDecimal, 1, text);
+          } else {
+            *result = rb_funcall(json->decimal_class, i_new, 1, text);
+          }
         }
         return p + 1;
     } else {
@@ -2101,6 +2119,7 @@ void Init_parser(void) https://github.com/ruby/ruby/blob/trunk/ext/json/parser/parser.c#L2119
     i_aref = rb_intern("[]");
     i_leftshift = rb_intern("<<");
     i_new = rb_intern("new");
+    i_BigDecimal = rb_intern("BigDecimal");
 }
 
 /*

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]