opal 0.3.26 → 0.3.27

Sign up to get free protection for your applications and to get access to all the features.
Files changed (74) hide show
  1. data/.gitignore +1 -6
  2. data/.travis.yml +1 -4
  3. data/Gemfile +1 -1
  4. data/README.md +3 -8
  5. data/Rakefile +26 -3
  6. data/core/array.rb +33 -91
  7. data/core/boolean.rb +1 -4
  8. data/core/class.rb +25 -71
  9. data/core/error.rb +5 -3
  10. data/core/hash.rb +3 -2
  11. data/core/kernel.rb +40 -1
  12. data/core/load_order +1 -1
  13. data/core/nil_class.rb +1 -3
  14. data/core/runtime.js +4 -44
  15. data/core/template.rb +20 -0
  16. data/core/{opal-spec → test_runner}/runner.js +0 -6
  17. data/lib/opal.rb +1 -12
  18. data/lib/opal/builder.rb +3 -10
  19. data/lib/opal/lexer.rb +1095 -1110
  20. data/lib/opal/parser.rb +229 -26
  21. data/lib/opal/rake_task.rb +3 -3
  22. data/lib/opal/version.rb +1 -1
  23. data/opal.gemspec +2 -2
  24. data/spec/core/array/assoc_spec.rb +2 -3
  25. data/spec/core/array/comparison_spec.rb +16 -0
  26. data/spec/core/array/constructor_spec.rb +0 -9
  27. data/spec/core/array/drop_spec.rb +21 -0
  28. data/spec/core/array/dup_spec.rb +15 -0
  29. data/spec/core/array/{eql_spec.rb → equal_value_spec.rb} +0 -0
  30. data/spec/core/array/index_spec.rb +26 -0
  31. data/spec/core/array/inspect_spec.rb +13 -0
  32. data/spec/core/array/intersection_spec.rb +22 -0
  33. data/spec/core/array/join_spec.rb +9 -0
  34. data/spec/core/array/keep_if_spec.rb +7 -0
  35. data/spec/core/array/minus_spec.rb +19 -9
  36. data/spec/core/array/multiply_spec.rb +13 -0
  37. data/spec/core/array/new_spec.rb +40 -0
  38. data/spec/core/array/rindex_spec.rb +21 -0
  39. data/spec/core/array/select_spec.rb +13 -0
  40. data/spec/core/array/shift_spec.rb +51 -0
  41. data/spec/core/array/slice_spec.rb +37 -0
  42. data/spec/core/array/take_spec.rb +21 -0
  43. data/spec/core/array/take_while_spec.rb +13 -0
  44. data/spec/core/array/to_a_spec.rb +7 -0
  45. data/spec/core/array/unshift_spec.rb +29 -0
  46. data/spec/core/hash/constructor_spec.rb +13 -0
  47. data/spec/core/hash/default_proc_spec.rb +20 -0
  48. data/spec/core/hash/default_spec.rb +8 -0
  49. data/spec/core/hash/delete_spec.rb +11 -0
  50. data/spec/core/hash/dup_spec.rb +10 -0
  51. data/spec/core/hash/reject_spec.rb +18 -0
  52. data/spec/core/hash/to_a_spec.rb +13 -0
  53. data/spec/core/kernel/Array_spec.rb +10 -0
  54. data/spec/core/kernel/class_spec.rb +6 -0
  55. data/spec/core/kernel/equal_spec.rb +12 -0
  56. data/spec/core/kernel/extend_spec.rb +21 -0
  57. data/spec/core/kernel/instance_eval_spec.rb +28 -0
  58. data/spec/core/kernel/instance_variable_get_spec.rb +14 -0
  59. data/spec/core/kernel/instance_variable_set_spec.rb +10 -0
  60. data/spec/core/kernel/match_spec.rb +5 -0
  61. data/spec/core/module/alias_method_spec.rb +10 -0
  62. data/spec/core/module/ancestors_spec.rb +11 -0
  63. data/spec/core/module/append_features_spec.rb +14 -0
  64. data/spec/core/proc/call_spec.rb +21 -0
  65. data/spec/core/proc/proc_tricks_spec.rb +1 -1
  66. data/spec/language/alias_spec.rb +1 -1
  67. data/spec/opal/class/instance_methods_spec.rb +13 -0
  68. data/spec/opal/kernel/attribute_spec.rb +57 -0
  69. data/spec/spec_helper.rb +1 -1
  70. data/spec/test_case.html +13 -0
  71. metadata +88 -12
  72. data/core/erb.rb +0 -32
  73. data/lib/opal/erb_parser.rb +0 -20
  74. data/spec/opal/erb/erb_spec.rb +0 -23
