mult 0.2.0 → 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,6 +1,9 @@
1
- # Mult: utility functions for playing with Ruby Objects
1
+ Mult: utility functions for playing with Ruby Objects
2
+ ======================================================
3
+
4
+ Methods:
5
+ ---------
2
6
 
3
- Includes:
4
7
  * actual_class
5
8
  * actual_class=
6
9
  * actual_super
@@ -12,7 +15,24 @@ Includes:
12
15
  * ivar_get
13
16
  * m_tbl
14
17
  * m_tbl=
18
+ * iv_tbl
19
+ * iv_tbl=
20
+ * has_flag?
21
+ * flags
22
+ * flags=
23
+
24
+ Constants:
25
+ ----------
26
+
27
+ * T_CLASS
28
+ * T_MODULE
29
+ * T_OBJECT
30
+ * T_ICLASS
31
+ * T_DATA
32
+ * FL_SINGLETON
33
+ * FL_FREEZE
15
34
 
16
35
  TO USE:
36
+ --------
17
37
 
18
- gem install mult
38
+ `gem install mult`
data/Rakefile CHANGED
@@ -20,12 +20,12 @@ spec = Gem::Specification.new do |s|
20
20
  s.date = Time.now.strftime '%Y-%m-%d'
21
21
  s.require_path = 'lib'
22
22
  s.homepage = "http://banisterfiend.wordpress.com"
23
- # s.platform = 'i386-mingw32'
23
+ #s.platform = 'i386-mingw32'
24
24
  s.platform = Gem::Platform::RUBY
25
25
  s.extensions = FileList["ext/**/extconf.rb"]
26
26
  s.has_rdoc = 'yard'
27
- s.files = ["Rakefile", "README.rdoc", "LICENSE", "lib/mult.rb", "lib/mult/version.rb"]
28
- #+ ["lib/1.8/mult.#{dlext}", "lib/1.9/mult.#{dlext}"] +
27
+ s.files = ["Rakefile", "README.rdoc", "LICENSE", "lib/mult.rb", "lib/mult/version.rb"] +
28
+ # ["lib/1.8/mult.#{dlext}", "lib/1.9/mult.#{dlext}"] +
29
29
  FileList["ext/**/extconf.rb", "ext/**/*.h", "ext/**/*.c"].to_a
30
30
  end
31
31
 
@@ -0,0 +1,23 @@
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
+ # include "st.h"
15
+ #endif
16
+
17
+ # define FALSE 0
18
+ # define TRUE 1
19
+
20
+ /* a useful macro. cannot use ordinary CLASS_OF as it does not return an lvalue */
21
+ #define KLASS_OF(c) (RBASIC(c)->klass)
22
+
23
+ #endif
@@ -1,3 +1,9 @@
1
1
  require 'mkmf'
2
2
 
3
+ # 1.9 compatibility
4
+ $CFLAGS += " -DRUBY_19" if RUBY_VERSION =~ /1.9/
5
+
6
+ # let's use c99
7
+ $CFLAGS += " -std=c99"
8
+
3
9
  create_makefile('mult')
