ffi-yajl 0.0.2-universal-java → 0.0.3-universal-java

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a82a3a5ac433463f78e468ed4f6f12d57047e847
4
- data.tar.gz: ac9aa998c59d3e92ea1b69a2eb2dc8370c189deb
3
+ metadata.gz: daf6de7794be628881f188aedebb67c615815e37
4
+ data.tar.gz: 6e97fbc2c9949ad9cda706001f91284ac5bb9a71
5
5
  SHA512:
6
- metadata.gz: b802c373b6b67a0388510095a47ee937c9b040646f17895e87138d5ddca85a9a8c18f6c3d552bbfebeaa7b6c44c570e7ec61be62dccedef008cc5bda16d4ca18
7
- data.tar.gz: 51ab0c3759c595ed85df07141a729edaa96c8d63f7bf016aa5c1cd2f7cf5e7e74de91d43419ae1872b2970e84e320d466654da2ef1db76c1af06767d663105c2
6
+ metadata.gz: 9b54009176b09579945529773a556f0a81830586bcd54c012f6fbf6dd8b6b36bb13587178b1bcf0291e168b5390e2a937b47bcb938d9a783dd672739238c22cf
7
+ data.tar.gz: d7ea87b526f45207613503615900929be5b2fedb90a3582f3461d3e67e82e78fe9b3880bbb39475eebdddf0303a44c029581ee2235de164e8cbe3c3aba410646
data/README.md CHANGED
@@ -3,7 +3,7 @@
3
3
 
4
4
  ## TODO
5
5
 
6
- - test both ffi and ext on platforms that support both (MRI, rbx)
6
+ - fix int conversion issue in chef specs in c extension
7
7
 
8
8
  ## BUILD NOTES
9
9
 
@@ -13,4 +13,3 @@
13
13
  ## KNOWN BUGS
14
14
 
15
15
  - 'rake compile' broken on mac, only tested to work on linux (ubuntu)
16
- - C Extension segfaults on ruby 1.8.7, so the ffi mode is forced for RUBY_VERSION < 1.9
data/Rakefile CHANGED
@@ -9,14 +9,30 @@ end
9
9
 
10
10
  require 'ffi_yajl/version'
11
11
 
12
- desc "Run all specs in spec directory"
13
- RSpec::Core::RakeTask.new(:spec) do |t|
14
- t.pattern = FileList['spec/**/*_spec.rb']
12
+ desc "Run all specs against both extensions"
13
+ task :spec do
14
+ Rake::Task["spec:ffi"].invoke
15
+ Rake::Task["spec:ext"].invoke
16
+ end
17
+
18
+ namespace :spec do
19
+ desc "Run all specs against ffi extension"
20
+ RSpec::Core::RakeTask.new(:ffi) do |t|
21
+ ENV['FORCE_FFI_YAJL'] = "ffi"
22
+ t.pattern = FileList['spec/**/*_spec.rb']
23
+ end
24
+ if !defined?(RUBY_ENGINE) || RUBY_ENGINE !~ /jruby/
25
+ desc "Run all specs again c extension"
26
+ RSpec::Core::RakeTask.new(:ext) do |t|
27
+ ENV['FORCE_FFI_YAJL'] = "ext"
28
+ t.pattern = FileList['spec/**/*_spec.rb']
29
+ end
30
+ end
15
31
  end
16
32
 
17
33
  desc "Build it and ship it"
18
34
  task :ship => [:clean, :gem] do
19
- sh("git tag #{Mixlib::ShellOut::VERSION}")
35
+ sh("git tag #{FFI_Yajl::VERSION}")
20
36
  sh("git push --tags")
21
37
  Dir[File.expand_path("../pkg/*.gem", __FILE__)].reverse.each do |built_gem|
22
38
  sh("gem push #{built_gem}")
@@ -2,16 +2,12 @@
2
2
  #include <yajl/yajl_gen.h>
3
3
 
4
4
  static VALUE mFFI_Yajl, mExt, mEncoder, mEncoder2, cEncodeError;
