rucy 0.1.6 → 0.1.7

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 (49) hide show
  1. checksums.yaml +7 -0
  2. data/.doc/ext/rucy/class.cpp +48 -56
  3. data/.doc/ext/rucy/exception.cpp +1 -1
  4. data/.doc/ext/rucy/function.cpp +14 -2
  5. data/.doc/ext/rucy/struct.cpp +2 -20
  6. data/.doc/ext/rucy/tester.cpp +7 -19
  7. data/.doc/ext/rucy/value.cpp +23 -1
  8. data/{README → README.md} +0 -0
  9. data/Rakefile +7 -5
  10. data/VERSION +1 -1
  11. data/bin/rucy2rdoc +8 -5
  12. data/ext/rucy/class.cpp +78 -87
  13. data/ext/rucy/class.h +12 -6
  14. data/ext/rucy/exception.cpp +17 -17
  15. data/ext/rucy/extconf.rb +11 -52
  16. data/ext/rucy/function.cpp +25 -12
  17. data/ext/rucy/struct.cpp +8 -26
  18. data/ext/rucy/tester.cpp +11 -24
  19. data/ext/rucy/value.cpp +32 -9
  20. data/include/rucy/class.h +5 -3
  21. data/include/rucy/exception.h +17 -71
  22. data/include/rucy/extension.h.erb +489 -0
  23. data/include/rucy/function.h.erb +20 -34
  24. data/include/rucy/module.h.erb +20 -14
  25. data/include/rucy/ruby.h +23 -0
  26. data/include/rucy/rucy.h +7 -66
  27. data/include/rucy/symbol.h +11 -11
  28. data/include/rucy/value.h.erb +98 -176
  29. data/include/rucy.h +9 -1
  30. data/lib/rucy/module.rb +11 -7
  31. data/rucy.gemspec +3 -4
  32. data/src/class.cpp +34 -6
  33. data/src/exception.cpp +69 -54
  34. data/src/extension.cpp +59 -0
  35. data/src/function.cpp.erb +17 -25
  36. data/src/module.cpp.erb +25 -16
  37. data/src/rucy.cpp +15 -25
  38. data/src/symbol.cpp +18 -17
  39. data/src/value.cpp.erb +374 -175
  40. data/task/doc.rake +5 -0
  41. data/test/helper.rb +6 -2
  42. data/test/test_class.rb +27 -20
  43. data/test/test_function.rb +6 -0
  44. data/test/test_value.rb +4 -0
  45. metadata +29 -39
  46. data/.gitignore +0 -22
  47. data/ChangeLog +0 -13
  48. data/include/rucy/defs.h.erb +0 -76
  49. data/include/rucy/extension.h +0 -206
data/include/rucy/rucy.h CHANGED
@@ -1,10 +1,9 @@
1
1
  // -*- c++ -*-
2
2
  #pragma once
3
- #ifndef __RUCY_H__
4
- #define __RUCY_H__
3
+ #ifndef __RUCY_RUCY_H__
4
+ #define __RUCY_RUCY_H__
5
5
 
6
6
 
7
- #include <ruby.h>
8
7
  #include <rucy/class.h>
9
8
 
10
9
 
