rucy 0.1.2 → 0.1.3

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/ext/rucy/tester.cpp CHANGED
@@ -4,12 +4,88 @@
4
4
  using namespace Rucy;
5
5
 
6
6
 
7
+ struct Tester
8
+ {
9
+
10
+ int value;
11
+
12
+ };// Tester
13
+
14
+
15
+ static Class cTester;
16
+
17
+
18
+ Class
19
+ tester_class ()
20
+ {
21
+ return cTester;
22
+ }
23
+
24
+
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
+ /*
46
+ alloc function.
47
+ */
48
+ static
49
+ RUBY_DEF_ALLOC(alloc, klass)
50
+ {
51
+ return new_type<Tester>(klass);
52
+ }
53
+ RUBY_END
54
+
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
65
+
66
+ /*
67
+ set value.
68
+ */
7
69
  static
8
- RUBY_DEF0(do_nothing)
70
+ RUBY_DEF1(set_value, value)
9
71
  {
72
+ Tester* t = to<Tester*>(self);
73
+ if (t) t->value = to<int>(value);
10
74
  }
11
75
  RUBY_END
12
76
 
77
+ /*
78
+ do nothing.
79
+ */
80
+ static
81
+ RUBY_DEFN(do_nothing)
82
+ {
83
+ }
84
+ RUBY_END
85
+
86
+ /*
87
+ return nil.
88
+ */
13
89
  static
14
90
  RUBY_DEF0(return_nil)
15
91
  {
@@ -17,27 +93,39 @@ RUBY_DEF0(return_nil)
17
93
  }
18
94
  RUBY_END
19
95
 
96
+ /*
97
+ return int.
98
+ */
20
99
  static
21
100
  RUBY_DEF0(return_int)
22
101
  {
23
- return Value(1);
102
+ return value(1);
24
103
  }
25
104
  RUBY_END
26
105
 
106
+ /*
107
+ return flaot.
108
+ */
27
109
  static
28
110
  RUBY_DEF0(return_float)
29
111
  {
30
- return Value(1.0f);
112
+ return value(1.0f);
31
113
  }
32
114
  RUBY_END
33
115
 
116
+ /*
117
+ return string.
118
+ */
34
119
  static
35
120
  RUBY_DEF0(return_string)
36
121
  {
37
- return Value("");
122
+ return value("");
38
123
  }
39
124
  RUBY_END
40
125
 
126
+ /*
127
+ raise ruby's exception.
128
+ */
41
129
  static
42
130
  RUBY_DEF0(raise_ruby_exception)
43
131
  {
@@ -45,6 +133,9 @@ RUBY_DEF0(raise_ruby_exception)
45
133
  }
46
134
  RUBY_END
47
135
 
136
+ /*
137
+ raise in eval.
138
+ */
48
139
  static
49
140
  RUBY_DEF0(raise_in_eval)
50
141
  {
@@ -52,6 +143,19 @@ RUBY_DEF0(raise_in_eval)
52
143
  }
53
144
  RUBY_END
54
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
+ */
55
159
  static
56
160
  RUBY_DEF0(throw_std_exception)
57
161
  {
@@ -59,6 +163,9 @@ RUBY_DEF0(throw_std_exception)
59
163
  }
60
164
  RUBY_END
61
165
 
166
+ /*
167
+ throw std::runtime_error.
168
+ */
62
169
  static
63
170
  RUBY_DEF0(throw_std_runtime_error)
64
171
  {
@@ -66,6 +173,24 @@ RUBY_DEF0(throw_std_runtime_error)
66
173
  }
67
174
  RUBY_END
68
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
+ */
69
194
  static
70
195
  RUBY_DEF0(throw_std_string)
71
196
  {
@@ -73,6 +198,9 @@ RUBY_DEF0(throw_std_string)
73
198
  }
74
199
  RUBY_END
75
200
 
201
+ /*
202
+ throw char*.
203
+ */
76
204
  static
77
205
  RUBY_DEF0(throw_cstring)
