This repository has been archived by the owner on May 13, 2019. It is now read-only.
/
kernel.rb
391 lines (378 loc) · 8.57 KB
/
kernel.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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
module JSOpen
def js_hash; @js_hash ||= Rubinius::LookupTable.new; end
def _js_key?(k); js_hash.key?(k); end
def _js_get(k); js_hash[k]; end
def _js_set(k,v); js_hash[k] = v; end
end
class Hash
def _js_key?(k); key?(k); end
def _js_get(k); self[k]; end
def _js_set(k,v); self[k] = v; end
end
module Kernel
def js_value; js_invoke(:valueOf); end
def js_div(n); self / n; end
def js_add(other)
if Numeric === self && Numeric === other
self + other
else
"#{self}#{other}"
end
end
def js_key; to_s.intern; end
def js_truthy?; self; end
def js_typeof; 'object'; end
def js_equal(v); self == v; end
def js_strictly_equal(v); eql?(v); end
def js_in(k)
if respond_to?(:_js_get)
if respond_to?(:_js_key?)
if _js_key?(name)
return true
end
elsif _js_get(name)
return true
end
end
respond_to?(:"js:#{k}")
end
def js_get(name)
getter = :"js:get:#{name}"
if respond_to?(getter)
return send(getter)
end
if respond_to?(:_js_get)
if respond_to?(:_js_key?)
if _js_key?(name)
return _js_get(name)
end
elsif v = _js_get(name)
return v
end
end
accessor = :"js:#{name}"
if respond_to?(accessor)
meth = js_method(accessor)
end
end
def js_set(name, value)
setter = :"js:set:#{name}"
if respond_to?(setter)
return send(setter, value)
end
if UnboundMethod === value || Capuchin::Function === value
metaclass.send(:_js_define_method, :"js:#{name}") do |*args|
instance_exec(*args, &value)
end
return value
end
_js_set(name, value)
end
def js_invoke(name, *args)
accessor = :"js:#{name}"
if respond_to?(accessor)
send(accessor, *args)
elsif f = js_get(name)
f.js_call(self, *args)
else
# Do the send anyway, for the exception
send(accessor, *args)
end
end
def js_method(name)
meth = method(name)
if meth.owner == metaclass
t = metaclass.instance_variable_get(:@js_methods)
t && t[name] || meth
else
meth
end
end
end
def test_js_respond_to(name, *args)
respond_to?(:"js:#{name}")
end
def invoke_js_respond_to(name, *args)
send(:"js:#{name}", *args)
end
def invoke_js_get(name, *args)
js_get(name).js_call(self, *args)
end
def Rubinius.bind_call(recv, meth, *args)
# meth == :js_invoke
case meth
when :js_invoke
Rubinius::CallUnit.test(
Rubinius::CallUnit.for_method(method(:test_js_respond_to)),
Rubinius::CallUnit.for_method(method(:invoke_js_respond_to)),
Rubinius::CallUnit.for_method(method(:invoke_js_get))
)
else
raise ArgumentError, "bind_call for unknown '#{meth}'"
end
end
class Capuchin::Proto
include JSOpen
end
class Capuchin::Function
include JSOpen
def initialize(name=nil, object={}, mod=nil, &block)
@name = name
@block = block || lambda {}
@proto = Capuchin::Proto.new
@module = mod
_js_set(:prototype, @proto)
object.each do |k,v|
_js_set(k, v)
end
end
end
module JSComparable
def js_lt(other); self.js_cmp < other.js_cmp; end
def js_gt(other); self.js_cmp > other.js_cmp; end
def js_lte(other); self.js_cmp <= other.js_cmp; end
def js_gte(other); self.js_cmp >= other.js_cmp; end
end
class Class
def js_new(*args); new(*args); end
def js_instance_of(v); self === v; end
def js_expose_method(*names)
names.each do |name|
alias_method :"js:#{name}", name
end
end
def js_expose_attr(*names)
names.each do |name|
alias_method :"js:get:#{name}", name
if method_defined?(:"#{name}=")
alias_method :"js:set:#{name}", :"#{name}="
end
end
end
def js_def(name, &block)
_js_define_method(:"js:#{name}", &block)
end
def js_attr(name, &block)
name = name.to_s.dup
if name.sub!(/=$/, '')
_js_define_method(:"js:set:#{name}", &block)
else
_js_define_method(:"js:get:#{name}", &block)
end
end
def _js_define_method(name, &block)
(@js_methods ||= Rubinius::LookupTable.new)[name] = Capuchin::Function.new(&block)
define_method(name, &block)
end
end
class Array
js_attr :length do
size
end
js_attr :length= do |n|
if n > size
slice!(n, size)
elsif n < size
fill(0, size, n)
end
end
js_expose_method :push
def js_hash; @js_hash ||= Rubinius::LookupTable.new; end
def _js_key?(k); Fixnum === k ? k <= size : js_hash.key?(k); end
def _js_get(k); Fixnum === k ? self[k] : js_hash[k]; end
def _js_set(k,v); Fixnum === k ? self[k] = v : js_hash[k] = v; end
def self.js_new(*args)
new(*args)
end
end
class Numeric
include JSComparable
def js_cmp; self; end
def js_typeof; 'number'; end
js_def :valueOf do
self
end
end
class Fixnum
def js_div(n); (0 == n ? self.to_f : self) / n; end
def js_key; self; end
def js_truthy?; 0 != self; end
end
class Float
js_def :toFixed do |x|
self.to_i
end
js_def :toPrecision do |digits|
"%.#{digits}f" % self
end
end
class Symbol
def js_key; self; end
end
class String
def js_cmp; to_f; end
js_attr :length do
size
end
js_expose_method :substring
js_def :indexOf do |needle|
index(needle) || -1
end
def js_key; intern; end
def js_truthy?; size > 0; end
def js_typeof; 'string'; end
end
class TrueClass
include JSComparable
def js_cmp; 1; end
def js_typeof; 'boolean'; end
end
class FalseClass
include JSComparable
def js_cmp; 0; end
def js_typeof; 'boolean'; end
end
class NilClass
include JSComparable
def js_cmp; 0; end
def js_div(n); 0.js_div(n); end
end
class Method
include JSOpen
def js_prototype
@proto || (
@proto = Capuchin::Proto.new
_js_set(:prototype, @proto)
)
end
def js_typeof; 'function'; end
def js_call(target, *args)
if receiver.eql? target
call(*args)
else
unbind.bind(target).call(*args)
end
end
def js_apply(target, args)
js_call(target, *args)
end
js_def :call do |this, *args|
js_call(this, *args)
end
js_def :apply do |this, args|
js_call(this, *args)
end
def js_new(*args)
o = Capuchin::Obj.new(self, js_prototype)
js_call(o, *args)
o
end
end
class UnboundMethod
include JSOpen
def js_prototype
@proto || (
@proto = Capuchin::Proto.new
_js_set(:prototype, @proto)
)
end
def js_typeof; 'function'; end
def js_call(target, *args)
bind(target).call(*args)
end
def js_apply(target, args)
js_call(target, *args)
end
js_def :call do |this, *args|
js_call(this, *args)
end
js_def :apply do |this, args|
js_call(this, *args)
end
def js_new(*args)
o = Capuchin::Obj.new(self, js_prototype)
js_call(o, *args)
o
end
end
class Capuchin::Obj
include JSOpen
def initialize(constructor, proto)
_js_set(:constructor, constructor)
@__proto__ = proto
end
def js_get(name)
super || @__proto__.js_get(name)
end
def js_in(k)
super || @__proto__.js_in(k)
end
def to_f
js_value.to_f
end
def to_i
js_value.to_i
end
def to_s
js_value.to_s
end
def to_str
js_value.to_s
end
def self.js_new
@empty_proto ||= Capuchin::Proto.new
new(nil, @empty_proto)
end
end
class Capuchin::Function
def call(*args)
js_call(nil, *args)
end
js_def :call do |this, *args|
js_call(this, *args)
end
js_def :apply do |this, args|
js_call(this, *args)
end
def js_typeof; 'function'; end
def js_call(target, *args)
target.instance_exec(*args, &@block)
end
def js_apply(target, args)
js_call(target, *args)
end
def js_new(*args)
o = Capuchin::Obj.new(self, @proto)
js_call(o, *args)
o.extend(@module) if @module
o
end
def to_proc
@block
end
end
module Capuchin::DateMethods
attr :t
def -(other)
(t - other.t) * 1000
end
end
Capuchin::Globals = Rubinius::LookupTable.new
Capuchin::Globals[:Array] = Array
Capuchin::Globals[:Object] = Capuchin::Obj
Capuchin::Globals[:Date] = Capuchin::Function.new('Date', {}, Capuchin::DateMethods) {|| @t = Time.new }
Capuchin::Globals[:print] = Capuchin::Function.new {|x| puts x }
Capuchin::Globals[:p] = Capuchin::Function.new {|x| p [x, x.methods.grep(/^js:/)] }
Capuchin::Globals[:Math] = {
:log => Capuchin::Function.new {|n| Math.log(n.js_value) },
:pow => Capuchin::Function.new {|a,b| a.js_value ** b.js_value },
:sqrt => Capuchin::Function.new {|n| Math.sqrt(n.js_value) },
:E => Math::E,
}
Capuchin::Globals[:run] = Capuchin::Globals[:load] = Capuchin::Function.new do |filename|
cx = Capuchin::Context.new
start = Time.now
cx.load(filename)
done = Time.now
(done - start) * 1000
end