@@ -12,81 +11,23 @@ namespace Rucy
12
11
  {
13
12
 
14
13
 
15
- bool init ();
16
-
17
- bool check_class (Value obj, Value klass);
14
+ void init ();
18
15
 
19
16
 
20
17
  Module rucy_module ();
21
18
  // module Rucy
22
19
 
23
-
24
20
  Class native_error_class ();
25
21
  // class Rucy::NativeError < RuntimeError
26
22
 
27
- Class system_error_class ();
28
- // class Rucy::SystemError < Rucy::NativeError
23
+ Class invalid_state_error_class ();
24
+ // class Rucy::InvalidStateError < Rucy::NativeError
29
25
 
30
26
  Class invalid_object_error_class ();
31
27
  // class Rucy::InvalidObjectError < Rucy::NativeError
32
28
 
33
-
34
- template <typename T> inline void mark_type (void* p)
35
- {
36
- if (p) ((T*) p)->mark();
37
- }
38
-
39
- template <typename T> inline void delete_type (void* p)
40
- {
41
- delete (T*) p;
42
- }
43
-
44
- template <typename T> inline void release_type (void* p)
45
- {
46
- if (p) ((T*) p)->release();
47
- }
48
-
49
- template <typename T> inline Value new_type (
50
- Value klass, T* ptr,
51
- RUBY_DATA_FUNC mark = NULL,
52
- RUBY_DATA_FUNC free = delete_type<T>)
53
- {
54
- if (!ptr) return Qnil;
55
- return Data_Wrap_Struct(klass, mark, free, ptr);
56
- }
57
-
58
- template <typename T> inline Value new_type (
59
- Value klass,
60
- RUBY_DATA_FUNC mark = NULL,
61
- RUBY_DATA_FUNC free = delete_type<T>)
62
- {
63
- return new_type(klass, new T, mark, free);
64
- }
65
-
66
- template <typename T> inline Value new_ref (
67
- Value klass, T* ptr,
68
- RUBY_DATA_FUNC mark = NULL,
69
- RUBY_DATA_FUNC free = release_type<T>)
70
- {
71
- if (ptr) ptr->retain();
72
- return new_type(klass, ptr, mark, free);
73
- }
74
-
75
- template <typename T> inline Value new_ref (
76
- Value klass,
77
- RUBY_DATA_FUNC mark = NULL,
78
- RUBY_DATA_FUNC free = release_type<T>)
79
- {
80
- return new_ref(klass, new T, mark, free);
81
- }
82
-
83
- template <typename T> inline T* get_type_ptr (Value obj, Value klass = Qnil)
84
- {
85
- if (klass && !check_class(obj, klass)) return NULL;
86
- T* p = NULL;
87
- Data_Get_Struct(obj.value(), T, p);
88
- return p;
89
- }
29
+ Class system_error_class ();
30
+ // class Rucy::SystemError < Rucy::NativeError
90
31
 
91
32
 
92
33
  }// Rucy
@@ -4,14 +4,14 @@
4
4
  #define __RUCY_SYMBOL_H__
5
5
 
6
6
 
7
- #include <ruby.h>
7
+ #include <rucy/ruby.h>
8
8
 
9
9
 
10
- #define SYMBOL(name, str) static const Rucy::Symbol name (str)
10
+ #define RUCY_SYMBOL(name, str) static const Rucy::Symbol name (str)
11
11
 
12
- #define SYM(s) SYMBOL(s, #s)
13
- #define SYM_Q(s) SYMBOL(s, #s "?")
14
- #define SYM_B(s) SYMBOL(s, #s "!")
12
+ #define RUCY_SYM(s) RUCY_SYMBOL(s, #s)
13
+ #define RUCY_SYM_Q(s) RUCY_SYMBOL(s, #s "?")
14
+ #define RUCY_SYM_B(s) RUCY_SYMBOL(s, #s "!")
15
15
 
16
16
 
17
17
  namespace Rucy
@@ -28,15 +28,15 @@ namespace Rucy
28
28
 
29
29
  Symbol ();
30
30
 
31
- Symbol (ID id);
31
+ Symbol (const char* s);
32
32
 
33
- Symbol (const Value& value);
33
+ Symbol (const char* s, size_t len);
34
34
 
35
- Symbol (const char* str);
35
+ Symbol (RubySymbol symbol);
36
36
 
37
- ID id () const;
37
+ RubySymbol symbol () const;
38
38
 
39
- VALUE value () const;
39
+ Value value () const;
40
40
 
41
41
  const char* c_str () const;
42
42
 
@@ -50,7 +50,7 @@ namespace Rucy
50
50
 
51
51
  private:
52
52
 
53
- ID id_;
53
+ RubySymbol sym;
54
54
 
55
55
  };// Symbol
56
56
 
@@ -4,7 +4,7 @@
4
4
  #define __RUCY_VALUE_H__
5
5
 
6
6
 
7
- #include <ruby.h>
7
+ #include <rucy/ruby.h>
8
8
  #include <rucy/symbol.h>
9
9
 
10
10
 
@@ -21,61 +21,59 @@ namespace Rucy
21
21
 
22
22
  Value (bool b);
23
23
 
24
- Value (int i);
24
+ Value ( int n);
25
25
 
26
- Value (float f);
26
+ Value (unsigned int n);
27
27
 