78
206
  {
@@ -86,17 +214,25 @@ Init_tester ()
86
214
  {
87
215
  if (!init()) return;
88
216
 
89
- define_module("Rucy")
90
- .define_class("Tester")
91
- .define_method("do_nothing", do_nothing)
92
- .define_method("return_nil", return_nil)
93
- .define_method("return_int", return_int)
94
- .define_method("return_float", return_float)
95
- .define_method("return_string", return_string)
96
- .define_method("raise_ruby_exception", raise_ruby_exception)
97
- .define_method("raise_in_eval", raise_in_eval)
98
- .define_method("throw_std_exception", throw_std_exception)
99
- .define_method("throw_std_runtime_error", throw_std_runtime_error)
100
- .define_method("throw_std_string", throw_std_string)
101
- .define_method("throw_cstring", throw_cstring);
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);
102
238
  }
data/include/rucy/class.h CHANGED
@@ -28,9 +28,6 @@ namespace Rucy
28
28
  };// Class
29
29
 
30
30
 
31
- Class define_class (const char* name, Value super = rb_cObject);
32
-
33
-
34
31
  }// Rucy
35
32
 
36
33
 
data/include/rucy/defs.h CHANGED
@@ -5,12 +5,16 @@
5
5
 
6
6
 
7
7
  #include <ruby.h>
8
+ #include <xot/string.h>
8
9
 
9
10
 
10
11
  namespace Rucy
11
12
  {
12
13
 
13
14
 
15
+ using Xot::String;
16
+
17
+
14
18
  class Value;
15
19
 
16
20
 
@@ -1,17 +1,20 @@
1
1
  // -*- c++ -*-
2
- % require 'support'
3
2
  #pragma once
4
3
  #ifndef __RUCY_DEFS_H__
5
4
  #define __RUCY_DEFS_H__
6
5
 
7
6
 
8
7
  #include <ruby.h>
8
+ #include <xot/string.h>
9
9
 
10
10
 
11
11
  namespace Rucy
12
12
  {
13
13
 
14
14
 
15
+ using Xot::String;
16
+
17
+
15
18
  class Value;
16
19
 
17
20
 
@@ -5,7 +5,7 @@
5
5
 
6
6
 
7
7
  #include <stdexcept>
8
- #include <rucy/value.h>
8
+ #include <typeinfo>
9
9
  #include <rucy/class.h>
10
10
 
11
11
 
@@ -13,38 +13,6 @@ namespace Rucy
13
13
  {
14
14
 
15
15
 
16
- Class native_error_class ();
17
- // class Rucy::NativeError < RuntimeError
18
-
19
- Class system_error_class ();
20
- // class Rucy::SystemError < Rucy::NativeError
21
-
22
- Class invalid_object_error_class ();
23
- // class Rucy::InvalidObjectError < Rucy::NativeError
24
-
25
-
26
- void error (const char* format, ...);
27
-
28
- void raise (Value exception, const char* format = NULL, ...);
29
-
30
-
31
- void type_error (const char* format = NULL, ...);
32
-
33
- void argument_error (const char* format = NULL, ...);
34
-
35
- void arg_count_error (
36
- const char* method, int nargs, int nargs_expected,
37
- int n1 = -1, int n2 = -1, int n3 = -1, int n4 = -1, int n5 = -1,
38
- int n6 = -1, int n7 = -1, int n8 = -1, int n9 = -1, int n10 = -1);
39
-
40
- void not_implemented_error (const char* format = NULL, ...);
41
-
42
-
43
- void system_error (const char* format = NULL, ...);
44
-
45
- void invalid_object_error (const char* format = NULL, ...);
46
-
47
-
48
16
  class RubyException : public std::runtime_error
49
17
  {
50
18
 
@@ -79,6 +47,28 @@ namespace Rucy
79
47
  };// RubyJumptag
80
48
 
81
49
 
50
+ void raise (const char* format = NULL, ...);
51
+
52
+ void raise (VALUE exception, const char* format = NULL, ...);
53
+
54
+
55
+ void type_error (const char* format = NULL, ...);
56
+
57
+ void argument_error (const char* format = NULL, ...);
58
+
59
+ void arg_count_error (
60
+ const char* method, int nargs, int nargs_expected,
61
+ int n1 = -1, int n2 = -1, int n3 = -1, int n4 = -1, int n5 = -1,
62
+ int n6 = -1, int n7 = -1, int n8 = -1, int n9 = -1, int n10 = -1);
63
+
64
+ void not_implemented_error (const char* format = NULL, ...);
65
+
66
+
67
+ void system_error (const char* format = NULL, ...);
68
+
69
+ void invalid_object_error (const char* format = NULL, ...);
70
+
71
+
82
72
  }// Rucy
83
73
 
84
74
 
@@ -112,7 +102,13 @@ namespace Rucy
112
102
  } \
113
103
  catch (const std::exception& e) \
114
104
  { \
115
- RUBY_THROW(rb_exc_new2(Rucy::native_error_class(), e.what())); \
105
+ Rucy::String text = e.what(), type = typeid(e).name(); \
106
+ if (!type.empty()) \
107
+ { \
108
+ if (!text.empty()) text += " "; \
109
+ text += "[" + type + "]"; \
110
+ } \
111
+ RUBY_THROW(rb_exc_new2(Rucy::native_error_class(), text.c_str())); \
116
112
  } \
117
113
  catch (const std::string& s) \
118
114
  { \
@@ -125,7 +121,7 @@ namespace Rucy
125
121
  catch (...) \
126
122
  { \
127
123
  RUBY_THROW(rb_exc_new2( \
128
- Rucy::native_error_class(), "Unknown C++ exception thrown.")); \
124
+ Rucy::native_error_class(), "Unknown C++ exception was thrown.")); \
129
125
  }