data/.gitignore CHANGED
@@ -3,9 +3,4 @@
3
3
  .yardoc
4
4
  .bundle
5
5
  Gemfile.lock
6
- pkg/*
7
- /gh-pages
8
- /examples/**/*.js
9
- /build
10
- Gemfile.local
11
- Gemfile.local.lock
6
+ build
data/.travis.yml CHANGED
@@ -2,7 +2,4 @@ language: ruby
2
2
 
3
3
  rvm:
4
4
  - 1.8.7
5
- - 1.9.3
6
-
7
- before_script:
8
- - "bundle exec rake opal"
5
+ - 1.9.3
data/Gemfile CHANGED
@@ -10,5 +10,5 @@ end
10
10
 
11
11
  # running tests on command line
12
12
  group :testing do
13
- gem 'opal-spec'
13
+ gem 'opal-spec', :git => 'git://github.com/opal/opal-spec.git'
14
14
  end
data/README.md CHANGED
@@ -14,16 +14,11 @@ See the website, [http://opal.github.com](http://opal.github.com).
14
14
 
15
15
  ## Running tests
16
16
 
17
- Build the runtime, tests and dependencies:
17
+ Build the runtime, tests and dependencies, and then run the tests all in one
18
+ go:
18
19
 
19
20
  ```
20
- rake opal
21
- ```
22
-
23
- Run tests using phantom.js runner:
24
-
25
- ```
26
- rake test
21
+ $ rake
27
22
  ```
28
23
 
29
24
  Alternatively, after building, you can open `spec/index.html` in any
data/Rakefile CHANGED
@@ -1,6 +1,4 @@
1
1
  require 'bundler/setup'
2
-
3
- require 'opal/version'
4
2
  require 'opal/rake_task'
5
3
 
6
4
  Opal::RakeTask.new do |t|
@@ -9,7 +7,8 @@ Opal::RakeTask.new do |t|
9
7
  t.parser = true # we want to also build opal-parser.js (used in specs)
10
8
  end
11
9
 
12
- task :default => 'opal:test'
10
+ # build runtime, dependencies and specs, then run the tests
11
+ task :default => %w[opal opal:test]
13
12
 
14
13
  desc "Check file sizes for opal.js runtime"
15
14
  task :sizes do
@@ -42,4 +41,28 @@ def gzip(str)
42
41
  i.close_write
43
42
  return i.read
44
43
  end
44
+ end
45
+
46
+ # For testing just specific sections of opal
47
+ desc "Build each test case"
48
+ task :test_cases do
49
+ FileUtils.mkdir_p 'build/test_cases'
50
+
51
+ sources = Dir['spec/core/*', 'spec/language', 'spec/lib', 'spec/opal']
52
+
53
+ sources.each do |c|
54
+ dest = "build/test_cases/#{File.basename c}"
55
+ FileUtils.mkdir_p dest
56
+ File.open("#{dest}/specs.js", "w+") do |out|
57
+ out.puts Opal.build_files(c)
58
+ end
59
+
60
+ File.open("#{dest}/index.html", "w+") do |out|
61
+ out.puts File.read("spec/test_case.html")
62
+ end
63
+ end
64
+
65
+ File.open("build/test_cases/runner.js", "w+") do |out|
66
+ out.puts Opal.parse(File.read("spec/spec_helper.rb"))
67
+ end
45
68
  end
data/core/array.rb CHANGED
@@ -11,7 +11,7 @@ class Array < `Array`
11
11
  }
