real_include 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG CHANGED
@@ -1,3 +1,6 @@
1
+ 6/10/210 version 0.1.3
2
+ * fixed native gem
3
+ * moved more version-dependant code to compat.h
1
4
  5/10/2010 version 0.1.2
2
5
  * added 1.8 support
3
6
  * added bacon tests
@@ -1,4 +1,4 @@
1
- Real Include
1
+ Real Include
2
2
  --------------
3
3
 
4
4
  (c) John Mair (banisterfiend)
@@ -8,6 +8,12 @@ Removes the shackles from Module#include - use Module#real_include to
8
8
  bring in singleton classes from modules. No more ugly ClassMethods and
9
9
  included() hook hacks.
10
10
 
11
+ ** This is BETA software and has not yet been thoroughly tested, use
12
+ at own risk **
13
+
14
+ install the gem: **for testing purposes only**
15
+ `gem install real_include`
16
+
11
17
  example:
12
18
 
13
19
  module M
data/Rakefile CHANGED
@@ -27,7 +27,7 @@ specification = Gem::Specification.new do |s|
27
27
 
28
28
  s.extensions = ["ext/real_include/extconf.rb"]
29
29
  s.files = ["Rakefile", "README.markdown", "CHANGELOG",
30
- "lib/real_include.rb", "lib/real_include/version.rb"]
30
+ "lib/real_include.rb", "lib/real_include/version.rb"] +
31
31
  FileList["ext/**/extconf.rb", "ext/**/*.h", "ext/**/*.c"].to_a
32
32
  end
33
33
 
