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

ruby-changes:64413

From: Kenta <ko1@a...>
Date: Mon, 21 Dec 2020 22:11:24 +0900 (JST)
Subject: [ruby-changes:64413] 4c2e7f26bd (master): [json] JSON_parse_float: Fix how to convert number

https://git.ruby-lang.org/ruby.git/commit/?id=4c2e7f26bd

From 4c2e7f26bda8df087a66e546744de0d0d467ea7c Mon Sep 17 00:00:00 2001
From: Kenta Murata <mrkn@m...>
Date: Mon, 21 Dec 2020 15:50:04 +0900
Subject: [json] JSON_parse_float: Fix how to convert number

Stop BigDecimal-specific optimization.  Instead, it tries the conversion
methods in the following order:

1. `try_convert`,
2. `new`, and
3. class-named function, e.g. `Foo::Bar.Baz` function for `Foo::Bar::Baz` class

If all the above candidates are unavailable, it fallbacks to Float.

diff --git a/ext/json/parser/parser.c b/ext/json/parser/parser.c
index aaef53a..f997295 100644
--- a/ext/json/parser/parser.c
+++ b/ext/json/parser/parser.c
@@ -91,13 +91,12 @@ static int convert_UTF32_to_UTF8(char *buf, UTF32 ch) 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_BigDecimal, i_freeze, i_uminus;
+          i_leftshift, i_new, i_try_convert, i_freeze, i_uminus;
 
 
 #line 126 "parser.rl"
@@ -991,19 +990,6 @@ enum {JSON_float_en_main = 1}; https://github.com/ruby/ruby/blob/trunk/ext/json/parser/parser.c#L990
 #line 346 "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;
@@ -1146,21 +1132,46 @@ case 7: https://github.com/ruby/ruby/blob/trunk/ext/json/parser/parser.c#L1132
 #line 368 "parser.rl"
 
     if (cs >= JSON_float_first_final) {
+        VALUE mod = Qnil;
+        ID method_id = 0;
+        if (rb_respond_to(json->decimal_class, i_try_convert)) {
+            mod = json->decimal_class;
+            method_id = i_try_convert;
+        } else if (rb_respond_to(json->decimal_class, i_new)) {
+            mod = json->decimal_class;
+            method_id = i_new;
+        } else if (RB_TYPE_P(json->decimal_class, T_CLASS)) {
+            VALUE name = rb_class_name(json->decimal_class);
+            const char *name_cstr = RSTRING_PTR(name);
+            const char *last_colon = strrchr(name_cstr, ':');
+            if (last_colon) {
+                const char *mod_path_end = last_colon - 1;
+                VALUE mod_path = rb_str_substr(name, 0, mod_path_end - name_cstr);
+                mod = rb_path_to_class(mod_path);
+
+                const char *method_name_beg = last_colon + 1;
+                long before_len = method_name_beg - name_cstr;
+                long len = RSTRING_LEN(name) - before_len;
+                VALUE method_name = rb_str_substr(name, before_len, len);
+                method_id = SYM2ID(rb_str_intern(method_name));
+            } else {
+                mod = rb_mKernel;
+                method_id = SYM2ID(rb_str_intern(name));
+            }
+        }
+
         long len = p - json->memo;
         fbuffer_clear(json->fbuffer);
         fbuffer_append(json->fbuffer, json->memo, len);
         fbuffer_append_char(json->fbuffer, '\0');
-        if (NIL_P(json->decimal_class)) {
-          *result = rb_float_new(rb_cstr_to_dbl(FBUFFER_PTR(json->fbuffer), 1));
+
+        if (method_id) {
+            VALUE text = rb_str_new2(FBUFFER_PTR(json->fbuffer));
+            *result = rb_funcallv(mod, method_id, 1, &text);
         } else {
-          VALUE text;
-          text = rb_str_new2(FBUFFER_PTR(json->fbuffer));
-          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);
-          }
+            *result = DBL2NUM(rb_cstr_to_dbl(FBUFFER_PTR(json->fbuffer), 1));
         }
