diff options
author | Zachary Scott <e@zzak.io> | 2014-12-25 15:43:37 -0500 |
---|---|---|
committer | Zachary Scott <e@zzak.io> | 2014-12-25 15:43:37 -0500 |
commit | 18b3000090d1044425d369a58ebe52f5342b9699 (patch) | |
tree | bf0bd35737237b5f178461c969c017fa39a5f51c /ext/json/ext/generator | |
parent | 17fe8e72f484e65e0c36670946934d1f8696e37b (diff) | |
download | json-18b3000090d1044425d369a58ebe52f5342b9699.tar.gz |
Sync with trunk
Diffstat (limited to 'ext/json/ext/generator')
-rw-r--r-- | ext/json/ext/generator/depend | 20 | ||||
-rw-r--r-- | ext/json/ext/generator/extconf.rb | 10 | ||||
-rw-r--r-- | ext/json/ext/generator/generator.c | 63 | ||||
-rw-r--r-- | ext/json/ext/generator/generator.h | 14 |
4 files changed, 72 insertions, 35 deletions
diff --git a/ext/json/ext/generator/depend b/ext/json/ext/generator/depend index 1a042a2..b7373cd 100644 --- a/ext/json/ext/generator/depend +++ b/ext/json/ext/generator/depend @@ -1 +1,21 @@ +$(OBJS): $(ruby_headers) generator.o: generator.c generator.h $(srcdir)/../fbuffer/fbuffer.h + +# AUTOGENERATED DEPENDENCIES START +generator.o: $(RUBY_EXTCONF_H) +generator.o: $(arch_hdrdir)/ruby/config.h +generator.o: $(hdrdir)/ruby/defines.h +generator.o: $(hdrdir)/ruby/encoding.h +generator.o: $(hdrdir)/ruby/intern.h +generator.o: $(hdrdir)/ruby/missing.h +generator.o: $(hdrdir)/ruby/oniguruma.h +generator.o: $(hdrdir)/ruby/re.h +generator.o: $(hdrdir)/ruby/regex.h +generator.o: $(hdrdir)/ruby/ruby.h +generator.o: $(hdrdir)/ruby/st.h +generator.o: $(hdrdir)/ruby/subst.h +generator.o: $(top_srcdir)/ext/json/fbuffer/fbuffer.h +generator.o: $(top_srcdir)/include/ruby.h +generator.o: generator.c +generator.o: generator.h +# AUTOGENERATED DEPENDENCIES END diff --git a/ext/json/ext/generator/extconf.rb b/ext/json/ext/generator/extconf.rb index c947501..8627c5f 100644 --- a/ext/json/ext/generator/extconf.rb +++ b/ext/json/ext/generator/extconf.rb @@ -1,14 +1,4 @@ require 'mkmf' -unless $CFLAGS.gsub!(/ -O[\dsz]?/, ' -O3') - $CFLAGS << ' -O3' -end -if CONFIG['CC'] =~ /gcc/ - $CFLAGS << ' -Wall' - unless $DEBUG && !$CFLAGS.gsub!(/ -O[\dsz]?/, ' -O0 -ggdb') - $CFLAGS << ' -O0 -ggdb' - end -end - $defs << "-DJSON_GENERATOR" create_makefile 'json/ext/generator' diff --git a/ext/json/ext/generator/generator.c b/ext/json/ext/generator/generator.c index 976afc5..f56ac09 100644 --- a/ext/json/ext/generator/generator.c +++ b/ext/json/ext/generator/generator.c @@ -486,8 +486,9 @@ static VALUE mObject_to_json(int argc, VALUE *argv, VALUE self) return cState_partial_generate(state, string); } -static void State_free(JSON_Generator_State *state) +static void State_free(void *ptr) { + JSON_Generator_State *state = ptr; if (state->indent) ruby_xfree(state->indent); if (state->space) ruby_xfree(state->space); if (state->space_before) ruby_xfree(state->space_before); @@ -499,17 +500,38 @@ static void State_free(JSON_Generator_State *state) ruby_xfree(state); } -static JSON_Generator_State *State_allocate() +static size_t State_memsize(const void *ptr) { - JSON_Generator_State *state = ALLOC(JSON_Generator_State); - MEMZERO(state, JSON_Generator_State, 1); + const JSON_Generator_State *state = ptr; + size_t size = sizeof(*state); + if (state->indent) size += state->indent_len + 1; + if (state->space) size += state->space_len + 1; + if (state->space_before) size += state->space_before_len + 1; + if (state->object_nl) size += state->object_nl_len + 1; + if (state->array_nl) size += state->array_nl_len + 1; + if (state->array_delim) size += FBUFFER_CAPA(state->array_delim); + if (state->object_delim) size += FBUFFER_CAPA(state->object_delim); + if (state->object_delim2) size += FBUFFER_CAPA(state->object_delim2); + return size; +} + +static const rb_data_type_t JSON_Generator_State_type = { + "JSON/Generator/State", + {NULL, State_free, State_memsize,}, + 0, 0, + RUBY_TYPED_FREE_IMMEDIATELY, +}; + +static JSON_Generator_State *State_allocate(void) +{ + JSON_Generator_State *state = ZALLOC(JSON_Generator_State); return state; } static VALUE cState_s_allocate(VALUE klass) { JSON_Generator_State *state = State_allocate(); - return Data_Wrap_Struct(klass, NULL, State_free, state); + return TypedData_Wrap_Struct(klass, &JSON_Generator_State_type, state); } /* @@ -646,7 +668,7 @@ static VALUE cState_to_h(VALUE self) /* * call-seq: [](name) * -* Returns the value returned by method +name+. +* Return the value returned by method +name+. */ static VALUE cState_aref(VALUE self, VALUE name) { @@ -661,7 +683,7 @@ static VALUE cState_aref(VALUE self, VALUE name) /* * call-seq: []=(name, value) * -* Sets the attribute name to value. +* Set the attribute name to value. */ static VALUE cState_aset(VALUE self, VALUE name, VALUE value) { @@ -812,10 +834,10 @@ static void generate_json_float(FBuffer *buffer, VALUE Vstate, JSON_Generator_St if (!allow_nan) { if (isinf(value)) { fbuffer_free(buffer); - rb_raise(eGeneratorError, "%u: %s not allowed in JSON", __LINE__, StringValueCStr(tmp)); + rb_raise(eGeneratorError, "%u: %"PRIsVALUE" not allowed in JSON", __LINE__, RB_OBJ_STRING(tmp)); } else if (isnan(value)) { fbuffer_free(buffer); - rb_raise(eGeneratorError, "%u: %s not allowed in JSON", __LINE__, StringValueCStr(tmp)); + rb_raise(eGeneratorError, "%u: %"PRIsVALUE" not allowed in JSON", __LINE__, RB_OBJ_STRING(tmp)); } } fbuffer_append_str(buffer, tmp); @@ -958,15 +980,16 @@ static VALUE cState_initialize(int argc, VALUE *argv, VALUE self) /* * call-seq: initialize_copy(orig) * - * Initializes this object from orig if it can be duplicated/cloned and returns + * Initializes this object from orig if it to be duplicated/cloned and returns * it. */ static VALUE cState_init_copy(VALUE obj, VALUE orig) { JSON_Generator_State *objState, *origState; - Data_Get_Struct(obj, JSON_Generator_State, objState); - Data_Get_Struct(orig, JSON_Generator_State, origState); + if (obj == orig) return obj; + GET_STATE_TO(obj, objState); + GET_STATE_TO(orig, origState); if (!objState) rb_raise(rb_eArgError, "unallocated JSON::State"); MEMCPY(objState, origState, JSON_Generator_State, 1); @@ -1005,7 +1028,7 @@ static VALUE cState_from_state_s(VALUE self, VALUE opts) /* * call-seq: indent() * - * Returns the string that is used to indent levels in the JSON text. + * This string is used to indent levels in the JSON text. */ static VALUE cState_indent(VALUE self) { @@ -1016,7 +1039,7 @@ static VALUE cState_indent(VALUE self) /* * call-seq: indent=(indent) * - * Sets the string that is used to indent levels in the JSON text. + * This string is used to indent levels in the JSON text. */ static VALUE cState_indent_set(VALUE self, VALUE indent) { @@ -1041,7 +1064,7 @@ static VALUE cState_indent_set(VALUE self, VALUE indent) /* * call-seq: space() * - * Returns the string that is used to insert a space between the tokens in a JSON + * This string is used to insert a space between the tokens in a JSON * string. */ static VALUE cState_space(VALUE self) @@ -1053,7 +1076,7 @@ static VALUE cState_space(VALUE self) /* * call-seq: space=(space) * - * Sets _space_ to the string that is used to insert a space between the tokens in a JSON + * This string is used to insert a space between the tokens in a JSON * string. */ static VALUE cState_space_set(VALUE self, VALUE space) @@ -1079,7 +1102,7 @@ static VALUE cState_space_set(VALUE self, VALUE space) /* * call-seq: space_before() * - * Returns the string that is used to insert a space before the ':' in JSON objects. + * This string is used to insert a space before the ':' in JSON objects. */ static VALUE cState_space_before(VALUE self) { @@ -1090,7 +1113,7 @@ static VALUE cState_space_before(VALUE self) /* * call-seq: space_before=(space_before) * - * Sets the string that is used to insert a space before the ':' in JSON objects. + * This string is used to insert a space before the ':' in JSON objects. */ static VALUE cState_space_before_set(VALUE self, VALUE space_before) { @@ -1297,7 +1320,7 @@ static VALUE cState_depth_set(VALUE self, VALUE depth) /* * call-seq: buffer_initial_length * - * This integer returns the current initial length of the buffer. + * This integer returns the current inital length of the buffer. */ static VALUE cState_buffer_initial_length(VALUE self) { @@ -1326,7 +1349,7 @@ static VALUE cState_buffer_initial_length_set(VALUE self, VALUE buffer_initial_l /* * */ -void Init_generator() +void Init_generator(void) { rb_require("json/common"); diff --git a/ext/json/ext/generator/generator.h b/ext/json/ext/generator/generator.h index e2fbf12..ddd1aa8 100644 --- a/ext/json/ext/generator/generator.h +++ b/ext/json/ext/generator/generator.h @@ -23,7 +23,7 @@ #define option_given_p(opts, key) RTEST(rb_funcall(opts, i_key_p, 1, key)) -/* unicode definitions */ +/* unicode defintions */ #define UNI_STRICT_CONVERSION 1 @@ -78,9 +78,12 @@ typedef struct JSON_Generator_StateStruct { long buffer_initial_length; } JSON_Generator_State; +#define GET_STATE_TO(self, state) \ + TypedData_Get_Struct(self, JSON_Generator_State, &JSON_Generator_State_type, state) + #define GET_STATE(self) \ JSON_Generator_State *state; \ - Data_Get_Struct(self, JSON_Generator_State, state) + GET_STATE_TO(self, state) #define GENERATE_JSON(type) \ FBuffer *buffer; \ @@ -89,7 +92,7 @@ typedef struct JSON_Generator_StateStruct { \ rb_scan_args(argc, argv, "01", &Vstate); \ Vstate = cState_from_state_s(cState, Vstate); \ - Data_Get_Struct(Vstate, JSON_Generator_State, state); \ + TypedData_Get_Struct(Vstate, JSON_Generator_State, &JSON_Generator_State_type, state); \ buffer = cState_prepare_buffer(Vstate); \ generate_json_##type(buffer, Vstate, state, self); \ return fbuffer_to_s(buffer) @@ -108,8 +111,8 @@ static VALUE mTrueClass_to_json(int argc, VALUE *argv, VALUE self); static VALUE mFalseClass_to_json(int argc, VALUE *argv, VALUE self); static VALUE mNilClass_to_json(int argc, VALUE *argv, VALUE self); static VALUE mObject_to_json(int argc, VALUE *argv, VALUE self); -static void State_free(JSON_Generator_State *state); -static JSON_Generator_State *State_allocate(); +static void State_free(void *state); +static JSON_Generator_State *State_allocate(void); static VALUE cState_s_allocate(VALUE klass); static VALUE cState_configure(VALUE self, VALUE opts); static VALUE cState_to_h(VALUE self); @@ -144,5 +147,6 @@ static VALUE cState_ascii_only_p(VALUE self); static VALUE cState_depth(VALUE self); static VALUE cState_depth_set(VALUE self, VALUE depth); static FBuffer *cState_prepare_buffer(VALUE self); +static const rb_data_type_t JSON_Generator_State_type; #endif |