28
- Value (double d);
28
+ Value (float n);
29
29
 
30
- Value (const char* s, bool tainted = false);
30
+ Value (double n);
31
31
 
32
- Value (const char* s, size_t len, bool tainted = false);
32
+ Value (const char* s);
33
33
 
34
- Value (size_t size, const Value* values);
34
+ Value (const char* s, size_t len);
35
35
 
36
- Value (VALUE v);
36
+ Value (size_t size, const Value* array);
37
37
 
38
- void mark () const;
39
-
40
- bool is_kind_of (Value klass) const;
38
+ Value (RubyValue v);
41
39
 
42
- VALUE value () const;
40
+ bool is_i () const;
43
41
 
44
- int type () const;
42
+ bool is_f () const;
45
43
 
46
- Value klass () const;
44
+ bool is_s () const;
47
45
 
48
- int size () const;
46
+ bool is_sym () const;
49
47
 
50
- int length () const;
48
+ bool is_array () const;
51
49
 
52
- Value to_i () const;
50
+ bool is_hash () const;
53
51
 
54
- Value to_f () const;
52
+ bool is_nil () const;
55
53
 
56
- Value to_s () const;
54
+ int as_i (bool convert = false) const;
57
55
 
58
- Value to_sym () const;
56
+ double as_f (bool convert = false) const;
59
57
 
60
- int as_i (bool convert = false) const;
58
+ const char* as_s (bool convert = false) const;
61
59
 
62
- double as_f (bool convert = false) const;
60
+ Symbol as_sym (bool convert = false) const;
63
61
 
64
- const char* as_s (bool convert = false) const;
62
+ Value* as_array (bool convert = false);
65
63
 
66
- Symbol as_sym (bool convert = false) const;
64
+ const Value* as_array (bool convert = false) const;
67
65
 
68
- bool is_i () const;
66
+ Value to_i () const;
69
67
 
70
- bool is_f () const;
68
+ Value to_f () const;
71
69
 
72
- bool is_s () const;
70
+ Value to_s () const;
73
71
 
74
- bool is_sym () const;
72
+ Value to_sym () const;
75
73
 
76
- bool is_a () const;
74
+ Value to_array () const;
77
75
 
78
- bool is_nil () const;
76
+ template <typename T> T as (bool convert = false) const;
79
77
 
80
78
  % ["call", "operator ()"].each do |op|
81
79
  Value <%= op %> (Symbol name, int argc, const Value* argv) const;
@@ -84,15 +82,13 @@ namespace Rucy
84
82
  % end
85
83
  % end
86
84
 
87
- Value inspect () const;
88
-
89
- const char* c_str () const;
85
+ void mark () const;
90
86
 
91
- Value& operator [] (int i);
87
+ RubyValue value () const;
92
88
 
93
- const Value& operator [] (int i) const;
89
+ RubyValueType type () const;
94
90
 
95
- operator VALUE () const;
91
+ operator RubyValue () const;
96
92
 
97
93
  operator bool () const;
98
94
 
@@ -100,8 +96,20 @@ namespace Rucy
100
96
 
101
97
  bool operator == (const Value& rhs) const;
102
98
 
99
+ bool operator == (RubyValue rhs) const;
100
+
103
101
  bool operator != (const Value& rhs) const;
104
102
 
103
+ bool operator != (RubyValue rhs) const;
104
+
105
+ // Object
106
+
107
+ Value klass () const;
108
+
109
+ bool is_kind_of (Value klass) const;
110
+
111
+ Value inspect () const;
112
+
105
113
  // Array
106
114
 
107
115
  Value push (Value obj);
@@ -112,9 +120,23 @@ namespace Rucy
112
120
 
113
121
  Value unshift (Value obj);
114
122
 
123
+ Value& operator [] (int i);
124
+
125
+ const Value& operator [] (int i) const;
126
+
127
+ // String
128
+
129
+ const char* c_str () const;
130
+
131
+ // Array / String
132
+
133
+ int length () const;
134
+
135
+ int size () const;
136
+
115
137
  protected:
116
138
 
117
- VALUE v;
139
+ RubyValue val;
118
140
 
119
141
  };// Value
120
142
 
@@ -130,21 +152,23 @@ namespace Rucy
130
152
 
