Skip to content

Commit cb3656b

Browse files
committed
Merge branch 'ruby-2.2' of https://github.com/zzak/json into zzak-ruby-2.2
Conflicts: .travis.yml json.gemspec json_pure.gemspec
2 parents 6ae8732 + b798187 commit cb3656b

File tree

17 files changed

+125
-59
lines changed

17 files changed

+125
-59
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
.*.sw[pon]
2+
*.bundle
23
coverage
34
tags
45
pkg
@@ -11,3 +12,5 @@ Gemfile.lock
1112
.rbx
1213
.AppleDouble
1314
.DS_Store
15+
*/**/Makefile
16+
*/**/*.o

.travis.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Passes arguments to bundle install (http://gembundler.com/man/bundle-install.1.html)
2-
bundler_args: --binstubs
2+
#bundler_args: --binstubs
33

44
# Specify which ruby versions you wish to run your tests on, each version will be used
55
rvm:
@@ -8,6 +8,7 @@ rvm:
88
- 1.9.3
99
- 2.0.0
1010
- 2.1
11+
- 2.2
1112
- ree
1213
- rbx-18mode
1314
- rbx-19mode
@@ -18,4 +19,5 @@ matrix:
1819
allow_failures:
1920
- rvm: rbx-18mode
2021
- rvm: rbx-19mode
22+
- rvm: ruby-head
2123
script: "bundle exec rake"

ext/json/ext/fbuffer/fbuffer.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,15 @@
2525
#define RSTRING_LEN(string) RSTRING(string)->len
2626
#endif
2727

28+
#ifdef PRIsVALUE
29+
# define RB_OBJ_CLASSNAME(obj) rb_obj_class(obj)
30+
# define RB_OBJ_STRING(obj) (obj)
31+
#else
32+
# define PRIsVALUE "s"
33+
# define RB_OBJ_CLASSNAME(obj) rb_obj_classname(obj)
34+
# define RB_OBJ_STRING(obj) StringValueCStr(obj)
35+
#endif
36+
2837
#ifdef HAVE_RUBY_ENCODING_H
2938
#include "ruby/encoding.h"
3039
#define FORCE_UTF8(obj) rb_enc_associate((obj), rb_utf8_encoding())
@@ -172,7 +181,7 @@ static FBuffer *fbuffer_dup(FBuffer *fb)
172181

173182
static VALUE fbuffer_to_s(FBuffer *fb)
174183
{
175-
VALUE result = rb_str_new(FBUFFER_PAIR(fb));
184+
VALUE result = rb_str_new(FBUFFER_PTR(fb), FBUFFER_LEN(fb));
176185
fbuffer_free(fb);
177186
FORCE_UTF8(result);
178187
return result;

ext/json/ext/generator/extconf.rb

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,4 @@
11
require 'mkmf'
22

3-
unless $CFLAGS.gsub!(/ -O[\dsz]?/, ' -O3')
4-
$CFLAGS << ' -O3'
5-
end
6-
if CONFIG['CC'] =~ /gcc/
7-
$CFLAGS << ' -Wall'
8-
unless $DEBUG && !$CFLAGS.gsub!(/ -O[\dsz]?/, ' -O0 -ggdb')
9-
$CFLAGS << ' -O0 -ggdb'
10-
end
11-
end
12-
133
$defs << "-DJSON_GENERATOR"
144
create_makefile 'json/ext/generator'

ext/json/ext/generator/generator.c

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -486,8 +486,9 @@ static VALUE mObject_to_json(int argc, VALUE *argv, VALUE self)
486486
return cState_partial_generate(state, string);
487487
}
488488

489-
static void State_free(JSON_Generator_State *state)
489+
static void State_free(void *ptr)
490490
{
491+
JSON_Generator_State *state = ptr;
491492
if (state->indent) ruby_xfree(state->indent);
492493
if (state->space) ruby_xfree(state->space);
493494
if (state->space_before) ruby_xfree(state->space_before);
@@ -499,7 +500,31 @@ static void State_free(JSON_Generator_State *state)
499500
ruby_xfree(state);
500501
}
501502

502-
static JSON_Generator_State *State_allocate()
503+
static size_t State_memsize(const void *ptr)
504+
{
505+
const JSON_Generator_State *state = ptr;
506+
size_t size = sizeof(*state);
507+
if (state->indent) size += state->indent_len + 1;
508+
if (state->space) size += state->space_len + 1;
509+
if (state->space_before) size += state->space_before_len + 1;
510+
if (state->object_nl) size += state->object_nl_len + 1;
511+
if (state->array_nl) size += state->array_nl_len + 1;
512+
if (state->array_delim) size += FBUFFER_CAPA(state->array_delim);
513+
if (state->object_delim) size += FBUFFER_CAPA(state->object_delim);
514+
if (state->object_delim2) size += FBUFFER_CAPA(state->object_delim2);
515+
return size;
516+
}
517+
518+
static const rb_data_type_t JSON_Generator_State_type = {
519+
"JSON/Generator/State",
520+
{NULL, State_free, State_memsize,},
521+
#ifdef RUBY_TYPED_FREE_IMMEDIATELY
522+
0, 0,
523+
RUBY_TYPED_FREE_IMMEDIATELY,
524+
#endif
525+
};
526+
527+
static JSON_Generator_State *State_allocate(void)
503528
{
504529
JSON_Generator_State *state = ALLOC(JSON_Generator_State);
505530
MEMZERO(state, JSON_Generator_State, 1);
@@ -509,7 +534,7 @@ static JSON_Generator_State *State_allocate()
509534
static VALUE cState_s_allocate(VALUE klass)
510535
{
511536
JSON_Generator_State *state = State_allocate();
512-
return Data_Wrap_Struct(klass, NULL, State_free, state);
537+
return TypedData_Wrap_Struct(klass, &JSON_Generator_State_type, state);
513538
}
514539

515540
/*
@@ -812,10 +837,10 @@ static void generate_json_float(FBuffer *buffer, VALUE Vstate, JSON_Generator_St
812837
if (!allow_nan) {
813838
if (isinf(value)) {
814839
fbuffer_free(buffer);
815-
rb_raise(eGeneratorError, "%u: %s not allowed in JSON", __LINE__, StringValueCStr(tmp));
840+
rb_raise(eGeneratorError, "%u: %"PRIsVALUE" not allowed in JSON", __LINE__, RB_OBJ_STRING(tmp));
816841
} else if (isnan(value)) {
817842
fbuffer_free(buffer);
818-
rb_raise(eGeneratorError, "%u: %s not allowed in JSON", __LINE__, StringValueCStr(tmp));
843+
rb_raise(eGeneratorError, "%u: %"PRIsVALUE" not allowed in JSON", __LINE__, RB_OBJ_STRING(tmp));
819844
}
820845
}
821846
fbuffer_append_str(buffer, tmp);
@@ -965,8 +990,9 @@ static VALUE cState_init_copy(VALUE obj, VALUE orig)
965990
{
966991
JSON_Generator_State *objState, *origState;
967992

968-
Data_Get_Struct(obj, JSON_Generator_State, objState);
969-
Data_Get_Struct(orig, JSON_Generator_State, origState);
993+
if (obj == orig) return obj;
994+
GET_STATE_TO(obj, objState);
995+
GET_STATE_TO(orig, origState);
970996
if (!objState) rb_raise(rb_eArgError, "unallocated JSON::State");
971997

972998
MEMCPY(objState, origState, JSON_Generator_State, 1);
@@ -1326,7 +1352,7 @@ static VALUE cState_buffer_initial_length_set(VALUE self, VALUE buffer_initial_l
13261352
/*
13271353
*
13281354
*/
1329-
void Init_generator()
1355+
void Init_generator(void)
13301356
{
13311357
rb_require("json/common");
13321358

ext/json/ext/generator/generator.h

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,12 @@ typedef struct JSON_Generator_StateStruct {
7878
long buffer_initial_length;
7979
} JSON_Generator_State;
8080

81+
#define GET_STATE_TO(self, state) \
82+
TypedData_Get_Struct(self, JSON_Generator_State, &JSON_Generator_State_type, state)
83+
8184
#define GET_STATE(self) \
8285
JSON_Generator_State *state; \
83-
Data_Get_Struct(self, JSON_Generator_State, state)
86+
GET_STATE_TO(self, state)
8487

8588
#define GENERATE_JSON(type) \
8689
FBuffer *buffer; \
@@ -89,7 +92,7 @@ typedef struct JSON_Generator_StateStruct {
8992
\
9093
rb_scan_args(argc, argv, "01", &Vstate); \
9194
Vstate = cState_from_state_s(cState, Vstate); \
92-
Data_Get_Struct(Vstate, JSON_Generator_State, state); \
95+
TypedData_Get_Struct(Vstate, JSON_Generator_State, &JSON_Generator_State_type, state); \
9396
buffer = cState_prepare_buffer(Vstate); \
9497
generate_json_##type(buffer, Vstate, state, self); \
9598
return fbuffer_to_s(buffer)
@@ -108,8 +111,8 @@ static VALUE mTrueClass_to_json(int argc, VALUE *argv, VALUE self);
108111
static VALUE mFalseClass_to_json(int argc, VALUE *argv, VALUE self);
109112
static VALUE mNilClass_to_json(int argc, VALUE *argv, VALUE self);
110113
static VALUE mObject_to_json(int argc, VALUE *argv, VALUE self);
111-
static void State_free(JSON_Generator_State *state);
112-
static JSON_Generator_State *State_allocate();
114+
static void State_free(void *state);
115+
static JSON_Generator_State *State_allocate(void);
113116
static VALUE cState_s_allocate(VALUE klass);
114117
static VALUE cState_configure(VALUE self, VALUE opts);
115118
static VALUE cState_to_h(VALUE self);
@@ -144,5 +147,6 @@ static VALUE cState_ascii_only_p(VALUE self);
144147
static VALUE cState_depth(VALUE self);
145148
static VALUE cState_depth_set(VALUE self, VALUE depth);
146149
static FBuffer *cState_prepare_buffer(VALUE self);
150+
static const rb_data_type_t JSON_Generator_State_type;
147151

148152
#endif

ext/json/ext/parser/extconf.rb

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,3 @@
11
require 'mkmf'
22

3-
unless $CFLAGS.gsub!(/ -O[\dsz]?/, ' -O3')
4-
$CFLAGS << ' -O3'
5-
end
6-
if CONFIG['CC'] =~ /gcc/
7-
$CFLAGS << ' -Wall'
8-
if $DEBUG && !$CFLAGS.gsub!(/ -O[\dsz]?/, ' -O0 -ggdb')
9-
$CFLAGS << ' -O0 -ggdb'
10-
end
11-
end
12-
133
create_makefile 'json/ext/parser'

ext/json/ext/parser/parser.c

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2092,33 +2092,50 @@ static VALUE cParser_parse(VALUE self)
20922092
}
20932093

20942094

2095-
static JSON_Parser *JSON_allocate()
2095+
static JSON_Parser *JSON_allocate(void)
20962096
{
20972097
JSON_Parser *json = ALLOC(JSON_Parser);
20982098
MEMZERO(json, JSON_Parser, 1);
20992099
json->fbuffer = fbuffer_alloc(0);
21002100
return json;
21012101
}
21022102

2103-
static void JSON_mark(JSON_Parser *json)
2103+
static void JSON_mark(void *ptr)
21042104
{
2105+
JSON_Parser *json = ptr;
21052106
rb_gc_mark_maybe(json->Vsource);
21062107
rb_gc_mark_maybe(json->create_id);
21072108
rb_gc_mark_maybe(json->object_class);
21082109
rb_gc_mark_maybe(json->array_class);
21092110
rb_gc_mark_maybe(json->match_string);
21102111
}
21112112

2112-
static void JSON_free(JSON_Parser *json)
2113+
static void JSON_free(void *ptr)
21132114
{
2115+
JSON_Parser *json = ptr;
21142116
fbuffer_free(json->fbuffer);
21152117
ruby_xfree(json);
21162118
}
21172119

2120+
static size_t JSON_memsize(const void *ptr)
2121+
{
2122+
const JSON_Parser *json = ptr;
2123+
return sizeof(*json) + FBUFFER_CAPA(json->fbuffer);
2124+
}
2125+
2126+
static const rb_data_type_t JSON_Parser_type = {
2127+
"JSON/Parser",
2128+
{JSON_mark, JSON_free, JSON_memsize,},
2129+
#ifdef RUBY_TYPED_FREE_IMMEDIATELY
2130+
0, 0,
2131+
RUBY_TYPED_FREE_IMMEDIATELY,
2132+
#endif
2133+
};
2134+
21182135
static VALUE cJSON_parser_s_allocate(VALUE klass)
21192136
{
21202137
JSON_Parser *json = JSON_allocate();
2121-
return Data_Wrap_Struct(klass, JSON_mark, JSON_free, json);
2138+
return TypedData_Wrap_Struct(klass, &JSON_Parser_type, json);
21222139
}
21232140

21242141
/*

ext/json/ext/parser/parser.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ typedef struct JSON_ParserStruct {
5151
if (!json->Vsource) rb_raise(rb_eTypeError, "uninitialized instance")
5252
#define GET_PARSER_INIT \
5353
JSON_Parser *json; \
54-
Data_Get_Struct(self, JSON_Parser, json)
54+
TypedData_Get_Struct(self, JSON_Parser, &JSON_Parser_type, json)
5555

5656
#define MinusInfinity "-Infinity"
5757
#define EVIL 0x666
@@ -68,10 +68,11 @@ static char *JSON_parse_string(JSON_Parser *json, char *p, char *pe, VALUE *resu
6868
static VALUE convert_encoding(VALUE source);
6969
static VALUE cParser_initialize(int argc, VALUE *argv, VALUE self);
7070
static VALUE cParser_parse(VALUE self);
71-
static JSON_Parser *JSON_allocate();
72-
static void JSON_mark(JSON_Parser *json);
73-
static void JSON_free(JSON_Parser *json);
71+
static JSON_Parser *JSON_allocate(void);
72+
static void JSON_mark(void *json);
73+
static void JSON_free(void *json);
7474
static VALUE cJSON_parser_s_allocate(VALUE klass);
7575
static VALUE cParser_source(VALUE self);
76+
static const rb_data_type_t JSON_Parser_type;
7677

7778
#endif

ext/json/ext/parser/parser.rl

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -815,33 +815,50 @@ static VALUE cParser_parse(VALUE self)
815815
}
816816

817817

818-
static JSON_Parser *JSON_allocate()
818+
static JSON_Parser *JSON_allocate(void)
819819
{
820820
JSON_Parser *json = ALLOC(JSON_Parser);
821821
MEMZERO(json, JSON_Parser, 1);
822822
json->fbuffer = fbuffer_alloc(0);
823823
return json;
824824
}
825825

826-
static void JSON_mark(JSON_Parser *json)
826+
static void JSON_mark(void *ptr)
827827
{
828+
JSON_Parser *json = ptr;
828829
rb_gc_mark_maybe(json->Vsource);
829830
rb_gc_mark_maybe(json->create_id);
830831
rb_gc_mark_maybe(json->object_class);
831832
rb_gc_mark_maybe(json->array_class);
832833
rb_gc_mark_maybe(json->match_string);
833834
}
834835

835-
static void JSON_free(JSON_Parser *json)
836+
static void JSON_free(void *ptr)
836837
{
838+
JSON_Parser *json = ptr;
837839
fbuffer_free(json->fbuffer);
838840
ruby_xfree(json);
839841
}
840842

843+
static size_t JSON_memsize(const void *ptr)
844+
{
845+
const JSON_Parser *json = ptr;
846+
return sizeof(*json) + FBUFFER_CAPA(json->fbuffer);
847+
}
848+
849+
static const rb_data_type_t JSON_Parser_type = {
850+
"JSON/Parser",
851+
{JSON_mark, JSON_free, JSON_memsize,},
852+
#ifdef RUBY_TYPED_FREE_IMMEDIATELY
853+
0, 0,
854+
RUBY_TYPED_FREE_IMMEDIATELY,
855+
#endif
856+
};
857+
841858
static VALUE cJSON_parser_s_allocate(VALUE klass)
842859
{
843860
JSON_Parser *json = JSON_allocate();
844-
return Data_Wrap_Struct(klass, JSON_mark, JSON_free, json);
861+
return TypedData_Wrap_Struct(klass, &JSON_Parser_type, json);
845862
}
846863

847864
/*

0 commit comments

Comments
 (0)