rucy 0.1.3 → 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (50) hide show
  1. data/.doc/ext/rucy/class.cpp +244 -0
  2. data/.doc/ext/rucy/exception.cpp +99 -0
  3. data/.doc/ext/rucy/function.cpp +63 -0
  4. data/.doc/ext/rucy/struct.cpp +80 -0
  5. data/.doc/ext/rucy/tester.cpp +41 -184
  6. data/.doc/ext/rucy/value.cpp +42 -0
  7. data/.gitignore +22 -0
  8. data/Rakefile +4 -29
  9. data/VERSION +1 -1
  10. data/ext/rucy/class.cpp +262 -0
  11. data/ext/rucy/class.h +96 -0
  12. data/ext/rucy/exception.cpp +107 -0
  13. data/ext/rucy/extconf.rb +9 -8
  14. data/ext/rucy/function.cpp +68 -0
  15. data/ext/rucy/struct.cpp +83 -0
  16. data/ext/rucy/tester.cpp +41 -197
  17. data/ext/rucy/tester.h +10 -0
  18. data/ext/rucy/value.cpp +46 -0
  19. data/include/rucy/defs.h.erb +17 -0
  20. data/include/rucy/exception.h +2 -0
  21. data/include/rucy/extension.h +206 -0
  22. data/include/rucy/rucy.h +25 -2
  23. data/include/rucy/symbol.h +11 -0
  24. data/include/rucy/value.h.erb +66 -14
  25. data/include/rucy.h +1 -1
  26. data/lib/rucy/module.rb +9 -2
  27. data/rucy.gemspec +16 -40
  28. data/src/exception.cpp +17 -25
  29. data/src/rucy.cpp +2 -2
  30. data/src/symbol.cpp +24 -0
  31. data/src/value.cpp.erb +161 -25
  32. data/task/doc.rake +50 -0
  33. data/test/helpers.rb +7 -0
  34. data/test/test_class.rb +161 -0
  35. data/test/test_exception.rb +39 -0
  36. data/test/test_function.rb +31 -0
  37. data/test/test_struct.rb +30 -0
  38. data/test/test_value.rb +18 -0
  39. metadata +123 -74
  40. data/include/rucy/defs.h +0 -101
  41. data/include/rucy/function.h +0 -245
  42. data/include/rucy/gc.h +0 -59
  43. data/include/rucy/module.h +0 -98
  44. data/include/rucy/value.h +0 -291
  45. data/src/function.cpp +0 -158
  46. data/src/gc.cpp +0 -63
  47. data/src/module.cpp +0 -325
  48. data/src/value.cpp +0 -511
  49. data/task/ext.rake +0 -96
  50. data/test/test_rucy.rb +0 -73
