opal 0.3.10 → 0.3.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/corelib/numeric.rb DELETED
@@ -1,352 +0,0 @@
1
- # Numeric objects represent numbers in opal. Unlike ruby, this class is
2
- # used to represent both floats and integers, and there is currently no
3
- # class representing bignums. Numeric values may only be made using
4
- # their literal representations:
5
- #
6
- # 1 # => 1
7
- # 2.0 # => 2
8
- # 0.05 # => 0.05
9
- #
10
- # Implementation details
11
- # ----------------------
12
- #
13
- # Opal numbers are toll-free bridged to native javascript numbers so
14
- # that anywhere a ruby number is expected, a native javascript number
15
- # may be used in its place. Javascript only has a single class/prototype
16
- # to represent numbers, meaning that all integers and floats contain the
17
- # same prototype. For this reason, Opal follows suit and implements all
18
- # numbers as a direct instance of {Numeric}.
19
- #
20
- # Floats and Integers could be truley represented using wrappers, but
21
- # this would **dramatically** increase performance overhead at the very
22
- # core parts of the opal runtime. This overhead is too great, and the
23
- # benefits would be too few.
24
- #
25
- # Ruby compatibility
26
- # ------------------
27
- #
28
- # As discussed, {Numeric} is the only class used to represent numbers in
29
- # opal. Most of the useful methods from `Fixnum`, `Integer` and `Float`
30
- # are implemented on this class.
31
- #
32
- # It is also important to note that there is no distinguishment between
33
- # floats and integers, so that, `1` and `1.0` are exactly equal.
34
- #
35
- # Custom subclasses of Numeric may be used so that a numeric literal is
36
- # passed in. This differs from the ruby approach of number inheritance,
37
- # but does allow for certain circumstances where a subclass of number
38
- # might be useful. Opal does not try to implement `Integer` or `Float`
39
- # for these purposes, but it is easily done. This approach will still
40
- # not allow for literals to be used to make these subclass instances.
41
- class Numeric
42
-
43
- def +(other)
44
- `return self + other;`
45
- end
46
-
47
- def -(other)
48
- `return self - other;`
49
- end
50
-
51
- def *(other)
52
- `return self * other;`
53
- end
54
-
55
- def /(other)
56
- `return self / other;`
57
- end
58
-
59
- def ==(other)
60
- `return self.valueOf() === other.valueOf();`
61
- end
62
-
63
- def <(other)
64
- `return self < other;`
65
- end
66
-
67
- def <=(other)
68
- `return self <= other;`
69
- end
70
-
71
- def >(other)
72
- `return self > other;`
73
- end
74
-
75
- def >=(other)
76
- `return self >= other;`
77
- end
78
-
79
- # Returns `self` modulo `other`. See `divmod` for more information.
80
- #
81
- # @param [Numeric] other number to use for module
82
- # @return [Numeric] result
83
- def %(other)
84
- `return self % other;`
85
- end
86
-
87
- def modulo(other)
88
- `return self % other;`
89
- end
90
-
91
- # Bitwise AND.
92
- #
93
- # @param [Numeric] other numeric to AND with
94
- # @return [Numeric]
95
- def &(num2)
96
- `return self & num2;`
97
- end
98
-
99
- # Raises `self` to the power of `other`.
100
- #
101
- # @param [Numeric] other number to raise to
102
- # @return [Numeric]
103
- def **(other)
104
- `return Math.pow(self, other);`
105
- end
106
-
107
- # Shift `self` left by `count` positions.
108
- #
109
- # @param [Numeric] count number to shift
110
- # @return [Numeric]
111
- def <<(count)
112
- `return self << count;`
113
- end
114
-
115
- # Shifts 'self' right by `count` positions.
116
- #
117
- # @param [Numeric] count number to shift
118
- # @return [Numeric]
119
- def >>(count)
120
- `return self >> count;`
121
- end
122
-
123
- # Comparison - Returns '-1', '0', '1' or nil depending on whether `self` is
124
- # less than, equal to or greater than `other`.
125
- #
126
- # @param [Numeric] other number to compare with
127
- # @return [Number, nil]
128
- def <=>(other)
129
- `if (typeof other != 'number') return nil;
130
- else if (self < other) return -1;
131
- else if (self > other) return 1;
132
- return 0;`
133
- end
134
-
135
- # Bitwise EXCLUSIVE OR.
136
- #
137
- # @param [Numeric] other number to XOR with
138
- # @return [Numeric]
139
- def ^(other)
140
- `return self ^ other;`
141
- end
142
-
143
- # Returns the absolute value of `self`.
144
- #
145
- # @example
146
- #
147
- # -1234.abs
148
- # # => 1234
149
- # 1234.abs
150
- # # => 1234
151
- #
152
- # @return [Numeric]
153
- def abs
154
- `return Math.abs(self);`
155
- end
156
-
157
- def magnitude
158
- `return Math.abs(self);`
159
- end
160
-
161
- # Returns `true` if self is even, `false` otherwise.
162
- #
163
- # @return [true, false]
164
- def even?
165
- `return (self % 2 == 0);`
166
- end
167
-
168
- # Returns `true` if self is odd, `false` otherwise.
169
- #
170
- # @return [true, false]
171
- def odd?
172
- `return (self % 2 == 0) ? false : true;`
173
- end
174
-
175
- # Returns the number equal to `self` + 1.
176
- #
177
- # @example
178
- #
179
- # 1.next
180
- # # => 2
181
- # (-1).next
182
- # # => 0
183
- #
184
- # @return [Numeric]
185
- def succ
186
- `return self + 1;`
187
- end
188
-
189
- def next
190
- `return self + 1;`
191
- end
192
-
193
- # Returns the number equal to `self` - 1
194
- #
195
- # @example
196
- #
197
- # 1.pred
198
- # # => 0
199
- # (-1).pred
200
- # # => -2
201
- #
202
- # @return [Numeric]
203
- def pred
204
- `return self - 1;`
205
- end
206
-
207
- # Iterates the block, passing integer values from `self` upto and including
208
- # `finish`.
209
- #
210
- # @example
211
- #
212
- # 5.upto(10) { |i| puts i }
213
- # # => 5
214
- # # => 6
215
- # # => 7
216
- # # => 8
217
- # # => 9
218
- # # => 10
219
- #
220
- # @param [Numeric] finish where to stop iteration
221
- # @return [Numeric] returns the receiver
222
- def upto(finish)
223
- `for (var i = self; i <= finish; i++) {
224
- #{yield `i`};
225
- }
226
-
227
- return self;`
228
- end
229
-
230
- # Iterates the block, passing decreasing values from `self` downto and
231
- # including `finish`.
232
- #
233
- # @example
234
- #
235
- # 5.downto(1) { |x| puts x }
236
- # # => 5
237
- # # => 4
238
- # # => 3
239
- # # => 2
240
- # # => 1
241
- #
242
- # @param [Numeric] finish where to stop iteration
243
- # @return [Numeric] returns the receiver
244
- def downto(finish)
245
- `for (var i = self; i >= finish; i--) {
246
- #{yield `i`};
247
- }
248
-
249
- return self;`
250
- end
251
-
252
- # Iterates the block `self` number of times, passing values in the range 0 to
253
- # `self` - 1.
254
- #
255
- # @example
256
- #
257
- # 5.times { |x| puts x }
258
- # # => 0
259
- # # => 1
260
- # # => 2
261
- # # => 3
262
- # # => 4
263
- #
264
- # @return [Numeric] returns the receiver
265
- def times
266
- raise "no block given" unless block_given?
267
- `for (var i = 0; i < self; i++) {
268
- #{yield `i`};
269
- }
270
-
271
- return self;`
272
- end
273
-
274
- # Bitwise OR.
275
- #
276
- # @param [Numeric] other number to OR with
277
- # @return [Numeric]
278
- def |(other)
279
- `return self | other;`
280
- end
281
-
282
- # Returns `true` if `self` is zero, `false` otherwise.
283
- #
284
- # @return [true, false]
285
- def zero?
286
- `return self == 0;`
287
- end
288
-
289
- # Returns the receiver if it is not zero, `nil` otherwise
290
- #
291
- # @return [Numeric, nil]
292
- def nonzero?
293
- `return self == 0 ? nil : self;`
294
- end
295
-
296
- # One's complement: returns a number where each bit is flipped.
297
- #
298
- # @return [Numeric]
299
- def ~
300
- `return ~self;`
301
- end
302
-
303
- # Returns the smallest integer greater than or equal to `num`.
304
- #
305
- # @example
306
- #
307
- # 1.ceil # => 1
308
- # 1.2.ceil # => 2
309
- # (-1.2).ceil # => -1
310
- # (-1.0).ceil # => -1
311
- #
312
- # @return [Numeric]
313
- def ceil
314
- `return Math.ceil(self);`
315
- end
316
-
317
- # Returns the largest integer less than or equal to `self`.
318
- #
319
- # @example
320
- #
321
- # 1.floor # => 1
322
- # (-1).floor # => -1
323
- #
324
- # @return [Numeric]
325
- def floor
326
- `return Math.floor(self);`
327
- end
328
-
329
- # Returns `true` if self is an ineteger, `false` otherwise.
330
- #
331
- # @return [true, false]
332
- def integer?
333
- `return self % 1 == 0;`
334
- end
335
-
336
- def inspect
337
- `return self.toString();`
338
- end
339
-
340
- def to_s
341
- `return self.toString();`
342
- end
343
-
344
- def to_i
345
- `return parseInt(self);`
346
- end
347
-
348
- def self.allocate
349
- raise RuntimeError, "cannot instantiate instance of Numeric class"
350
- end
351
- end
352
-
data/corelib/object.rb DELETED
@@ -1,37 +0,0 @@
1
- # Core object in hierarchy. Most of the implementation of this core
2
- # object are implemented in {Kernel}.
3
- class Object
4
-
5
- def initialize(*a)
6
- # ...
7
- end
8
-
9
- def ==(other)
10
- `return self === other;`
11
- end
12
-
13
- def equal?(other)
14
- self == other
15
- end
16
-
17
- def __send__(method_id, *args, &block)
18
- `var method = self['m$' + method_id];
19
-
20
- if ($B.f == arguments.callee) {
21
- $B.f = method;
22
- }
23
-
24
- return method.apply(self, args);`
25
- end
26
-
27
- def instance_eval(&block)
28
- raise ArgumentError, "block not supplied" unless block_given?
29
- `block.call(self);`
30
- self
31
- end
32
-
33
- def method_missing(sym, *args)
34
- raise NoMethodError, "undefined method `#{sym}` for #{self.inspect}"
35
- end
36
- end
37
-
data/corelib/proc.rb DELETED
@@ -1,55 +0,0 @@
1
- # `Proc` objects are blocks of code that can also be bound to local
2
- # variables in their defined scope. When called, a proc will maintain
3
- # its `self` value, and still have the ability to access variables
4
- # defined within the same scope. A proc may also be called in another
5
- # context and have its `self` value tempararily adjusted.
6
- #
7
- # Creation of procs may be done by passing a block into the {Proc.new}
8
- # constructor, or the {Kernel} method {Kernel#proc}:
9
- #
10
- # a = Proc.new { 14 } # => #<Proc:0x98aef>
11
- # b = proc { 42 + a.call } # => #<Proc:0x98ef3>
12
- #
13
- # a.call # => 14
14
- # b.call # => 56
15
- #
16
- # Implementation details
17
- # ----------------------
18
- #
19
- # Due to their obvious similarities in functionality, a proc instance is
20
- # simply a native javascript function allowing it to maintain access to
21
- # variables in its outer scope, and to have its `self` value changed on
22
- # demand.
23
- #
24
- # When a proc is defined, its `self` value is stored on the function
25
- # instance itself as a `.$self` property, so when the proc is called in
26
- # future, this is the default value passed as the self property. This
27
- # also means that every function used in the same context as opal may be
28
- # used as a `Proc` meaning the transition back and forth between ruby
29
- # and javascript contexts is easy.
30
- class Proc
31
-
32
- def self.new(&block)
33
- raise ArgumentError,
34
- "tried to create Proc object without a block" unless block_given?
35
-
36
- block
37
- end
38
-
39
- def to_proc
40
- self
41
- end
42
-
43
- def call(*args)
44
- `return self.apply(self.o$s,args);`
45
- end
46
-
47
- def to_s
48
- `return "#<Proc:0x" + (self.$h() * 400487).toString(16) + (self.$lambda ? ' (lambda)' : '') + ">";`
49
- end
50
-
51
- def lambda?
52
- `return self.$lambda;`
53
- end
54
- end
55
-
data/corelib/range.rb DELETED
@@ -1,27 +0,0 @@
1
- class Range
2
-
3
- def begin
4
- `return self.beg;`
5
- end
6
-
7
- alias_method :first, :begin
8
-
9
- def end
10
- `return self.end;`
11
- end
12
-
13
- def to_s
14
- `var str = #{`self.beg`.to_s};
15
- var str2 = #{`self.end`.to_s};
16
- var join = self.exc ? '...' : '..';
17
- return str + join + str2;`
18
- end
19
-
20
- def inspect
21
- `var str = #{`self.beg`.inspect};
22
- var str2 = #{`self.end`.inspect};
23
- var join = self.exc ? '...' : '..';
24
- return str + join + str2;`
25
- end
26
- end
27
-