+
         return p + 1;
     } else {
         return NULL;
@@ -2150,7 +2161,7 @@ void Init_parser(void) https://github.com/ruby/ruby/blob/trunk/ext/json/parser/parser.c#L2161
     i_aref = rb_intern("[]");
     i_leftshift = rb_intern("<<");
     i_new = rb_intern("new");
-    i_BigDecimal = rb_intern("BigDecimal");
+    i_try_convert = rb_intern("try_convert");
     i_freeze = rb_intern("freeze");
     i_uminus = rb_intern("-@");
 }
diff --git a/ext/json/parser/parser.rl b/ext/json/parser/parser.rl
index 4629052..24aed60 100644
--- a/ext/json/parser/parser.rl
+++ b/ext/json/parser/parser.rl
@@ -89,13 +89,12 @@ static int convert_UTF32_to_UTF8(char *buf, UTF32 ch) 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_BigDecimal, i_freeze, i_uminus;
+          i_leftshift, i_new, i_try_convert, i_freeze, i_uminus;
 
 %%{
     machine JSON_common;
@@ -345,19 +344,6 @@ static char *JSON_parse_integer(JSON_Parser *json, char *p, char *pe, VALUE *res https://github.com/ruby/ruby/blob/trunk/ext/json/parser/parser.rl#L344
              )  (^[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;
@@ -367,21 +353,46 @@ static char *JSON_parse_float(JSON_Parser *json, char *p, char *pe, VALUE *resul https://github.com/ruby/ruby/blob/trunk/ext/json/parser/parser.rl#L353
     %% write exec;
 
     if (cs >= JSON_float_first_final) {
+        VALUE mod = Qnil;
+        ID method_id = 0;
+        if (rb_respond_to(json->decimal_class, i_try_convert)) {
+            mod = json->decimal_class;
+            method_id = i_try_convert;
+        } else if (rb_respond_to(json->decimal_class, i_new)) {
+            mod = json->decimal_class;
+            method_id = i_new;
+        } else if (RB_TYPE_P(json->decimal_class, T_CLASS)) {
+            VALUE name = rb_class_name(json->decimal_class);
+            const char *name_cstr = RSTRING_PTR(name);
+            const char *last_colon = strrchr(name_cstr, ':');
+            if (last_colon) {
+                const char *mod_path_end = last_colon - 1;
+                VALUE mod_path = rb_str_substr(name, 0, mod_path_end - name_cstr);
+                mod = rb_path_to_class(mod_path);
+
+                const char *method_name_beg = last_colon + 1;
+                long before_len = method_name_beg - name_cstr;
+                long len = RSTRING_LEN(name) - before_len;
+                VALUE method_name = rb_str_substr(name, before_len, len);
+                method_id = SYM2ID(rb_str_intern(method_name));
+            } else {
+                mod = rb_mKernel;
+                method_id = SYM2ID(rb_str_intern(name));
+            }
+        }
+
         long len = p - json->memo;
         fbuffer_clear(json->fbuffer);
         fbuffer_append(json->fbuffer, json->memo, len);
         fbuffer_append_char(json->fbuffer, '\0');
-        if (NIL_P(json->decimal_class)) {
-          *result = rb_float_new(rb_cstr_to_dbl(FBUFFER_PTR(json->fbuffer), 1));
+
+        if (method_id) {
+            VALUE text = rb_str_new2(FBUFFER_PTR(json->fbuffer));
+            *result = rb_funcallv(mod, method_id, 1, &text);
         } else {
-          VALUE text;
-          text = rb_str_new2(FBUFFER_PTR(json->fbuffer));
-          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);
-          }
+            *result = DBL2NUM(rb_cstr_to_dbl(FBUFFER_PTR(json->fbuffer), 1));
         }
+
         return p + 1;
     } else {
         return NULL;
@@ -910,7 +921,7 @@ void Init_parser(void) https://github.com/ruby/ruby/blob/trunk/ext/json/parser/parser.rl#L921
     i_aref = rb_intern("[]");
     i_leftshift = rb_intern("<<");
     i_new = rb_intern("new");
-    i_BigDecimal = rb_intern("BigDecimal");
+    i_try_convert = rb_intern("try_convert");
     i_freeze = rb_intern("freeze");
     i_uminus = rb_intern("-@");
 }
-- 
cgit v0.10.2


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

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