130
126
 
131
127
  #define RUBY_THROW(exception) \
@@ -1,5 +1,4 @@
1
1
  // -*- c++ -*-
2
- % require 'support'
3
2
  #pragma once
4
3
  #ifndef __RUCY_FUNCTION_H__
5
4
  #define __RUCY_FUNCTION_H__
@@ -39,6 +39,8 @@ namespace Rucy
39
39
 
40
40
  Module include_module (Value module);
41
41
 
42
+ Module extend_module (Value module);
43
+
42
44
  Module define_function (const char* name, RubyFunctionN fun);
43
45
  Module define_function (const char* name, RubyFunction0 fun);
44
46
  Module define_function (const char* name, RubyFunction1 fun);
@@ -85,9 +87,9 @@ namespace Rucy
85
87
 
86
88
  Module define_module (const char* name);
87
89
 
90
+ Class define_class (const char* name, Value super = rb_cObject);
88
91
 
89
- Module rucy_module ();
90
- // module Rucy
92
+ void define_const (const char* name, Value val);
91
93
 
92
94
 
93
95
  }// Rucy
@@ -1,5 +1,4 @@
1
1
  // -*- c++ -*-
2
- % require 'support'
3
2
  #pragma once
4
3
  #ifndef __RUCY_MODULE_H__
5
4
  #define __RUCY_MODULE_H__
@@ -40,6 +39,8 @@ namespace Rucy
40
39
 
41
40
  Module include_module (Value module);
42
41
 
42
+ Module extend_module (Value module);
43
+
43
44
  % [
44
45
  % 'define_function',
45
46
  % 'define_method',
@@ -57,9 +58,9 @@ namespace Rucy
57
58
 
58
59
  Module define_module (const char* name);
59
60
 
61
+ Class define_class (const char* name, Value super = rb_cObject);
60
62
 
61
- Module rucy_module ();
62
- // module Rucy
63
+ void define_const (const char* name, Value val);
63
64
 
64
65
 
65
66
  }// Rucy
data/include/rucy/rucy.h CHANGED
@@ -5,7 +5,7 @@
5
5
 
6
6
 
7
7
  #include <ruby.h>
8
- #include <rucy/value.h>
8
+ #include <rucy/class.h>
9
9
 
10
10
 
11
11
  namespace Rucy
@@ -14,13 +14,21 @@ namespace Rucy
14
14
 
15
15
  bool init ();
16
16
 
17
+ bool check_class (Value obj, Value klass);
17
18
 
18
- Value require (const char* name);
19
19
 
20
- void define_const (const char* name, Value val);
20
+ Module rucy_module ();
21
+ // module Rucy
21
22
 
22
23
 
23
- bool check_class (Value obj, Value class_);
24
+ Class native_error_class ();
25
+ // class Rucy::NativeError < RuntimeError
26
+
27
+ Class system_error_class ();
28
+ // class Rucy::SystemError < Rucy::NativeError
29
+
30
+ Class invalid_object_error_class ();
31
+ // class Rucy::InvalidObjectError < Rucy::NativeError
24
32
 
25
33
 
26
34
  template <typename T> inline void mark_type (void* p)
@@ -1,5 +1,4 @@
1
1
  // -*- c++ -*-
2
- % require 'support'
3
2
  #pragma once
4
3
  #ifndef __RUCY_VALUE_H__
5
4
  #define __RUCY_VALUE_H__