@@ -0,0 +1,167 @@
1
+ #include <ruby.h>
2
+ #include "compat.h"
3
+
4
+ static VALUE cRubyTable;
5
+
6
+ typedef struct {
7
+ st_table* table;
8
+ } RubyTable;
9
+
10
+ /* return the actual class of an object */
11
+ static VALUE
12
+ get_class(VALUE self) {
13
+ return CLASS_OF(self);
14
+ }
15
+
16
+ /* set the class of an object */
17
+ static VALUE
18
+ set_class(VALUE self, VALUE klass) {
19
+ KLASS_OF(self) = klass;
20
+ return klass;
21
+ }
22
+
23
+ /* get the actual super class of an object */
24
+ static VALUE
25
+ get_super(VALUE self)
26
+ {
27
+ return RCLASS_SUPER(self);
28
+ }
29
+
30
+ /* set the super class of an object */
31
+ static VALUE
32
+ set_super(VALUE self, VALUE klass) {
33
+ RCLASS_SUPER(self) = klass;
34
+ return klass;
35
+ }
36
+
37
+ /* is the object a singleton class ? */
38
+ static VALUE
39
+ is_singleton(VALUE self, VALUE klass) {
40
+ return FL_TEST(self, FL_SINGLETON) ? Qtrue : Qfalse;
41
+ }
42
+
43
+ /* get a raw instance var */
44
+ static VALUE
45
+ get_ivar(VALUE self, VALUE sym)
46
+ {
47
+ return rb_ivar_get(self, rb_to_id(sym));
48
+ }
49
+
50
+ /* set a raw instance var */
51
+ static VALUE
52
+ set_ivar(VALUE self, VALUE sym, VALUE val)
53
+ {
54
+ return rb_ivar_set(self, rb_to_id(sym), val);
55
+ }
56
+
57
+ /* get the attached class if receiver is a singleton class */
58
+ static VALUE
59
+ get_attached(VALUE self)
60
+ {
61
+ if(FL_TEST(self, FL_SINGLETON))
62
+ return rb_iv_get(self, "__attached__");
63
+ else
64
+ return Qnil;
65
+ }
66
+
67
+ /* set the attached class if receiver is a singleton class */
68
+ static VALUE
69
+ set_attached(VALUE self, VALUE klass) {
70
+ if(FL_TEST(self, FL_SINGLETON)) {
71
+ rb_iv_set(self, "__attached__", klass);
72
+ return self;
73
+ }
74
+ else
75
+ return Qnil;
76
+ }
77
+
78
+ /* generate table get methods */
79
+ #define CREATE_GET_TBL_FUNC(func_name, table_type) \
80
+ static VALUE func_name(VALUE self) { \
81
+ RubyTable * tbl_struct = ALLOC(RubyTable); \
82
+ if (!table_type(self)) \
83
+ table_type(self) = st_init_numtable(); \
84
+ tbl_struct->table = table_type(self); \
85
+ return Data_Wrap_Struct(cRubyTable, 0, free, tbl_struct); \
86
+ }
87
+
88
+ /* generate table set methods */
89
+ #define CREATE_SET_TBL_FUNC(func_name, table_type) \
90
+ static VALUE \
91
+ func_name(VALUE self, VALUE rb_tbl) { \
92
+ Check_Type(rb_tbl, T_DATA); \
93
+ RubyTable * tbl_struct; \
94
+ Data_Get_Struct(rb_tbl, RubyTable, tbl_struct); \
95
+ table_type(self) = tbl_struct->table; \
96
+ rb_clear_cache(); \
97
+ return self; \
98
+ }
99
+
100
+ /* get the method table */
101
+ CREATE_GET_TBL_FUNC(get_m_tbl, RCLASS_M_TBL)
102
+ /* set the method table */
103
+ CREATE_SET_TBL_FUNC(set_m_tbl, RCLASS_M_TBL)
104
+
105
+ /* get the iv table */
106
+ CREATE_GET_TBL_FUNC(get_iv_tbl, RCLASS_IV_TBL)
107
+ /* set the iv table */
108
+ CREATE_SET_TBL_FUNC(set_iv_tbl, RCLASS_IV_TBL)
109
+
110
+ static VALUE
111
+ set_flags(VALUE self, VALUE flag)
112
+ {
113
+ Check_Type(flag, T_FIXNUM);
114
+ FL_SET(self, FIX2INT(flag));
115
+ return self;
116
+ }
117
+
118
+ static VALUE
119
+ get_flags(VALUE self)
120
+ {
121
+ return INT2FIX(RBASIC(self)->flags);
122
+ }
123
+
124
+ static VALUE
125
+ has_flag_p(VALUE self, VALUE flag)
126
+ {
127
+ Check_Type(flag, T_FIXNUM);
128
+ return FL_TEST(self, FIX2INT(flag)) ? Qtrue : Qfalse;
129
+ }
130
+
131
+ void
132
+ Init_mult() {
133
+
134
+ cRubyTable = rb_define_class("RubyTable", rb_cObject);
135
+
136
+ rb_define_method(rb_cObject, "actual_class", get_class, 0);
137
+ rb_define_method(rb_cObject, "actual_class=", set_class, 1);
138
+
139
+ rb_define_method(rb_cObject, "ivar_get", get_ivar, 1);
140
+ rb_define_method(rb_cObject, "ivar_set", set_ivar, 2);
141
+
142
+ rb_define_method(rb_cModule, "m_tbl", get_m_tbl, 0);
143
+ rb_define_method(rb_cModule, "m_tbl=", set_m_tbl, 1);
144
+ rb_define_method(rb_cModule, "iv_tbl", get_iv_tbl, 0);
145
+ rb_define_method(rb_cModule, "iv_tbl=", set_iv_tbl, 1);
146
+
147
+ rb_define_method(rb_cModule, "actual_super", get_super, 0);
148
+ rb_define_method(rb_cModule, "actual_super=", set_super, 1);
149
+
150
+ rb_define_method(rb_cClass, "is_singleton?", is_singleton, 0);
151
+
152
+ rb_define_method(rb_cClass, "attached", get_attached, 0);
153
+ rb_define_method(rb_cClass, "attached=", set_attached, 1);
154
+
155
+ rb_define_method(rb_cObject, "has_flag?", has_flag_p, 1);
156
+ rb_define_method(rb_cObject, "flags=", set_flags, 1);
157
+ rb_define_method(rb_cObject, "flags", get_flags, 0);
158
+
159
+ // constants
160
+ rb_define_const(rb_cObject, "T_CLASS", INT2FIX(T_CLASS));
161
+ rb_define_const(rb_cObject, "T_MODULE", INT2FIX(T_MODULE));
162
+ rb_define_const(rb_cObject, "T_OBJECT", INT2FIX(T_OBJECT));
163
+ rb_define_const(rb_cObject, "T_DATA", INT2FIX(T_DATA));
164
+ rb_define_const(rb_cObject, "T_ICLASS", INT2FIX(T_ICLASS));
165
+ rb_define_const(rb_cObject, "FL_FREEZE", INT2FIX(FL_FREEZE));
166
+ rb_define_const(rb_cObject, "FL_SINGLETON", INT2FIX(FL_SINGLETON));
167
+ }
@@ -1,3 +1,3 @@
1
1
  module Mult
2
- VERSION = "0.2.0"
2
+ VERSION = "0.2.2"
3
3
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mult
3
3
  version: !ruby/object:Gem::Version
4
- hash: 23
4
+ hash: 19
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 2
9
- - 0
10
- version: 0.2.0
9
+ - 2
10
+ version: 0.2.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - John Mair (banisterfiend)
@@ -34,6 +34,8 @@ files:
34
34
  - lib/mult.rb
35
35
  - lib/mult/version.rb
36
36
  - ext/mult/extconf.rb
37
+ - ext/mult/compat.h
38
+ - ext/mult/mult.c
37
39
  has_rdoc: yard
38
40
  homepage: http://banisterfiend.wordpress.com
39
41
  licenses: []