real_include 0.2.1 → 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG CHANGED
@@ -1,5 +1,9 @@
1
- 20/10/2010 versio 0.2.1
1
+ 22/10/2010 version 0.2.2
2
+ * refactored compat.h, incorporated test for 1.9
3
+ * created inline static function create_class() in compat.h
4
+ 20/10/2010 version 0.2.1
2
5
  * constant lookup should now work
6
+ * added real_extend and tests
3
7
  15/10/2010 version 0.1.7
4
8
  * should be properly working with normal Module#include now
5
9
  * no more segfaults?
data/Rakefile CHANGED
@@ -28,7 +28,7 @@ specification = Gem::Specification.new do |s|
28
28
  s.extensions = ["ext/real_include/extconf.rb"]
29
29
  s.files = ["Rakefile", "README.markdown", "CHANGELOG",
30
30
  "lib/real_include.rb", "lib/real_include/version.rb"] +
31
- # ["lib/1.9/real_include.so", "lib/1.8/real_include.so"] +
31
+ # ["lib/1.9/real_include.so", "lib/1.8/real_include.so"] +
32
32
  FileList["ext/**/extconf.rb", "ext/**/*.h", "ext/**/*.c", "test/**/*.rb"].to_a
33
33
  end
34
34
 
@@ -5,6 +5,11 @@
5
5
 
6
6
  #include <ruby.h>
7
7
 
8
+ /* test for 1.9 */
9
+ #if !defined(RUBY_19) && defined(ROBJECT_EMBED_LEN_MAX)
10
+ # define RUBY_19
11
+ #endif
12
+
8
13
  /* macros for backwards compatibility with 1.8 */
9
14
  #ifndef RUBY_19
10
15
  # define RCLASS_M_TBL(c) (RCLASS(c)->m_tbl)
@@ -14,6 +19,35 @@
14
19
  # include "st.h"
15
20
  #endif
16
21
 
22
+ #ifdef RUBY_19
23
+ inline static VALUE
24
+ class_alloc(VALUE flags, VALUE klass)
25
+ {
26
+ rb_classext_t *ext = ALLOC(rb_classext_t);
27
+ NEWOBJ(obj, struct RClass);
28
+ OBJSETUP(obj, klass, flags);
29
+ obj->ptr = ext;
30
+ RCLASS_IV_TBL(obj) = 0;
31
+ RCLASS_M_TBL(obj) = 0;
32
+ RCLASS_SUPER(obj) = 0;
33
+ RCLASS_IV_INDEX_TBL(obj) = 0;
34
+ return (VALUE)obj;
35
+ }
36
+ #endif
37
+
38
+ inline static VALUE
39
+ create_class(VALUE flags, VALUE klass)
40
+ {
41
+ #ifdef RUBY_19
42
+ VALUE new_klass = class_alloc(flags, klass);
43
+ #else
44
+ NEWOBJ(new_klass, struct RClass);
45
+ OBJSETUP(new_klass, klass, flags);
46
+ #endif
47
+
48
+ return (VALUE)new_klass;
49
+ }
50
+
17
51
  # define FALSE 0
18
52
  # define TRUE 1
19
53
 
@@ -1,8 +1,5 @@
1
1
  require 'mkmf'
2
2
 
3
- # 1.9 compatibility
4
- $CFLAGS += " -DRUBY_19" if RUBY_VERSION =~ /1.9/
5
-
6
3
  # let's use c99
7
4
  $CFLAGS += " -std=c99"
8
5
 
@@ -4,32 +4,11 @@
4
4
  #include <ruby.h>
5
5
  #include "compat.h"
6
6
 
7
- #ifdef RUBY_19
8
- static VALUE
9
- class_alloc(VALUE flags, VALUE klass)
10
- {
11
- rb_classext_t *ext = ALLOC(rb_classext_t);
12
- NEWOBJ(obj, struct RClass);
13
- OBJSETUP(obj, klass, flags);
14
- obj->ptr = ext;
15
- RCLASS_IV_TBL(obj) = 0;
16
- RCLASS_M_TBL(obj) = 0;
17
- RCLASS_SUPER(obj) = 0;
18
- RCLASS_IV_INDEX_TBL(obj) = 0;
19
- return (VALUE)obj;
20
- }
21
- #endif
22
-
23
7
  /* patched to work well with real_include */
