ed-precompiled_msgpack 1.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (55) hide show
  1. checksums.yaml +7 -0
  2. data/ChangeLog +368 -0
  3. data/LICENSE +177 -0
  4. data/README.md +302 -0
  5. data/ext/java/org/msgpack/jruby/Buffer.java +233 -0
  6. data/ext/java/org/msgpack/jruby/Decoder.java +307 -0
  7. data/ext/java/org/msgpack/jruby/Encoder.java +456 -0
  8. data/ext/java/org/msgpack/jruby/ExtensionRegistry.java +167 -0
  9. data/ext/java/org/msgpack/jruby/ExtensionValue.java +128 -0
  10. data/ext/java/org/msgpack/jruby/Factory.java +130 -0
  11. data/ext/java/org/msgpack/jruby/MessagePackLibrary.java +45 -0
  12. data/ext/java/org/msgpack/jruby/Packer.java +266 -0
  13. data/ext/java/org/msgpack/jruby/Types.java +37 -0
  14. data/ext/java/org/msgpack/jruby/Unpacker.java +336 -0
  15. data/ext/msgpack/buffer.c +669 -0
  16. data/ext/msgpack/buffer.h +604 -0
  17. data/ext/msgpack/buffer_class.c +616 -0
  18. data/ext/msgpack/buffer_class.h +33 -0
  19. data/ext/msgpack/compat.h +26 -0
  20. data/ext/msgpack/extconf.rb +53 -0
  21. data/ext/msgpack/extension_value_class.c +34 -0
  22. data/ext/msgpack/extension_value_class.h +31 -0
  23. data/ext/msgpack/factory_class.c +276 -0
  24. data/ext/msgpack/factory_class.h +33 -0
  25. data/ext/msgpack/packer.c +199 -0
  26. data/ext/msgpack/packer.h +513 -0
  27. data/ext/msgpack/packer_class.c +442 -0
  28. data/ext/msgpack/packer_class.h +43 -0
  29. data/ext/msgpack/packer_ext_registry.c +74 -0
  30. data/ext/msgpack/packer_ext_registry.h +140 -0
  31. data/ext/msgpack/rbinit.c +35 -0
  32. data/ext/msgpack/rmem.c +93 -0
  33. data/ext/msgpack/rmem.h +109 -0
  34. data/ext/msgpack/sysdep.h +118 -0
  35. data/ext/msgpack/sysdep_endian.h +50 -0
  36. data/ext/msgpack/sysdep_types.h +46 -0
  37. data/ext/msgpack/unpacker.c +986 -0
  38. data/ext/msgpack/unpacker.h +152 -0
  39. data/ext/msgpack/unpacker_class.c +447 -0
  40. data/ext/msgpack/unpacker_class.h +43 -0
  41. data/ext/msgpack/unpacker_ext_registry.c +74 -0
  42. data/ext/msgpack/unpacker_ext_registry.h +62 -0
  43. data/lib/msgpack/bigint.rb +69 -0
  44. data/lib/msgpack/buffer.rb +9 -0
  45. data/lib/msgpack/core_ext.rb +139 -0
  46. data/lib/msgpack/factory.rb +211 -0
  47. data/lib/msgpack/packer.rb +37 -0
  48. data/lib/msgpack/symbol.rb +26 -0
  49. data/lib/msgpack/time.rb +29 -0
  50. data/lib/msgpack/timestamp.rb +76 -0
  51. data/lib/msgpack/unpacker.rb +41 -0
  52. data/lib/msgpack/version.rb +6 -0
  53. data/lib/msgpack.rb +53 -0
  54. data/msgpack.gemspec +41 -0
  55. metadata +216 -0
