зеркало из https://github.com/github/ruby.git
[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.
This commit is contained in:
Родитель
596da98b3f
Коммит
4c2e7f26bd
|
@ -91,13 +91,12 @@ static int convert_UTF32_to_UTF8(char *buf, UTF32 ch)
|
|||
|
||||
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};
|
|||
#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:
|
|||
#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)
|
|||
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("-@");
|
||||
}
|
||||
|
|
|
@ -89,13 +89,12 @@ static int convert_UTF32_to_UTF8(char *buf, UTF32 ch)
|
|||
|
||||
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
|
|||
) (^[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
|
|||
%% 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)
|
|||
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("-@");
|
||||
}
|
||||
|
|
Загрузка…
Ссылка в новой задаче