data/include/rucy.h CHANGED
@@ -1,10 +1,10 @@
1
1
  // -*- c++ -*-
2
+ #include <rucy/defs.h>
2
3
  #include <rucy/rucy.h>
3
- #include <rucy/class.h>
4
+ #include <rucy/value.h>
4
5
  #include <rucy/exception.h>
5
6
  #include <rucy/function.h>
6
- #include <rucy/gc.h>
7
7
  #include <rucy/module.h>
8
- #include <rucy/string.h>
8
+ #include <rucy/class.h>
9
9
  #include <rucy/symbol.h>
10
- #include <rucy/value.h>
10
+ #include <rucy/gc.h>
data/lib/rucy/module.rb CHANGED
@@ -10,12 +10,20 @@ module Rucy
10
10
  File.expand_path(File.join File.dirname(__FILE__), '..', '..')
11
11
  end
12
12
 
13
- def include_dirs ()
13
+ def include_dirs ()
14
14
  [File.join(root_dir, 'include')]
15
15
  end
16
16
 
17
17
  def library_dirs ()
18
- [File.join(root_dir, 'lib')]
18
+ %w[lib].map {|dir| File.join root_dir, dir}
19
+ end
20
+
21
+ def task_dir ()
22
+ File.join root_dir, 'task'
23
+ end
24
+
25
+ def load_tasks ()
26
+ Dir["#{task_dir}/**/*.rake"].each {|path| load path}
19
27
  end
20
28
 
21
29
  def version ()
data/rucy.gemspec CHANGED
@@ -7,38 +7,50 @@ require 'rake'
7
7
  require 'rucy/module'
8
8
 
9
9
 
10
- FILES = FileList[*%w[
10
+ MODULE = Rucy
11
+ NAME = MODULE.name.downcase
12
+
13
+
14
+ FILES = FileList[*%W[
11
15
  README
12
16
  ChangeLog
13
17
  Rakefile
14
- support.rb
15
- rucy.gemspec
18
+ #{NAME}.gemspec
16
19
  VERSION
17
20
  task/**/*.rake
18
21
  ext/**/*.rb
22
+ ext/**/*.h
19
23
  ext/**/*.cpp
20
- include/**/*.h
21
24
  include/**/*.erb
25
+ include/**/*.h
22
26
  lib/**/*.rb
23
- src/**/*.cpp
24
27
  src/**/*.erb
28
+ src/**/*.h
29
+ src/**/*.cpp
25
30
  test/**/*.rb
26
31
  ]]
27
32
 
33
+ RDOCS = FileList[*%W[
34
+ README
35
+ .doc/ext/**/*.cpp
36
+ ]]
37
+
38
+
28
39
  Gem::Specification.new do |s|
29
- s.name = 'rucy'
40
+ s.name = NAME
30
41
  s.summary = 'A Ruby C++ Extension Helper Library.'
31
42
  s.description = 'This library helps you to develop Ruby Extension by C++.'
32
- s.version = Rucy.version
43
+ s.version = MODULE.version
33
44
 
34
45
  s.authors = %w[snori]
35
46
  s.email = 'snori@xord.org'
36
- s.homepage = 'http://github.com/xord/rucy'
47
+ s.homepage = "http://github.com/xord/#{NAME}"
37
48
 
38
49
  s.platform = Gem::Platform::RUBY
39
50
  s.required_ruby_version = '>=1.9.0'
40
51
  s.require_paths << 'ext'
41
52
 
53
+ s.add_runtime_dependency 'xot'
42
54
  s.add_development_dependency 'rake'
43
55
  s.add_development_dependency 'gemcutter'
44
56
 
@@ -46,7 +58,9 @@ Gem::Specification.new do |s|
46
58
  s.test_files = FileList['test/**/test_*.rb'].to_a
47
59
 
48
60
  s.has_rdoc = true
49
- s.extra_rdoc_files = ['README']
61
+ s.extra_rdoc_files = RDOCS.to_a
62
+
63
+ s.executables << 'rucy2rdoc'
50
64
 
51
65
  s.extensions << 'Rakefile'
52
66
  end
data/src/class.cpp CHANGED
@@ -25,11 +25,4 @@ namespace Rucy
25
25
  }
26
26
 
27
27
 
28
- Class
29
- define_class (const char* name, Value super)
30
- {
31
- return rb_define_class(name, super);
32
- }
33
-
34
-
35
28
  }// Rucy