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/src/exception.cpp CHANGED
@@ -2,12 +2,13 @@
2
2
  #include "rucy/exception.h"
3
3
 
4
4
 
5
- #include <ruby.h>
6
- #include <rucy/rucy.h>
7
-
5
+ #include <typeinfo>
8
6
  #ifdef WIN32
9
7
  #include <windows.h>
10
8
  #endif
9
+ #include <xot/exception.h>
10
+ #include "rucy/ruby.h"
11
+ #include "rucy/rucy.h"
11
12
 
12
13
 
13
14
  namespace Rucy
@@ -19,17 +20,17 @@ namespace Rucy
19
20
  {
20
21
  }
21
22
 
22
- RubyException::RubyException (Value type, const char* format, ...)
23
- : Super(""), val(Qnil)
23
+ RubyException::RubyException (Value exception, const char* format, ...)
24
+ : Super(""), val(nil())
24
25
  {
25
26
  XOT_STRINGF(format, s);
26
- val = rb_exc_new2(type, s.c_str());
27
+ val = rb_exc_new2(exception, s.c_str());
27
28
  }
28
29
 
29
30
  const char*
30
31
  RubyException::what () const throw()
31
32
  {
32
- SYM(message);
33
+ RUCY_SYM(message);
33
34
  return value().call(message).c_str();
34
35
  }
35
36
 
@@ -40,7 +41,7 @@ namespace Rucy
40
41
  }
41
42
 
42
43
 
43
- RubyJumptag::RubyJumptag (int tag)
44
+ RubyJumpTag::RubyJumpTag (int tag)
44
45
  : tag(tag)
45
46
  {
46
47
  }
@@ -51,98 +52,112 @@ namespace Rucy
51
52
  {
52
53
  if (!format) throw;
53
54
 
54
- XOT_STRINGF(format, message);
55
- throw message;
55
+ XOT_STRINGF(format, s);
56
+ throw s.c_str();
56
57
  }
57
58
 
58
59
  void
59
- raise (VALUE exception, const char* format, ...)
60
+ raise (RubyValue exception, const char* format, ...)
60
61
  {
61
- XOT_STRINGF(format, message);
62
- throw RubyException(exception, message.c_str());
62
+ XOT_STRINGF(format, s);
63
+ throw RubyException(exception, s.c_str());
63
64
  }
64
65
 
65
66
 
66
67
  void
67
- type_error (const char* format, ...)
68
+ rucy_error (const char* file, int line, const char* format, ...)
68
69
  {
69
- XOT_STRINGF(format, message);
70
- raise(rb_eTypeError, message.c_str());
70
+ XOT_STRINGF(format, s);
71
+ raise(Xot::error_text(file, line, s));
71
72
  }
72
73
 
73
74
  void
74
- argument_error (const char* format, ...)
75
+ type_error (const char* file, int line, const char* format, ...)
75
76
  {
76
- XOT_STRINGF(format, message);
77
- raise(rb_eArgError, message.c_str());
77
+ XOT_STRINGF(format, s);
78
+ raise(rb_eTypeError, Xot::error_text(file, line, s));
79
+ }
80
+
81
+ void
82
+ argument_error (const char* file, int line, const char* format, ...)
83
+ {
84
+ XOT_STRINGF(format, s);
85
+ raise(rb_eArgError, Xot::error_text(file, line, s));
78
86
  }
79
87
 
80
88
  void
81
89
  arg_count_error (
90
+ const char* file, int line,
82
91
  const char* method, int nargs, int nargs_expected,
83
92
  int n1, int n2, int n3, int n4, int n5,
84
93
  int n6, int n7, int n8, int n9, int n10)
