rucy 0.1.12 → 0.1.18

Sign up to get free protection for your applications and to get access to all the features.
@@ -41,7 +41,7 @@ namespace Rucy
41
41
  void extend_module (Value module);
42
42
 
43
43
  % [
44
- % 'define_function',
44
+ % 'define_module_function',
45
45
  % 'define_method',
46
46
  % 'define_private_method',
47
47
  % 'define_singleton_method',
@@ -5,7 +5,6 @@
5
5
 
6
6
 
7
7
  #include <ruby.h>
8
- #include <ruby/intern.h>
9
8
 
10
9
 
11
10
  namespace Rucy
@@ -41,6 +41,8 @@ namespace Rucy
41
41
 
42
42
  bool is_f () const;
43
43
 
44
+ bool is_num () const;
45
+
44
46
  bool is_s () const;
45
47
 
46
48
  bool is_sym () const;
@@ -120,9 +122,9 @@ namespace Rucy
120
122
 
121
123
  Value unshift (Value obj);
122
124
 
123
- Value& operator [] (int i);
125
+ Value& operator [] (size_t i);
124
126
 
125
- const Value& operator [] (int i) const;
127
+ const Value& operator [] (size_t i) const;
126
128
 
127
129
  // String
128
130
 
@@ -134,6 +136,8 @@ namespace Rucy
134
136
 
135
137
  int size () const;
136
138
 
139
+ bool empty () const;
140
+
137
141
  protected:
138
142
 
139
143
  RubyValue val;
@@ -144,7 +148,9 @@ namespace Rucy
144
148
  class GlobalValue : public Value
145
149
  {
146
150
 
147
- typedef Value Super;
151
+ typedef Value Super;
152
+
153
+ typedef GlobalValue This;
148
154
 
149
155
  public:
150
156
 
@@ -170,24 +176,24 @@ namespace Rucy
170
176
 
171
177
  GlobalValue (const Value& v, bool gc = false);
172
178
 
173
- GlobalValue (const GlobalValue& obj, bool gc = false);
179
+ GlobalValue (const This& obj, bool gc = false);
174
180
 
175
181
  GlobalValue& operator = (const Value& v);
176
182
 
183
+ GlobalValue& operator = (const This& obj);
184
+
177
185
  ~GlobalValue ();
178
186
 
179
- void gc (bool enable) const;
187
+ void enable_gc () const;
180
188
 
181
- private:
189
+ void disable_gc () const;
182
190
 
183
- mutable short gc_disable_count;
191
+ private:
184
192
 
185
- mutable bool gc_guarded;
193
+ mutable int gc_disable_count;
186
194
 
187
195
  void init (bool gc);
188
196
 
189
- void update_guard () const;
190
-
191
197
  };// GlobalValue
192
198
 
193
199
 
@@ -224,21 +230,30 @@ namespace Rucy
224
230
  Value value (const char* s, size_t len);
225
231
 
226
232
  Value value (size_t size, const Value* array);
227
-
228
- Value value (void* ptr);
229
- % (1..10).each do |n|
233
+ % (1..16).each do |n|
230
234
 
231
235
  Value array (<%= params(n, ', ') {|i| "Value v#{i}"} %>);
232
236
  % end
233
237
 
234
238
 
235
- template <typename T> T value_to (Value obj, bool convert = true);
239
+ template <typename T> T
240
+ value_to (Value obj, bool convert = true);
236
241
 
237
- template <typename T> inline T to (Value obj, bool convert = true)
242
+ template <typename T> inline T
243
+ to (Value obj, bool convert = true)
238
244
  {
239
245
  return value_to<T>(obj, convert);
240
246
  }
241
247
 
248
+ template <typename T> T
249
+ value_to (int argc, const Value* argv, bool convert = true);
250
+
251
+ template <typename T> inline T
252
+ to (int argc, const Value* argv, bool convert = true)
253
+ {
254
+ return value_to<T>(argc, argv, convert);
255
+ }
256
+
242
257
  template <typename T> inline T Value::as (bool convert) const
243
258
  {
244
259
  return value_to<T>(*this, convert);
@@ -6,6 +6,8 @@ module Rucy
6
6
 
7
7
  module Module
8
8
 
9
+ module_function
10
+
9
11
  def name ()
10
12
  super.split('::')[-2]
11
13
  end
@@ -15,10 +17,10 @@ module Rucy
15
17
  end
16
18
 
17
19
  def root_dir (path = '')
18
- File.expand_path "../../../#{path}", __FILE__
20
+ File.expand_path "../../#{path}", __dir__
19
21
  end
20
22
 
21
- def include_dir ()
23
+ def inc_dir ()
22
24
  root_dir 'include'
23
25
  end
24
26
 
@@ -26,23 +28,6 @@ module Rucy
26
28
  root_dir 'lib'
27
29
  end
28
30
 
29
- def task_dir ()
30
- root_dir 'task'
31
- end
32
-
33
- def load_tasks (*names)
34
- if names.empty?
35
- Dir["#{task_dir}/**/*.rake"].each {|path| load path}
36
- else
37
- names.each do |name|
38
- path = "#{task_dir}/#{name}.rake"
39
- load path if File.exist? path
40
- end
41
- end
42
- end
43
-
44
- extend self
45
-
46
31
  end# Module
47
32
 
48
33
 
@@ -0,0 +1,62 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+
4
+ require 'xot/rake'
5
+
6
+
7
+ module Rucy
8
+
9
+
10
+ module Rake
11
+
12
+
13
+ def rdoc ()
14
+ env :RDOC, 'rdoc'# 'yardoc'
15
+ end
16
+
17
+ def rucy2rdoc ()
18
+ env :RUCY2RDOC, 'rucy2rdoc'
19
+ end
20
+
21
+ def generate_documents ()
22
+ rdocdir = ".doc/#{ext_dir}"
23
+ srcs = FileList["#{ext_dir}/**/*.{#{src_exts.join ','}}"]
24
+ rdocs = Hash[srcs.map{|path| [path, "#{rdocdir}/#{File.basename path}"]}]
25
+ out = "#{doc_dir}/index.html"
26
+
27
+ alias_task :doc => out
28
+ alias_task :clean => 'doc:clean'
29
+
30
+ namespace :doc do
31
+ desc "build documents"
32
+ file out => rdocs.values do
33
+ sh %( #{rdoc} #{rdocs.values.join ' '} )
34
+ end
35
+
36
+ rdocs.each do |(src, rdoc)|
37
+ file rdoc => [src, rdocdir] do
38
+ sh %( #{rucy2rdoc} #{src} > #{rdoc} )
39
+ end
40
+ end
41
+
42
+ directory rdocdir
43
+
44
+ task :clean do
45
+ sh %( rm -rf #{rdocdir} #{rdocs.values.join ' '} )
46
+ end
47
+ end
48
+ end
49
+
50
+
51
+ end# Rake
52
+
53
+
54
+ end# Rucy
55
+
56
+
57
+ include Rucy::Rake
58
+
59
+
60
+ File.expand_path('../../bin', __dir__).tap do |path|
61
+ ENV['PATH'] += ":#{path}" unless ENV['PATH'].split(':').include?(path)
62
+ end
@@ -21,16 +21,14 @@ Gem::Specification.new do |s|
21
21
  s.description = 'This library helps you to develop Ruby Extension by C++.'
22
22
  s.version = mod.version
23
23
 
24
- s.authors = %w[snori]
25
- s.email = 'snori@xord.org'
24
+ s.authors = %w[xordog]
25
+ s.email = 'xordog@gmail.com'
26
26
  s.homepage = "https://github.com/xord/rucy"
27
27
 
28
28
  s.platform = Gem::Platform::RUBY
29
- s.required_ruby_version = '>=1.9.0'
29
+ s.required_ruby_version = '~> 2'
30
30
 
31
- s.add_runtime_dependency 'rake'
32
- s.add_runtime_dependency 'xot'
33
- s.add_development_dependency 'gemcutter'
31
+ s.add_runtime_dependency 'xot', '~> 0.1.16'
34
32
 
35
33
  s.files = `git ls-files`.split $/
36
34
  s.executables = s.files.grep(%r{^bin/}) {|f| File.basename f}
@@ -58,7 +58,7 @@ namespace Rucy
58
58
  }
59
59
 
60
60
  % [
61
- % ['define_function', 'rb_define_module_function'],
61
+ % ['define_module_function', 'rb_define_module_function'],
62
62
  % ['define_method', 'rb_define_method'],
63
63
  % ['define_private_method', 'rb_define_private_method'],
64
64
  % ['define_singleton_method', 'rb_define_singleton_method']
@@ -3,8 +3,10 @@
3
3
 
4
4
 
5
5
  #include <assert.h>
6
+ #include <ruby/encoding.h>
6
7
  #include "rucy/function.h"
7
8
  #include "rucy/exception.h"
9
+ #include "rucy/debug.h"
8
10
 
9
11
 
10
12
  namespace Rucy
@@ -54,15 +56,29 @@ namespace Rucy
54
56
  {
55
57
  }
56
58
 
59
+ static VALUE
60
+ str_new (const char* s)
61
+ {
62
+ rb_encoding* e = rb_default_internal_encoding();
63
+ return e ? rb_enc_str_new_cstr(s, e) : rb_str_new_cstr(s);
64
+ }
65
+
66
+ static VALUE
67
+ str_new (const char* s, size_t len)
68
+ {
69
+ rb_encoding* e = rb_default_internal_encoding();
70
+ return e ? rb_enc_str_new(s, len, e) : rb_str_new(s, len);
71
+ }
72
+
57
73
  Value::Value (const char* s)
58
- : val(s ? rb_str_new2(s) : Qnil)
74
+ : val(s ? str_new(s) : Qnil)
59
75
  {
60
76
  if (!s)
61
77
  argument_error(__FILE__, __LINE__);
62
78
  }
63
79
 
64
80
  Value::Value (const char* s, size_t len)
65
- : val(s ? rb_str_new(s, len) : Qnil)
81
+ : val(s ? str_new(s, len) : Qnil)
66
82
  {
67
83
  if (!s)
68
84
  argument_error(__FILE__, __LINE__);
@@ -101,6 +117,12 @@ namespace Rucy
101
117
  return RB_FLOAT_TYPE_P(val) || is_kind_of(rb_cFloat);
102
118
  }
103
119
 
120
+ bool
121
+ Value::is_num () const
122
+ {
123
+ return is_i() || is_f();
124
+ }
125
+
104
126
  bool
105
127
  Value::is_s () const
106
128
  {
@@ -325,13 +347,13 @@ namespace Rucy
325
347
  }
326
348
 
327
349
  Value&
328
- Value::operator [] (int i)
350
+ Value::operator [] (size_t i)
329
351
  {
330
352
  return as_array()[i];
331
353
  }
332
354
 
333
355
  const Value&
334
- Value::operator [] (int i) const
356
+ Value::operator [] (size_t i) const
335
357
  {
336
358
  return const_cast<Value*>(this)->operator[](i);
337
359
  }
@@ -339,7 +361,7 @@ namespace Rucy
339
361
  const char*
340
362
  Value::c_str () const
341
363
  {
342
- return as<const char*>(true);
364
+ return as_s(true);
343
365
  }
344
366
 
345
367
  static int
@@ -367,6 +389,12 @@ namespace Rucy
367
389
  return get_length(*this, size);
368
390
  }
369
391
 
392
+ bool
393
+ Value::empty () const
394
+ {
395
+ return size() <= 0;
396
+ }
397
+
370
398
 
371
399
  GlobalValue::GlobalValue ()
372
400
  {
@@ -433,7 +461,7 @@ namespace Rucy
433
461
  init(gc_);
434
462
  }
435
463
 
436
- GlobalValue::GlobalValue (const GlobalValue& obj, bool gc_)
464
+ GlobalValue::GlobalValue (const This& obj, bool gc_)
437
465
  : Super(obj)
438
466
  {
439
467
  init(gc_);
@@ -443,50 +471,41 @@ namespace Rucy
443
471
  GlobalValue::operator = (const Value& v)
444
472
  {
445
473
  Super::operator=(v);
446
- update_guard();
474
+ return *this;
475
+ }
476
+
477
+ GlobalValue&
478
+ GlobalValue::operator = (const This& obj)
479
+ {
480
+ Super::operator=(obj);
447
481
  return *this;
448
482
  }
449
483
 
450
484
  GlobalValue::~GlobalValue ()
451
485
  {
452
- while (gc_guarded) gc(true);
486
+ if (gc_disable_count > 0)
487
+ rb_gc_unregister_address(&val);
453
488
  }
454
489
 
455
490
  void
456
- GlobalValue::gc (bool enable) const
491
+ GlobalValue::enable_gc () const
457
492
  {
458
- gc_disable_count += enable ? -1 : +1;
459
- update_guard();
493
+ if (--gc_disable_count == 0)
494
+ rb_gc_unregister_address(const_cast<RubyValue*>(&val));
460
495
  }
461
496
 
462
497
  void
463
- GlobalValue::init (bool gc_)
498
+ GlobalValue::disable_gc () const
464
499
  {
465
- gc_disable_count = 0;
466
- gc_guarded = false;
467
- if (!gc_) gc(false);
500
+ if (++gc_disable_count == 1)
501
+ rb_gc_register_address(const_cast<RubyValue*>(&val));
468
502
  }
469
503
 
470
504
  void
471
- GlobalValue::update_guard () const
505
+ GlobalValue::init (bool gc_)
472
506
  {
473
- assert(gc_disable_count >= 0);
474
-
475
- if (IMMEDIATE_P(val))
476
- {
477
- if (gc_guarded) rb_gc_unregister_address((RubyValue*) &val);
478
- gc_guarded = false;
479
- }
480
- else if (gc_disable_count > 0)
481
- {
482
- if (!gc_guarded) rb_gc_register_address((RubyValue*) &val);
483
- gc_guarded = true;
484
- }
485
- else
486
- {
487
- if (gc_guarded) rb_gc_unregister_address((RubyValue*) &val);
488
- gc_guarded = false;
489
- }
507
+ gc_disable_count = 0;
508
+ if (!gc_) disable_gc();
490
509
  }
491
510
 
492
511
 
@@ -591,16 +610,7 @@ namespace Rucy
591
610
  {
592
611
  return Value(size, array);
593
612
  }
594
-
595
- Value
596
- value (void* ptr)
597
- {
598
- if (ptr)
599
- argument_error(__FILE__, __LINE__, "Rucy::value(void*) can take only (void*) NULL.");
600
-
601
- return nil();
602
- }
603
- % (1..10).each do |n|
613
+ % (1..16).each do |n|
604
614
 
605
615
  Value
606
616
  array (<%= params(n, ', ') {|i| "Value v#{i}"} %>)