@@ -0,0 +1,22 @@
1
+ /* contains basic macros to facilitate ruby 1.8 and ruby 1.9 compatibility */
2
+
3
+ #ifndef GUARD_COMPAT_H
4
+ #define GUARD_COMPAT_H
5
+
6
+ #include <ruby.h>
7
+
8
+ /* macros for backwards compatibility with 1.8 */
9
+ #ifndef RUBY_19
10
+ # define RCLASS_M_TBL(c) (RCLASS(c)->m_tbl)
11
+ # define RCLASS_SUPER(c) (RCLASS(c)->super)
12
+ # define RCLASS_IV_TBL(c) (RCLASS(c)->iv_tbl)
13
+ # define OBJ_UNTRUSTED OBJ_TAINTED
14
+ # define FALSE 0
15
+ # define TRUE 1
16
+ # include "st.h"
17
+ #endif
18
+
19
+ /* a useful macro. cannot use ordinary CLASS_OF as it does not return an lvalue */
20
+ #define KLASS_OF(c) (RBASIC(c)->klass)
21
+
22
+ #endif
@@ -0,0 +1,132 @@
1
+ /* (c) 2010 John Mair (banisterfiend), MIT license */
2
+
3
+ #include "compat.h"
4
+ #include "ruby.h"
5
+
6
+ #ifdef RUBY_19
7
+ static VALUE
8
+ class_alloc(VALUE flags, VALUE klass)
9
+ {
10
+ rb_classext_t *ext = ALLOC(rb_classext_t);
11
+ NEWOBJ(obj, struct RClass);
12
+ OBJSETUP(obj, klass, flags);
13
+ obj->ptr = ext;
14
+ RCLASS_IV_TBL(obj) = 0;
15
+ RCLASS_M_TBL(obj) = 0;
16
+ RCLASS_SUPER(obj) = 0;
17
+ RCLASS_IV_INDEX_TBL(obj) = 0;
18
+ return (VALUE)obj;
19
+ }
20
+ #endif
21
+
22
+ static VALUE
23
+ include_class_new(VALUE module, VALUE super)
24
+ {
25
+ /* base case for recursion */
26
+ if (module == rb_singleton_class(rb_cModule))
27
+ return module;
28
+
29
+ /* allocate iclass */
30
+ #ifdef RUBY_19
31
+ VALUE klass = class_alloc(T_ICLASS, rb_singleton_class(rb_cModule));
32
+ #else
33
+ NEWOBJ(klass, struct RClass);
34
+ OBJSETUP(klass, rb_cClass, T_ICLASS);
35
+ #endif
36
+ /* we want a fresh ivtbl */
37
+ RCLASS_IV_TBL(klass) = st_init_numtable();
38
+
39
+ /* we want to copy the mtbl */
40
+ RCLASS_M_TBL(klass) = RCLASS_M_TBL(module);
41
+ RCLASS_SUPER(klass) = super;
42
+
43
+ /* create IClass for module's singleton */
44
+ /* if super is 0 then we're including into a module (not a class), so treat as special case */
45
+ VALUE meta = include_class_new(KLASS_OF(module), super ? KLASS_OF(super) : rb_cModule);
46
+
47
+ /* don't mess with (Module) */
48
+ if (meta != rb_singleton_class(rb_cModule)) {
49
+
50
+ /* set it as a singleton */
51
+ FL_SET(meta, FL_SINGLETON);
52
+
53
+ /* attach singleton to module */
54
+ rb_singleton_class_attached((VALUE)meta, (VALUE)klass);
55
+ }
56
+ /* assign the metaclass to module's klass */
57
+ KLASS_OF(klass) = meta;
58
+
59
+ OBJ_INFECT(klass, module);
60
+ OBJ_INFECT(klass, super);
61
+
62
+ return (VALUE)klass;
63
+ }
64
+
65
+ VALUE
66
+ rb_real_include_module(VALUE klass, VALUE module)
67
+ {
68
+ VALUE p, c;
69
+ int changed = 0;
70
+
71
+ rb_frozen_class_p(klass);
72
+ if (!OBJ_UNTRUSTED(klass)) {
73
+ rb_secure(4);
74
+ }
75
+
76
+ if (TYPE(module) != T_MODULE) {
77
+ Check_Type(module, T_MODULE);
78
+ }
79
+
80
+ OBJ_INFECT(klass, module);
81
+ c = klass;
82
+
83
+ /* ensure singleton classes exist, both for includer and includee */
84
+ rb_singleton_class(module);
85
+ rb_singleton_class(klass);
86
+
87
+ while (module) {
88
+ int superclass_seen = FALSE;
89
+
90
+ if (RCLASS_M_TBL(klass) == RCLASS_M_TBL(module))
91
+ rb_raise(rb_eArgError, "cyclic include detected");
92
+ /* ignore if the module included already in superclasses */
93
+ for (p = RCLASS_SUPER(klass); p; p = RCLASS_SUPER(p)) {
94
+ switch (BUILTIN_TYPE(p)) {
95
+ case T_ICLASS:
96
+ if (RCLASS_M_TBL(p) == RCLASS_M_TBL(module)) {
97
+ if (!superclass_seen) {
98
+ c = p; /* move insertion point */
99
+ }
100
+ goto skip;
101
+ }
102
+ break;
103
+ case T_CLASS:
104
+ superclass_seen = TRUE;
105
+ break;
106
+ }
107
+ }
108
+
109
+ /* we're including the module, so create the iclass */
110
+ VALUE imod = include_class_new(module, RCLASS_SUPER(c));
111
+
112
+ /* module gets included directly above c, so set the super */
113
+ RCLASS_SUPER(c) = imod;
114
+
115
+ /* also do the same for parallel inheritance chain (singleton classes) */
116
+ RCLASS_SUPER(KLASS_OF(c)) = KLASS_OF(imod);
117
+ c = imod;
118
+
119
+ if (RMODULE_M_TBL(module) && RMODULE_M_TBL(module)->num_entries)
120
+ changed = 1;
121
+ skip:
122
+ module = RCLASS_SUPER(module);
123
+ }
124
+ if (changed) rb_clear_cache();
125
+
126
+ return Qnil;
127
+ }
128
+
129
+ void
130
+ Init_real_include() {
131
+ rb_define_method(rb_cModule, "real_include", rb_real_include_module, 1);
132
+ }
@@ -1,4 +1,3 @@
1
- # bring in user-defined extensions to TexPlay
2
1
  direc = File.dirname(__FILE__)
3
2
 
4
3
  begin
@@ -8,6 +7,7 @@ begin
8
7
  require "#{direc}/1.8/real_include"
9
8
  end
10
9
  rescue LoadError => e
10
+ require 'rbconfig'
11
11
  dlext = Config::CONFIG['DLEXT']
12
12
  require "#{direc}/real_include.#{dlext}"
13
13
  end
@@ -1,3 +1,3 @@
1
1
  module RealInclude
2
- VERSION = "0.1.2"
2
+ VERSION = "0.1.3"
3
3
  end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 1
8
- - 2
9
- version: 0.1.2
8
+ - 3
9
+ version: 0.1.3
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-04 00:00:00 +13:00
17
+ date: 2010-10-05 00:00:00 +13:00
18
18
  default_executable:
19
19
  dependencies: []
20
20
 
@@ -33,6 +33,8 @@ files:
33
33
  - lib/real_include.rb
34
34
  - lib/real_include/version.rb
35
35
  - ext/real_include/extconf.rb
36
+ - ext/real_include/compat.h
37
+ - ext/real_include/real_include.c
36
38
  has_rdoc: false
37
39
  homepage: http://banisterfiend.wordpress.com
38
40
  licenses: []