131
153
  GlobalValue (bool b, bool gc = false);
132
154
 
133
- GlobalValue (int i, bool gc = false);
155
+ GlobalValue ( int n, bool gc = false);
156
+
157
+ GlobalValue (unsigned int n, bool gc = false);
134
158
 
135
- GlobalValue (float f, bool gc = false);
159
+ GlobalValue (float n, bool gc = false);
136
160
 
137
- GlobalValue (double d, bool gc = false);
161
+ GlobalValue (double n, bool gc = false);
138
162
 
139
- GlobalValue (const char* s, bool tainted = false, bool gc = false);
163
+ GlobalValue (const char* s, bool gc = false);
140
164
 
141
- GlobalValue (const char* s, size_t len, bool tainted = false, bool gc = false);
165
+ GlobalValue (const char* s, size_t len, bool gc = false);
142
166
 
143
- GlobalValue (size_t size, const Value* values, bool gc = false);
167
+ GlobalValue (size_t size, const Value* array, bool gc = false);
144
168
 
145
- GlobalValue (VALUE v, bool gc = false);
169
+ GlobalValue (RubyValue v, bool gc = false);
146
170
 
147
- GlobalValue (const Value& obj, bool gc = false);
171
+ GlobalValue (const Value& v, bool gc = false);
148
172
 
149
173
  GlobalValue (const GlobalValue& obj, bool gc = false);
150
174
 
@@ -152,174 +176,72 @@ namespace Rucy
152
176
 
153
177
  ~GlobalValue ();
154
178
 
155
- void gc (bool enable = true);
179
+ void gc (bool enable) const;
156
180
 
157
181
  private:
158
182
 
159
- short gc_disable_count;
183
+ mutable short gc_disable_count;
160
184
 
161
- bool gc_guarded;
185
+ mutable bool gc_guarded;
162
186
 
163
187
  void init (bool gc);
164
188
 
165
- void update_guard ();
189
+ void update_guard () const;
166
190
 
167
191
  };// GlobalValue
168
192
 
169
193
 
170
- Value value (bool b);
194
+ Value nil ();
171
195
 
172
- Value value (char c);
196
+ Value value (bool b);
173
197
 
174
- Value value (unsigned char c);
198
+ Value value ( char n);
175
199
 
176
- Value value (short s);
200
+ Value value (unsigned char n);
177
201
 
178
- Value value (unsigned short s);
202
+ Value value ( short n);
179
203
 
180
- Value value (int i);
204
+ Value value (unsigned short n);
181
205
 
182
- Value value (unsigned int i);
206
+ Value value ( int n);
183
207
 
184
- Value value (long l);
208
+ Value value (unsigned int n);
185
209
 
186
- Value value (unsigned long l);
210
+ Value value ( long n);
187
211
 
188
- Value value (long long ll);
212
+ Value value (unsigned long n);
189
213
 
190
- Value value (unsigned long long ll);
214
+ Value value ( long long n);
191
215
 
192
- Value value (float f);
216
+ Value value (unsigned long long n);
193
217
 
194
- Value value (double d);
218
+ Value value (float n);
195
219
 
196
- Value value (const char* s, bool tainted = false);
220
+ Value value (double n);
197
221
 
198
- Value value (const char* s, size_t len, bool tainted = false);
222
+ Value value (const char* s);
199
223
 
200
- Value value (size_t size, const Value* values);
224
+ Value value (const char* s, size_t len);
201
225
 
202
- Value value (Symbol sym);
226
+ Value value (size_t size, const Value* array);
203
227
 
204
228
  Value value (void* ptr);
229
+ % (1..10).each do |n|
205
230
 
