31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
|
# File 'lib/fastruby/object.rb', line 31
def self.fastruby(rubycode, *options_hashes)
tree = RubyParser.new.parse rubycode
options_hash = {:validate_lvar_types => true}
options_hashes.each do |opt|
options_hash.merge!(opt)
end
if tree[0] != :defn
raise ArgumentError, "Only definition of methods are accepted"
end
method_name = tree[1]
args_tree = tree[2]
hashname = "$hash" + rand(1000000).to_s
hash = Hash.new
locals = Set.new
locals << :self
FastRuby::GetLocalsProcessor.get_locals(RubyParser.new.parse(rubycode)).each do |local|
locals << local
end
self_ = self
hash.instance_eval{@klass = self_}
hash.instance_eval{@method_name = method_name}
class_eval do
class << self
include FastRuby::BuilderModule
end
end
self_.method_tree[method_name] = tree
self_.method_locals[method_name] = locals
self_.method_options[method_name] = options_hash
def hash.build(key)
@klass.build(key, @method_name)
end
eval("#{hashname} = hash")
value_cast = ( ["VALUE"]*(args_tree.size+1) ).join(",")
main_signature_argument = args_tree[1..-1].first || "self"
strmethodargs = ""
strmethodargs_class = (["self"] + args_tree[1..-1]).map{|arg| "CLASS_OF(#{arg.to_s})"}.join(",")
if args_tree.size > 1
strmethodargs = "self,block,#{args_tree[1..-1].map(&:to_s).join(",") }"
else
strmethodargs = "self,block"
end
strmethod_signature = (["self"] + args_tree[1..-1]).map { |arg|
"sprintf(method_name+strlen(method_name), \"%lu\", CLASS_OF(#{arg}));\n"
}.join
c_code = "VALUE #{method_name}(#{args_tree[1..-1].map{|arg| "VALUE #{arg}" }.join(",")}) {
VALUE method_hash = (VALUE)#{hash.internal_value};
VALUE klass = (VALUE)#{self.internal_value};
char method_name[0x100];
method_name[0] = '_';
method_name[1] = 0;
sprintf(method_name+1, \"#{method_name}\");
#{strmethod_signature}
NODE* body;
ID id;
id = rb_intern(method_name);
body = rb_method_node(klass,id);
if (body == 0) {
VALUE argv_class[] = {#{strmethodargs_class} };
VALUE signature = rb_ary_new4(#{args_tree.size},argv_class);
rb_funcall(method_hash, #{:build.to_i}, 1, signature);
body = rb_method_node(klass,id);
}
if (nd_type(body) == NODE_CFUNC) {
int argc = body->nd_argc;
VALUE block = Qfalse;
if (rb_block_given_p()) {
struct {
void *block_function_address;
void *block_function_param;
} block_struct;
block_struct.block_function_address = re_yield;
block_struct.block_function_param = 0;
block = (VALUE)&block_struct;
}
if (argc == #{args_tree.size}) {
return ((VALUE(*)(#{value_cast}))body->nd_cfnc)(#{strmethodargs});
} else if (argc == -1) {
VALUE argv[] = {#{(["block"]+args_tree[1..-1]).map(&:to_s).join(",")} };
return ((VALUE(*)(int,VALUE*,VALUE))body->nd_cfnc)(#{args_tree.size},argv,self);
} else if (argc == -2) {
VALUE argv[] = {#{(["block"]+args_tree[1..-1]).map(&:to_s).join(",")} };
return ((VALUE(*)(VALUE,VALUE))body->nd_cfnc)(self, rb_ary_new4(#{args_tree.size},argv));
} else {
rb_raise(rb_eArgError, \"wrong number of arguments (#{args_tree.size-1} for %d)\", argc);
}
}
return Qnil;
}"
inline :C do |builder|
builder.include "<node.h>"
builder.inc << "static VALUE re_yield(int argc, VALUE* argv, VALUE param) {
return rb_yield_splat(rb_ary_new4(argc,argv));
}"
builder.c c_code
end
end
|