12
12
  end
13
13
 
14
- def self.new(size, obj = nil)
14
+ def self.new(size, obj = nil, &block)
15
15
  %x{
16
16
  var arr = [];
17
17
 
@@ -21,8 +21,15 @@ class Array < `Array`
21
21
  }
22
22
  }
23
23
  else {
24
- for (var i = 0; i < size; i++) {
25
- arr[i] = obj;
24
+ if (block === nil) {
25
+ for (var i = 0; i < size; i++) {
26
+ arr[i] = obj;
27
+ }
28
+ }
29
+ else {
30
+ for (var i = 0; i < size; i++) {
31
+ arr[i] = block.call(__context, i);
32
+ }
26
33
  }
27
34
  }
28
35
 
@@ -63,7 +70,7 @@ class Array < `Array`
63
70
 
64
71
  var result = [];
65
72
 
66
- for (var i = 0, length = #{self}.length; i < length; i++) {
73
+ for (var i = 0; i < other; i++) {
67
74
  result = result.concat(#{self});
68
75
  }
69
76
 
@@ -121,7 +128,6 @@ class Array < `Array`
121
128
  }
122
129
  end
123
130
 
124
- # TODO: does not yet work with ranges
125
131
  def [](index, length = undefined)
126
132
  %x{
127
133
  var size = #{self}.length;
@@ -169,7 +175,6 @@ class Array < `Array`
169
175
  }
170
176
  end
171
177
 
172
- # TODO: need to expand functionality
173
178
  def []=(index, value)
174
179
  %x{
175
180
  var size = #{self}.length;
@@ -209,7 +214,7 @@ class Array < `Array`
209
214
  end
210
215
 
211
216
  def clear
212
- `#{self}.splice(0);`
217
+ `#{self}.splice(0, #{self}.length)`
213
218
 
214
219
  self
215
220
  end
@@ -365,22 +370,6 @@ class Array < `Array`
365
370
  `#{self}.slice(number)`
366
371
  end
367
372
 
368
- def drop_while(&block)
369
- %x{
370
- for (var i = 0, length = #{self}.length, value; i < length; i++) {
371
- if ((value = block(__context, #{self}[i])) === __breaker) {
372
- return __breaker.$v;
373
- }
374
-
375
- if (value === false || value === nil) {
376
- return #{self}.slice(i);
377
- }
378
- }
379
-
380
- return [];
381
- }
382
- end
383
-
384
373
  alias dup clone
385
374
 
386
375
  def each(&block)
@@ -399,14 +388,6 @@ class Array < `Array`
399
388
  self
400
389
  end
401
390
 
402
- def each_with_index(&block)
403
- `for (var i = 0, length = #{self}.length; i < length; i++) {`
404
- yield `#{self}[i]`, `i`
405
- `}`
406
-
407
- self
408
- end
409
-
410
391
  def empty?
411
392
  `!#{self}.length`
412
393
  end
@@ -481,22 +462,6 @@ class Array < `Array`
481
462
  }
482
463
  end
483
464
 
484
- def grep(pattern)
485
- %x{
486
- var result = [];
487
-
488
- for (var i = 0, length = #{self}.length, item; i < length; i++) {
489
- item = #{self}[i];
490
-
491
- if (#{ pattern === `item` }) {
492
- result.push(item);
493
- }
494
- }
495
-
496
- return result;
497
- }
498
- end
499
-
500
465
  def hash
501
466
  `#{self}._id || (#{self}._id = unique_id++)`
502
467
  end
@@ -513,11 +478,18 @@ class Array < `Array`
513
478
  }
514
479
  end
515
480
 
516
- def index(object, &block)
481
+ def index(object=undefined, &block)
517
482
  %x{
518
- if (block !== nil) {
483
+ if (object != null) {
484
+ for (var i = 0, length = #{self}.length; i < length; i++) {
485
+ if (#{`#{self}[i]` == object}) {
486
+ return i;
487
+ }
488
+ }
489
+ }
490
+ else if (block !== nil) {
519
491
  for (var i = 0, length = #{self}.length, value; i < length; i++) {
520
- if ((value = block.call(__context, '', #{self}[i])) === __breaker) {
492
+ if ((value = block.call(__context, #{self}[i])) === __breaker) {
521
493
  return __breaker.$v;
522
494
  }
523
495
 
@@ -526,41 +498,11 @@ class Array < `Array`
526
498
  }
527
499
  }
528
500
  }
529
- else {
530
- for (var i = 0, length = #{self}.length; i < length; i++) {
531
- if (#{`#{self}[i]` == object}) {
532
- return i;
533
- }
534
- }
535
- }
536
501
 
537
502
  return nil;
538
503
  }
539
504
  end
540
505
 
541
- def inject(initial, &block)
542
- %x{
543
- var result, i;
544
-
545
- if (initial == null) {
546
- result = #{self}[0], i = 1;
547
- }
548
- else {
549
- result = initial, i = 0;
550
- }
551
-
552
- for (var length = #{self}.length, value; i < length; i++) {
553
- if ((value = block(__context, result, #{self}[i])) === __breaker) {
554
- return __breaker.$v;
555
- }
556
-
557
- result = value;
558
- }
559
-
560
- return result;
561
- }
562
- end
563
-
564
506
  def insert(index, *objects)
565
507
  %x{
566
508
  if (objects.length > 0) {
@@ -619,7 +561,7 @@ class Array < `Array`
619
561
  def keep_if(&block)
620
562
  %x{
621
563
  for (var i = 0, length = #{self}.length, value; i < length; i++) {
622
- if ((value = block(__context, #{self}[i])) === __breaker) {
564
+ if ((value = block.call(__context, #{self}[i])) === __breaker) {
623
565
  return __breaker.$v;
624
566
  }
625
567
 
@@ -674,7 +616,7 @@ class Array < `Array`
674
616
  #{ raise "negative count given" };
675
617
  }
676
618
 
677
- return count > length ? #{self}.splice(0) : #{self}.splice(length - count, length);
619
+ return count > length ? #{self}.splice(0, #{self}.length) : #{self}.splice(length - count, length);
678
620
  }
679
621
  end
680
622
 
@@ -744,7 +686,7 @@ class Array < `Array`
744
686
 
745
687
  def replace(other)
746
688
  %x{
747
- #{self}.splice(0);
689
+ #{self}.splice(0, #{self}.length);
748
690
  #{self}.push.apply(#{self}, other);
749
691
  return #{self};
750
692
  }
@@ -770,7 +712,7 @@ class Array < `Array`
770
712
  %x{
771
713
  if (block !== nil) {
772
714
  for (var i = #{self}.length - 1, value; i >= 0; i--) {
773
- if ((value = block(__context, #{self}[i])) === __breaker) {
715
+ if ((value = block.call(__context, #{self}[i])) === __breaker) {
774
716
  return __breaker.$v;
775
717
  }
776
718
 
@@ -798,7 +740,7 @@ class Array < `Array`
798
740
  for (var i = 0, length = #{self}.length, item, value; i < length; i++) {
799
741
  item = #{self}[i];
800
742
 
801
- if ((value = block(__context, item)) === __breaker) {
743
+ if ((value = block.call(__context, item)) === __breaker) {
802
744
  return __breaker.$v;
803
745
  }
804
746
 
@@ -854,12 +796,12 @@ class Array < `Array`
854
796
  index += #{self}.length;
855
797
  }
856
798
 
857
- if (index < 0 || index >= #{self}.length) {
858
- return nil;
799
+ if (length != null) {
800
+ return #{self}.splice(index, length);
859
801
  }
860
802
 
861
- if (length != null) {
862
- return #{self}.splice(index, index + length);
803
+ if (index < 0 || index >= #{self}.length) {
804
+ return nil;
863
805
  }
864
806
 
865
807
  return #{self}.splice(index, 1)[0];
@@ -877,7 +819,7 @@ class Array < `Array`
877
819
  for (var i = 0, length = #{self}.length, item, value; i < length; i++) {
878
820
  item = #{self}[i];
879
821
 
880
- if ((value = block(__context, item)) === __breaker) {
822
+ if ((value = block.call(__context, item)) === __breaker) {
881
823
  return __breaker.$v;
882
824
  }
883
825
 
@@ -958,7 +900,7 @@ class Array < `Array`
958
900
 
959
901
  def unshift(*objects)
960
902
  %x{
961
- for (var i = 0, length = objects.length; i < length; i++) {
903
+ for (var i = objects.length - 1; i >= 0; i--) {
962
904
  #{self}.unshift(objects[i]);
963
905
  }
964
906
 
data/core/boolean.rb CHANGED
@@ -28,7 +28,4 @@ class Boolean < `Boolean`
28
28
  def to_s
29
29
  `(#{self} == true) ? 'true' : 'false'`
30
30
  end
31
- end
32
-
33
- TRUE = true
34
- FALSE = false
31
+ end
data/core/class.rb CHANGED
@@ -81,63 +81,12 @@ class Class
81
81
  self
82
82
  end
83
83
 
84
- # Private helper function to define attributes
85
- %x{
86
- function define_attr(klass, name, getter, setter) {
87
- if (getter) {
88
- klass.prototype['$' + name] = function() {
89
- var res = this[name];
90
- return res == null ? nil : res;
91
- };
92
-
93
- klass._donate([name]);
94
- }
95
-
96
- if (setter) {
97
- klass.prototype['$' + name + '='] = function(val) {
98
- return this[name] = val;
99
- };
100
-
101
- klass._donate([name]);
102
- }
103
- }
104
- }
105
-
106
- def attr_accessor(*attrs)
107
- %x{
108
- for (var i = 0, length = attrs.length; i < length; i++) {
109
- define_attr(#{self}, attrs[i], true, true);
110
- }
111
-
112
- return nil;
113
- }
114
- end
115
-
116
- def attr_reader(*attrs)
117
- %x{
118
- for (var i = 0, length = attrs.length; i < length; i++) {
119
- define_attr(#{self}, attrs[i], true, false);
120
- }
121
-
122
- return nil;
123
- }
124
- end
125
-
126
- def attr_writer(*attrs)
127
- %x{
128
- for (var i = 0, length = attrs.length; i < length; i++) {
129
- define_attr(#{self}, attrs[i], false, true);
130
- }
84
+ # handled by parser
85
+ def attr_accessor(*); end
131
86
 
132
- return nil;
133
- }
134
- end
135
-
136
- def attr(name, setter = false)
137
- `define_attr(#{self}, name, true, setter)`
138
-
139
- self
140
- end
87
+ alias attr_reader attr_accessor
88
+ alias attr_writer attr_accessor
89
+ alias attr attr_accessor
141
90
 
142
91
  def define_method(name, &block)
143
92
  %x{
@@ -175,9 +124,22 @@ class Class
175
124
  }
176
125
  end
177
126
 
178
- # FIXME
179
- def instance_methods
180
- []
127
+ def instance_methods(include_super = false)
128
+ %x{
129
+ var methods = [], proto = #{self}.prototype;
130
+
131
+ for (var prop in #{self}.prototype) {
132
+ if (!include_super && !proto.hasOwnProperty(prop)) {
133
+ continue;
134
+ }
135
+
136
+ if (prop.charAt(0) === '$') {
137
+ methods.push(prop.substr(1));
138
+ }
139
+ }
140
+
141
+ return methods;
142
+ }
181
143
  end
182
144
 
183
145
  def included(mod)
@@ -213,20 +175,12 @@ class Class
213
175
  }
214
176
  end
215
177
 
216
- def singleton_class
217
- %x{
218
- if (#{self}._singleton) {
219
- return #{self}._singleton;
220
- }
221
-
222
- var meta = new __opal.Class;
223
- #{self}._singleton = meta;
224
- meta.prototype = #{self};
225
-
226
- return meta;
227
- }
178
+ def public(*)
228
179
  end
229
180
 
181
+ alias private public
182
+ alias protected public
183
+
230
184
  def superclass
231
185
  %x{
232
186
  return #{self}._super || nil;