206
- Value nil ();
207
-
208
-
209
- template <typename T> T value_to (Value obj, bool convert = true);
210
-
211
- template <> inline bool
212
- value_to<bool> (Value obj, bool convert)
213
- {
214
- return (bool) obj;
215
- }
216
-
217
- template <> inline int
218
- value_to<int> (Value obj, bool convert)
219
- {
220
- if (convert) obj = obj.to_i();
221
- return NUM2INT(obj.value());
222
- }
223
-
224
- template <> inline unsigned int
225
- value_to<unsigned int> (Value obj, bool convert)
226
- {
227
- if (convert) obj = obj.to_i();
228
- return NUM2UINT(obj.value());
229
- }
230
-
231
- template <> inline char
232
- value_to<char> (Value obj, bool convert)
233
- {
234
- return (char) value_to<int>(obj, convert);
235
- }
236
-
237
- template <> inline unsigned char
238
- value_to<unsigned char> (Value obj, bool convert)
239
- {
240
- return (unsigned char) value_to<int>(obj, convert);
241
- }
242
-
243
- template <> inline short
244
- value_to<short> (Value obj, bool convert)
245
- {
246
- return (short) value_to<int>(obj, convert);
247
- }
248
-
249
- template <> inline unsigned short
250
- value_to<unsigned short> (Value obj, bool convert)
251
- {
252
- return (unsigned short) value_to<int>(obj, convert);
253
- }
254
-
255
- template <> inline long
256
- value_to<long> (Value obj, bool convert)
257
- {
258
- if (convert) obj = obj.to_i();
259
- return NUM2LONG(obj.value());
260
- }
261
-
262
- template <> inline unsigned long
263
- value_to<unsigned long> (Value obj, bool convert)
264
- {
265
- if (convert) obj = obj.to_i();
266
- return NUM2ULONG(obj.value());
267
- }
268
-
269
- template <> inline long long
270
- value_to<long long> (Value obj, bool convert)
271
- {
272
- if (convert) obj = obj.to_i();
273
- return NUM2LL(obj.value());
274
- }
275
-
276
- template <> inline unsigned long long
277
- value_to<unsigned long long> (Value obj, bool convert)
278
- {
279
- if (convert) obj = obj.to_i();
280
- return NUM2ULL(obj.value());
281
- }
282
-
283
- template <> inline double
284
- value_to<double> (Value obj, bool convert)
285
- {
286
- if (convert) obj = obj.to_f();
287
- Check_Type(obj, T_FLOAT);
288
- return RFLOAT_VALUE(obj.value());
289
- }
231
+ Value array (<%= params(n, ', ') {|i| "Value v#{i}"} %>);
232
+ % end
290
233
 
291
- template <> inline float
292
- value_to<float> (Value obj, bool convert)
293
- {
294
- return (float) value_to<double>(obj, convert);
295
- }
296
234
 
297
- template <> inline char*
298
- value_to<char*> (Value obj, bool convert)
299
- {
300
- if (convert) obj = obj.to_s();
301
- VALUE s = obj.value();
302
- return StringValueCStr(s);
303
- }
235
+ template <typename T> T value_to (Value obj, bool convert = true);
304
236
 
305
- template <> inline const char*
306
- value_to<const char*> (Value obj, bool convert)
237
+ template <typename T> inline T to (Value obj, bool convert = true)
307
238
  {
308
- return value_to<char*>(obj, convert);
309
- }
310
-
311
- template <> inline Symbol
312
- value_to<Symbol> (Value obj, bool convert)
313
- {
314
- if (convert) obj = obj.to_sym();
315
- Check_Type(obj, T_SYMBOL);
316
- return SYM2ID(obj.value());
239
+ return value_to<T>(obj, convert);
317
240
  }
318
241
 
319
-
320
- template <typename T> inline T to (Value obj, bool convert = true)
242
+ template <typename T> inline T Value::as (bool convert) const
321
243
  {
322
- return value_to<T>(obj, convert);
244
+ return value_to<T>(*this, convert);
323
245
  }
324
246
 
325
247
 
data/include/rucy.h CHANGED
@@ -1,5 +1,10 @@
1
1
  // -*- c++ -*-
2
- #include <rucy/defs.h>
2
+ #pragma once
3
+ #ifndef __RUCY_H__
4
+ #define __RUCY_H__
5
+
6
+
7
+ #include <rucy/ruby.h>
3
8
  #include <rucy/rucy.h>
4
9
  #include <rucy/value.h>
5
10
  #include <rucy/exception.h>
@@ -8,3 +13,6 @@
8
13
  #include <rucy/class.h>
9
14
  #include <rucy/symbol.h>
10
15
  #include <rucy/extension.h>
16
+
17
+
18
+ #endif//EOH
data/lib/rucy/module.rb CHANGED
@@ -4,7 +4,11 @@
4
4
  module Rucy
