opal 0.5.0 → 0.5.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (55) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile +0 -2
  3. data/README.md +1 -1
  4. data/Rakefile +11 -8
  5. data/lib/opal.rb +1 -1
  6. data/lib/opal/version.rb +1 -1
  7. data/opal.gemspec +1 -1
  8. data/{corelib → opal/core}/array.rb +47 -40
  9. data/{corelib → opal/core}/basic_object.rb +4 -0
  10. data/{corelib → opal/core}/boolean.rb +2 -6
  11. data/{corelib → opal/core}/class.rb +0 -0
  12. data/{corelib → opal/core}/comparable.rb +0 -0
  13. data/{corelib → opal/core}/encoding.rb +0 -0
  14. data/{corelib → opal/core}/enumerable.rb +160 -38
  15. data/opal/core/enumerator.rb +416 -0
  16. data/{corelib → opal/core}/error.rb +0 -0
  17. data/{corelib → opal/core}/hash.rb +38 -48
  18. data/{corelib → opal/core}/io.rb +13 -9
  19. data/{corelib → opal/core}/kernel.rb +75 -49
  20. data/{corelib → opal/core}/main.rb +0 -0
  21. data/{corelib → opal/core}/match_data.rb +0 -4
  22. data/{corelib → opal/core}/method.rb +0 -0
  23. data/{corelib → opal/core}/module.rb +24 -3
  24. data/{corelib → opal/core}/nil_class.rb +0 -4
  25. data/{corelib → opal/core}/numeric.rb +3 -4
  26. data/{corelib → opal/core}/proc.rb +0 -4
  27. data/{corelib → opal/core}/range.rb +0 -0
  28. data/{corelib → opal/core}/regexp.rb +0 -4
  29. data/{corelib → opal/core}/runtime.js +0 -0
  30. data/{corelib → opal/core}/string.rb +4 -6
  31. data/{corelib → opal/core}/struct.rb +3 -21
  32. data/{corelib → opal/core}/time.rb +0 -4
  33. data/opal/opal.rb +121 -0
  34. data/spec/corelib/array/select_spec.rb +14 -0
  35. data/spec/filters/20.rb +4 -0
  36. data/spec/filters/bugs/enumerable.rb +1 -48
  37. data/spec/filters/unsupported/enumerator.rb +13 -0
  38. data/spec/opal/compiler/irb_spec.rb +1 -0
  39. data/spec/rubyspecs +1 -0
  40. data/spec/{corelib → stdlib}/native/alias_native_spec.rb +6 -4
  41. data/spec/{corelib → stdlib}/native/each_spec.rb +3 -1
  42. data/spec/{corelib → stdlib}/native/element_reference_spec.rb +3 -1
  43. data/spec/stdlib/native/ext_spec.rb +19 -0
  44. data/spec/{corelib → stdlib}/native/initialize_spec.rb +4 -4
  45. data/spec/{corelib → stdlib}/native/method_missing_spec.rb +13 -1
  46. data/spec/{corelib → stdlib}/native/new_spec.rb +3 -1
  47. data/stdlib/enumerator.rb +1 -0
  48. data/stdlib/json.rb +1 -1
  49. data/stdlib/native.rb +483 -0
  50. metadata +52 -47
  51. data/corelib/enumerator.rb +0 -55
  52. data/corelib/native.rb +0 -270
  53. data/corelib/opal.rb +0 -88
  54. data/spec/corelib/native/ext_spec.rb +0 -5
  55. data/spec/filters/bugs/enumerator.rb +0 -6
@@ -38,32 +38,36 @@ class IO
38
38
  end
39
39
  end
40
40
 
41
- STDERR = $stderr = IO.new
42
- STDIN = $stdin = IO.new
43
- STDOUT = $stdout = IO.new
41
+ STDERR = $stderr = IO.new
42
+ STDIN = $stdin = IO.new
43
+ STDOUT = $stdout = IO.new
44
44
 
45
45
  def $stdout.puts(*strs)
46
46
  %x{
47
47
  for (var i = 0; i < strs.length; i++) {
48
- if(strs[i] instanceof Array) {
49
- #{ puts(*`strs[i]`) }
50
- } else {
48
+ if (strs[i] instanceof Array) {
49
+ #{puts(*`strs[i]`)};
50
+ }
51
+ else {
51
52
  console.log(#{`strs[i]`.to_s});
52
53
  }
53
54
  }
54
55
  }
56
+
55
57
  nil
56
58
  end
57
59
 
58
60
  def $stderr.puts(*strs)
59
61
  %x{
60
62
  for (var i = 0; i < strs.length; i++) {
61
- if(strs[i] instanceof Array) {
62
- #{ puts(*`strs[i]`) }
63
- } else {
63
+ if (strs[i] instanceof Array) {
64
+ #{puts(*`strs[i]`)};
65
+ }
66
+ else {
64
67
  console.warn(#{`strs[i]`.to_s});
65
68
  }
66
69
  }
67
70
  }
71
+
68
72
  nil
69
73
  end
@@ -36,16 +36,19 @@ module Kernel
36
36
  def methods(all = true)
37
37
  %x{
38
38
  var methods = [];
39
- for(var k in self) {
40
- if(k[0] == "$" && typeof (self)[k] === "function") {
41
- if(all === #{false} || all === #{nil}) {
42
- if(!Object.hasOwnProperty.call(self, k)) {
39
+
40
+ for (var key in self) {
41
+ if (key[0] == "$" && typeof(self[key]) === "function") {
42
+ if (all == false || all === nil) {
43
+ if (!$opal.hasOwnProperty.call(self, key)) {
43
44
  continue;
44
45
  }
45
46
  }
46
- methods.push(k.substr(1));
47
+
48
+ methods.push(key.substr(1));
47
49
  }
48
50
  }
51
+
49
52
  return methods;
50
53
  }
51
54
  end
@@ -55,9 +58,6 @@ module Kernel
55
58
  if (object == null || object === nil) {
56
59
  return [];
57
60
  }
58
- else if (#{native?(object)}) {
59
- return #{Native::Array.new(object, *args, &block).to_a};
60
- }
61
61
  else if (#{object.respond_to? :to_ary}) {
62
62
  return #{object.to_ary};
63
63
  }
@@ -80,18 +80,44 @@ module Kernel
80
80
  `self._klass`
81
81
  end
82
82
 
83
- def define_singleton_method(name, &body)
83
+ def copy_instance_variables(other)
84
84
  %x{
85
- if (body === nil) {
86
- throw new Error("no block given");
85
+ for (var name in other) {
86
+ if (name.charAt(0) !== '$') {
87
+ if (name !== '_id' && name !== '_klass') {
88
+ self[name] = other[name];
89
+ }
90
+ }
87
91
  }
92
+ }
93
+ end
94
+
95
+ def clone
96
+ copy = self.class.allocate
88
97
 
98
+ copy.copy_instance_variables(self)
99
+ copy.initialize_clone(self)
100
+
101
+ copy
102
+ end
103
+
104
+ def initialize_clone(other)
105
+ initialize_copy(other)
106
+ end
107
+ private :initialize_clone
108
+
109
+ def define_singleton_method(name, &body)
110
+ unless body
111
+ raise ArgumentError, "tried to create Proc object without a block"
112
+ end
113
+
114
+ %x{
89
115
  var jsid = '$' + name;
90
116
  body._jsid = name;
91
117
  body._s = null;
92
118
  body._def = body;
93
119
 
94
- #{self.singleton_class}._proto[jsid] = body;
120
+ #{singleton_class}._proto[jsid] = body;
95
121
 
96
122
  return self;
97
123
  }
@@ -100,22 +126,19 @@ module Kernel
100
126
  def dup
101
127
  copy = self.class.allocate
102
128
 
103
- %x{
104
- for (var name in self) {
105
- if (name.charAt(0) !== '$') {
106
- if (name !== '_id' && name !== '_klass') {
107
- copy[name] = self[name];
108
- }
109
- }
110
- }
111
- }
129
+ copy.copy_instance_variables(self)
130
+ copy.initialize_dup(self)
112
131
 
113
- copy.initialize_copy self
114
132
  copy
115
133
  end
116
134
 
117
- def enum_for(method = :each, *args)
118
- Enumerator.new self, method, *args
135
+ def initialize_dup(other)
136
+ initialize_copy(other)
137
+ end
138
+ private :initialize_dup
139
+
140
+ def enum_for(method = :each, *args, &block)
141
+ Enumerator.for(self, method, *args, &block)
119
142
  end
120
143
 
121
144
  def equal?(other)
@@ -369,9 +392,13 @@ module Kernel
369
392
  end
370
393
 
371
394
  def loop(&block)
372
- `while (true) {`
373
- yield
374
- `}`
395
+ %x{
396
+ while (true) {
397
+ if (block() === $breaker) {
398
+ return $breaker.$v;
399
+ }
400
+ }
401
+ }
375
402
 
376
403
  self
377
404
  end
@@ -380,15 +407,13 @@ module Kernel
380
407
  false
381
408
  end
382
409
 
383
- def object_id
384
- `self._id || (self._id = Opal.uid())`
385
- end
410
+ alias object_id __id__
386
411
 
387
412
  def printf(*args)
388
413
  if args.length > 0
389
- fmt = args.shift
390
- print format(fmt, *args)
414
+ print format(*args)
391
415
  end
416
+
392
417
  nil
393
418
  end
394
419
 
@@ -397,13 +422,12 @@ module Kernel
397
422
  end
398
423
 
399
424
  def proc(&block)
400
- %x{
401
- if (block === nil) {
402
- #{ raise ArgumentError, 'no block given' };
403
- }
404
- block.is_lambda = false;
405
- return block;
406
- }
425
+ unless block
426
+ raise ArgumentError, "tried to create Proc object without a block"
427
+ end
428
+
429
+ `block.is_lambda = false`
430
+ block
407
431
  end
408
432
 
409
433
  def puts(*strs)
@@ -428,7 +452,7 @@ module Kernel
428
452
  if (exception == null && #$!) {
429
453
  exception = #$!;
430
454
  }
431
- else if (typeof(exception) === 'string') {
455
+ else if (exception._isString) {
432
456
  exception = #{RuntimeError.new exception};
433
457
  }
434
458
  else if (!#{exception.is_a? Exception}) {
@@ -443,15 +467,17 @@ module Kernel
443
467
 
444
468
  def rand(max = undefined)
445
469
  %x{
446
- if(!max) {
470
+ if (max === undefined) {
447
471
  return Math.random();
448
- } else {
449
- if (max._isRange) {
450
- var arr = max.$to_a();
451
- return arr[#{rand(`arr.length`)}];
452
- } else {
453
- return Math.floor(Math.random() * Math.abs(parseInt(max)));
454
- }
472
+ }
473
+ else if (max._isRange) {
474
+ var arr = #{max.to_a};
475
+
476
+ return arr[#{rand(`arr.length`)}];
477
+ }
478
+ else {
479
+ return Math.floor(Math.random() *
480
+ Math.abs(#{Opal.coerce_to max, Integer, :to_int}));
455
481
  }
456
482
  }
457
483
  end
@@ -533,7 +559,7 @@ module Kernel
533
559
  end
534
560
 
535
561
  def to_s
536
- `"#<" + self._klass._name + ":" + self._id + ">"`
562
+ `"#<" + #{self.class.name} + ":" + self._id + ">"`
537
563
  end
538
564
 
539
565
  def freeze
File without changes
@@ -85,10 +85,6 @@ class MatchData
85
85
  `#@matches[0]`
86
86
  end
87
87
 
88
- def to_n
89
- @matches
90
- end
91
-
92
88
  def values_at(*indexes)
93
89
  %x{
94
90
  var values = [],
File without changes
@@ -366,8 +366,24 @@ class Module
366
366
  end
367
367
 
368
368
  alias class_eval module_eval
369
- alias class_exec module_eval
370
- alias module_exec module_eval
369
+
370
+ def module_exec(&block)
371
+ %x{
372
+ if (block === nil) {
373
+ throw new Error("no block given");
374
+ }
375
+
376
+ var block_self = block._s, result;
377
+
378
+ block._s = null;
379
+ result = block.apply(self, $slice.call(arguments));
380
+ block._s = block_self;
381
+
382
+ return result;
383
+ }
384
+ end
385
+
386
+ alias class_exec module_exec
371
387
 
372
388
  def method_defined?(method)
373
389
  %x{
@@ -428,12 +444,17 @@ class Module
428
444
  alias private public
429
445
  alias protected public
430
446
 
447
+ def private_method_defined?(obj)
448
+ false
449
+ end
450
+
451
+ alias protected_method_defined? private_method_defined?
452
+
431
453
  alias public_instance_methods instance_methods
432
454
 
433
455
  alias public_method_defined? method_defined?
434
456
 
435
457
  def remove_class_variable(*)
436
-
437
458
  end
438
459
 
439
460
  def remove_const(name)
@@ -45,10 +45,6 @@ class NilClass
45
45
 
46
46
  alias to_f to_i
47
47
 
48
- def to_n
49
- `null`
50
- end
51
-
52
48
  def to_s
53
49
  ''
54
50
  end
@@ -384,10 +384,6 @@ class Numeric
384
384
  [q, r]
385
385
  end
386
386
 
387
- def to_n
388
- `self.valueOf()`
389
- end
390
-
391
387
  def upto(finish, &block)
392
388
  return enum_for :upto, finish unless block
393
389
 
@@ -440,4 +436,7 @@ class Float < Numeric
440
436
  def self.===(other)
441
437
  `!!(other._isNumber && (other % 1) != 0)`
442
438
  end
439
+
440
+ INFINITY = `Infinity`
441
+ NAN = `NaN`
443
442
  end
@@ -49,8 +49,4 @@ class Proc
49
49
  def arity
50
50
  `self.length`
51
51
  end
52
-
53
- def to_n
54
- self
55
- end
56
52
  end
File without changes
@@ -117,8 +117,4 @@ class Regexp
117
117
  end
118
118
 
119
119
  alias to_s source
120
-
121
- def to_n
122
- `self.valueOf()`
123
- end
124
120
  end
File without changes
@@ -267,7 +267,7 @@ class String
267
267
  def end_with?(*suffixes)
268
268
  %x{
269
269
  for (var i = 0, length = suffixes.length; i < length; i++) {
270
- var suffix = suffixes[i];
270
+ var suffix = #{Opal.coerce_to `suffixes[i]`, String, :to_str};
271
271
 
272
272
  if (self.length >= suffix.length && self.substr(0 - suffix.length) === suffix) {
273
273
  return true;
@@ -563,7 +563,9 @@ class String
563
563
  def start_with?(*prefixes)
564
564
  %x{
565
565
  for (var i = 0, length = prefixes.length; i < length; i++) {
566
- if (#{self}.indexOf(prefixes[i]) === 0) {
566
+ var prefix = #{Opal.coerce_to `prefixes[i]`, String, :to_str};
567
+
568
+ if (self.indexOf(prefix) === 0) {
567
569
  return true;
568
570
  }
569
571
  }
@@ -713,10 +715,6 @@ class String
713
715
 
714
716
  alias to_sym intern
715
717
 
716
- def to_n
717
- `#{self}.valueOf()`
718
- end
719
-
720
718
  def tr(from, to)
721
719
  %x{
722
720
  if (from.length == 0 || from === to) {
@@ -52,17 +52,9 @@ class Struct
52
52
  include Enumerable
53
53
 
54
54
  def initialize(*args)
55
- if args.length == 1 && native?(args[0])
56
- object = args[0]
57
-
58
- members.each {|name|
59
- instance_variable_set "@#{name}", Native(`#{object}[#{name}]`)
60
- }
61
- else
62
- members.each_with_index {|name, index|
63
- instance_variable_set "@#{name}", args[index]
64
- }
65
- end
55
+ members.each_with_index {|name, index|
56
+ instance_variable_set "@#{name}", args[index]
57
+ }
66
58
  end
67
59
 
68
60
  def members
@@ -123,16 +115,6 @@ class Struct
123
115
 
124
116
  alias values to_a
125
117
 
126
- def to_n
127
- result = `{}`
128
-
129
- each_pair {|name, value|
130
- `#{result}[#{name}] = #{value.to_n}`
131
- }
132
-
133
- result
134
- end
135
-
136
118
  def inspect
137
119
  result = "#<struct "
138
120
 
@@ -508,10 +508,6 @@ class Time
508
508
  def year
509
509
  `self.getFullYear()`
510
510
  end
511
-
512
- def to_n
513
- self
514
- end
515
511
  end
516
512
 
517
513
  # FIXME: move this to stdlib when the corelib has its own path