summaryrefslogtreecommitdiff
path: root/ext/-test-/cxxanyargs/cxxanyargs.cpp
blob: c0117b363322697a2855a19b5c086821fb68187c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
#include <ruby/ruby.h>

#if 0 // Warnings expected, should just suppress them

#elif defined(_MSC_VER)
#pragma warning(disable : 4996)

#elif defined(__clang__)
#pragma clang diagnostic ignored "-Wdeprecated-declarations"

#elif defined(__GNUC__)
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"

#else
// :FIXME: improve here for your compiler.

#endif

namespace test_rb_define_virtual_variable {
    VALUE
    getter(ID, VALUE *data)
    {
        return *data;
    }

    void
    setter(VALUE val, ID, VALUE *data)
    {
        *data = val;
    }

    VALUE
    test(VALUE self)
    {
        rb_define_virtual_variable("test",
            RUBY_METHOD_FUNC(getter),
            reinterpret_cast<void(*)(ANYARGS)>(setter)); // old
        rb_define_virtual_variable("test", getter, setter); // new
        return self;
    }
}

struct test_rb_define_hooked_variable {
    static VALUE v;

    static VALUE
    getter(ID, VALUE *data)
    {
        return *data;
    }

    static void
    setter(VALUE val, ID, VALUE *data)
    {
        *data = val;
    }

    static VALUE
    test(VALUE self)
    {
        rb_define_hooked_variable("test", &v,
            RUBY_METHOD_FUNC(getter),
            reinterpret_cast<void(*)(ANYARGS)>(setter)); // old
        rb_define_hooked_variable("test", &v, getter, setter); // new
        return self;
    }
};
VALUE test_rb_define_hooked_variable::v = Qundef;

namespace test_rb_iterate {
    VALUE
    iter(VALUE self)
    {
        return rb_funcall(self, rb_intern("yield"), 0);
    }

    VALUE
    block(RB_BLOCK_CALL_FUNC_ARGLIST(arg, param))
    {
        return rb_funcall(arg, rb_intern("=="), 1, param);
    }

    VALUE
    test(VALUE self)
    {
        rb_iterate(iter, self, RUBY_METHOD_FUNC(block), self); // old
        return rb_iterate(iter, self, block, self); // new
    }
}

namespace test_rb_block_call {
    VALUE
    block(RB_BLOCK_CALL_FUNC_ARGLIST(arg, param))
    {
        return rb_funcall(arg, rb_intern("=="), 1, param);
    }

    VALUE
    test(VALUE self)
    {
        const ID mid = rb_intern("each");
        const VALUE argv[] = { Qundef };
        rb_block_call(self, mid, 0, argv, RUBY_METHOD_FUNC(block), self); // old
        return rb_block_call(self, mid, 0, argv, block, self); // new
    }
}

namespace test_rb_rescue {
    VALUE
    begin(VALUE arg)
    {
        return arg;
    }

    VALUE
    rescue(VALUE arg, VALUE exc)
    {
        return exc;
    }

    VALUE
    test(VALUE self)
    {
        rb_rescue(RUBY_METHOD_FUNC(begin), self, RUBY_METHOD_FUNC(rescue), self); // old
        return rb_rescue(begin, self, rescue, self); // new
    }
}

namespace test_rb_rescue2 {
    VALUE
    begin(VALUE arg)
    {
        return arg;
    }

    VALUE
    rescue(VALUE arg, VALUE exc)
    {
        return exc;
    }

    VALUE
    test(VALUE self)
    {
        rb_rescue2(RUBY_METHOD_FUNC(begin), self, RUBY_METHOD_FUNC(rescue), self,
                   rb_eStandardError, rb_eFatal, 0); // old
        return rb_rescue2(begin, self, rescue, self, rb_eStandardError, rb_eFatal, 0); // new
    }
}

namespace test_rb_ensure {
    VALUE
    begin(VALUE arg)
    {
        return arg;
    }

    VALUE
    ensure(VALUE arg)
    {
        return arg;
    }

    VALUE
    test(VALUE self)
    {
        rb_ensure(RUBY_METHOD_FUNC(begin), self, RUBY_METHOD_FUNC(ensure), self); // old
        return rb_ensure(begin, self, ensure, self); // new
    }
}

namespace test_rb_catch {
    VALUE
    catcher(RB_BLOCK_CALL_FUNC_ARGLIST(arg, param))
    {
        return arg;
    }

    VALUE
    test(VALUE self)
    {
        static const char *zero = 0;
        rb_catch(zero, RUBY_METHOD_FUNC(catcher), self); // old
        return rb_catch(zero, catcher, self); // new
    }
}

