rucy 0.1.3 → 0.1.4

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 (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/ext/rucy/class.h ADDED
@@ -0,0 +1,96 @@
1
+ // -*- c++ -*-
2
+ #pragma once
3
+ #ifndef __CLASS_H__
4
+ #define __CLASS_H__
5
+
6
+
7
+ #include <xot/ref.h>
8
+ #include <rucy/extension.h>
9
+ #include "tester.h"
10
+
11
+
12
+ class Base : public Xot::RefCountable<>
13
+ {
14
+
15
+ public:
16
+
17
+ Base ()
18
+ {
19
+ log("Base()");
20
+ }
21
+
22
+ virtual ~Base ()
23
+ {
24
+ log("~Base()");
25
+ }
26
+
27
+ virtual const char* name () const
28
+ {
29
+ return "Base::name";
30
+ }
31
+
32
+ virtual const char* name_overridable () const
33
+ {
34
+ return "Base::name_overridable";
35
+ }
36
+
37
+ virtual const char* name_overridable_faster () const
38
+ {
39
+ return "Base::name_overridable_faster";
40
+ }
41
+
42
+ };// Base
43
+
44
+
45
+ class Sub : public Base
46
+ {
47
+
48
+ public:
49
+
50
+ Sub ()
51
+ {
52
+ log("Sub()");
53
+ }
54
+
55
+ virtual ~Sub ()
56
+ {
57
+ log("~Sub()");
58
+ }
59
+
60
+ virtual const char* name () const
61
+ {
62
+ return "Sub::name";
63
+ }
64
+
65
+ virtual const char* name_overridable () const
66
+ {
67
+ return "Sub::name_overridable";
68
+ }
69
+
70
+ virtual const char* name_overridable_faster () const
71
+ {
72
+ return "Sub::name_overridable_faster";
73
+ }
74
+
75
+ };// Sub
76
+
77
+
78
+ class RubyObj : public Xot::RefCountable<>
79
+ {
80
+
81
+ public:
82
+
83
+ RubyObj ()
84
+ {
85
+ log("RubyObj()");
86
+ }
87
+
88
+ ~RubyObj ()
89
+ {
90
+ log("~RubyObj()");
91
+ }
92
+
93
+ };// Obj
94
+
95
+
96
+ #endif//EOH
@@ -0,0 +1,107 @@
1
+ #include <rucy.h>
2
+
3
+
4
+ using namespace Rucy;
5
+
6
+
7
+ /*
8
+ raise ruby's exception.
9
+ */
10
+ static
11
+ RUBY_DEF0(raise_ruby_exception)
12
+ {
13
+ throw RubyException(rb_eStandardError, "raise_ruby_exception");
14
+ }
15
+ RUBY_END
16
+
17
+ /*
18
+ raise in eval.
19
+ */
20
+ static
21
+ RUBY_DEF0(raise_in_eval)
22
+ {
23
+ eval("raise 'raise_in_eval'");
24
+ }
25
+ RUBY_END
26
+
27
+ /*
28
+ throw nothing.
29
+ */
30
+ static
31
+ RUBY_DEF0(throw_nothing)
32
+ {
33
+ throw;
34
+ }
35
+ RUBY_END
36
+
37
+ /*
38
+ throw std::exception.
39
+ */
40
+ static
41
+ RUBY_DEF0(throw_std_exception)
42
+ {
43
+ throw std::exception();
44
+ }
45
+ RUBY_END
46
+
47
+ /*
48
+ throw std::runtime_error.
49
+ */
50
+ static
51
+ RUBY_DEF0(throw_std_runtime_error)
52
+ {
53
+ throw std::runtime_error("std::runtime_error");
54
+ }
55
+ RUBY_END
56
+
57
+ struct MyException : public std::runtime_error
58
+ {
59
+ MyException() : runtime_error("") {}
60
+ };
61
+
62
+ /*
63
+ throw custom exception class.
64
+ */
65
+ static
66
+ RUBY_DEF0(throw_custom_exception)
67
+ {
68
+ throw MyException();
69
+ }
70
+ RUBY_END
71
+
72
+ /*
73
+ throw std::string.
74
+ */
75
+ static
76
+ RUBY_DEF0(throw_std_string)
77
+ {
78
+ throw std::string("std::string");
79
+ }
80
+ RUBY_END
81
+
82
+ /*
83
+ throw char*.
84
+ */
85
+ static
86
+ RUBY_DEF0(throw_cstring)
87
+ {
88
+ throw "cstring";
89
+ }
90
+ RUBY_END
91
+
92
+
93
+ void
94
+ Init_exception ()
95
+ {
96
+ Module mRucy = define_module("Rucy");
97
+ Module mTester = mRucy.define_module("Tester");
98
+
99
+ mTester.define_method("raise_ruby_exception", raise_ruby_exception);
100
+ mTester.define_method("raise_in_eval", raise_in_eval);
101
+ mTester.define_method("throw_nothing", throw_nothing);
102
+ mTester.define_method("throw_std_exception", throw_std_exception);
103
+ mTester.define_method("throw_std_runtime_error", throw_std_runtime_error);
104
+ mTester.define_method("throw_custom_exception", throw_custom_exception);
105
+ mTester.define_method("throw_std_string", throw_std_string);
106
+ mTester.define_method("throw_cstring", throw_cstring);
107
+ }
data/ext/rucy/extconf.rb CHANGED
@@ -1,11 +1,7 @@
1
1
  # -*- coding: utf-8 -*-
2
2
 
3
3
 
4
- %w[. ../xot].map {|s| "../../#{s}/lib"}.each do |path|
5
- $: << File.expand_path(File.join File.dirname(__FILE__), *path.split('/'))
6
- end
7
-
8
- require 'rubygems'
4
+ require 'bundler/setup'
9
5
  require 'mkmf'
10
6
  require 'xot/rake/helpers'
11
7
  require 'xot/module'
@@ -14,7 +10,8 @@ require 'rucy/module'
14
10
  include Xot::Rake
15
11
 
16
12
 
17
- DEBUG = env :DEBUG, false
13
+ debug = env :DEBUG, false
14
+
18
15
 
19
16
  DEFS = []
20
17
  INCDIRS = %w[
@@ -36,7 +33,8 @@ LIBS = %w[
36
33
  ]
37
34
 
38
35
 
39
- DEFS << '_DEBUG' if DEBUG
36
+ DEFS << '_DEBUG' if debug
37
+ DEFS << 'NDEBUG' unless debug
40
38
  DEFS << 'WINDOWS' << 'WIN32' if win32?
41
39
  DEFS << 'COCOA' if cocoa?
42
40
  DEFS << $~[0].upcase if RUBY_PLATFORM =~ /mswin|ming|cygwin|darwin/i
@@ -44,9 +42,12 @@ DEFS << $~[0].upcase if RUBY_PLATFORM =~ /mswin|ming|cygwin|darwin/i
44
42
  $CPPFLAGS << DEFS.map {|s| " -D#{s}"}.join
45
43
  $CPPFLAGS << INCDIRS.map {|s| " -I#{s}"}.join
46
44
  $LDFLAGS << LIBDIRS.map {|s| " -L#{s}"}.join
45
+ $CFLAGS << ' --stdlib=libc++' if clang?
47
46
  $LOCAL_LIBS << ' -lrucy'
48
47
 
49
- Config::CONFIG.each {|key, val| val.gsub!(/gcc/, 'g++')}
48
+ RbConfig::CONFIG.each do |key, val|
49
+ {'gcc' => 'g++', 'clang' => 'clang++'}.each {|from, to| val.gsub! from, to}
50
+ end
50
51
 
51
52
 
52
53
  dir_config 'boost'
@@ -0,0 +1,68 @@
1
+ #include <rucy.h>
2
+
3
+
4
+ using namespace Rucy;
5
+
6
+
7
+ /*
8
+ do nothing.
9
+ */
10
+ static
11
+ RUBY_DEFN(do_nothing)
12
+ {
13
+ }
14
+ RUBY_END
15
+
16
+ /*
17
+ return nil.
18
+ */
19
+ static
20
+ RUBY_DEF0(return_nil)
21
+ {
22
+ return Qnil;
23
+ }
24
+ RUBY_END
25
+
26
+ /*
27
+ return int.
28
+ */
29
+ static
30
+ RUBY_DEF0(return_int)
31
+ {
32
+ return value(1);
33
+ }
34
+ RUBY_END
35
+
36
+ /*
37
+ return flaot.
38
+ */
39
+ static
40
+ RUBY_DEF0(return_float)
41
+ {
42
+ return value(1.0f);
43
+ }
44
+ RUBY_END
45
+
46
+ /*
47
+ return string.
48
+ */
49
+ static
50
+ RUBY_DEF0(return_string)
51
+ {
52
+ return value("");
53
+ }
54
+ RUBY_END
55
+
56
+
57
+ void
58
+ Init_function ()
59
+ {
60
+ Module mRucy = define_module("Rucy");
61
+ Module mTester = mRucy.define_module("Tester");
62
+
63
+ mTester.define_method("do_nothing", do_nothing);
64
+ mTester.define_method("return_nil", return_nil);
65
+ mTester.define_method("return_int", return_int);
66
+ mTester.define_method("return_float", return_float);
67
+ mTester.define_method("return_string", return_string);
68
+ }
@@ -0,0 +1,83 @@
1
+ #include <rucy.h>
2
+
3
+
4
+ using namespace Rucy;
5
+
6
+
7
+ struct Struct
8
+ {
9
+
10
+ int num;
11
+
12
+ Struct () : num(0) {}
13
+
14
+ };// Struct
15
+
16
+
17
+ static Class cStruct;
18
+
19
+
20
+ namespace Rucy
21
+ {
22
+
23
+
24
+ static Value
25
+ value (const Struct& obj)
26
+ {
27
+ return new_type(cStruct, new Struct(obj));
28
+ }
29
+
30
+ template <> inline Struct*
31
+ value_to<Struct*> (Value val, bool)
32
+ {
33
+ return get_type_ptr<Struct>(val, cStruct);
34
+ }
35
+
36
+
37
+ }// Rucy
38
+
39
+
40
+ /*
41
+ alloc function.
42
+ */
43
+ static
44
+ RUBY_DEF_ALLOC(alloc, klass)
45
+ {
46
+ return new_type<Struct>(klass);
47
+ }
48
+ RUBY_END
49
+
50
+ /*
51
+ get num.
52
+ */
53
+ static
54
+ RUBY_DEF0(get_num)
55
+ {
56
+ Struct* obj = to<Struct*>(self);
57
+ if (obj) return value(obj->num);
58
+ }
59
+ RUBY_END
60
+
61
+ /*
62
+ set num.
63
+ */
64
+ static
65
+ RUBY_DEF1(set_num, num)
66
+ {
67
+ Struct* obj = to<Struct*>(self);
68
+ if (obj) obj->num = to<int>(num);
69
+ }
70
+ RUBY_END
71
+
72
+
73
+ void
74
+ Init_struct ()
75
+ {
76
+ Module mRucy = define_module("Rucy");
77
+ Module mTester = mRucy.define_module("Tester");
78
+
79
+ cStruct = mTester.define_class("Struct");
80
+ cStruct.define_alloc_func(alloc);
81
+ cStruct.define_method("num", get_num);
82
+ cStruct.define_method("num=", set_num);
83
+ }
data/ext/rucy/tester.cpp CHANGED
@@ -1,212 +1,65 @@
1
- #include <rucy.h>
2
-
3
-
4
- using namespace Rucy;
1
+ #include "tester.h"
5
2
 
6
3
 
7
- struct Tester
8
- {
4
+ #include <vector>
5
+ #include <rucy.h>
9
6
 
10
- int value;
11
7
 
12
- };// Tester
8
+ using namespace Rucy;
13
9
 
14
10
 
15
- static Class cTester;
11
+ static std::vector<String> logs;
16
12
 
17
13
 
18
- Class
19
- tester_class ()
14
+ void
15
+ log (const char* str)
20
16
  {
21
- return cTester;
17
+ logs.push_back(str);
22
18
  }
23
19
 
24
20
 
25
- namespace Rucy
26
- {
27
-
28
-
29
- static Value
30
- value (const Tester& obj)
31
- {
32
- return new_type<Tester>(tester_class(), new Tester(obj));
33
- }
34
-
35
- template <> inline Tester*
36
- value_to<Tester*> (Value obj, bool)
37
- {
38
- return get_type<Tester>(obj, tester_class());
39
- }
40
-
41
-
42
- }// Rucy
43
-
44
-
45
21
  /*
46
- alloc function.
22
+ return last log.
47
23
  */
48
- static
49
- RUBY_DEF_ALLOC(alloc, klass)
24
+ RUBY_DEFN(last_log)
50
25
  {
51
- return new_type<Tester>(klass);
52
- }
53
- RUBY_END
26
+ if (argc > 1)
27
+ arg_count_error("#last_log", argc, 0, 1);
54
28
 
55
- /*
56
- get value.
57
- */
58
- static
59
- RUBY_DEF0(get_value)
60
- {
61
- Tester* t = to<Tester*>(self);
62
- if (t) return value(t->value);
63
- }
64
- RUBY_END
29
+ size_t index = (argc >= 1) ? to<size_t>(argv[0]) : logs.size() - 1;
30
+ if (index >= logs.size())
31
+ index_error();
65
32
 
66
- /*
67
- set value.
68
- */
69
- static
70
- RUBY_DEF1(set_value, value)
71
- {
72
- Tester* t = to<Tester*>(self);
73
- if (t) t->value = to<int>(value);
33
+ return value(logs[index].c_str());
74
34
  }
75
35
  RUBY_END
76
36
 
77
37
  /*
78
- do nothing.
38
+ return all logs.
79
39
  */
80
- static
81
- RUBY_DEFN(do_nothing)
40
+ RUBY_DEF0(all_logs)
82
41
  {
42
+ std::vector<Value> a;
43
+ for (size_t i = 0; i < logs.size(); ++i) a.push_back(logs[i].c_str());
44
+ return value(a.size(), &a[0]);
83
45
  }
84
46
  RUBY_END
85
47
 
86
48
  /*
87
- return nil.
49
+ clcear all logs.
88
50
  */
89
- static
90
- RUBY_DEF0(return_nil)
51
+ RUBY_DEF0(clear_logs)
91
52
  {
92
- return Qnil;
53
+ logs.clear();
93
54
  }
94
55
  RUBY_END
95
56
 
96
- /*
97
- return int.
98
- */
99
- static
100
- RUBY_DEF0(return_int)
101
- {
102
- return value(1);
103
- }
104
- RUBY_END
105
57
 
106
- /*
107
- return flaot.
108
- */
109
- static
110
- RUBY_DEF0(return_float)
111
- {
112
- return value(1.0f);
113
- }
114
- RUBY_END
115
-
116
- /*
117
- return string.
118
- */
119
- static
120
- RUBY_DEF0(return_string)
121
- {
122
- return value("");
123
- }
124
- RUBY_END
125
-
126
- /*
127
- raise ruby's exception.
128
- */
129
- static
130
- RUBY_DEF0(raise_ruby_exception)
131
- {
132
- throw RubyException(rb_eStandardError, "raise_ruby_exception");
133
- }
134
- RUBY_END
135
-
136
- /*
137
- raise in eval.
138
- */
139
- static
140
- RUBY_DEF0(raise_in_eval)
141
- {
142
- eval("raise 'raise_in_eval'");
143
- }
144
- RUBY_END
145
-
146
- /*
147
- throw nothing.
148
- */
149
- static
150
- RUBY_DEF0(throw_nothing)
151
- {
152
- throw;
153
- }
154
- RUBY_END
155
-
156
- /*
157
- throw std::exception.
158
- */
159
- static
160
- RUBY_DEF0(throw_std_exception)
161
- {
162
- throw std::exception();
163
- }
164
- RUBY_END
165
-
166
- /*
167
- throw std::runtime_error.
168
- */
169
- static
170
- RUBY_DEF0(throw_std_runtime_error)
171
- {
172
- throw std::runtime_error("std::runtime_error");
173
- }
174
- RUBY_END
175
-
176
- struct MyException : public std::runtime_error
177
- {
178
- MyException() : runtime_error("") {}
179
- };
180
-
181
- /*
182
- throw custom exception class.
183
- */
184
- static
185
- RUBY_DEF0(throw_custom_exception)
186
- {
187
- throw MyException();
188
- }
189
- RUBY_END
190
-
191
- /*
192
- throw std::string.
193
- */
194
- static
195
- RUBY_DEF0(throw_std_string)
196
- {
197
- throw std::string("std::string");
198
- }
199
- RUBY_END
200
-
201
- /*
202
- throw char*.
203
- */
204
- static
205
- RUBY_DEF0(throw_cstring)
206
- {
207
- throw "cstring";
208
- }
209
- RUBY_END
58
+ void Init_value ();
59
+ void Init_exception ();
60
+ void Init_function ();
61
+ void Init_struct ();
62
+ void Init_class ();
210
63
 
211
64
 
212
65
  extern "C" void
@@ -214,25 +67,16 @@ Init_tester ()
214
67
  {
215
68
  if (!init()) return;
216
69
 
217
- Module m = define_module("Rucy");
218
-
219
- Class c = m.define_class("Tester");
220
- cTester = c;
221
-
222
- c.define_alloc_func(alloc);
223
- c.define_method("value", get_value);
224
- c.define_method("value=", set_value);
225
- c.define_method("do_nothing", do_nothing);
226
- c.define_method("return_nil", return_nil);
227
- c.define_method("return_int", return_int);
228
- c.define_method("return_float", return_float);
229
- c.define_method("return_string", return_string);
230
- c.define_method("raise_ruby_exception", raise_ruby_exception);
231
- c.define_method("raise_in_eval", raise_in_eval);
232
- c.define_method("throw_nothing", throw_nothing);
233
- c.define_method("throw_std_exception", throw_std_exception);
234
- c.define_method("throw_std_runtime_error", throw_std_runtime_error);
235
- c.define_method("throw_custom_exception", throw_custom_exception);
236
- c.define_method("throw_std_string", throw_std_string);
237
- c.define_method("throw_cstring", throw_cstring);
70
+ Module mRucy = define_module("Rucy");
71
+ Module mTester = mRucy.define_module("Tester");
72
+
73
+ mTester.define_function("last_log", last_log);
74
+ mTester.define_function("all_logs", all_logs);
75
+ mTester.define_function("clear_logs", clear_logs);
76
+
77
+ Init_value();
78
+ Init_exception();
79
+ Init_function();
80
+ Init_struct ();
81
+ Init_class ();
238
82
  }
data/ext/rucy/tester.h ADDED
@@ -0,0 +1,10 @@
1
+ // -*- c++ -*-
2
+ #pragma once
3
+ #ifndef __TESTER_H__
4
+ #define __TESTER_H__
5
+
6
+
7
+ void log (const char* str);
8
+
9
+
10
+ #endif//EOH