24
8
  static VALUE
25
9
  include_class_new(VALUE module, VALUE super)
26
10
  {
27
- #ifdef RUBY_19
28
- VALUE klass = class_alloc(T_ICLASS, rb_cClass);
29
- #else
30
- NEWOBJ(klass, struct RClass);
31
- OBJSETUP(klass, rb_singleton_class(rb_cModule), T_ICLASS);
32
- #endif
11
+ VALUE klass = create_class(T_ICLASS, rb_cClass);
33
12
 
34
13
  if (BUILTIN_TYPE(module) == T_ICLASS) {
35
14
 
@@ -6,23 +6,6 @@
6
6
  #include <ruby.h>
7
7
  #include "compat.h"
8
8
 
9
- #ifdef RUBY_19
10
- static VALUE
11
- class_alloc(VALUE flags, VALUE klass)
12
- {
13
- rb_classext_t *ext = ALLOC(rb_classext_t);
14
- NEWOBJ(obj, struct RClass);
15
- OBJSETUP(obj, klass, flags);
16
- obj->ptr = ext;
17
- RCLASS_IV_TBL(obj) = 0;
18
- RCLASS_M_TBL(obj) = 0;
19
- RCLASS_SUPER(obj) = 0;
20
- RCLASS_IV_INDEX_TBL(obj) = 0;
21
- return (VALUE)obj;
22
- }
23
- #endif
24
-
25
-
26
9
  static VALUE
27
10
  class_to_s(VALUE self)
28
11
  {
@@ -54,12 +37,7 @@ include_class_new(VALUE module, VALUE super)
54
37
  }
55
38
 
56
39
  /* allocate iclass */
57
- #ifdef RUBY_19
58
- VALUE klass = class_alloc(T_ICLASS, rb_singleton_class(rb_cModule));
59
- #else
60
- NEWOBJ(klass, struct RClass);
61
- OBJSETUP(klass, rb_singleton_class(rb_cModule), T_ICLASS);
62
- #endif
40
+ VALUE klass = create_class(T_ICLASS, rb_singleton_class(rb_cModule));
63
41
 
64
42
  /* if the module hasn't yet got an ivtbl, create it */
65
43
  if (!RCLASS_IV_TBL(module))
@@ -55,7 +55,6 @@ class Object
55
55
  # end
56
56
  # o = Object.new
57
57
  # o.real_extend M
58
- # end
59
58
  # o.singleton_class.hello #=> "hello"
60
59
  def real_extend(*mods)
61
60
  class << self; self; end.send(:real_include, *mods)
@@ -1,3 +1,3 @@
1
1
  module RealInclude
2
- VERSION = "0.2.1"
2
+ VERSION = "0.2.2"
3
3
  end
@@ -35,6 +35,34 @@ describe 'Including a module into a class using real_include' do
35
35
  end
36
36
  end
37
37
 
38
+ describe 'Extending a module into a class using real_extend' do
39
+ before do
40
+ @m = Module.new {
41
+ def self.class_method
42
+ :class_method
43
+ end
44
+
45
+ def instance_method
46
+ :instance_method
47
+ end
48
+ }
49
+
50
+ @m::CONST = :const
51
+
52
+ @c = Class.new
53
+
54
+ @c.send(:real_extend, @m)
55
+ end
56
+
57
+ it 'should make instance methods from the module available as class methods on the class' do
58
+ @c.instance_method.should.equal :instance_method
59
+ end
60
+
61
+ it 'should make class methods from the module available as class methods on the singleton class' do
62
+ class << @c; self; end.class_method.should.equal :class_method
63
+ end
64
+ end
65
+
38
66
 
39
67
  describe 'Including a module into a module and then into a class using real_include' do
40
68
  before do
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 2
8
- - 1
9
- version: 0.2.1
8
+ - 2
9
+ version: 0.2.2
10
10
  platform: ruby
11
11
  authors:
12
12
  - John Mair (banisterfiend)
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-10-21 00:00:00 +13:00
17
+ date: 2010-10-22 00:00:00 +13:00
18
18
  default_executable:
19
19
  dependencies: []
20
20