@@ -0,0 +1,53 @@
1
+ require 'mkmf'
2
+
3
+ have_func("rb_enc_interned_str", "ruby.h") # Ruby 3.0+
4
+ have_func("rb_hash_new_capa", "ruby.h") # Ruby 3.2+
5
+ have_func("rb_proc_call_with_block", "ruby.h") # CRuby (TruffleRuby doesn't have it)
6
+ have_func("rb_gc_mark_locations", "ruby.h") # Missing on TruffleRuby
7
+
8
+ append_cflags([
9
+ "-fvisibility=hidden",
10
+ "-I..",
11
+ "-Wall",
12
+ "-std=gnu99"
13
+ ])
14
+
15
+ if ENV["MSGPACK_DEBUG"]
16
+ append_cflags(RbConfig::CONFIG["debugflags"]) if RbConfig::CONFIG["debugflags"]
17
+ append_cflags("-DRUBY_DEBUG=1")
18
+ end
19
+
20
+ if RUBY_VERSION.start_with?('3.0.') && RUBY_VERSION <= '3.0.5'
21
+ # https://bugs.ruby-lang.org/issues/18772
22
+ append_cflags("-DRB_ENC_INTERNED_STR_NULL_CHECK=1")
23
+ end
24
+
25
+ # checking if Hash#[]= (rb_hash_aset) dedupes string keys (Ruby 2.6+)
26
+ h = {}
27
+ x = {}
28
+ r = rand.to_s
29
+ h[%W(#{r}).join('')] = :foo
30
+ x[%W(#{r}).join('')] = :foo
31
+ if x.keys[0].equal?(h.keys[0])
32
+ append_cflags("-DHASH_ASET_DEDUPE=1")
33
+ else
34
+ append_cflags("-DHASH_ASET_DEDUPE=0")
35
+ end
36
+
37
+ # checking if String#-@ (str_uminus) directly interns frozen strings... ' (Ruby 3.0+)
38
+ begin
39
+ s = rand.to_s.freeze
40
+ if (-s).equal?(s) && (-s.dup).equal?(s)
41
+ append_cflags("-DSTR_UMINUS_DEDUPE_FROZEN=1")
42
+ else
43
+ append_cflags("-DSTR_UMINUS_DEDUPE_FROZEN=0")
44
+ end
45
+ rescue NoMethodError
46
+ append_cflags("-DSTR_UMINUS_DEDUPE_FROZEN=0")
47
+ end
48
+
49
+ if warnflags = CONFIG['warnflags']
50
+ warnflags.slice!(/ -Wdeclaration-after-statement/)
51
+ end
52
+
53
+ create_makefile('msgpack/msgpack')
@@ -0,0 +1,34 @@
1
+ /*
2
+ * MessagePack for Ruby
3
+ *
4
+ * Copyright (C) 2008-2013 Sadayuki Furuhashi
5
+ *
6
+ * Licensed under the Apache License, Version 2.0 (the "License");
7
+ * you may not use this file except in compliance with the License.
8
+ * You may obtain a copy of the License at
9
+ *
10
+ * http://www.apache.org/licenses/LICENSE-2.0
11
+ *
12
+ * Unless required by applicable law or agreed to in writing, software
13
+ * distributed under the License is distributed on an "AS IS" BASIS,
14
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ * See the License for the specific language governing permissions and
16
+ * limitations under the License.
17
+ */
18
+
19
+ #include "factory_class.h"
20
+
21
+ VALUE cMessagePack_ExtensionValue;
22
+
23
+ VALUE MessagePack_ExtensionValue_new(int ext_type, VALUE payload)
24
+ {
25
+ return rb_struct_new(cMessagePack_ExtensionValue, INT2FIX(ext_type), payload);
26
+ }
27
+
28
+ void MessagePack_ExtensionValue_module_init(VALUE mMessagePack)
29
+ {
30
+ /* rb_struct_define_under is not available ruby < 2.1 */
31
+ //cMessagePack_ExtensionValue = rb_struct_define_under(mMessagePack, "ExtensionValue", "type", "payload", NULL);
32
+ cMessagePack_ExtensionValue = rb_struct_define(NULL, "type", "payload", NULL);
33
+ rb_define_const(mMessagePack, "ExtensionValue", cMessagePack_ExtensionValue);
34
+ }
@@ -0,0 +1,31 @@
1
+ /*
2
+ * MessagePack for Ruby
3
+ *
4
+ * Copyright (C) 2008-2015 Sadayuki Furuhashi
5
+ *
6
+ * Licensed under the Apache License, Version 2.0 (the "License");
7
+ * you may not use this file except in compliance with the License.
8
+ * You may obtain a copy of the License at
9
+ *
10
+ * http://www.apache.org/licenses/LICENSE-2.0
11
+ *
12
+ * Unless required by applicable law or agreed to in writing, software
13
+ * distributed under the License is distributed on an "AS IS" BASIS,
14
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ * See the License for the specific language governing permissions and
16
+ * limitations under the License.
17
+ */
18
+ #ifndef MSGPACK_RUBY_EXTENSION_VALUE_CLASS_H__
19
+ #define MSGPACK_RUBY_EXTENSION_VALUE_CLASS_H__
20
+
21
+ #include "compat.h"
22
+ #include "sysdep.h"
23
+
24
+ extern VALUE cMessagePack_ExtensionValue;
25
+
26
+ VALUE MessagePack_ExtensionValue_new(int ext_type, VALUE payload);
27
+
28
+ void MessagePack_ExtensionValue_module_init(VALUE mMessagePack);
29
+
30
+ #endif
31
+
@@ -0,0 +1,276 @@
1
+ /*
2
+ * MessagePack for Ruby
3
+ *
4
+ * Copyright (C) 2008-2013 Sadayuki Furuhashi
5
+ *
6
+ * Licensed under the Apache License, Version 2.0 (the "License");
7
+ * you may not use this file except in compliance with the License.
8
+ * You may obtain a copy of the License at
9
+ *
10
+ * http://www.apache.org/licenses/LICENSE-2.0
11
+ *
12
+ * Unless required by applicable law or agreed to in writing, software
13
+ * distributed under the License is distributed on an "AS IS" BASIS,
14
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ * See the License for the specific language governing permissions and
16
+ * limitations under the License.
17
+ */
18
+
19
+ #include "factory_class.h"
20
+ #include "packer_ext_registry.h"
21
+ #include "unpacker_ext_registry.h"
22
+ #include "buffer_class.h"
23
+ #include "packer_class.h"
24
+ #include "unpacker_class.h"
25
+
26
+ VALUE cMessagePack_Factory;
27
+
28
+ struct msgpack_factory_t;
29
+ typedef struct msgpack_factory_t msgpack_factory_t;
30
+
31
+ struct msgpack_factory_t {
32
+ msgpack_packer_ext_registry_t pkrg;
33
+ msgpack_unpacker_ext_registry_t *ukrg;
34
+ bool has_bigint_ext_type;
35
+ bool has_symbol_ext_type;
36
+ bool optimized_symbol_ext_type;
37
+ int symbol_ext_type;
38
+ };
39
+
40
+ static void Factory_free(void *ptr)
41
+ {
42
+ msgpack_factory_t *fc = ptr;
43
+
44
+ if(fc == NULL) {
45
+ return;
46
+ }
47
+ msgpack_packer_ext_registry_destroy(&fc->pkrg);
48
+ msgpack_unpacker_ext_registry_release(fc->ukrg);
49
+ xfree(fc);
50
+ }
51
+
52
+ void Factory_mark(void *ptr)
53
+ {
54
+ msgpack_factory_t *fc = ptr;
55
+ msgpack_packer_ext_registry_mark(&fc->pkrg);
56
+ msgpack_unpacker_ext_registry_mark(fc->ukrg);
57
+ }
58
+
59
+ static size_t Factory_memsize(const void *ptr)
60
+ {
61
+ const msgpack_factory_t *fc = ptr;
62
+ size_t total_size = sizeof(msgpack_factory_t);
63
+
64
+ if (fc->ukrg) {
65
+ total_size += sizeof(msgpack_unpacker_ext_registry_t) / (fc->ukrg->borrow_count + 1);
66
+ }
67
+
68
+ return total_size;
69
+ }
70
+
71
+ static const rb_data_type_t factory_data_type = {
72
+ .wrap_struct_name = "msgpack:factory",
73
+ .function = {
74
+ .dmark = Factory_mark,
75
+ .dfree = Factory_free,
76
+ .dsize = Factory_memsize,
77
+ },
78
+ .flags = RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED
79
+ };
80
+
81
+ static inline msgpack_factory_t *Factory_get(VALUE object)
82
+ {
83
+ msgpack_factory_t *factory;
84
+ TypedData_Get_Struct(object, msgpack_factory_t, &factory_data_type, factory);
85
+ if (!factory) {
86
+ rb_raise(rb_eArgError, "Uninitialized Factory object");
87
+ }
88
+ return factory;
89
+ }
90
+
91
+ static VALUE Factory_alloc(VALUE klass)
92
+ {
93
+ msgpack_factory_t *fc;
94
+ return TypedData_Make_Struct(klass, msgpack_factory_t, &factory_data_type, fc);
95
+ }
96
+
97
+ static VALUE Factory_initialize(int argc, VALUE* argv, VALUE self)
98
+ {
99
+ msgpack_factory_t *fc = Factory_get(self);
100
+
101
+ msgpack_packer_ext_registry_init(self, &fc->pkrg);
102
+ // fc->ukrg is lazily initialized
103
+
104
+ fc->has_symbol_ext_type = false;
105
+
106
+ switch (argc) {
107
+ case 0:
108
+ break;
109
+ default:
110
+ // TODO options is not supported yet
111
+ rb_raise(rb_eArgError, "wrong number of arguments (%d for 0)", argc);
112
+ }
113
+
114
+ return Qnil;
115
+ }
116
+
117
+ static VALUE Factory_dup(VALUE self)
118
+ {
119
+ VALUE clone = Factory_alloc(rb_obj_class(self));
120
+
121
+ msgpack_factory_t *fc = Factory_get(self);
122
+ msgpack_factory_t *cloned_fc = Factory_get(clone);
123
+
124
+ cloned_fc->has_symbol_ext_type = fc->has_symbol_ext_type;
125
+ cloned_fc->pkrg = fc->pkrg;
126
+ msgpack_unpacker_ext_registry_borrow(fc->ukrg, &cloned_fc->ukrg);
127
+ msgpack_packer_ext_registry_dup(clone, &fc->pkrg, &cloned_fc->pkrg);
128
+
129
+ return clone;
130
+ }
131
+
132
+ static VALUE Factory_freeze(VALUE self) {
133
+ if(!rb_obj_frozen_p(self)) {
134
+ msgpack_factory_t *fc = Factory_get(self);
135
+
136
+ if (RTEST(fc->pkrg.hash)) {
137
+ rb_hash_freeze(fc->pkrg.hash);
138
+ if (!RTEST(fc->pkrg.cache)) {
139
+ // If the factory is frozen, we can safely share the packer cache between
140
+ // all packers. So we eagerly create it now so it's available when #packer
141
+ // is called.
142
+ RB_OBJ_WRITE(self, &fc->pkrg.cache, rb_hash_new());
143
+ }
144
+ }
145
+
146
+ rb_obj_freeze(self);
147
+ }
148
+
149
+ return self;
150
+ }
151
+
152
+ VALUE MessagePack_Factory_packer(int argc, VALUE* argv, VALUE self)
153
+ {
154
+ msgpack_factory_t *fc = Factory_get(self);
155
+
156
+ VALUE packer = MessagePack_Packer_alloc(cMessagePack_Packer);
157
+ MessagePack_Packer_initialize(argc, argv, packer);
158
+
159
+ msgpack_packer_t* pk = MessagePack_Packer_get(packer);
160
+ msgpack_packer_ext_registry_destroy(&pk->ext_registry);
161
+ msgpack_packer_ext_registry_borrow(packer, &fc->pkrg, &pk->ext_registry);
162
+ pk->has_bigint_ext_type = fc->has_bigint_ext_type;
163
+ pk->has_symbol_ext_type = fc->has_symbol_ext_type;
164
+
165
+ return packer;
166
+ }
167
+
168
+ VALUE MessagePack_Factory_unpacker(int argc, VALUE* argv, VALUE self)
169
+ {
170
+ msgpack_factory_t *fc = Factory_get(self);
171
+
172
+ VALUE unpacker = MessagePack_Unpacker_alloc(cMessagePack_Unpacker);
173
+ MessagePack_Unpacker_initialize(argc, argv, unpacker);
174
+
175
+ msgpack_unpacker_t* uk = MessagePack_Unpacker_get(unpacker);
176
+ msgpack_unpacker_ext_registry_borrow(fc->ukrg, &uk->ext_registry);
177
+ uk->optimized_symbol_ext_type = fc->optimized_symbol_ext_type;
178
+ uk->symbol_ext_type = fc->symbol_ext_type;
179
+
180
+ return unpacker;
181
+ }
182
+
183
+ static VALUE Factory_registered_types_internal(VALUE self)
184
+ {
185
+ msgpack_factory_t *fc = Factory_get(self);
186
+
187
+ VALUE uk_mapping = rb_hash_new();
188
+ if (fc->ukrg) {
189
+ for(int i=0; i < 256; i++) {
190
+ if(!NIL_P(fc->ukrg->array[i])) {
191
+ rb_hash_aset(uk_mapping, INT2FIX(i - 128), fc->ukrg->array[i]);
192
+ }
193
+ }
194
+ }
195
+
196
+ return rb_ary_new3(
197
+ 2,
198
+ RTEST(fc->pkrg.hash) ? rb_hash_dup(fc->pkrg.hash) : rb_hash_new(),
199
+ uk_mapping
200
+ );
201
+ }
202
+
203
+ static VALUE Factory_register_type_internal(VALUE self, VALUE rb_ext_type, VALUE ext_module, VALUE options)
204
+ {
205
+ msgpack_factory_t *fc = Factory_get(self);
206
+
207
+ Check_Type(rb_ext_type, T_FIXNUM);
208
+
209
+ if(rb_type(ext_module) != T_MODULE && rb_type(ext_module) != T_CLASS) {
210
+ rb_raise(rb_eArgError, "expected Module/Class but found %s.", rb_obj_classname(ext_module));
211
+ }
212
+
213
+ int flags = 0;
214
+
215
+ VALUE packer_proc = Qnil;
216
+ VALUE unpacker_proc = Qnil;
217
+ if(!NIL_P(options)) {
218
+ Check_Type(options, T_HASH);
219
+ packer_proc = rb_hash_aref(options, ID2SYM(rb_intern("packer")));
220
+ unpacker_proc = rb_hash_aref(options, ID2SYM(rb_intern("unpacker")));
221
+ }
222
+
223
+ if (OBJ_FROZEN(self)) {
224
+ rb_raise(rb_eFrozenError, "can't modify frozen MessagePack::Factory");
225
+ }
226
+
227
+ int ext_type = NUM2INT(rb_ext_type);
228
+ if(ext_type < -128 || ext_type > 127) {
229
+ rb_raise(rb_eRangeError, "integer %d too big to convert to `signed char'", ext_type);
230
+ }
231
+
232
+ if(ext_module == rb_cSymbol) {
233
+ if(NIL_P(options) || RTEST(rb_hash_aref(options, ID2SYM(rb_intern("packer"))))) {
234
+ fc->has_symbol_ext_type = true;
235
+ }
236
+ if(RTEST(options) && RTEST(rb_hash_aref(options, ID2SYM(rb_intern("optimized_symbols_parsing"))))) {
237
+ fc->optimized_symbol_ext_type = true;
238
+ }
239
+ }
240
+
241
+ if(RTEST(options)) {
242
+ if(RTEST(rb_hash_aref(options, ID2SYM(rb_intern("oversized_integer_extension"))))) {
243
+ if(ext_module == rb_cInteger) {
244
+ fc->has_bigint_ext_type = true;
245
+ } else {
246
+ rb_raise(rb_eArgError, "oversized_integer_extension: true is only for Integer class");
247
+ }
248
+ }
249
+
250
+ if(RTEST(rb_hash_aref(options, ID2SYM(rb_intern("recursive"))))) {
251
+ flags |= MSGPACK_EXT_RECURSIVE;
252
+ }
253
+ }
254
+
255
+ msgpack_packer_ext_registry_put(self, &fc->pkrg, ext_module, ext_type, flags, packer_proc);
256
+ msgpack_unpacker_ext_registry_put(self, &fc->ukrg, ext_module, ext_type, flags, unpacker_proc);
257
+
258
+ return Qnil;
259
+ }
260
+
261
+ void MessagePack_Factory_module_init(VALUE mMessagePack)
262
+ {
263
+ cMessagePack_Factory = rb_define_class_under(mMessagePack, "Factory", rb_cObject);
264
+
265
+ rb_define_alloc_func(cMessagePack_Factory, Factory_alloc);
266
+
267
+ rb_define_method(cMessagePack_Factory, "initialize", Factory_initialize, -1);
268
+ rb_define_method(cMessagePack_Factory, "dup", Factory_dup, 0);
269
+ rb_define_method(cMessagePack_Factory, "freeze", Factory_freeze, 0);
270
+
271
+ rb_define_method(cMessagePack_Factory, "packer", MessagePack_Factory_packer, -1);
272
+ rb_define_method(cMessagePack_Factory, "unpacker", MessagePack_Factory_unpacker, -1);
273
+
274
+ rb_define_private_method(cMessagePack_Factory, "registered_types_internal", Factory_registered_types_internal, 0);
275
+ rb_define_private_method(cMessagePack_Factory, "register_type_internal", Factory_register_type_internal, 3);
276
+ }
@@ -0,0 +1,33 @@
1
+ /*
2
+ * MessagePack for Ruby
3
+ *
4
+ * Copyright (C) 2008-2015 Sadayuki Furuhashi
5
+ *
6
+ * Licensed under the Apache License, Version 2.0 (the "License");
7
+ * you may not use this file except in compliance with the License.
8
+ * You may obtain a copy of the License at
9
+ *
10
+ * http://www.apache.org/licenses/LICENSE-2.0
11
+ *
12
+ * Unless required by applicable law or agreed to in writing, software
13
+ * distributed under the License is distributed on an "AS IS" BASIS,
14
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ * See the License for the specific language governing permissions and
16
+ * limitations under the License.
17
+ */
18
+ #ifndef MSGPACK_RUBY_FACTORY_CLASS_H__
19
+ #define MSGPACK_RUBY_FACTORY_CLASS_H__
20
+
21
+ #include "compat.h"
22
+ #include "sysdep.h"
23
+
24
+ extern VALUE cMessagePack_Factory;
25
+
26
+ extern VALUE MessagePack_Factory_packer(int argc, VALUE* argv, VALUE self);
27
+
28
+ extern VALUE MessagePack_Factory_unpacker(int argc, VALUE* argv, VALUE self);
29
+
30
+ void MessagePack_Factory_module_init(VALUE mMessagePack);
31
+
32
+ #endif
33
+
@@ -0,0 +1,199 @@
1
+ /*
2
+ * MessagePack for Ruby
3
+ *
4
+ * Copyright (C) 2008-2013 Sadayuki Furuhashi
5
+ *
6
+ * Licensed under the Apache License, Version 2.0 (the "License");
7
+ * you may not use this file except in compliance with the License.
8
+ * You may obtain a copy of the License at
9
+ *
10
+ * http://www.apache.org/licenses/LICENSE-2.0
11
+ *
12
+ * Unless required by applicable law or agreed to in writing, software
13
+ * distributed under the License is distributed on an "AS IS" BASIS,
14
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ * See the License for the specific language governing permissions and
16
+ * limitations under the License.
17
+ */
18
+
19
+ #include "packer.h"
20
+ #include "buffer_class.h"
21
+
22
+ #if !defined(HAVE_RB_PROC_CALL_WITH_BLOCK)
23
+ #define rb_proc_call_with_block(recv, argc, argv, block) rb_funcallv(recv, rb_intern("call"), argc, argv)
24
+ #endif
25
+
26
+ void msgpack_packer_init(msgpack_packer_t* pk)
27
+ {
28
+ msgpack_buffer_init(PACKER_BUFFER_(pk));
29
+ }
30
+
31
+ void msgpack_packer_destroy(msgpack_packer_t* pk)
32
+ {
33
+ msgpack_buffer_destroy(PACKER_BUFFER_(pk));
34
+ }
35
+
36
+ void msgpack_packer_mark(msgpack_packer_t* pk)
37
+ {
38
+ /* See MessagePack_Buffer_wrap */
39
+ /* msgpack_buffer_mark(PACKER_BUFFER_(pk)); */
40
+ rb_gc_mark(pk->buffer_ref);
41
+ rb_gc_mark(pk->to_msgpack_arg);
42
+ }
43
+
44
+ void msgpack_packer_reset(msgpack_packer_t* pk)
45
+ {
46
+ msgpack_buffer_clear(PACKER_BUFFER_(pk));
47
+
48
+ pk->buffer_ref = Qnil;
49
+ }
50
+
51
+
52
+ void msgpack_packer_write_array_value(msgpack_packer_t* pk, VALUE v)
53
+ {
54
+ /* actual return type of RARRAY_LEN is long */
55
+ unsigned long len = RARRAY_LEN(v);
56
+ if(len > 0xffffffffUL) {
57
+ rb_raise(rb_eArgError, "size of array is too long to pack: %lu bytes should be <= %lu", len, 0xffffffffUL);
58
+ }
59
+ unsigned int len32 = (unsigned int)len;
60
+ msgpack_packer_write_array_header(pk, len32);
61
+
62
+ unsigned int i;
63
+ for(i=0; i < len32; ++i) {
64
+ VALUE e = rb_ary_entry(v, i);
65
+ msgpack_packer_write_value(pk, e);
66
+ }
67
+ }
68
+
69
+ static int write_hash_foreach(VALUE key, VALUE value, VALUE pk_value)
70
+ {
71
+ if (key == Qundef) {
72
+ return ST_CONTINUE;
73
+ }
74
+ msgpack_packer_t* pk = (msgpack_packer_t*) pk_value;
75
+ msgpack_packer_write_value(pk, key);
76
+ msgpack_packer_write_value(pk, value);
77
+ return ST_CONTINUE;
78
+ }
79
+
80
+ void msgpack_packer_write_hash_value(msgpack_packer_t* pk, VALUE v)
81
+ {
82
+ /* actual return type of RHASH_SIZE is long (if SIZEOF_LONG == SIZEOF_VOIDP
83
+ * or long long (if SIZEOF_LONG_LONG == SIZEOF_VOIDP. See st.h. */
84
+ unsigned long len = RHASH_SIZE(v);
85
+ if(len > 0xffffffffUL) {
86
+ rb_raise(rb_eArgError, "size of array is too long to pack: %ld bytes should be <= %lu", len, 0xffffffffUL);
87
+ }
88
+ unsigned int len32 = (unsigned int)len;
89
+ msgpack_packer_write_map_header(pk, len32);
90
+
91
+ rb_hash_foreach(v, write_hash_foreach, (VALUE) pk);
92
+ }
93
+
94
+ struct msgpack_call_proc_args_t;
95
+ typedef struct msgpack_call_proc_args_t msgpack_call_proc_args_t;
96
+ struct msgpack_call_proc_args_t {
97
+ VALUE proc;
98
+ VALUE args[2];
99
+ };
100
+
101
+ VALUE msgpack_packer_try_calling_proc(VALUE value)
102
+ {
103
+ msgpack_call_proc_args_t *args = (msgpack_call_proc_args_t *)value;
104
+ return rb_proc_call_with_block(args->proc, 2, args->args, Qnil);
105
+ }
106
+
107
+ bool msgpack_packer_try_write_with_ext_type_lookup(msgpack_packer_t* pk, VALUE v)
108
+ {
109
+ int ext_type, ext_flags;
110
+
111
+ VALUE proc = msgpack_packer_ext_registry_lookup(&pk->ext_registry, v, &ext_type, &ext_flags);
112
+
113
+ if(proc == Qnil) {
114
+ return false;
115
+ }
116
+
117
+ if(ext_flags & MSGPACK_EXT_RECURSIVE) {
118
+ VALUE held_buffer = MessagePack_Buffer_hold(&pk->buffer);
119
+
120
+ msgpack_buffer_t parent_buffer = pk->buffer;
121
+ msgpack_buffer_init(PACKER_BUFFER_(pk));
122
+
123
+ int exception_occured = 0;
124
+ msgpack_call_proc_args_t args = { proc, { v, pk->to_msgpack_arg } };
125
+ rb_protect(msgpack_packer_try_calling_proc, (VALUE)&args, &exception_occured);
126
+
127
+ if (exception_occured) {
128
+ msgpack_buffer_destroy(PACKER_BUFFER_(pk));
129
+ pk->buffer = parent_buffer;
130
+ rb_jump_tag(exception_occured); // re-raise the exception
131
+ } else {
132
+ VALUE payload = msgpack_buffer_all_as_string(PACKER_BUFFER_(pk));
133
+ StringValue(payload);
134
+ msgpack_buffer_destroy(PACKER_BUFFER_(pk));
135
+ pk->buffer = parent_buffer;
136
+ msgpack_packer_write_ext(pk, ext_type, payload);
137
+ }
138
+
139
+ RB_GC_GUARD(held_buffer);
140
+ } else {
141
+ VALUE payload = rb_proc_call_with_block(proc, 1, &v, Qnil);
142
+ StringValue(payload);
143
+ msgpack_packer_write_ext(pk, ext_type, payload);
144
+ }
145
+
146
+ return true;
147
+ }
148
+
149
+ void msgpack_packer_write_other_value(msgpack_packer_t* pk, VALUE v)
150
+ {
151
+ if(!(msgpack_packer_try_write_with_ext_type_lookup(pk, v))) {
152
+ rb_funcall(v, pk->to_msgpack_method, 1, pk->to_msgpack_arg);
153
+ }
154
+ }
155
+
156
+ void msgpack_packer_write_value(msgpack_packer_t* pk, VALUE v)
157
+ {
158
+ switch(rb_type(v)) {
159
+ case T_NIL:
160
+ msgpack_packer_write_nil(pk);
161
+ break;
162
+ case T_TRUE:
163
+ msgpack_packer_write_true(pk);
164
+ break;
165
+ case T_FALSE:
166
+ msgpack_packer_write_false(pk);
167
+ break;
168
+ case T_FIXNUM:
169
+ msgpack_packer_write_fixnum_value(pk, v);
170
+ break;
171
+ case T_SYMBOL:
172
+ msgpack_packer_write_symbol_value(pk, v);
173
+ break;
174
+ case T_STRING:
175
+ if(rb_class_of(v) == rb_cString || !msgpack_packer_try_write_with_ext_type_lookup(pk, v)) {
176
+ msgpack_packer_write_string_value(pk, v);
177
+ }
178
+ break;
179
+ case T_ARRAY:
180
+ if(rb_class_of(v) == rb_cArray || !msgpack_packer_try_write_with_ext_type_lookup(pk, v)) {
181
+ msgpack_packer_write_array_value(pk, v);
182
+ }
183
+ break;
184
+ case T_HASH:
185
+ if(rb_class_of(v) == rb_cHash || !msgpack_packer_try_write_with_ext_type_lookup(pk, v)) {
186
+ msgpack_packer_write_hash_value(pk, v);
187
+ }
188
+ break;
189
+ case T_BIGNUM:
190
+ msgpack_packer_write_bignum_value(pk, v);
191
+ break;
192
+ case T_FLOAT:
193
+ msgpack_packer_write_float_value(pk, v);
194
+ break;
195
+ default:
196
+ msgpack_packer_write_other_value(pk, v);
197
+ }
198
+ }
199
+