85
94
  {
86
- String message = Xot::stringf(
95
+ Xot::String s = Xot::stringf(
87
96
  "wrong number of arguments for %s: %d for %d",
88
97
  method, nargs, nargs_expected);
89
98
 
90
99
  int n[10] = {n1, n2, n3, n4, n5, n6, n7, n8, n9, n10};
91
100
  for (int i = 0; i < 5 && n[i] >= 0; ++i)
92
- message += Xot::stringf(" or %d", n[i]);
101
+ s += Xot::stringf(" or %d", n[i]);
93
102
 
94
- message += ".";
95
- argument_error(message.c_str());
103
+ s += ".";
104
+ argument_error(file, line, s);
105
+ }
106
+
107
+ void
108
+ invalid_state_error (const char* file, int line, const char* format, ...)
109
+ {
110
+ XOT_STRINGF(format, s);
111
+ raise(invalid_state_error_class(), Xot::error_text(file, line, s));
96
112
  }
97
113
 
98
114
  void
99
- index_error (const char* format, ...)
115
+ invalid_object_error (const char* file, int line, const char* format, ...)
100
116
  {
101
- XOT_STRINGF(format, message);
102
- raise(rb_eIndexError, message.c_str());
117
+ XOT_STRINGF(format, s);
118
+ raise(invalid_object_error_class(), Xot::error_text(file, line, s));
103
119
  }
104
120
 
105
121
  void
106
- not_implemented_error (const char* format, ...)
122
+ index_error (const char* file, int line, const char* format, ...)
107
123
  {
108
- XOT_STRINGF(format, message);
109
- raise(rb_eNotImpError, message.c_str());
124
+ XOT_STRINGF(format, s);
125
+ raise(rb_eIndexError, Xot::error_text(file, line, s));
110
126
  }
111
127
 
128
+ void
129
+ not_implemented_error (const char* file, int line, const char* format, ...)
130
+ {
131
+ XOT_STRINGF(format, s);
132
+ raise(rb_eNotImpError, Xot::error_text(file, line, s));
133
+ }
112
134
 
113
135
  void
114
- system_error (const char* format, ...)
136
+ system_error (const char* file, int line, const char* format, ...)
115
137
  {
116
- XOT_STRINGF(format, message);
138
+ XOT_STRINGF(format, s);
117
139
 
118
140
  #ifdef WIN32
119
- DWORD lasterror = GetLastError();
120
- if (lasterror != 0)
121
- {
122
- LPVOID msg = NULL;
123
- DWORD flags =
124
- FORMAT_MESSAGE_ALLOCATE_BUFFER |
125
- FORMAT_MESSAGE_FROM_SYSTEM |
126
- FORMAT_MESSAGE_IGNORE_INSERTS;
127
- if (FormatMessageA(
128
- flags, NULL, lasterror, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
129
- (LPSTR) &msg, 0, NULL))
141
+ DWORD lasterror = GetLastError();
142
+ if (lasterror != 0)
130
143
  {
131
- String s = (LPCSTR) msg;
132
- if (!s.empty()) message = s + ": " + message;
144
+ LPVOID msg = NULL;
145
+ DWORD flags =
146
+ FORMAT_MESSAGE_ALLOCATE_BUFFER |
147
+ FORMAT_MESSAGE_FROM_SYSTEM |
148
+ FORMAT_MESSAGE_IGNORE_INSERTS;
149
+ if (FormatMessageA(
150
+ flags, NULL, lasterror, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
151
+ (LPSTR) &msg, 0, NULL))
152
+ {
153
+ Xot::String m = (LPCSTR) msg;
154
+ if (!m.empty()) s += ": " + m;
155
+ }
156
+ LocalFree(msg);
133
157
  }
134
- LocalFree(msg);
135
- }
136
158
  #endif
137
159
 
138
- raise(system_error_class(), message.c_str());
139
- }
140
-
141
- void
142
- invalid_object_error (const char* format, ...)
143
- {
144
- XOT_STRINGF(format, message);
145
- raise(invalid_object_error_class(), message.c_str());
160
+ raise(system_error_class(), Xot::error_text(file, line, s));
146
161
  }
147
162
 
148
163
 
data/src/extension.cpp ADDED
@@ -0,0 +1,59 @@
1
+ // -*- c++ -*-
2
+ #include "rucy/extension.h"
3
+
4
+
5
+ #include <xot/string.h>
6
+
7
+
8
+ namespace Rucy
9
+ {
10
+
11
+
12
+ void
13
+ check_class (Value obj, Value klass)
14
+ {
15
+ if (!klass)
16
+ argument_error(__FILE__, __LINE__);
17
+
18
+ if (!obj.is_kind_of(klass))
19
+ {
20
+ RUCY_SYMBOL(clas, "class");
21
+ RUCY_SYM(name);
22
+ type_error(__FILE__, __LINE__,
23
+ Xot::stringf(
24
+ "object is instance of %s, but %s is expected.",
25
+ obj(clas)(name).c_str(), klass(name).c_str()));
26
+ }
27
+ }
28
+
29
+ void
30
+ check_arg_count (
31
+ const char* file, int line,
32
+ const char* method, int nargs, int nargs_expected_n0,
33
+ int n1, int n2, int n3, int n4, int n5,
34
+ int n6, int n7, int n8, int n9, int n10)
35
+ {
36
+ if (nargs < 0 || nargs_expected_n0 < 0)
37
+ argument_error(__FILE__, __LINE__);
38
+
39
+ if (
40
+ nargs != nargs_expected_n0 &&
41
+ (n1 < 0 || nargs != n1) &&
42
+ (n2 < 0 || nargs != n2) &&
43
+ (n3 < 0 || nargs != n3) &&
44
+ (n4 < 0 || nargs != n4) &&
45
+ (n5 < 0 || nargs != n5) &&
46
+ (n6 < 0 || nargs != n6) &&
47
+ (n7 < 0 || nargs != n7) &&
48
+ (n8 < 0 || nargs != n8) &&
49
+ (n9 < 0 || nargs != n9) &&
50
+ (n10 < 0 || nargs != n10))
51
+ {
52
+ arg_count_error(
53
+ file, line, method, nargs, nargs_expected_n0,
54
+ n1, n2, n3, n4, n5, n6, n7, n8, n9, n10);
55
+ }
56
+ }
57
+
58
+
59
+ }// Rucy
data/src/function.cpp.erb CHANGED
@@ -2,7 +2,7 @@
2
2
  #include "rucy/function.h"
3
3
 
4
4
 
5
- #include <rucy/exception.h>
5
+ #include "rucy/exception.h"
6
6
 
7
7
 
8
8
  #ifndef TAG_RAISE
@@ -14,57 +14,49 @@ namespace Rucy
14
14
  {
15
15
 
16
16
 
17
- void
18
- define_function (const char* name, RubyFunctionN fun)
19
- {
20
- rb_define_global_function(name, RUBY_METHOD_FUNC(fun), -1);
21
- }
22
- % NTIMES.each do |n|
23
- void
24
- define_function (const char* name, RubyFunction<%= n %> fun)
25
- {
26
- rb_define_global_function(name, RUBY_METHOD_FUNC(fun), <%= n %>);
27
- }
28
- % end
29
-
30
17
  Value
31
18
  call (Symbol name, int argc, const Value* argv)
32
19
  {
33
- return protect(rb_funcall2, Qnil, name.id(), argc, (const VALUE*) argv);
20
+ return protect(
21
+ rb_funcall2, nil().value(), name.symbol(), argc, (const RubyValue*) argv);
34
22
  }
35
23
  % NTIMES.each do |n|
24
+
36
25
  Value
37
26
  call (Symbol name<%= params(n) {|i| ", Value v#{i}"} %>)
38
27
  {
39
- const VALUE args[] = {<%= params(n, ", ") {|i| "v#{i}"} %>};
40
- return protect(rb_funcall2, Qnil, name.id(), <%= n %>, args);
28
+ const RubyValue args[] = {<%= params(n, ', ') {|i| "v#{i}.value()"} %>};
29
+ return protect(
30
+ rb_funcall2, nil().value(), name.symbol(), <%= n %>, args);
41
31
  }
42
32
  % end
43
33
 
34
+
44
35
  Value
45
- eval (const char* str)
36
+ eval (const char* format, ...)
46
37
  {
47
- SYM(eval);
48
- return call(eval, str);
38
+ RUCY_SYM(eval);
39
+ XOT_STRINGF(format, s);
40
+ return call(eval, s.c_str());
49
41
  }
50
42
 
51
43
 
52
44
  Value
53
- call_protect (VALUE (*fun)(VALUE), VALUE arg)
45
+ call_protect (RubyValue (*fun)(RubyValue), RubyValue data)
54
46
  {
55
47
  int state = 0;
56
- VALUE ret = rb_protect(fun, arg, &state);
48
+ RubyValue ret = rb_protect(fun, data, &state);
57
49
 
58
50
  if (state != 0)
59
51
  {
60
- VALUE exception = rb_errinfo();
52
+ RubyValue exception = rb_errinfo();
61
53
  if (state == TAG_RAISE && RTEST(exception))
62
54
  {
63
- rb_set_errinfo(Qnil);
55
+ rb_set_errinfo(nil().value());
64
56
  throw RubyException(exception);
65
57
  }
66
58
 
67
- throw RubyJumptag(state);
59
+ throw RubyJumpTag(state);
68
60
  }
69
61
 
70
62
  return ret;
data/src/module.cpp.erb CHANGED
@@ -2,14 +2,14 @@
2
2
  #include "rucy/module.h"
3
3
 
4
4
 
5
- #include <rucy/class.h>
5
+ #include "rucy/class.h"
6
6
 
7
7
 
8
8
  namespace Rucy
9
9
  {
10
10
 
11
11
 
12
- Module::Module (VALUE v)
12
+ Module::Module (RubyValue v)
13
13
  : Super(v)
14
14
  {
15
15
  }
@@ -23,42 +23,38 @@ namespace Rucy
23
23
  Class
24
24
  Module::define_class (const char* name, Value super)
25
25
  {
26
+ if (super.is_nil()) super = rb_cObject;
26
27
  return rb_define_class_under(value(), name, super);
27
28
  }
28
29
 
29
- Module
30
+ void
30
31
  Module::define_const (const char* name, Value val)
31
32
  {
32
33
  rb_define_const(value(), name, val);
33
- return *this;
34
34
  }
35
35
 
36
- Module
36
+ void
37
37
  Module::define_attr (const char* name, bool read, bool write)
38
38
  {
39
39
  rb_define_attr(value(), name, read, write);
40
- return *this;
41
40
  }
42
41
 
43
- Module
42
+ void
44
43
  Module::define_alias (const char* new_, const char* old)
45
44
  {
46
45
  rb_define_alias(value(), new_, old);
47
- return *this;
48
46
  }
49
47
 
50
- Module
48
+ void
51
49
  Module::undef_method (const char* name)
52
50
  {
53
51
  rb_undef_method(value(), name);
54
- return *this;
55
52
  }
56
53
 
57
- Module
54
+ void
58
55
  Module::include_module (Value module)
59
56
  {
60
57
  rb_include_module(value(), module);
61
- return *this;
62
58
  }
63
59
 
64
60
  % [
@@ -67,18 +63,16 @@ namespace Rucy
67
63
  % ['define_private_method', 'rb_define_private_method'],
68
64
  % ['define_singleton_method', 'rb_define_singleton_method']
69
65
  % ].each do |(op, rb_op)|
70
- Module
66
+ void
71
67
  Module::<%= op %> (const char* name, RubyFunctionN fun)
72
68
  {
73
69
  <%= rb_op %>(value(), name, RUBY_METHOD_FUNC(fun), -1);
74
- return *this;
75
70
  }
76
71
  % NTIMES.each do |n|
77
- Module
72
+ void
78
73
  Module::<%= op %> (const char* name, RubyFunction<%= n %> fun)
79
74
  {
80
75
  <%= rb_op %>(value(), name, RUBY_METHOD_FUNC(fun), <%= n %>);
81
- return *this;
82
76
  }
83
77
  % end
84
78
  % end
@@ -93,6 +87,7 @@ namespace Rucy
93
87
  Class
94
88
  define_class (const char* name, Value super)
95
89
  {
90
+ if (super.is_nil()) super = rb_cObject;
96
91
  return rb_define_class(name, super);
97
92
  }
98
93
 
@@ -102,5 +97,19 @@ namespace Rucy
102
97
  rb_define_global_const(name, val);
103
98
  }
104
99
 
100
+ void
101
+ define_function (const char* name, RubyFunctionN fun)
102
+ {
103
+ rb_define_global_function(name, RUBY_METHOD_FUNC(fun), -1);
104
+ }
105
+ % NTIMES.each do |n|
106
+
107
+ void
108
+ define_function (const char* name, RubyFunction<%= n %> fun)
109
+ {
110
+ rb_define_global_function(name, RUBY_METHOD_FUNC(fun), <%= n %>);
111
+ }
112
+ % end
113
+
105
114
 
106
115
  }// Rucy
data/src/rucy.cpp CHANGED
@@ -2,42 +2,25 @@
2
2
  #include "rucy/rucy.h"
3
3
 
4
4
 
5
- #include <rucy/exception.h>
5
+ #include "rucy/exception.h"
6
6
 
7
7
 
8
8
  namespace Rucy
9
9
  {
10
10
 
11
11
 
12
- bool
12
+ void
13
13
  init ()
14
14
  {
15
15
  static bool done = false;
16
- if (done) return true;
16
+ if (done) return;
17
17
  done = true;
18
18
 
19
19
  rucy_module();
20
20
  native_error_class();
21
- system_error_class();
21
+ invalid_state_error_class();
22
22
  invalid_object_error_class();
23
-
24
- return true;
25
- }
26
-
27
- bool
28
- check_class (Value obj, Value klass)
29
- {
30
- return klass && obj.is_kind_of(klass);
31
-
32
- #if 0
33
- SYMBOL(klass, "class");
34
- SYM(name);
35
- String message = stringf(
36
- "object is instance of %s, but %s is expected.",
37
- obj(klass)(name).c_str(), class_(name).c_str());
38
-
39
- type_error(message.c_str());
40
- #endif
23
+ system_error_class();
41
24
  }
42
25
 
43
26
 
@@ -48,7 +31,6 @@ namespace Rucy
48
31
  return m;
49
32
  }
50
33
 
51
-
52
34
  Class
53
35
  native_error_class ()
54
36
  {
@@ -58,10 +40,10 @@ namespace Rucy
58
40
  }
59
41
 
60
42
  Class
61
- system_error_class ()
43
+ invalid_state_error_class ()
62
44
  {
63
45
  static Class c =
64
- rucy_module().define_class("SystemError", native_error_class());
46
+ rucy_module().define_class("InvalidStateError", native_error_class());
65
47
  return c;
66
48
  }
67
49
 
@@ -73,5 +55,13 @@ namespace Rucy
73
55
  return c;
74
56
  }
75
57
 
58
+ Class
59
+ system_error_class ()
60
+ {
61
+ static Class c =
62
+ rucy_module().define_class("SystemError", native_error_class());
63
+ return c;
64
+ }
65
+
76
66
 
77
67
  }// Rucy