5
+ static VALUE cYajl_Gen;
5
6
 
6
7
  /* FIXME: the json gem does a whole bunch of indirection around monkeypatching... not sure if we need to as well... */
7
8
 
8
9
  #define CHECK_STATUS(call) \
9
- if ((status = (call)) != yajl_gen_status_ok) { rb_funcall(mEncoder2, rb_intern("raise_error_for_status"), 1, status); }
10
-
11
- typedef struct {
12
- VALUE json_opts;
13
- int processing_key;
14
- } ffi_state_t;
10
+ if ((status = (call)) != yajl_gen_status_ok) { rb_funcall(mEncoder2, rb_intern("raise_error_for_status"), 1, INT2FIX(status)); }
15
11
 
16
12
  static VALUE mEncoder_do_yajl_encode(VALUE self, VALUE obj, VALUE yajl_gen_opts) {
17
13
  ID sym_ffi_yajl = rb_intern("ffi_yajl");
@@ -21,9 +17,10 @@ static VALUE mEncoder_do_yajl_encode(VALUE self, VALUE obj, VALUE yajl_gen_opts)
21
17
  yajl_gen yajl_gen;
22
18
  const unsigned char *buf;
23
19
  size_t len;
24
- ffi_state_t state;
20
+ VALUE state;
25
21
  VALUE ret;
26
22
  VALUE indent_string;
23
+ VALUE rb_yajl_gen;
27
24
 
28
25
  yajl_gen = yajl_gen_alloc(NULL);
29
26
 
@@ -41,9 +38,13 @@ static VALUE mEncoder_do_yajl_encode(VALUE self, VALUE obj, VALUE yajl_gen_opts)
41
38
  yajl_gen_config(yajl_gen, yajl_gen_indent_string, " ");
42
39
  }
43
40
 
44
- state.processing_key = 0;
41
+ state = rb_hash_new();
42
+
43
+ rb_hash_aset(state, rb_str_new2("processing_key"), Qfalse);
45
44
 
46
- rb_funcall(obj, sym_ffi_yajl, 2, yajl_gen, (VALUE) &state);
45
+ rb_yajl_gen = Data_Wrap_Struct(cYajl_Gen, NULL, NULL, yajl_gen);
46
+
47
+ rb_funcall(obj, sym_ffi_yajl, 2, rb_yajl_gen, state);
47
48
 
48
49
  yajl_gen_get_buf(yajl_gen, &buf, &len);
49
50
 
@@ -54,177 +55,201 @@ static VALUE mEncoder_do_yajl_encode(VALUE self, VALUE obj, VALUE yajl_gen_opts)
54
55
  return ret;
55
56
  }
56
57
 
57
- typedef struct {
58
- VALUE yajl_gen;
59
- ffi_state_t *state;
60
- } ffs_extra_t;
61
-
62
58
  int rb_cHash_ffi_yajl_callback(VALUE key, VALUE val, VALUE extra) {
63
- ffs_extra_t *extra_p = (ffs_extra_t *)extra;
64
59
  ID sym_ffi_yajl = rb_intern("ffi_yajl");
60
+ VALUE state = rb_hash_aref(extra, rb_str_new2("state"));
61
+ VALUE rb_yajl_gen = rb_hash_aref(extra, rb_str_new2("yajl_gen"));
65
62
 
66
- extra_p->state->processing_key = 1;
67
- rb_funcall(key, sym_ffi_yajl, 2, extra_p->yajl_gen, extra_p->state);
68
- extra_p->state->processing_key = 0;
69
- rb_funcall(val, sym_ffi_yajl, 2, extra_p->yajl_gen, extra_p->state);
63
+
64
+ rb_hash_aset(state, rb_str_new2("processing_key"), Qtrue);
65
+ rb_funcall(key, sym_ffi_yajl, 2, rb_yajl_gen, state);
66
+ rb_hash_aset(state, rb_str_new2("processing_key"), Qfalse);
67
+
68
+ rb_funcall(val, sym_ffi_yajl, 2, rb_yajl_gen, state);
70
69
 
71
70
  return 0;
72
71
  }
