forked from rubinius/rubinius
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproc.rb
276 lines (221 loc) · 5.76 KB
/
proc.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
class Proc
def self.allocate
raise TypeError, "allocator undefined for Proc"
end
def self.__allocate__
Rubinius.primitive :proc_allocate
raise PrimitiveFailure, "Proc#allocate primitive failed"
end
def call_prim(*args)
Rubinius.primitive :proc_call
raise PrimitiveFailure, "Proc#call primitive failed"
end
def call(*args, &block)
if @ruby_method
@ruby_method.call(*args, &block)
else
call_prim(*args, &block)
end
end
def call_on_object(*args)
Rubinius.primitive :proc_call_on_object
raise PrimitiveFailure, "Proc#call_on_object primitive failed"
end
def lambda_style!
@lambda = true
end
def lambda?
!!@lambda
end
end
class Proc
def self.__from_block__(env)
# The compiler must be fixed before this method can be removed.
Rubinius::Mirror::Proc.from_block self, env
end
def self.new(*args)
env = nil
Rubinius.asm do
push_block
# assign a pushed block to the above local variable "env"
# Note that "env" is indexed at 1, not 0. "args" is indexed at 0.
set_local 1
end
unless env
# Support for ancient pre-block-pass style:
# def something; Proc.new; end
# something { a_block } => Proc instance
env = Rubinius::BlockEnvironment.of_sender
unless env
raise ArgumentError, "tried to create a Proc object without a block"
end
end
block = Rubinius::Mirror::Proc.from_block self, env
if block.class != self
block = block.dup
Rubinius::Unsafe.set_class(block, self)
end
Rubinius.asm(block, args) do |b, a|
run b
run a
run b
send_with_splat :initialize, 0, true
end
return block
end
attr_accessor :block
attr_accessor :bound_method
attr_accessor :ruby_method
def binding
bind = @block.to_binding
bind.proc_environment = @block
bind
end
def arity
if @ruby_method
return @ruby_method.arity
elsif @bound_method
arity = @bound_method.arity
return arity < 0 ? -1 : arity
end
arity = @block.arity
return arity if self.lambda? ||
@block.compiled_code.splat ||
@block.compiled_code.total_args == arity
-arity - 1
end
alias_method :===, :call
def curry(curried_arity = nil)
if lambda? && curried_arity
if arity >= 0 && curried_arity != arity
raise ArgumentError, "Wrong number of arguments (%i for %i)" % [
curried_arity,
arity
]
end
if arity < 0 && curried_arity < (-arity - 1)
raise ArgumentError, "Wrong number of arguments (%i for %i)" % [
curried_arity,
-arity - 1
]
end
end
args = []
m = Rubinius::Mirror.reflect self
f = m.curry self, [], arity
f.singleton_class.send(:define_method, :binding) do
raise ArgumentError, "cannot create binding from f proc"
end
f.singleton_class.thunk_method :parameters, [[:rest]]
f.singleton_class.thunk_method :source_location, nil
f
end
def source_location
if @ruby_method
@ruby_method.source_location
elsif @bound_method
if @bound_method.respond_to?(:source_location)
@bound_method.source_location
else
nil
end
else
@block.source_location
end
end
def to_s
file, line = source_location
l = " (lambda)" if lambda?
if file and line
"#<#{self.class}:0x#{self.object_id.to_s(16)}@#{file}:#{line}#{l}>"
else
"#<#{self.class}:0x#{self.object_id.to_s(16)}#{l}>"
end
end
alias_method :inspect, :to_s
def self.__from_method__(meth)
obj = __allocate__
obj.ruby_method = meth
obj.lambda_style!
return obj
end
def __yield__(*args, &block)
@ruby_method.call(*args, &block)
end
def parameters
if @ruby_method
return @ruby_method.parameters
elsif @bound_method
return @bound_method.parameters
end
code = @block.compiled_code
params = []
return params unless code.respond_to? :local_names
m = code.required_args - code.post_args
o = m + code.total_args - code.required_args
p = o + code.post_args
p += 1 if code.splat
required_status = self.lambda? ? :req : :opt
code.local_names.each_with_index do |name, i|
if i < m
params << [required_status, name]
elsif i < o
params << [:opt, name]
elsif code.splat == i
if name == :*
params << [:rest]
else
params << [:rest, name]
end
elsif i < p
params << [required_status, name]
elsif code.block_index == i
params << [:block, name]
end
end
params
end
def to_proc
self
end
alias_method :[], :call
alias_method :yield, :call
def clone
copy = self.class.__allocate__
Rubinius.invoke_primitive :object_copy_object, copy, self
Rubinius.invoke_primitive :object_copy_singleton_class, copy, self
Rubinius.privately do
copy.initialize_copy self
end
copy.freeze if frozen?
copy
end
def dup
copy = self.class.__allocate__
Rubinius.invoke_primitive :object_copy_object, copy, self
Rubinius.privately do
copy.initialize_copy self
end
copy
end
def for_define_method(name, klass)
if @ruby_method
code, scope = @ruby_method.for_define_method(name, klass, self)
else
be = @block.dup
be.change_name name
duped_proc = self.dup
duped_proc.lambda_style!
code = Rubinius::BlockEnvironment::AsMethod.new(be)
scope = duped_proc.block.scope
end
[code, scope]
end
def self.from_method(meth)
if meth.kind_of? Method
return __from_method__(meth)
else
raise ArgumentError, "tried to create a Proc object without a Method"
end
end
end