data/src/symbol.cpp CHANGED
@@ -2,8 +2,8 @@
2
2
  #include "rucy/symbol.h"
3
3
 
4
4
 
5
- #include <rucy/rucy.h>
6
- #include <rucy/value.h>
5
+ #include "rucy/rucy.h"
6
+ #include "rucy/value.h"
7
7
 
8
8
 
9
9
  namespace Rucy
@@ -11,46 +11,47 @@ namespace Rucy
11
11
 
12
12
 
13
13
  Symbol::Symbol ()
14
- : id_(0)
14
+ : sym(0)
15
15
  {
16
16
  }
17
17
 
18
- Symbol::Symbol (ID id)
19
- : id_(id)
18
+ Symbol::Symbol (const char* s)
19
+ : sym(rb_intern(s))
20
20
  {
21
21
  }
22
22
 
23
- Symbol::Symbol (const Value& value)
24
- : id_(value.as_sym().id())
23
+ Symbol::Symbol (const char* s, size_t len)
24
+ : sym(rb_intern2(s, len))
25
25
  {
26
26
  }
27
27
 
28
- Symbol::Symbol (const char* str)
29
- : id_(rb_intern(str))
28
+ Symbol::Symbol (RubySymbol symbol)
29
+ : sym(symbol)
30
30
  {
31
31
  }
32
32
 
33
- ID
34
- Symbol::id () const
33
+ RubySymbol
34
+ Symbol::symbol () const
35
35
  {
36
- return id_;
36
+ return sym;
37
37
  }
38
38
 
39
- VALUE
39
+ Value
40
40
  Symbol::value () const
41
41
  {
42
- return id_ != 0 ? ID2SYM(id_) : Qnil;
42
+ if (sym == 0) return nil();
43
+ return ID2SYM(sym);
43
44
  }
44
45
 
45
46
  const char*
46
47
  Symbol::c_str () const
47
48
  {
48
- return rb_id2name(id_);
49
+ return rb_id2name(sym);
49
50
  }
50
51
 
51
52
  Symbol::operator bool () const
52
53
  {
53
- return id_ != 0;
54
+ return sym != 0;
54
55
  }
55
56
 
56
57
  bool
@@ -62,7 +63,7 @@ namespace Rucy
62
63
  bool
63
64
  operator == (const Symbol& lhs, const Symbol& rhs)
64
65
  {
65
- return lhs.id_ == rhs.id_;
66
+ return lhs.sym == rhs.sym;
66
67
  }
67
68
 
68
69
  bool