From 49f9883220c5ef7e764eafee4b09cbf515f8136c Mon Sep 17 00:00:00 2001 From: Lamont Granquist Date: Sun, 12 Jan 2014 20:13:55 -0800 Subject: wrap yajl_gen c struct for rbx --- ext/ffi_yajl/ext/encoder/encoder.c | 93 ++++++++++++++++++++++++-------------- 1 file changed, 60 insertions(+), 33 deletions(-) (limited to 'ext') diff --git a/ext/ffi_yajl/ext/encoder/encoder.c b/ext/ffi_yajl/ext/encoder/encoder.c index 073c6ff..6707632 100644 --- a/ext/ffi_yajl/ext/encoder/encoder.c +++ b/ext/ffi_yajl/ext/encoder/encoder.c @@ -2,6 +2,7 @@ #include static VALUE mFFI_Yajl, mExt, mEncoder, mEncoder2, cEncodeError; +static VALUE cYajl_Gen; /* FIXME: the json gem does a whole bunch of indirection around monkeypatching... not sure if we need to as well... */ @@ -19,6 +20,7 @@ static VALUE mEncoder_do_yajl_encode(VALUE self, VALUE obj, VALUE yajl_gen_opts) VALUE state; VALUE ret; VALUE indent_string; + VALUE rb_yajl_gen; yajl_gen = yajl_gen_alloc(NULL); @@ -40,7 +42,9 @@ static VALUE mEncoder_do_yajl_encode(VALUE self, VALUE obj, VALUE yajl_gen_opts) rb_hash_aset(state, rb_str_new2("processing_key"), Qfalse); - rb_funcall(obj, sym_ffi_yajl, 2, yajl_gen, state); + rb_yajl_gen = Data_Wrap_Struct(cYajl_Gen, NULL, NULL, yajl_gen); + + rb_funcall(obj, sym_ffi_yajl, 2, rb_yajl_gen, state); yajl_gen_get_buf(yajl_gen, &buf, &len); @@ -54,176 +58,198 @@ static VALUE mEncoder_do_yajl_encode(VALUE self, VALUE obj, VALUE yajl_gen_opts) int rb_cHash_ffi_yajl_callback(VALUE key, VALUE val, VALUE extra) { ID sym_ffi_yajl = rb_intern("ffi_yajl"); VALUE state = rb_hash_aref(extra, rb_str_new2("state")); - VALUE yajl_gen = rb_hash_aref(extra, rb_str_new2("yajl_gen")); + VALUE rb_yajl_gen = rb_hash_aref(extra, rb_str_new2("yajl_gen")); rb_hash_aset(state, rb_str_new2("processing_key"), Qtrue); - rb_funcall(key, sym_ffi_yajl, 2, yajl_gen, state); + rb_funcall(key, sym_ffi_yajl, 2, rb_yajl_gen, state); rb_hash_aset(state, rb_str_new2("processing_key"), Qfalse); - rb_funcall(val, sym_ffi_yajl, 2, yajl_gen, state); + rb_funcall(val, sym_ffi_yajl, 2, rb_yajl_gen, state); return 0; } -static VALUE rb_cHash_ffi_yajl(VALUE self, VALUE yajl_gen, VALUE state) { +static VALUE rb_cHash_ffi_yajl(VALUE self, VALUE rb_yajl_gen, VALUE state) { yajl_gen_status status; VALUE extra; + struct yajl_gen_t *yajl_gen; + Data_Get_Struct(rb_yajl_gen, struct yajl_gen_t, yajl_gen); extra = rb_hash_new(); /* FIXME: reduce garbage */ - rb_hash_aset(extra, rb_str_new2("yajl_gen"), yajl_gen); + rb_hash_aset(extra, rb_str_new2("yajl_gen"), rb_yajl_gen); rb_hash_aset(extra, rb_str_new2("state"), state); CHECK_STATUS( - yajl_gen_map_open((struct yajl_gen_t *) yajl_gen) + yajl_gen_map_open(yajl_gen) ); rb_hash_foreach(self, rb_cHash_ffi_yajl_callback, extra); CHECK_STATUS( - yajl_gen_map_close((struct yajl_gen_t *) yajl_gen) + yajl_gen_map_close(yajl_gen) ); return Qnil; } -static VALUE rb_cArray_ffi_yajl(VALUE self, VALUE yajl_gen, VALUE state) { +static VALUE rb_cArray_ffi_yajl(VALUE self, VALUE rb_yajl_gen, VALUE state) { yajl_gen_status status; ID sym_ffi_yajl = rb_intern("ffi_yajl"); long i; VALUE val; + struct yajl_gen_t *yajl_gen; + Data_Get_Struct(rb_yajl_gen, struct yajl_gen_t, yajl_gen); CHECK_STATUS( - yajl_gen_array_open((struct yajl_gen_t *) yajl_gen) + yajl_gen_array_open(yajl_gen) ); for(i=0; i