5
5
 
6
6
 
7
- extend module ModuleInfo
7
+ module Module
8
+
9
+ def name ()
10
+ super.split('::')[-2]
11
+ end
8
12
 
9
13
  def version ()
10
14
  open(root_dir 'VERSION') {|f| f.readline.chomp}
@@ -14,12 +18,12 @@ module Rucy
14
18
  File.expand_path "../../../#{path}", __FILE__
15
19
  end
16
20
 
17
- def include_dirs ()
18
- %w[include].map {|dir| root_dir dir}
21
+ def include_dir ()
22
+ root_dir 'include'
19
23
  end
20
24
 
21
- def lib_dirs ()
22
- %w[lib].map {|dir| root_dir dir}
25
+ def lib_dir ()
26
+ root_dir 'lib'
23
27
  end
24
28
 
25
29
  def task_dir ()
@@ -37,9 +41,9 @@ module Rucy
37
41
  end
38
42
  end
39
43
 
40
- self
44
+ extend self
41
45
 
42
- end# ModuleInfo
46
+ end# Module
43
47
 
44
48
 
45
49
  end# Rucy
data/rucy.gemspec CHANGED
@@ -1,9 +1,8 @@
1
1
  # -*- mode: ruby; coding: utf-8 -*-
2
2
 
3
3
 
4
- $:.unshift File.expand_path('../lib', __FILE__) do |path|
5
- $:.unshift path if !$:.include?(path) && File.directory?(path)
6
- end
4
+ File.expand_path('../lib', __FILE__)
5
+ .tap {|s| $:.unshift s if !$:.include?(s) && File.directory?(s)}
7
6
 
8
7
  require 'rucy/module'
9
8
 
@@ -13,7 +12,7 @@ Gem::Specification.new do |s|
13
12
  patterns.map {|pat| Dir.glob(pat).to_a}.flatten
14
13
  end
15
14
 
16
- mod = Rucy
15
+ mod = Rucy::Module
17
16
  name = mod.name.downcase
18
17
  rdocs = glob.call *%w[README .doc/ext/**/*.cpp]
19
18
 
data/src/class.cpp CHANGED
@@ -2,26 +2,54 @@
2
2
  #include "rucy/class.h"
3
3
 
4
4
 
5
+ #include <xot/string.h>
6
+ #include "rucy/symbol.h"
7
+ #include "rucy/extension.h"
8
+
9
+
5
10
  namespace Rucy
6
11
  {
7
12
 
8
13
 
9
- Class::Class (VALUE v)
14
+ Class::Class (RubyValue v)
10
15
  : Super(v)
11
16
  {
12
17
  }
13
18
 
14
- Class
19
+ void
15
20
  Class::define_alloc_func (RubyFunction0 fun)
16
21
  {
17
- rb_define_alloc_func(value(), (VALUE(*)(VALUE)) fun);
18
- return *this;
22
+ rb_define_alloc_func(value(), (RubyValue(*)(RubyValue)) fun);
19
23
  }
20
24
 
21
- Class
25
+ void
22
26
  Class::define_alloc_func (const char*, RubyFunction0 fun)
23
27
  {
24
- return define_alloc_func(fun);
28
+ define_alloc_func(fun);
29
+ }
30
+
31
+ template <bool singleton>
32
+ static
33
+ RUCY_DEF1(method_added_or_removed, method_name)
34
+ {
35
+ RUCY_SYMBOL(klass, "class");
36
+ RUCY_SYM(name);
37
+ Xot::String code =
38
+ Xot::stringf(
39
+ "ObjectSpace.each_object(%s) {|o| o.clear_override_flags}",
40
+ (singleton ? self(klass) : self)(name).c_str());
41
+ eval(code);
42
+ }
43
+ RUCY_END
44
+
45
+ void
46
+ Class::define_clear_override_flags (RubyFunction0 fun)
47
+ {
48
+ define_method("clear_override_flags", fun);
49
+ define_method("singleton_method_added", method_added_or_removed<true>);
50
+ define_method("singleton_method_removed", method_added_or_removed<true>);
51
+ define_singleton_method("method_added", method_added_or_removed<false>);
52
+ define_singleton_method("method_removed", method_added_or_removed<false>);
25
53
  }
26
54
 
27
55