73
72
 
74
- static VALUE rb_cHash_ffi_yajl(VALUE self, VALUE yajl_gen, VALUE state) {
73
+ static VALUE rb_cHash_ffi_yajl(VALUE self, VALUE rb_yajl_gen, VALUE state) {
75
74
  yajl_gen_status status;
76
- ffs_extra_t extra;
75
+ VALUE extra;
76
+ struct yajl_gen_t *yajl_gen;
77
+ Data_Get_Struct(rb_yajl_gen, struct yajl_gen_t, yajl_gen);
78
+
79
+ extra = rb_hash_new(); /* FIXME: reduce garbage */
80
+
81
+ rb_hash_aset(extra, rb_str_new2("yajl_gen"), rb_yajl_gen);
77
82
 
78
- extra.yajl_gen = yajl_gen;
79
- extra.state = (ffi_state_t *)state;
83
+ rb_hash_aset(extra, rb_str_new2("state"), state);
80
84
 
81
85
  CHECK_STATUS(
82
- yajl_gen_map_open((struct yajl_gen_t *) yajl_gen)
86
+ yajl_gen_map_open(yajl_gen)
83
87
  );
84
- rb_hash_foreach(self, rb_cHash_ffi_yajl_callback, (VALUE) &extra);
88
+ rb_hash_foreach(self, rb_cHash_ffi_yajl_callback, extra);
85
89
  CHECK_STATUS(
86
- yajl_gen_map_close((struct yajl_gen_t *) yajl_gen)
90
+ yajl_gen_map_close(yajl_gen)
87
91
  );
88
92
 
89
93
  return Qnil;
90
94
  }
91
95
 
92
- static VALUE rb_cArray_ffi_yajl(VALUE self, VALUE yajl_gen, VALUE state) {
96
+ static VALUE rb_cArray_ffi_yajl(VALUE self, VALUE rb_yajl_gen, VALUE state) {
93
97
  yajl_gen_status status;
94
98
  ID sym_ffi_yajl = rb_intern("ffi_yajl");
95
99
  long i;
96
100
  VALUE val;
101
+ struct yajl_gen_t *yajl_gen;
102
+ Data_Get_Struct(rb_yajl_gen, struct yajl_gen_t, yajl_gen);
97
103
 
98
104
  CHECK_STATUS(
99
- yajl_gen_array_open((struct yajl_gen_t *) yajl_gen)
105
+ yajl_gen_array_open(yajl_gen)
100
106
  );
101
107
  for(i=0; i<RARRAY_LEN(self); i++) {
102
108
  val = rb_ary_entry(self, i);
103
- rb_funcall(val, sym_ffi_yajl, 2, yajl_gen, state);
109
+ rb_funcall(val, sym_ffi_yajl, 2, rb_yajl_gen, state);
104
110
  }
105
111
  CHECK_STATUS(
106
- yajl_gen_array_close((struct yajl_gen_t *) yajl_gen)
112
+ yajl_gen_array_close(yajl_gen)
107
113
  );
108
114
 
109
115
  return Qnil;
110
116
  }
111
117
 
112
- static VALUE rb_cNilClass_ffi_yajl(VALUE self, VALUE yajl_gen, VALUE state) {
118
+ static VALUE rb_cNilClass_ffi_yajl(VALUE self, VALUE rb_yajl_gen, VALUE state) {
113
119
  yajl_gen_status status;
120
+ struct yajl_gen_t *yajl_gen;
121
+ Data_Get_Struct(rb_yajl_gen, struct yajl_gen_t, yajl_gen);
114
122
  CHECK_STATUS(
115
- yajl_gen_null((struct yajl_gen_t *) yajl_gen)
123
+ yajl_gen_null(yajl_gen)
116
124
  );
117
125
  return Qnil;
118
126
  }
119
127
 
120
- static VALUE rb_cTrueClass_ffi_yajl(VALUE self, VALUE yajl_gen, VALUE state) {
128
+ static VALUE rb_cTrueClass_ffi_yajl(VALUE self, VALUE rb_yajl_gen, VALUE state) {
121
129
  yajl_gen_status status;
130
+ struct yajl_gen_t *yajl_gen;
131
+ Data_Get_Struct(rb_yajl_gen, struct yajl_gen_t, yajl_gen);
122
132
  CHECK_STATUS(
123
- yajl_gen_bool((struct yajl_gen_t *) yajl_gen, 1)
133
+ yajl_gen_bool(yajl_gen, 1)
124
134
  );
125
135
  return Qnil;
126
136
  }
127
137
 
128
- static VALUE rb_cFalseClass_ffi_yajl(VALUE self, VALUE yajl_gen, VALUE state) {
138
+ static VALUE rb_cFalseClass_ffi_yajl(VALUE self, VALUE rb_yajl_gen, VALUE state) {
129
139
  yajl_gen_status status;
140
+ struct yajl_gen_t *yajl_gen;
141
+ Data_Get_Struct(rb_yajl_gen, struct yajl_gen_t, yajl_gen);
130
142
  CHECK_STATUS(
131
- yajl_gen_bool((struct yajl_gen_t *) yajl_gen, 0)
143
+ yajl_gen_bool(yajl_gen, 0)
132
144
  );
133
145
  return Qnil;
134
146
  }
135
147
 
136
- static VALUE rb_cFixnum_ffi_yajl(VALUE self, VALUE yajl_gen, VALUE state) {
148
+ static VALUE rb_cFixnum_ffi_yajl(VALUE self, VALUE rb_yajl_gen, VALUE state) {
137
149
  yajl_gen_status status;
138
150
  ID sym_to_s = rb_intern("to_s");
139
151
  VALUE str = rb_funcall(self, sym_to_s, 0);
140
152
  char *cptr = RSTRING_PTR(str);
141
153
  int len = RSTRING_LEN(str);
154
+ struct yajl_gen_t *yajl_gen;
155
+ Data_Get_Struct(rb_yajl_gen, struct yajl_gen_t, yajl_gen);
142
156
 
143
157
  if (memcmp(cptr, "NaN", 3) == 0 || memcmp(cptr, "Infinity", 8) == 0 || memcmp(cptr, "-Infinity", 9) == 0) {
144
158
  rb_raise(cEncodeError, "'%s' is an invalid number", cptr);
145
159
  }
146
- if ( ((ffi_state_t *)state)->processing_key ) {
160
+ if ( rb_hash_aref(state, rb_str_new2("processing_key")) == Qtrue ) {
147
161
  CHECK_STATUS(
148
- yajl_gen_string((struct yajl_gen_t *) yajl_gen, (unsigned char *)cptr, len)
162
+ yajl_gen_string(yajl_gen, (unsigned char *)cptr, len)
149
163
  );
150
164
  } else {
151
165
  CHECK_STATUS(
152
- yajl_gen_integer((struct yajl_gen_t *) yajl_gen, NUM2INT(self))
166
+ yajl_gen_integer(yajl_gen, NUM2INT(self))
153
167
  );
154
168
  }
155
169
  return Qnil;
156
170
  }
157
171
 
158
- static VALUE rb_cBignum_ffi_yajl(VALUE self, VALUE yajl_gen, VALUE state) {
172
+ static VALUE rb_cBignum_ffi_yajl(VALUE self, VALUE rb_yajl_gen, VALUE state) {
159
173
  yajl_gen_status status;
160
174
  ID sym_to_s = rb_intern("to_s");
161
175
  VALUE str = rb_funcall(self, sym_to_s, 0);
162
176
  char *cptr = RSTRING_PTR(str);
163
177
  int len = RSTRING_LEN(str);
178
+ struct yajl_gen_t *yajl_gen;
179
+ Data_Get_Struct(rb_yajl_gen, struct yajl_gen_t, yajl_gen);
164
180
  if (memcmp(cptr, "NaN", 3) == 0 || memcmp(cptr, "Infinity", 8) == 0 || memcmp(cptr, "-Infinity", 9) == 0) {
165
181
  rb_raise(cEncodeError, "'%s' is an invalid number", cptr);
166
182
  }
167
- if ( ((ffi_state_t *)state)->processing_key ) {
183
+ if ( rb_hash_aref(state, rb_str_new2("processing_key")) == Qtrue ) {
168
184
  CHECK_STATUS(
169
- yajl_gen_string((struct yajl_gen_t *) yajl_gen, (unsigned char *)cptr, len)
185
+ yajl_gen_string(yajl_gen, (unsigned char *)cptr, len)
170
186
  );
171
187
  } else {
172
188
  CHECK_STATUS(
173
- yajl_gen_number((struct yajl_gen_t *) yajl_gen, cptr, len)
189
+ yajl_gen_number(yajl_gen, cptr, len)
174
190
  );
175
191
  }
176
192
  return Qnil;
177
193
  }
178
194
 
179
- static VALUE rb_cFloat_ffi_yajl(VALUE self, VALUE yajl_gen, VALUE state) {
195
+ static VALUE rb_cFloat_ffi_yajl(VALUE self, VALUE rb_yajl_gen, VALUE state) {
180
196
  yajl_gen_status status;
181
197
  ID sym_to_s = rb_intern("to_s");
182
198
  VALUE str = rb_funcall(self, sym_to_s, 0);
183
199
  char *cptr = RSTRING_PTR(str);
184
200
  int len = RSTRING_LEN(str);
201
+ struct yajl_gen_t *yajl_gen;
202
+ Data_Get_Struct(rb_yajl_gen, struct yajl_gen_t, yajl_gen);
185
203
  if (memcmp(cptr, "NaN", 3) == 0 || memcmp(cptr, "Infinity", 8) == 0 || memcmp(cptr, "-Infinity", 9) == 0) {
186
204
  rb_raise(cEncodeError, "'%s' is an invalid number", cptr);
187
205
  }
188
- if ( ((ffi_state_t *)state)->processing_key ) {
206
+ if ( rb_hash_aref(state, rb_str_new2("processing_key")) == Qtrue ) {
189
207
  CHECK_STATUS(
190
- yajl_gen_string((struct yajl_gen_t *) yajl_gen, (unsigned char *)cptr, len)
208
+ yajl_gen_string(yajl_gen, (unsigned char *)cptr, len)
191
209
  );
192
210
  } else {
193
211
  CHECK_STATUS(
194
- yajl_gen_number((struct yajl_gen_t *) yajl_gen, cptr, len)
212
+ yajl_gen_number(yajl_gen, cptr, len)
195
213
  );
196
214
  }
197
215
  return Qnil;
198
216
  }
199
217
 
200
- static VALUE rb_cString_ffi_yajl(VALUE self, VALUE yajl_gen, VALUE state) {
218
+ static VALUE rb_cString_ffi_yajl(VALUE self, VALUE rb_yajl_gen, VALUE state) {
201
219
  yajl_gen_status status;
220
+ struct yajl_gen_t *yajl_gen;
221
+ Data_Get_Struct(rb_yajl_gen, struct yajl_gen_t, yajl_gen);
202
222
  CHECK_STATUS(
203
- yajl_gen_string((struct yajl_gen_t *) yajl_gen, (unsigned char *)RSTRING_PTR(self), RSTRING_LEN(self))
223
+ yajl_gen_string(yajl_gen, (unsigned char *)RSTRING_PTR(self), RSTRING_LEN(self))
204
224
  );
205
225
  return Qnil;
206
226
  }
207
227
 
208
- static VALUE rb_cSymbol_ffi_yajl(VALUE self, VALUE yajl_gen, VALUE state) {
228
+ static VALUE rb_cSymbol_ffi_yajl(VALUE self, VALUE rb_yajl_gen, VALUE state) {
209
229
  yajl_gen_status status;
210
230
  ID sym_to_s = rb_intern("to_s");
211
231
  VALUE str = rb_funcall(self, sym_to_s, 0);
212
232
  char *cptr = RSTRING_PTR(str);
213
233
  int len = RSTRING_LEN(str);
234
+ struct yajl_gen_t *yajl_gen;
235
+ Data_Get_Struct(rb_yajl_gen, struct yajl_gen_t, yajl_gen);
214
236
  CHECK_STATUS(
215
- yajl_gen_string((struct yajl_gen_t *) yajl_gen, (unsigned char *)cptr, len)
237
+ yajl_gen_string(yajl_gen, (unsigned char *)cptr, len)
216
238
  );
217
239
  return Qnil;
218
240
  }
219
241
 
220
- static VALUE rb_cObject_ffi_yajl(VALUE self, VALUE yajl_gen, VALUE state) {
242
+ static VALUE rb_cObject_ffi_yajl(VALUE self, VALUE rb_yajl_gen, VALUE state) {
221
243
  yajl_gen_status status;
222
244
  ID sym_to_json = rb_intern("to_json");
223
245
  VALUE str;
246
+ VALUE json_opts = rb_hash_aref(state, rb_str_new2("json_opts"));
247
+ struct yajl_gen_t *yajl_gen;
248
+ Data_Get_Struct(rb_yajl_gen, struct yajl_gen_t, yajl_gen);
224
249
 
225
- str = rb_funcall(self, sym_to_json, 1, ((ffi_state_t *)state)->json_opts);
250
+ str = rb_funcall(self, sym_to_json, 1, json_opts);
226
251
  CHECK_STATUS(
227
- yajl_gen_number((struct yajl_gen_t *) yajl_gen, (char *)RSTRING_PTR(str), RSTRING_LEN(str))
252
+ yajl_gen_number(yajl_gen, (char *)RSTRING_PTR(str), RSTRING_LEN(str))
228
253
  );
229
254
  return Qnil;
230
255
  }
@@ -235,6 +260,7 @@ void Init_encoder() {
235
260
  cEncodeError = rb_define_class_under(mFFI_Yajl, "EncodeError", rb_eStandardError);
236
261
  mExt = rb_define_module_under(mFFI_Yajl, "Ext");
237
262
  mEncoder = rb_define_module_under(mExt, "Encoder");
263
+ cYajl_Gen = rb_define_class_under(mEncoder, "YajlGen", rb_cObject);
238
264
  rb_define_method(mEncoder, "do_yajl_encode", mEncoder_do_yajl_encode, 2);
239
265
 
240
266
  rb_define_method(rb_cHash, "ffi_yajl", rb_cHash_ffi_yajl, 2);
data/lib/ffi_yajl/ext.rb CHANGED
@@ -3,10 +3,10 @@ require 'rubygems'
3
3
  require 'ffi_yajl/encoder'
4
4
  require 'ffi_yajl/parser'
5
5
 
6
- unless RUBY_VERSION.to_f >= 1.9
7
- # segfaults on ruby 1.8 and this is an exceedingly low priority to fix, use ffi instead
8
- raise NotImplementedError, "The C-extension is disabled on Ruby 1.8"
9
- end
6
+ #unless RUBY_VERSION.to_f >= 1.9
7
+ # # segfaults on ruby 1.8 and this is an exceedingly low priority to fix, use ffi instead
8
+ # raise NotImplementedError, "The C-extension is disabled on Ruby 1.8"
9
+ #end
10
10
 
11
11
  module FFI_Yajl
12
12
  class Parser
@@ -1,3 +1,3 @@
1
1
  module FFI_Yajl
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ffi-yajl
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: universal-java
6
6
  authors:
7
7
  - Lamont Granquist
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-01-11 00:00:00.000000000 Z
11
+ date: 2014-01-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake