opal 0.5.2 → 0.5.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (62) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +2 -0
  3. data/lib/opal.rb +0 -5
  4. data/lib/opal/compiler.rb +24 -44
  5. data/lib/opal/nodes/base.rb +5 -8
  6. data/lib/opal/nodes/call.rb +4 -0
  7. data/lib/opal/nodes/class.rb +6 -7
  8. data/lib/opal/nodes/def.rb +4 -4
  9. data/lib/opal/nodes/definitions.rb +0 -14
  10. data/lib/opal/nodes/iter.rb +51 -38
  11. data/lib/opal/nodes/literal.rb +21 -24
  12. data/lib/opal/nodes/module.rb +4 -4
  13. data/lib/opal/nodes/runtime_helpers.rb +45 -0
  14. data/lib/opal/nodes/scope.rb +280 -0
  15. data/lib/opal/nodes/singleton_class.rb +4 -5
  16. data/lib/opal/nodes/super.rb +1 -1
  17. data/lib/opal/nodes/top.rb +9 -7
  18. data/lib/opal/nodes/yield.rb +14 -3
  19. data/lib/opal/parser.rb +4 -18
  20. data/lib/opal/parser/grammar.rb +3745 -3667
  21. data/lib/opal/parser/grammar.y +1692 -1778
  22. data/lib/opal/parser/keywords.rb +35 -35
  23. data/lib/opal/parser/lexer.rb +356 -325
  24. data/lib/opal/parser/sexp.rb +1 -1
  25. data/lib/opal/version.rb +1 -1
  26. data/opal.gemspec +1 -0
  27. data/opal/core/array.rb +320 -81
  28. data/opal/core/enumerable.rb +46 -5
  29. data/opal/core/hash.rb +6 -64
  30. data/opal/core/helpers.rb +67 -0
  31. data/opal/core/method.rb +1 -1
  32. data/opal/core/module.rb +4 -4
  33. data/opal/core/range.rb +1 -12
  34. data/opal/core/regexp.rb +2 -8
  35. data/opal/core/runtime.js +74 -3
  36. data/opal/core/string.rb +99 -74
  37. data/opal/opal.rb +3 -72
  38. data/spec/filters/bugs/array.rb +2 -30
  39. data/spec/filters/bugs/basic_object.rb +0 -1
  40. data/spec/filters/bugs/string.rb +26 -21
  41. data/spec/filters/unsupported/enumerator.rb +3 -0
  42. data/spec/filters/unsupported/float.rb +1 -0
  43. data/spec/filters/unsupported/immutable_strings.rb +15 -0
  44. data/spec/filters/unsupported/tainted.rb +58 -30
  45. data/spec/filters/unsupported/trusted.rb +35 -15
  46. data/spec/opal/parser/class_spec.rb +4 -4
  47. data/spec/opal/parser/def_spec.rb +4 -4
  48. data/spec/opal/parser/lvar_spec.rb +6 -6
  49. data/spec/opal/parser/module_spec.rb +4 -4
  50. data/spec/opal/parser/sclass_spec.rb +2 -2
  51. data/spec/stdlib/native/exposure_spec.rb +33 -0
  52. data/stdlib/buffer.rb +1 -1
  53. data/stdlib/buffer/view.rb +1 -1
  54. data/stdlib/native.rb +193 -174
  55. data/stdlib/opal-parser.rb +0 -6
  56. data/stdlib/pp.rb +9 -0
  57. data/tasks/mspec.rake +3 -1
  58. metadata +9 -9
  59. data/lib/opal/nodes/base_scope.rb +0 -11
  60. data/lib/opal/target_scope.rb +0 -281
  61. data/spec/filters/20.rb +0 -4
  62. data/spec/filters/unsupported/array_subclasses.rb +0 -37
@@ -2,7 +2,7 @@ require 'buffer/array'
2
2
  require 'buffer/view'
3
3
 
4
4
  class Buffer
5
- include Native::Base
5
+ include Native
6
6
 
7
7
  def self.supported?
8
8
  not $$[:ArrayBuffer].nil?
@@ -1,7 +1,7 @@
1
1
  class Buffer
2
2
 
3
3
  class View
4
- include Native::Base
4
+ include Native
5
5
 
6
6
  def self.supported?
7
7
  not $$[:DataView].nil?
@@ -1,39 +1,3 @@
1
- module Kernel
2
- def native?(value)
3
- `value == null || !value._klass`
4
- end
5
-
6
- def Native(obj)
7
- if `#{obj} == null`
8
- nil
9
- elsif native?(obj)
10
- Native::Object.new(obj)
11
- else
12
- obj
13
- end
14
- end
15
-
16
- def Array(object, *args, &block)
17
- %x{
18
- if (object == null || object === nil) {
19
- return [];
20
- }
21
- else if (#{native?(object)}) {
22
- return #{Native::Array.new(object, *args, &block).to_a};
23
- }
24
- else if (#{object.respond_to? :to_ary}) {
25
- return #{object.to_ary};
26
- }
27
- else if (#{object.respond_to? :to_a}) {
28
- return #{object.to_a};
29
- }
30
- else {
31
- return [object];
32
- }
33
- }
34
- end
35
- end
36
-
37
1
  module Native
38
2
  def self.is_a?(object, klass)
39
3
  %x{
@@ -61,13 +25,17 @@ module Native
61
25
  end
62
26
 
63
27
  def self.convert(value)
64
- native = try_convert(value)
65
-
66
- if `#{native} === nil`
67
- raise ArgumentError, "the passed value isn't a native"
68
- end
69
-
70
- native
28
+ %x{
29
+ if (#{native?(value)}) {
30
+ return #{value};
31
+ }
32
+ else if (#{value.respond_to? :to_n}) {
33
+ return #{value.to_n};
34
+ }
35
+ else {
36
+ #{raise ArgumentError, "the passed value isn't a native"};
37
+ }
38
+ }
71
39
  end
72
40
 
73
41
  def self.call(obj, key, *args, &block)
@@ -142,187 +110,224 @@ module Native
142
110
  def to_n
143
111
  @native
144
112
  end
113
+ end
145
114
 
146
- class Object < BasicObject
147
- include Native
115
+ module Kernel
116
+ def native?(value)
117
+ `value == null || !value._klass`
118
+ end
148
119
 
149
- def ==(other)
150
- `#@native === #{Native.try_convert(other)}`
120
+ def Native(obj)
121
+ if `#{obj} == null`
122
+ nil
123
+ elsif native?(obj)
124
+ Native::Object.new(obj)
125
+ else
126
+ obj
151
127
  end
128
+ end
152
129
 
153
- def has_key?(name)
154
- `#@native.hasOwnProperty(#{name})`
155
- end
130
+ def Array(object, *args, &block)
131
+ %x{
132
+ if (object == null || object === nil) {
133
+ return [];
134
+ }
135
+ else if (#{native?(object)}) {
136
+ return #{Native::Array.new(object, *args, &block).to_a};
137
+ }
138
+ else if (#{object.respond_to? :to_ary}) {
139
+ return #{object.to_ary};
140
+ }
141
+ else if (#{object.respond_to? :to_a}) {
142
+ return #{object.to_a};
143
+ }
144
+ else {
145
+ return [object];
146
+ }
147
+ }
148
+ end
149
+ end
156
150
 
157
- alias key? has_key?
158
- alias include? has_key?
159
- alias member? has_key?
151
+ class Native::Object < BasicObject
152
+ include Native
160
153
 
161
- def each(*args)
162
- if block_given?
163
- %x{
164
- for (var key in #@native) {
165
- #{yield `key`, `#@native[key]`}
166
- }
167
- }
154
+ def ==(other)
155
+ `#@native === #{Native.try_convert(other)}`
156
+ end
168
157
 
169
- self
170
- else
171
- method_missing(:each, *args)
172
- end
173
- end
158
+ def has_key?(name)
159
+ `#@native.hasOwnProperty(#{name})`
160
+ end
174
161
 
175
- def [](key)
176
- %x{
177
- var prop = #@native[key];
162
+ alias key? has_key?
163
+ alias include? has_key?
164
+ alias member? has_key?
178
165
 
179
- if (prop instanceof Function) {
180
- return prop;
181
- }
182
- else {
183
- return #{::Native.call(@native, key)}
166
+ def each(*args)
167
+ if block_given?
168
+ %x{
169
+ for (var key in #@native) {
170
+ #{yield `key`, `#@native[key]`}
184
171
  }
185
172
  }
186
- end
187
-
188
- def []=(key, value)
189
- native = Native.try_convert(value)
190
173
 
191
- if `#{native} === nil`
192
- `#@native[key] = #{value}`
193
- else
194
- `#@native[key] = #{native}`
195
- end
174
+ self
175
+ else
176
+ method_missing(:each, *args)
196
177
  end
178
+ end
197
179
 
198
- def method_missing(mid, *args, &block)
199
- %x{
200
- if (mid.charAt(mid.length - 1) === '=') {
201
- return #{self[mid.slice(0, mid.length - 1)] = args[0]};
202
- }
203
- else {
204
- return #{::Native.call(@native, mid, *args, &block)};
205
- }
180
+ def [](key)
181
+ %x{
182
+ var prop = #@native[key];
183
+
184
+ if (prop instanceof Function) {
185
+ return prop;
206
186
  }
207
- end
187
+ else {
188
+ return #{::Native.call(@native, key)}
189
+ }
190
+ }
191
+ end
208
192
 
209
- def nil?
210
- false
211
- end
193
+ def []=(key, value)
194
+ native = Native.try_convert(value)
212
195
 
213
- def is_a?(klass)
214
- klass == Native
196
+ if `#{native} === nil`
197
+ `#@native[key] = #{value}`
198
+ else
199
+ `#@native[key] = #{native}`
215
200
  end
201
+ end
216
202
 
217
- alias kind_of? is_a?
203
+ def method_missing(mid, *args, &block)
204
+ %x{
205
+ if (mid.charAt(mid.length - 1) === '=') {
206
+ return #{self[mid.slice(0, mid.length - 1)] = args[0]};
207
+ }
208
+ else {
209
+ return #{::Native.call(@native, mid, *args, &block)};
210
+ }
211
+ }
212
+ end
218
213
 
219
- def instance_of?(klass)
220
- klass == Native
221
- end
214
+ def nil?
215
+ false
216
+ end
222
217
 
223
- def class
224
- `self._klass`
225
- end
218
+ def is_a?(klass)
219
+ klass == Native
220
+ end
226
221
 
227
- def to_a(options = {}, &block)
228
- Native::Array.new(@native, options, &block).to_a
229
- end
222
+ alias kind_of? is_a?
230
223
 
231
- def to_ary(options = {}, &block)
232
- Native::Array.new(@native, options, &block)
233
- end
224
+ def instance_of?(klass)
225
+ klass == Native
226
+ end
234
227
 
235
- def inspect
236
- "#<Native:#{`String(#@native)`}>"
237
- end
228
+ def class
229
+ `self._klass`
230
+ end
231
+
232
+ def to_a(options = {}, &block)
233
+ Native::Array.new(@native, options, &block).to_a
238
234
  end
239
235
 
240
- class Array
241
- include Native
242
- include Enumerable
236
+ def to_ary(options = {}, &block)
237
+ Native::Array.new(@native, options, &block)
238
+ end
243
239
 
244
- def initialize(native, options = {}, &block)
245
- super(native)
240
+ def inspect
241
+ "#<Native:#{`String(#@native)`}>"
242
+ end
243
+ end
246
244
 
247
- @get = options[:get] || options[:access]
248
- @named = options[:named]
249
- @set = options[:set] || options[:access]
250
- @length = options[:length] || :length
251
- @block = block
245
+ class Native::Array
246
+ include Native
247
+ include Enumerable
252
248
 
253
- if `#{length} == null`
254
- raise ArgumentError, "no length found on the array-like object"
255
- end
256
- end
249
+ def initialize(native, options = {}, &block)
250
+ super(native)
257
251
 
258
- def each(&block)
259
- return enum_for :each unless block
252
+ @get = options[:get] || options[:access]
253
+ @named = options[:named]
254
+ @set = options[:set] || options[:access]
255
+ @length = options[:length] || :length
256
+ @block = block
260
257
 
261
- index = 0
262
- length = self.length
258
+ if `#{length} == null`
259
+ raise ArgumentError, "no length found on the array-like object"
260
+ end
261
+ end
263
262
 
264
- while index < length
265
- block.call(self[index])
263
+ def each(&block)
264
+ return enum_for :each unless block
266
265
 
267
- index += 1
268
- end
266
+ %x{
267
+ for (var i = 0, length = #{length}; i < length; i++) {
268
+ var value = $opal.$yield1(block, #{self[`i`]});
269
269
 
270
- self
271
- end
270
+ if (value === $breaker) {
271
+ return $breaker.$v;
272
+ }
273
+ }
274
+ }
272
275
 
273
- def [](index)
274
- result = case index
275
- when String, Symbol
276
- @named ? `#@native[#@named](#{index})` : `#@native[#{index}]`
276
+ self
277
+ end
277
278
 
278
- when Integer
279
- @get ? `#@native[#@get](#{index})` : `#@native[#{index}]`
280
- end
279
+ def [](index)
280
+ result = case index
281
+ when String, Symbol
282
+ @named ? `#@native[#@named](#{index})` : `#@native[#{index}]`
281
283
 
282
- if result
283
- if @block
284
- @block.call(result)
285
- else
286
- Native(result)
287
- end
288
- end
284
+ when Integer
285
+ @get ? `#@native[#@get](#{index})` : `#@native[#{index}]`
289
286
  end
290
287
 
291
- def []=(index, value)
292
- if @set
293
- `#@native[#@set](#{index}, #{value})`
288
+ if result
289
+ if @block
290
+ @block.call(result)
294
291
  else
295
- `#@native[#{index}] = #{value}`
292
+ Native(result)
296
293
  end
297
294
  end
295
+ end
298
296
 
299
- def last(count = nil)
300
- if count
301
- index = length - 1
302
- result = []
297
+ def []=(index, value)
298
+ if @set
299
+ `#@native[#@set](#{index}, #{Native.convert(value)})`
300
+ else
301
+ `#@native[#{index}] = #{Native.convert(value)}`
302
+ end
303
+ end
303
304
 
304
- while index >= 0
305
- result << self[index]
306
- index -= 1
307
- end
305
+ def last(count = nil)
306
+ if count
307
+ index = length - 1
308
+ result = []
308
309
 
309
- result
310
- else
311
- self[length - 1]
310
+ while index >= 0
311
+ result << self[index]
312
+ index -= 1
312
313
  end
313
- end
314
314
 
315
- def length
316
- `#@native[#@length]`
315
+ result
316
+ else
317
+ self[length - 1]
317
318
  end
319
+ end
318
320
 
319
- def to_ary
320
- self
321
- end
321
+ def length
322
+ `#@native[#@length]`
323
+ end
322
324
 
323
- def inspect
324
- to_a.inspect
325
- end
325
+ def to_ary
326
+ self
327
+ end
328
+
329
+ def inspect
330
+ to_a.inspect
326
331
  end
327
332
  end
328
333
 
@@ -479,5 +484,19 @@ class Hash
479
484
  end
480
485
  end
481
486
 
487
+ class Module
488
+ def native_module
489
+ `Opal.global[#{self.name}] = #{self}`
490
+ end
491
+ end
492
+
493
+ class Class
494
+ def native_alias(jsid, mid)
495
+ `#{self}._proto[#{jsid}] = #{self}._proto['$' + #{mid}]`
496
+ end
497
+
498
+ alias native_class native_module
499
+ end
500
+
482
501
  # native global
483
502
  $$ = $global = Native(`Opal.global`)