data/src/value.cpp DELETED
@@ -1,511 +0,0 @@
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
data/task/ext.rake DELETED
@@ -1,96 +0,0 @@
1
- # -*- mode: ruby; coding: utf-8 -*-
2
-
3
-
4
- require 'rbconfig'
5
- require 'rucy/module'
6
-
7
-
8
- namespace :ext do
9
-
10
-
11
- rbconf = RbConfig::CONFIG
12
-
13
- mod = MODULE
14
- name = env :NAME, MODULE.name.downcase
15
- extdir = env :EXTDIR, 'ext'
16
- docdir = env :DOCDIR, 'doc'
17
- dlext = env :DLEXT, rbconf['DLEXT'] || 'so'
18
- ruby = env :RUBY, 'ruby'
19
- make = env :MAKE, 'make'
20
- cc = env :CC, rbconf['CC'] || 'g++'
21
- rdoc = env :RDOC, 'rdoc'# 'yardoc'
22
- rucy2rdoc = env :RUCY2RDOC, 'rucy2rdoc'
23
- defs = env :DEFS, []
24
- incdirs = env(:INCDIRS) ? env(:INCDIRS) : []
25
- cflags = env(:CFLAGS, '-Wall -O -g').dup
26
-
27
- dir = "#{extdir}/#{name}"
28
- modname = "#{name}/native"
29
- outname = "#{name}.#{dlext}"
30
- out = File.join extdir, outname
31
-
32
- srcs = FileList["#{dir}/**/*.cpp"]
33
-
34
- incroot = rbconf['rubyhdrdir']
35
- incdirs.concat mod.include_dirs + Rucy.include_dirs + [
36
- incroot,
37
- "#{incroot}/#{RUBY_PLATFORM}",
38
- '/opt/local/include',
39
- '/opt/include'
40
- ]
41
- incdirs.uniq!
42
-
43
- defs << 'WIN32' if win32?
44
- defs << 'COCOA' if cocoa?
45
- cflags << defs.map{|s| " -D#{s}"}.join
46
- cflags << incdirs.map{|s| " -I#{s}"}.join
47
-
48
- extconf = File.join dir, "extconf.rb"
49
- makefile = File.join dir, "Makefile"
50
- depend = File.join dir, "depend"
51
-
52
- rdocdir = ".doc/#{dir}"
53
- rdocs = Hash[srcs.map{|path| [path, "#{rdocdir}/#{File.basename path}"]}]
54
- docindex = "#{docdir}/index.html"
55
-
56
-
57
- task :build => out
58
-
59
- task :rdoc => rdocs.values
60
-
61
- task :doc => docindex
62
-
63
- task :clean do
64
- sh %( cd #{dir} && #{make} clean ) if File.exist? makefile
65
- sh %( rm -rf #{makefile} #{depend} #{docdir} #{rdocs.values.join ' '} )
66
- end
67
-
68
- file out => makefile do
69
- sh %( cd #{dir} && #{make} )
70
- end
71
-
72
- file makefile => [extconf, depend] do
73
- sh %( cd #{dir} && #{ruby} #{File.basename extconf} )
74
- end
75
-
76
- file depend => ["lib:build"] + srcs do
77
- inc = incdirs.map{|s| " -I#{s}"}.join
78
- src = srcs.map{|cpp| File.basename cpp}.join ' '
79
- dep = File.basename depend
80
- sh %( cd #{dir} && #{cc} -M #{cflags} #{inc} #{src} > #{dep} )
81
- end
82
-
83
- file docindex => "ext:rdoc" do
84
- sh %( #{rdoc} #{rdocs.values.join ' '} )
85
- end
86
-
87
- rdocs.each do |(cpp, rdoc)|
88
- file rdoc => [cpp, rdocdir] do
89
- sh %( #{rucy2rdoc} #{cpp} > #{rdoc} )
90
- end
91
- end
92
-
93
- directory rdocdir
94
-
95
-
96
- end# :ext
data/test/test_rucy.rb DELETED
@@ -1,73 +0,0 @@
1
- # -*- coding: utf-8 -*-
2
-
3
-
4
- $: << File.expand_path(File.join File.dirname(__FILE__), "..", "ext")
5
-
6
- require 'test/unit'
7
- require 'rucy/tester'
8
-
9
-
10
- class TestTester < Test::Unit::TestCase
11
-
12
- def setup ()
13
- @t = Rucy::Tester.new
14
- end
15
-
16
- def test_no_return_returns_nil ()
17
- assert_equal nil, @t.do_nothing
18
- end
19
-
20
- def test_set_get ()
21
- @t.value = 100
22
- assert_equal 100, @t.value
23
- end
24
-
25
- def test_no_return_returns_nil ()
26
- assert_equal nil, @t.do_nothing
27
- end
28
-
29
- def test_returns_nil ()
30
- assert_equal nil, @t.return_nil
31
- end
32
-
33
- def test_return_int ()
34
- assert_kind_of Integer, @t.return_int
35
- end
36
-
37
- def test_return_float ()
38
- assert_kind_of Float, @t.return_float
39
- end
40
-
41
- def test_return_string ()
42
- assert_kind_of String, @t.return_string
43
- end
44
-
45
- def test_raise_ruby_exception ()
46
- assert_raise(StandardError) {@t.raise_ruby_exception}
47
- end
48
-
49
- def test_raise_in_eval ()
50
- assert_raise(RuntimeError) {@t.raise_in_eval}
51
- end
52
-
53
- def test_throw_std_exception ()
54
- assert_raise(Rucy::NativeError) {@t.throw_std_exception}
55
- end
56
-
57
- def test_throw_std_runtime_error ()
58
- assert_raise(Rucy::NativeError) {@t.throw_std_runtime_error}
59
- end
60
-
61
- def test_throw_custom_exception ()
62
- assert_raise(Rucy::NativeError) {@t.throw_custom_exception}
63
- end
64
-
65
- def test_throw_std_string ()
66
- assert_raise(Rucy::NativeError) {@t.throw_std_string}
67
- end
68
-
69
- def test_throw_cstring ()
70
- assert_raise(Rucy::NativeError) {@t.throw_cstring}
71
- end
72
-
73
- end # TestTester