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/kernel.rb DELETED
@@ -1,251 +0,0 @@
1
- # The {Kernel} module is directly included into {Object} and provides a
2
- # lot of the core object functionality. It is not, however, included in
3
- # {BasicObject}.
4
- module Kernel
5
-
6
- def instance_variable_defined?(name)
7
- `name = #{name.to_s};
8
- return self[name.substr(1)] != undefined;`
9
- end
10
-
11
- def instance_variable_get(name)
12
- `name = #{name.to_s};
13
- return self[name.substr(1)] == undefined ? nil : self[name.substr(1)];`
14
- end
15
-
16
- def instance_variable_set(name, value)
17
- `name = #{name.to_s};
18
- return self[name.substr(1)] = value;`
19
- end
20
-
21
- # raw object flags (used by runtime)
22
- def __flags__
23
- `return self.$f;`
24
- end
25
-
26
- def hash
27
- `return self.$h();`
28
- end
29
-
30
- def to_a
31
- [self]
32
- end
33
-
34
- def tap
35
- raise LocalJumpError, "no block given" unless block_given?
36
- yield self
37
- self
38
- end
39
-
40
- def kind_of?(klass)
41
- `var search = self.$k;
42
-
43
- while (search) {
44
- if (search == klass) {
45
- return true;
46
- }
47
-
48
- search = search.$super;
49
- }
50
-
51
- return false;`
52
- end
53
-
54
- alias_method :is_a?, :kind_of?
55
-
56
- def nil?
57
- false
58
- end
59
-
60
- # Returns `true` if the method with the given id exists on the receiver,
61
- # `false` otherwise.
62
- #
63
- # Implementation Details
64
- # ----------------------
65
- # Opals' internals are constructed so that when a method is initially called,
66
- # a fake method is created on the root basic object, so that any subsequent
67
- # calls to that method on an object that has not defined it, will yield a
68
- # method_missing behaviour. For this reason, fake methods are tagged with a
69
- # `.$rbMM` property so that they will not be counted when this method checks
70
- # if a given method has been defined.
71
- #
72
- # @param [String, Symbol] method_id
73
- # @return [Boolean]
74
- def respond_to?(method_id)
75
- `var method = self['m$' + method_id];
76
-
77
-
78
- if (method ) {
79
- return true;
80
- }
81
-
82
- return false;`
83
- end
84
-
85
- def ===(other)
86
- self == other
87
- end
88
-
89
- def send(method_id, *args, &block)
90
- `var method = self['m$' + method_id];
91
-
92
- if ($B.f == arguments.callee) {
93
- $B.f = method;
94
- }
95
-
96
- return method.apply(self, args);`
97
- end
98
-
99
- def class
100
- `return rb_class_real(self.$k);`
101
- end
102
-
103
- def singleton_class
104
- `return rb_singleton_class(self);`
105
- end
106
-
107
- def methods
108
- `return self.$k.$methods;`
109
- end
110
-
111
- # Returns a random number. If max is `nil`, then the result is 0. Otherwise
112
- # returns a random number between 0 and max.
113
- #
114
- # @example
115
- #
116
- # rand # => 0.918378392234
117
- # rand # => 0.283842929289
118
- # rand 10 # => 9
119
- # rand 100 # => 21
120
- #
121
- # @param [Numeric] max
122
- # @return [Numeric]
123
- def rand(max = `undefined`)
124
- `if (max != undefined)
125
- return Math.floor(Math.random() * max);
126
- else
127
- return Math.random();`
128
- end
129
-
130
- def __id__
131
- `return self.$h();`
132
- end
133
-
134
- def object_id
135
- `return self.$h();`
136
- end
137
-
138
- # Returns a simple string representation of the receiver object. The id shown in the string
139
- # is not just the object_id, but it is mangled to resemble the format output by ruby, which
140
- # is basically a hex number.
141
- #
142
- # FIXME: proper hex output needed
143
- def to_s
144
- "#<#{`rb_class_real(self.$k)`}:0x#{`(self.$h() * 400487).toString(16)`}>"
145
- end
146
-
147
- def inspect
148
- to_s
149
- end
150
-
151
- def const_set(name, value)
152
- `return rb_const_set(rb_class_real(self.$k), name, value);`
153
- end
154
-
155
- def const_defined?(name)
156
- false
157
- end
158
-
159
- def =~(obj)
160
- nil
161
- end
162
-
163
- def extend(mod)
164
- `rb_extend_module(rb_singleton_class(self), mod);`
165
- nil
166
- end
167
-
168
- # Raises an exception. If given a string, this method will raise a
169
- # RuntimeError with the given string as a message. Otherwise, if the first
170
- # parameter is a subclass of Exception, then the method will raise a new
171
- # instance of the given exception class with the string as a message, if it
172
- # exists, or a fdefault message otherwise.
173
- #
174
- # @example String message
175
- #
176
- # raise "some error"
177
- # # => RuntimeError: some error
178
- #
179
- # @example Exception subclass
180
- #
181
- # raise StandardError, "something went wrong"
182
- # # => StandardError: something went wrong
183
- #
184
- # @param [Exception, String] exception
185
- # @param [String]
186
- # @return [nil]
187
- def raise(exception, string = nil)
188
- `var msg = nil, exc;
189
-
190
- if (exception.$f & T_STRING) {
191
- msg = exception;
192
- exc = #{RuntimeError.new `msg`};
193
- } else if (#{`exception`.kind_of? Exception}) {
194
- exc = exception;
195
- } else {
196
- if (string != nil) msg = string;
197
- exc = #{`exception`.new `msg`};
198
- }
199
- rb_raise_exc(exc);`
200
- end
201
-
202
- alias_method :fail, :raise
203
-
204
- # Repeatedly executes the given block.
205
- #
206
- # @example
207
- #
208
- # loop do
209
- # puts "this will infinetly repeat"
210
- # end
211
- #
212
- # @return [Object] returns the receiver.
213
- def loop
214
- `while (true) {
215
- #{yield};
216
- }
217
-
218
- return self;`
219
- end
220
-
221
- # Executed in reverse order
222
- def at_exit(&proc)
223
- raise ArgumentError, "called without a block" unless block_given?
224
- `rb_end_procs.push(proc);`
225
-
226
- proc
227
- end
228
-
229
- # Simple equivalent to `Proc.new`. Returns a new proc from the given block.
230
- #
231
- # @example
232
- #
233
- # proc { puts "a" }
234
- # # => #<Proc 02002>
235
- #
236
- # @return [Proc]
237
- def proc(&block)
238
- raise ArgumentError,
239
- "tried to create Proc object without a block" unless block_given?
240
- block
241
- end
242
-
243
- def lambda(&block)
244
- raise ArgumentError,
245
- "tried to create Proc object without a block" unless block_given?
246
- `return rb_make_lambda(block);`
247
- end
248
-
249
- # @endgroup
250
- end
251
-
data/corelib/load_order DELETED
@@ -1,21 +0,0 @@
1
- core
2
- object
3
- module
4
- class
5
- kernel
6
- top_self
7
- nil_class
8
- boolean
9
- enumerable
10
- array
11
- numeric
12
- hash
13
- error
14
- string
15
- proc
16
- range
17
- regexp
18
- match_data
19
- file
20
- dir
21
-
@@ -1,33 +0,0 @@
1
- class MatchData
2
-
3
- def inspect
4
- "#<MatchData #{`self.$data[0]`.inspect}>"
5
- end
6
-
7
- def to_s
8
- `return self.$data[0];`
9
- end
10
-
11
- def length
12
- `return self.$data.length;`
13
- end
14
-
15
- def size
16
- `return self.$data.length;`
17
- end
18
-
19
- def to_a
20
- `return [].slice.call(self.$data, 0);`
21
- end
22
-
23
- def [](index)
24
- `var length = self.$data.length;
25
-
26
- if (index < 0) index += length;
27
-
28
- if (index >= length || index < 0) return nil;
29
-
30
- return self.$data[index];`
31
- end
32
- end
33
-
data/corelib/module.rb DELETED
@@ -1,101 +0,0 @@
1
- # Implements the core functionality of modules. This is inherited from
2
- # by instances of {Class}, so these methods are also available to
3
- # classes.
4
- class Module
5
-
6
- def name
7
- `return self.__classid__;`
8
- end
9
-
10
- def ===(obj)
11
- obj.kind_of? self
12
- end
13
-
14
- def define_method(method_id, &block)
15
- raise LocalJumpError, "no block given" unless block_given?
16
- `$rb.dm(self, #{method_id.to_s}.toString(), block)`
17
- nil
18
- end
19
-
20
- def attr_accessor(*attrs)
21
- attr_reader *attrs
22
- attr_writer *attrs
23
- end
24
-
25
- def attr_reader(*attrs)
26
- attrs.each do |a|
27
- method_id = a.to_s
28
- `$rb.dm(self, method_id, function() {
29
- var iv = this[method_id];
30
- return iv == undefined ? nil : iv;
31
- });`
32
- end
33
- nil
34
- end
35
-
36
- def attr_writer(*attrs)
37
- attrs.each do |a|
38
- method_id = a.to_s
39
- `$rb.dm(self, method_id + '=', function(val) {
40
- return this[method_id] = val;
41
- });`
42
- end
43
- nil
44
- end
45
-
46
- def alias_method(new_name, old_name)
47
- `$rb.alias_method(self, #{new_name.to_s}, #{old_name.to_s});`
48
- self
49
- end
50
-
51
- def public_instance_methods(include_super = true)
52
- `return self.$methods;`
53
- end
54
-
55
- def instance_methods
56
- `return self.$methods;`
57
- end
58
-
59
- def ancestors
60
- `var ary = [], parent = self;
61
-
62
- while (parent) {
63
- if (parent.$f & FL_SINGLETON) {
64
- // nothing?
65
- }
66
- else {
67
- ary.push(parent);
68
- }
69
-
70
- parent = parent.$super;
71
- }
72
-
73
- return ary;`
74
- end
75
-
76
- def to_s
77
- `return self.__classid__;`
78
- end
79
-
80
- def const_set(id, value)
81
- `return rb_const_set(self, #{id.to_s}, value);`
82
- end
83
-
84
- def class_eval(str = nil, &block)
85
- if block_given?
86
- `block.call(self)`
87
- else
88
- raise "need to compile str"
89
- end
90
- end
91
-
92
- def module_eval(str = nil, &block)
93
- class_eval str, &block
94
- end
95
-
96
- def extend(mod)
97
- `rb_extend_module(self, mod)`
98
- nil
99
- end
100
- end
101
-
data/corelib/nil_class.rb DELETED
@@ -1,54 +0,0 @@
1
- # `NilClass` has a single instance `nil`. No more instances of this
2
- # class can be created, and attempts to do so will yield an error.
3
- #
4
- # Implementation details
5
- # ----------------------
6
- #
7
- # `nil` is an actual ruby object, and is not just a reference to the
8
- # native `null` or `undefined` values in javascript. Sending messages to
9
- # `nil` in ruby is a very useful feature of ruby, and this would not be
10
- # possible in opal if `nil` was just the `null` or `undefined` value.
11
- #
12
- # To access `nil` from javascript, `Qnil` points to this instance and is
13
- # available in both ruby and javascript sources loaded by opal.
14
- class NilClass
15
-
16
- def to_i
17
- 0
18
- end
19
-
20
- def to_f
21
- 0.0
22
- end
23
-
24
- def to_s
25
- ""
26
- end
27
-
28
- def to_a
29
- []
30
- end
31
-
32
- def inspect
33
- "nil"
34
- end
35
-
36
- def nil?
37
- true
38
- end
39
-
40
- def &(other)
41
- false
42
- end
43
-
44
- def |(other)
45
- `return other != false && other != nil;`
46
- end
47
-
48
- def ^(other)
49
- `return other != false && other != nil;`
50
- end
51
- end
52
-
53
- NIL = nil
54
-