rucy 0.1.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.
- data/Gemfile +3 -0
- data/Gemfile.lock +12 -0
- data/README +4 -0
- data/Rakefile +58 -0
- data/VERSION +1 -0
- data/ext/rucy/extconf.rb +50 -0
- data/ext/rucy/tester.cpp +102 -0
- data/include/rucy/class.h +37 -0
- data/include/rucy/defs.h +97 -0
- data/include/rucy/defs.h.erb +56 -0
- data/include/rucy/exception.h +124 -0
- data/include/rucy/function.h +245 -0
- data/include/rucy/function.h.erb +73 -0
- data/include/rucy/gc.h +59 -0
- data/include/rucy/module.h +96 -0
- data/include/rucy/module.h.erb +68 -0
- data/include/rucy/rucy.h +64 -0
- data/include/rucy/string.h +28 -0
- data/include/rucy/symbol.h +50 -0
- data/include/rucy/value.h +291 -0
- data/include/rucy/value.h.erb +278 -0
- data/include/rucy.h +10 -0
- data/lib/rucy.rb +24 -0
- data/rucy.gemspec +50 -0
- data/src/class.cpp +35 -0
- data/src/exception.cpp +124 -0
- data/src/function.cpp +157 -0
- data/src/function.cpp.erb +74 -0
- data/src/gc.cpp +63 -0
- data/src/module.cpp +321 -0
- data/src/module.cpp.erb +103 -0
- data/src/rucy.cpp +56 -0
- data/src/string.cpp +78 -0
- data/src/symbol.cpp +51 -0
- data/src/value.cpp +511 -0
- data/src/value.cpp.erb +409 -0
- data/support.rb +58 -0
- data/task/ext.rake +36 -0
- data/task/gem.rake +33 -0
- data/task/lib.rake +47 -0
- data/test/test_rucy.rb +57 -0
- metadata +116 -0
data/src/value.cpp
ADDED
@@ -0,0 +1,511 @@
|
|
1
|
+
// -*- c++ -*-
|
2
|
+
#include "rucy/value.h"
|
3
|
+
|
4
|
+
|
5
|
+
#include <rucy/rucy.h>
|
6
|
+
#include <rucy/function.h>
|
7
|
+
#include <rucy/exception.h>
|
8
|
+
|
9
|
+
|
10
|
+
namespace Rucy
|
11
|
+
{
|
12
|
+
|
13
|
+
|
14
|
+
Value::Value ()
|
15
|
+
: v(Qnil)
|
16
|
+
{
|
17
|
+
}
|
18
|
+
|
19
|
+
Value::Value (bool b)
|
20
|
+
: v(b ? Qtrue : Qfalse)
|
21
|
+
{
|
22
|
+
}
|
23
|
+
|
24
|
+
Value::Value (int n)
|
25
|
+
: v(INT2FIX(n))
|
26
|
+
{
|
27
|
+
}
|
28
|
+
|
29
|
+
Value::Value (float f)
|
30
|
+
: v(rb_float_new(f))
|
31
|
+
{
|
32
|
+
}
|
33
|
+
|
34
|
+
Value::Value (double d)
|
35
|
+
: v(rb_float_new(d))
|
36
|
+
{
|
37
|
+
}
|
38
|
+
|
39
|
+
Value::Value (const char* s, bool tainted)
|
40
|
+
: v(tainted ? rb_tainted_str_new2(s) : rb_str_new2(s))
|
41
|
+
{
|
42
|
+
}
|
43
|
+
|
44
|
+
Value::Value (const char* s, size_t len, bool tainted)
|
45
|
+
: v(tainted ? rb_tainted_str_new(s, len) : rb_str_new(s, len))
|
46
|
+
{
|
47
|
+
}
|
48
|
+
|
49
|
+
Value::Value (size_t size, const Value* values)
|
50
|
+
: v(size > 0 ? rb_ary_new2(size) : rb_ary_new())
|
51
|
+
{
|
52
|
+
if (!values) return;
|
53
|
+
for (size_t i = 0; i < size; ++i)
|
54
|
+
push(values[i]);
|
55
|
+
}
|
56
|
+
|
57
|
+
Value::Value (VALUE v)
|
58
|
+
: v(v)
|
59
|
+
{
|
60
|
+
}
|
61
|
+
|
62
|
+
VALUE
|
63
|
+
Value::value () const
|
64
|
+
{
|
65
|
+
return v;
|
66
|
+
}
|
67
|
+
|
68
|
+
void
|
69
|
+
Value::mark () const
|
70
|
+
{
|
71
|
+
rb_gc_mark(v);
|
72
|
+
}
|
73
|
+
|
74
|
+
int
|
75
|
+
Value::type () const
|
76
|
+
{
|
77
|
+
return TYPE(v);
|
78
|
+
}
|
79
|
+
|
80
|
+
Value
|
81
|
+
Value::klass () const
|
82
|
+
{
|
83
|
+
return RBASIC(v)->klass;
|
84
|
+
}
|
85
|
+
|
86
|
+
bool
|
87
|
+
Value::is_kind_of (Value klass) const
|
88
|
+
{
|
89
|
+
SYM_Q(kind_of);
|
90
|
+
return call(kind_of, klass);
|
91
|
+
}
|
92
|
+
|
93
|
+
static int
|
94
|
+
get_object_size (Value obj, Symbol method)
|
95
|
+
{
|
96
|
+
if (obj.type() == T_STRING)
|
97
|
+
return RSTRING_LEN(obj.value());
|
98
|
+
else if (obj.type() == T_ARRAY)
|
99
|
+
return RARRAY_LEN(obj.value());
|
100
|
+
else
|
101
|
+
return obj.call(method);
|
102
|
+
}
|
103
|
+
|
104
|
+
int
|
105
|
+
Value::size () const
|
106
|
+
{
|
107
|
+
SYM(size);
|
108
|
+
return get_object_size(v, size);
|
109
|
+
}
|
110
|
+
|
111
|
+
int
|
112
|
+
Value::length () const
|
113
|
+
{
|
114
|
+
SYM(length);
|
115
|
+
return get_object_size(v, length);
|
116
|
+
}
|
117
|
+
|
118
|
+
Value
|
119
|
+
Value::to_i () const
|
120
|
+
{
|
121
|
+
if (FIXNUM_P(v) || TYPE(v) == T_BIGNUM) return *this;
|
122
|
+
SYM(to_i);
|
123
|
+
return call(to_i);
|
124
|
+
}
|
125
|
+
|
126
|
+
Value
|
127
|
+
Value::to_f () const
|
128
|
+
{
|
129
|
+
if (TYPE(v) == T_FLOAT) return *this;
|
130
|
+
SYM(to_f);
|
131
|
+
return call(to_f);
|
132
|
+
}
|
133
|
+
|
134
|
+
Value
|
135
|
+
Value::to_s () const
|
136
|
+
{
|
137
|
+
if (TYPE(v) == T_STRING) return *this;
|
138
|
+
SYM(to_s);
|
139
|
+
return call(to_s);
|
140
|
+
}
|
141
|
+
|
142
|
+
Value
|
143
|
+
Value::to_sym () const
|
144
|
+
{
|
145
|
+
if (TYPE(v) == T_SYMBOL) return *this;
|
146
|
+
SYM(to_sym);
|
147
|
+
return call(to_sym);
|
148
|
+
}
|
149
|
+
|
150
|
+
bool
|
151
|
+
Value::is_i () const
|
152
|
+
{
|
153
|
+
return is_kind_of(rb_cFixnum);
|
154
|
+
}
|
155
|
+
|
156
|
+
bool
|
157
|
+
Value::is_f () const
|
158
|
+
{
|
159
|
+
return is_kind_of(rb_cFloat);
|
160
|
+
}
|
161
|
+
|
162
|
+
bool
|
163
|
+
Value::is_s () const
|
164
|
+
{
|
165
|
+
return is_kind_of(rb_cString);
|
166
|
+
}
|
167
|
+
|
168
|
+
bool
|
169
|
+
Value::is_sym () const
|
170
|
+
{
|
171
|
+
return is_kind_of(rb_cSymbol);
|
172
|
+
}
|
173
|
+
|
174
|
+
bool
|
175
|
+
Value::is_a () const
|
176
|
+
{
|
177
|
+
return is_kind_of(rb_cArray);
|
178
|
+
}
|
179
|
+
|
180
|
+
int
|
181
|
+
Value::as_i (bool convert) const
|
182
|
+
{
|
183
|
+
return to<int>(*this, convert);
|
184
|
+
}
|
185
|
+
|
186
|
+
double
|
187
|
+
Value::as_f (bool convert) const
|
188
|
+
{
|
189
|
+
return to<double>(*this, convert);
|
190
|
+
}
|
191
|
+
|
192
|
+
const char*
|
193
|
+
Value::as_s (bool convert) const
|
194
|
+
{
|
195
|
+
return to<const char*>(*this, convert);
|
196
|
+
}
|
197
|
+
|
198
|
+
Symbol
|
199
|
+
Value::as_sym (bool convert) const
|
200
|
+
{
|
201
|
+
return to<Symbol>(*this, convert);
|
202
|
+
}
|
203
|
+
|
204
|
+
Value
|
205
|
+
Value::call (Symbol name, int argc, const Value* argv) const
|
206
|
+
{
|
207
|
+
return protect(rb_funcall2, value(), name.id(), argc, (const VALUE*) argv);
|
208
|
+
}
|
209
|
+
Value
|
210
|
+
Value::call (Symbol name) const
|
211
|
+
{
|
212
|
+
const VALUE args[] = {};
|
213
|
+
return protect(rb_funcall2, value(), name.id(), 0, args);
|
214
|
+
}
|
215
|
+
Value
|
216
|
+
Value::call (Symbol name, Value v1) const
|
217
|
+
{
|
218
|
+
const VALUE args[] = {v1};
|
219
|
+
return protect(rb_funcall2, value(), name.id(), 1, args);
|
220
|
+
}
|
221
|
+
Value
|
222
|
+
Value::call (Symbol name, Value v1, Value v2) const
|
223
|
+
{
|
224
|
+
const VALUE args[] = {v1, v2};
|
225
|
+
return protect(rb_funcall2, value(), name.id(), 2, args);
|
226
|
+
}
|
227
|
+
Value
|
228
|
+
Value::call (Symbol name, Value v1, Value v2, Value v3) const
|
229
|
+
{
|
230
|
+
const VALUE args[] = {v1, v2, v3};
|
231
|
+
return protect(rb_funcall2, value(), name.id(), 3, args);
|
232
|
+
}
|
233
|
+
Value
|
234
|
+
Value::call (Symbol name, Value v1, Value v2, Value v3, Value v4) const
|
235
|
+
{
|
236
|
+
const VALUE args[] = {v1, v2, v3, v4};
|
237
|
+
return protect(rb_funcall2, value(), name.id(), 4, args);
|
238
|
+
}
|
239
|
+
Value
|
240
|
+
Value::call (Symbol name, Value v1, Value v2, Value v3, Value v4, Value v5) const
|
241
|
+
{
|
242
|
+
const VALUE args[] = {v1, v2, v3, v4, v5};
|
243
|
+
return protect(rb_funcall2, value(), name.id(), 5, args);
|
244
|
+
}
|
245
|
+
Value
|
246
|
+
Value::call (Symbol name, Value v1, Value v2, Value v3, Value v4, Value v5, Value v6) const
|
247
|
+
{
|
248
|
+
const VALUE args[] = {v1, v2, v3, v4, v5, v6};
|
249
|
+
return protect(rb_funcall2, value(), name.id(), 6, args);
|
250
|
+
}
|
251
|
+
Value
|
252
|
+
Value::call (Symbol name, Value v1, Value v2, Value v3, Value v4, Value v5, Value v6, Value v7) const
|
253
|
+
{
|
254
|
+
const VALUE args[] = {v1, v2, v3, v4, v5, v6, v7};
|
255
|
+
return protect(rb_funcall2, value(), name.id(), 7, args);
|
256
|
+
}
|
257
|
+
Value
|
258
|
+
Value::call (Symbol name, Value v1, Value v2, Value v3, Value v4, Value v5, Value v6, Value v7, Value v8) const
|
259
|
+
{
|
260
|
+
const VALUE args[] = {v1, v2, v3, v4, v5, v6, v7, v8};
|
261
|
+
return protect(rb_funcall2, value(), name.id(), 8, args);
|
262
|
+
}
|
263
|
+
Value
|
264
|
+
Value::operator () (Symbol name, int argc, const Value* argv) const
|
265
|
+
{
|
266
|
+
return protect(rb_funcall2, value(), name.id(), argc, (const VALUE*) argv);
|
267
|
+
}
|
268
|
+
Value
|
269
|
+
Value::operator () (Symbol name) const
|
270
|
+
{
|
271
|
+
const VALUE args[] = {};
|
272
|
+
return protect(rb_funcall2, value(), name.id(), 0, args);
|
273
|
+
}
|
274
|
+
Value
|
275
|
+
Value::operator () (Symbol name, Value v1) const
|
276
|
+
{
|
277
|
+
const VALUE args[] = {v1};
|
278
|
+
return protect(rb_funcall2, value(), name.id(), 1, args);
|
279
|
+
}
|
280
|
+
Value
|
281
|
+
Value::operator () (Symbol name, Value v1, Value v2) const
|
282
|
+
{
|
283
|
+
const VALUE args[] = {v1, v2};
|
284
|
+
return protect(rb_funcall2, value(), name.id(), 2, args);
|
285
|
+
}
|
286
|
+
Value
|
287
|
+
Value::operator () (Symbol name, Value v1, Value v2, Value v3) const
|
288
|
+
{
|
289
|
+
const VALUE args[] = {v1, v2, v3};
|
290
|
+
return protect(rb_funcall2, value(), name.id(), 3, args);
|
291
|
+
}
|
292
|
+
Value
|
293
|
+
Value::operator () (Symbol name, Value v1, Value v2, Value v3, Value v4) const
|
294
|
+
{
|
295
|
+
const VALUE args[] = {v1, v2, v3, v4};
|
296
|
+
return protect(rb_funcall2, value(), name.id(), 4, args);
|
297
|
+
}
|
298
|
+
Value
|
299
|
+
Value::operator () (Symbol name, Value v1, Value v2, Value v3, Value v4, Value v5) const
|
300
|
+
{
|
301
|
+
const VALUE args[] = {v1, v2, v3, v4, v5};
|
302
|
+
return protect(rb_funcall2, value(), name.id(), 5, args);
|
303
|
+
}
|
304
|
+
Value
|
305
|
+
Value::operator () (Symbol name, Value v1, Value v2, Value v3, Value v4, Value v5, Value v6) const
|
306
|
+
{
|
307
|
+
const VALUE args[] = {v1, v2, v3, v4, v5, v6};
|
308
|
+
return protect(rb_funcall2, value(), name.id(), 6, args);
|
309
|
+
}
|
310
|
+
Value
|
311
|
+
Value::operator () (Symbol name, Value v1, Value v2, Value v3, Value v4, Value v5, Value v6, Value v7) const
|
312
|
+
{
|
313
|
+
const VALUE args[] = {v1, v2, v3, v4, v5, v6, v7};
|
314
|
+
return protect(rb_funcall2, value(), name.id(), 7, args);
|
315
|
+
}
|
316
|
+
Value
|
317
|
+
Value::operator () (Symbol name, Value v1, Value v2, Value v3, Value v4, Value v5, Value v6, Value v7, Value v8) const
|
318
|
+
{
|
319
|
+
const VALUE args[] = {v1, v2, v3, v4, v5, v6, v7, v8};
|
320
|
+
return protect(rb_funcall2, value(), name.id(), 8, args);
|
321
|
+
}
|
322
|
+
|
323
|
+
Value
|
324
|
+
Value::inspect () const
|
325
|
+
{
|
326
|
+
SYM(inspect);
|
327
|
+
return call(inspect);
|
328
|
+
}
|
329
|
+
|
330
|
+
const char*
|
331
|
+
Value::c_str () const
|
332
|
+
{
|
333
|
+
return value_to<const char*>(*this);
|
334
|
+
}
|
335
|
+
|
336
|
+
Value&
|
337
|
+
Value::operator [] (int i)
|
338
|
+
{
|
339
|
+
if (type() != T_ARRAY) type_error();
|
340
|
+
return *(Value*) &RARRAY_PTR(v)[i];
|
341
|
+
}
|
342
|
+
|
343
|
+
const Value&
|
344
|
+
Value::operator [] (int i) const
|
345
|
+
{
|
346
|
+
return const_cast<Value*>(this)->operator[](i);
|
347
|
+
}
|
348
|
+
|
349
|
+
Value::operator VALUE () const
|
350
|
+
{
|
351
|
+
return value();
|
352
|
+
}
|
353
|
+
|
354
|
+
static bool
|
355
|
+
test_value (VALUE v)
|
356
|
+
{
|
357
|
+
return !NIL_P(v) && v != Qfalse;// RTEST(v);
|
358
|
+
}
|
359
|
+
|
360
|
+
Value::operator bool () const
|
361
|
+
{
|
362
|
+
return test_value(v);
|
363
|
+
}
|
364
|
+
|
365
|
+
bool
|
366
|
+
Value::operator ! () const
|
367
|
+
{
|
368
|
+
return !operator bool();
|
369
|
+
}
|
370
|
+
|
371
|
+
bool
|
372
|
+
Value::operator == (const Value& rhs) const
|
373
|
+
{
|
374
|
+
return v == rhs.v;// test_value(rb_obj_equal(v, rhs.v);
|
375
|
+
}
|
376
|
+
|
377
|
+
bool
|
378
|
+
Value::operator != (const Value& rhs) const
|
379
|
+
{
|
380
|
+
return !operator==(rhs);
|
381
|
+
}
|
382
|
+
|
383
|
+
Value
|
384
|
+
Value::push (Value obj)
|
385
|
+
{
|
386
|
+
return rb_ary_push(value(), obj.value());
|
387
|
+
}
|
388
|
+
|
389
|
+
Value
|
390
|
+
Value::pop ()
|
391
|
+
{
|
392
|
+
return rb_ary_pop(value());
|
393
|
+
}
|
394
|
+
|
395
|
+
Value
|
396
|
+
Value::shift ()
|
397
|
+
{
|
398
|
+
return rb_ary_shift(value());
|
399
|
+
}
|
400
|
+
|
401
|
+
Value
|
402
|
+
Value::unshift (Value obj)
|
403
|
+
{
|
404
|
+
return rb_ary_unshift(value(), obj.value());
|
405
|
+
}
|
406
|
+
|
407
|
+
|
408
|
+
Value
|
409
|
+
value (bool b)
|
410
|
+
{
|
411
|
+
return b;
|
412
|
+
}
|
413
|
+
|
414
|
+
Value
|
415
|
+
value (char n)
|
416
|
+
{
|
417
|
+
return (int) n;
|
418
|
+
}
|
419
|
+
|
420
|
+
Value
|
421
|
+
value (unsigned char n)
|
422
|
+
{
|
423
|
+
return (int) n;
|
424
|
+
}
|
425
|
+
|
426
|
+
Value
|
427
|
+
value (short n)
|
428
|
+
{
|
429
|
+
return (int) n;
|
430
|
+
}
|
431
|
+
|
432
|
+
Value
|
433
|
+
value (unsigned short n)
|
434
|
+
{
|
435
|
+
return (int) n;
|
436
|
+
}
|
437
|
+
|
438
|
+
Value
|
439
|
+
value (int n)
|
440
|
+
{
|
441
|
+
return n;
|
442
|
+
}
|
443
|
+
|
444
|
+
Value
|
445
|
+
value (unsigned int n)
|
446
|
+
{
|
447
|
+
return UINT2NUM(n);
|
448
|
+
}
|
449
|
+
|
450
|
+
Value
|
451
|
+
value (long n)
|
452
|
+
{
|
453
|
+
return LONG2NUM(n);
|
454
|
+
}
|
455
|
+
|
456
|
+
Value
|
457
|
+
value (unsigned long n)
|
458
|
+
{
|
459
|
+
return ULONG2NUM(n);
|
460
|
+
}
|
461
|
+
|
462
|
+
Value
|
463
|
+
value (long long n)
|
464
|
+
{
|
465
|
+
return LL2NUM(n);
|
466
|
+
}
|
467
|
+
|
468
|
+
Value
|
469
|
+
value (unsigned long long n)
|
470
|
+
{
|
471
|
+
return ULL2NUM(n);
|
472
|
+
}
|
473
|
+
|
474
|
+
Value
|
475
|
+
value (float f)
|
476
|
+
{
|
477
|
+
return f;
|
478
|
+
}
|
479
|
+
|
480
|
+
Value
|
481
|
+
value (double d)
|
482
|
+
{
|
483
|
+
return d;
|
484
|
+
}
|
485
|
+
|
486
|
+
Value
|
487
|
+
value (const char* s, bool tainted)
|
488
|
+
{
|
489
|
+
return Value(s, tainted);
|
490
|
+
}
|
491
|
+
|
492
|
+
Value
|
493
|
+
value (const char* s, size_t len, bool tainted)
|
494
|
+
{
|
495
|
+
return Value(s, len, tainted);
|
496
|
+
}
|
497
|
+
|
498
|
+
Value
|
499
|
+
value (size_t size, const Value* values)
|
500
|
+
{
|
501
|
+
return Value(size, values);
|
502
|
+
}
|
503
|
+
|
504
|
+
Value
|
505
|
+
value (Symbol sym)
|
506
|
+
{
|
507
|
+
return ID2SYM(sym.id());
|
508
|
+
}
|
509
|
+
|
510
|
+
|
511
|
+
}// Rucy
|