namespace test_rb_catch_obj {
    VALUE
    catcher(RB_BLOCK_CALL_FUNC_ARGLIST(arg, param))
    {
        return arg;
    }

    VALUE
    test(VALUE self)
    {
        rb_catch_obj(self, RUBY_METHOD_FUNC(catcher), self); // old
        return rb_catch_obj(self, catcher, self); // new
    }
}

namespace test_rb_fiber_new {
    VALUE
    fiber(RB_BLOCK_CALL_FUNC_ARGLIST(arg, param))
    {
        return arg;
    }

    VALUE
    test(VALUE self)
    {
        rb_fiber_new(RUBY_METHOD_FUNC(fiber), self); // old
        return rb_fiber_new(fiber, self); // new
    }
}

namespace test_rb_proc_new {
    VALUE
    proc(RB_BLOCK_CALL_FUNC_ARGLIST(arg, param))
    {
        return arg;
    }

    VALUE
    test(VALUE self)
    {
        rb_fiber_new(RUBY_METHOD_FUNC(proc), self); // old
        return rb_fiber_new(proc, self); // new
    }
}

struct test_rb_thread_create {
    static VALUE v;

    static VALUE
    thread(void *ptr)
    {
        const VALUE *w = reinterpret_cast<const VALUE*>(ptr);
        return *w;
    }

    static VALUE
    test(VALUE self)
    {
        v = self;
        rb_thread_create(RUBY_METHOD_FUNC(thread), &v); // old
        return rb_thread_create(thread, &v); // new
    }
};
VALUE test_rb_thread_create::v = Qundef;

namespace test_st_foreach {
    static int
    iter(st_data_t, st_data_t, st_data_t)
    {
        return ST_CONTINUE;
    }

    VALUE
    test(VALUE self)
    {
        st_data_t data = 0;
        st_table *st = st_init_numtable();
        st_foreach(st, reinterpret_cast<int(*)(ANYARGS)>(iter), data); // old
        st_foreach(st, iter, data); // new
        return self;
    }
}

namespace test_st_foreach_check {
    static int
    iter(st_data_t, st_data_t, st_data_t, int x)
    {
        return x ? ST_STOP : ST_CONTINUE;
    }

    VALUE
    test(VALUE self)
    {
        st_data_t data = 0;
        st_table *st = st_init_numtable();
        st_foreach_check(st, reinterpret_cast<int(*)(ANYARGS)>(iter), data, data); // old
        st_foreach_check(st, iter, data, data); // new
        return self;
    }
}

namespace test_st_foreach_safe {
    static int
    iter(st_data_t, st_data_t, st_data_t)
    {
        return ST_CONTINUE;
    }

    VALUE
    test(VALUE self)
    {
        st_data_t data = 0;
        st_table *st = st_init_numtable();
        st_foreach_safe(st, reinterpret_cast<int(*)(ANYARGS)>(iter), data); // old
        st_foreach_safe(st, iter, data); // new
        return self;
    }
}

namespace test_rb_hash_foreach {
    static int
    iter(VALUE, VALUE, VALUE)
    {
        return ST_CONTINUE;
    }

    VALUE
    test(VALUE self)
    {
        VALUE h = rb_hash_new();
        rb_hash_foreach(h, reinterpret_cast<int(*)(ANYARGS)>(iter), self); // old
        rb_hash_foreach(h, iter, self); // new
        return self;
    }
}

namespace test_rb_ivar_foreach {
    static int
    iter(VALUE, VALUE, VALUE)
    {
        return ST_CONTINUE;
    }

    VALUE
    test(VALUE self)
    {
        rb_ivar_foreach(self, reinterpret_cast<int(*)(ANYARGS)>(iter), self); // old
        rb_ivar_foreach(self, iter, self); // new
        return self;
    }
}

extern "C" void
Init_cxxanyargs(void)
{
    VALUE b = rb_define_module("Bug");
#define test(sym) \
    rb_define_module_function(b, #sym, RUBY_METHOD_FUNC(test_ ## sym::test), 0)

    test(rb_define_virtual_variable);
    test(rb_define_hooked_variable);
    test(rb_iterate);
    test(rb_block_call);
    test(rb_rescue);
    test(rb_rescue2);
    test(rb_ensure);
    test(rb_catch);
    test(rb_catch_obj);
    test(rb_fiber_new);
    test(rb_proc_new);
    test(rb_thread_create);
    test(st_foreach);
    test(st_foreach_check);
    test(st_foreach_safe);
    test(rb_hash_foreach);
    test(rb_ivar_foreach);
}