-
Notifications
You must be signed in to change notification settings - Fork 603
/
proc.rb
254 lines (207 loc) · 5.21 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
class Proc
def self.__from_block__(env)
Rubinius.primitive :proc_from_env
if Rubinius::Type.object_kind_of? env, Rubinius::BlockEnvironment
raise PrimitiveFailure, "Proc.__from_block__ primitive failed to create Proc from BlockEnvironment"
else
begin
env.to_proc
rescue Exception
raise ArgumentError, "Unable to convert #{env.inspect} to a Proc"
end
end
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 = __from_block__(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
@block.arity
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 = []
my_self = self
m = lambda? ? :lambda : :proc
f = __send__(m) {|*x|
call_args = args + x
if call_args.length >= my_self.arity
my_self[*call_args]
else
args = call_args
f
end
}
f.singleton_class.send(:define_method, :binding) {
raise ArgumentError, "cannot create binding from f proc"
}
f.singleton_class.send(:define_method, :parameters) {
[[:rest]]
}
f.singleton_class.send(:define_method, :source_location) {
nil
}
f
end
def source_location
if @ruby_method
code = @ruby_method.executable
if code.respond_to? :file
file = code.file
if code.lines
line = code.first_line
else
line = -1
end
else
file = "(unknown)"
line = -1
end
[file.to_s, line]
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 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