opal 0.3.22 → 0.3.25

Sign up to get free protection for your applications and to get access to all the features.
Files changed (104) hide show
  1. data/.travis.yml +1 -1
  2. data/Gemfile +1 -9
  3. data/README.md +46 -5
  4. data/Rakefile +4 -94
  5. data/core/array.rb +13 -46
  6. data/core/basic_object.rb +2 -12
  7. data/core/boolean.rb +1 -1
  8. data/core/browser.js +27 -1
  9. data/core/class.rb +1 -7
  10. data/core/enumerable.rb +3 -15
  11. data/core/error.rb +2 -4
  12. data/core/hash.rb +9 -18
  13. data/core/json.rb +5 -0
  14. data/core/kernel.rb +2 -21
  15. data/core/load_order +5 -7
  16. data/core/nil_class.rb +4 -0
  17. data/core/numeric.rb +0 -6
  18. data/core/object.rb +1 -5
  19. data/core/proc.rb +0 -4
  20. data/core/range.rb +10 -17
  21. data/core/regexp.rb +2 -6
  22. data/core/runtime.js +2 -26
  23. data/core/string.rb +28 -26
  24. data/core/struct.rb +0 -4
  25. data/core/{erb.rb → templates.rb} +6 -4
  26. data/core/time.rb +7 -19
  27. data/core/top_self.rb +2 -2
  28. data/lib/opal.rb +13 -2
  29. data/lib/opal/builder.rb +3 -3
  30. data/lib/opal/erb_parser.rb +3 -2
  31. data/lib/opal/parser.rb +10 -2
  32. data/lib/opal/rake_task.rb +10 -2
  33. data/lib/opal/scope.rb +6 -1
  34. data/lib/opal/version.rb +1 -1
  35. data/opal.gemspec +1 -3
  36. data/spec/core/array/constructor_spec.rb +0 -8
  37. data/spec/core/enumerable/drop_while_spec.rb +0 -4
  38. data/spec/core/enumerable/find_index_spec.rb +0 -4
  39. data/spec/core/enumerable/select_spec.rb +13 -0
  40. data/spec/core/kernel/respond_to_spec.rb +4 -0
  41. data/spec/core/string/empty_spec.rb +0 -1
  42. data/spec/core/string/include_spec.rb +0 -6
  43. data/spec/core/string/swapcase_spec.rb +0 -5
  44. data/spec/core/string/to_s_spec.rb +0 -7
  45. data/spec/core/string/to_str_spec.rb +0 -7
  46. data/spec/lib/erb_parser/simple.erb +1 -0
  47. data/spec/{grammar → lib/grammar}/alias_spec.rb +0 -0
  48. data/spec/{grammar → lib/grammar}/and_spec.rb +0 -0
  49. data/spec/{grammar → lib/grammar}/array_spec.rb +0 -0
  50. data/spec/{grammar → lib/grammar}/attrasgn_spec.rb +0 -0
  51. data/spec/{grammar → lib/grammar}/begin_spec.rb +0 -0
  52. data/spec/{grammar → lib/grammar}/block_spec.rb +0 -0
  53. data/spec/{grammar → lib/grammar}/break_spec.rb +0 -0
  54. data/spec/{grammar → lib/grammar}/call_spec.rb +0 -0
  55. data/spec/{grammar → lib/grammar}/class_spec.rb +0 -0
  56. data/spec/{grammar → lib/grammar}/const_spec.rb +0 -0
  57. data/spec/{grammar → lib/grammar}/cvar_spec.rb +0 -0
  58. data/spec/{grammar → lib/grammar}/def_spec.rb +0 -0
  59. data/spec/{grammar → lib/grammar}/false_spec.rb +0 -0
  60. data/spec/{grammar → lib/grammar}/file_spec.rb +0 -0
  61. data/spec/{grammar → lib/grammar}/gvar_spec.rb +0 -0
  62. data/spec/{grammar → lib/grammar}/hash_spec.rb +0 -0
  63. data/spec/{grammar → lib/grammar}/iasgn_spec.rb +0 -0
  64. data/spec/{grammar → lib/grammar}/if_spec.rb +0 -0
  65. data/spec/{grammar → lib/grammar}/iter_spec.rb +0 -0
  66. data/spec/{grammar → lib/grammar}/ivar_spec.rb +0 -0
  67. data/spec/{grammar → lib/grammar}/lambda_spec.rb +0 -0
  68. data/spec/{grammar → lib/grammar}/lasgn_spec.rb +0 -0
  69. data/spec/{grammar → lib/grammar}/line_spec.rb +0 -0
  70. data/spec/{grammar → lib/grammar}/lvar_spec.rb +0 -0
  71. data/spec/{grammar → lib/grammar}/masgn_spec.rb +0 -0
  72. data/spec/{grammar → lib/grammar}/module_spec.rb +0 -0
  73. data/spec/{grammar → lib/grammar}/nil_spec.rb +0 -0
  74. data/spec/{grammar → lib/grammar}/not_spec.rb +0 -0
  75. data/spec/{grammar → lib/grammar}/op_asgn1_spec.rb +0 -0
  76. data/spec/{grammar → lib/grammar}/op_asgn2_spec.rb +0 -0
  77. data/spec/{grammar → lib/grammar}/or_spec.rb +0 -0
  78. data/spec/{grammar → lib/grammar}/return_spec.rb +0 -0
  79. data/spec/{grammar → lib/grammar}/sclass_spec.rb +0 -0
  80. data/spec/{grammar → lib/grammar}/self_spec.rb +0 -0
  81. data/spec/{grammar → lib/grammar}/str_spec.rb +0 -0
  82. data/spec/{grammar → lib/grammar}/super_spec.rb +0 -0
  83. data/spec/{grammar → lib/grammar}/true_spec.rb +0 -0
  84. data/spec/{grammar → lib/grammar}/undef_spec.rb +0 -0
  85. data/spec/{grammar → lib/grammar}/unless_spec.rb +0 -0
  86. data/spec/{grammar → lib/grammar}/while_spec.rb +0 -0
  87. data/spec/{grammar → lib/grammar}/xstr_spec.rb +0 -0
  88. data/spec/{grammar → lib/grammar}/yield_spec.rb +0 -0
  89. data/spec/{parser → lib/parser}/simple_spec.rb +1 -1
  90. data/spec/opal/erb/erb_spec.rb +10 -2
  91. data/spec/opal/string/demodulize_spec.rb +10 -0
  92. data/spec/opal/string/underscore_spec.rb +17 -0
  93. data/spec/spec_helper.rb +1 -1
  94. data/vendor/opal_spec_runner.js +43 -0
  95. metadata +99 -114
  96. data/.rspec +0 -2
  97. data/docs/index.md +0 -616
  98. data/docs/post.html +0 -3
  99. data/docs/pre.html +0 -35
  100. data/docs/try.html +0 -52
  101. data/spec/core/string/fixtures/classes.rb +0 -3
  102. data/spec/opal/array/subclassing_spec.rb +0 -32
  103. data/spec/opal/exception/subclassing_spec.rb +0 -17
  104. data/spec/opal/string/subclassing_spec.rb +0 -26
data/.travis.yml CHANGED
@@ -5,4 +5,4 @@ rvm:
5
5
  - 1.9.3
6
6
 
7
7
  before_script:
8
- - "bundle exec rake opal parser"
8
+ - "bundle exec rake opal"
data/Gemfile CHANGED
@@ -1,5 +1,4 @@
1
1
  source :rubygems
2
-
3
2
  gemspec
4
3
 
5
4
  gem "rake"
@@ -11,12 +10,5 @@ end
11
10
 
12
11
  # running tests on command line
13
12
  group :testing do
14
- gem "therubyracer", :require => 'v8'
15
- gem 'opal-spec', :git => 'git://github.com/adambeynon/opal-spec.git'
16
- end
17
-
18
- group :docs do
19
- gem "redcarpet"
20
- gem "albino"
21
- gem "rack"
13
+ gem 'opal-spec', :git => 'git://github.com/opal/opal-spec.git'
22
14
  end
data/README.md CHANGED
@@ -1,12 +1,12 @@
1
1
  # Opal
2
2
 
3
- [![Build Status](https://secure.travis-ci.org/adambeynon/opal.png?branch=master)](http://travis-ci.org/adambeynon/opal)
3
+ [![Build Status](https://secure.travis-ci.org/opal/opal.png?branch=master)](http://travis-ci.org/opal/opal)
4
4
 
5
5
  Opal is a ruby to javascript source-to-source compiler. It also has an
6
6
  implementation of the ruby corelib.
7
7
 
8
- Opal is [hosted on github](http://github.com/adambeynon/opal), and there
9
- is a Freenode IRC channel at `#opal`.
8
+ Opal is [hosted on github](http://github.com/opal/opal), and there
9
+ is a Freenode IRC channel at `#opal`. There is also a [google group for opal](http://groups.google.com/forum/#!forum/opalrb).
10
10
 
11
11
  ## Usage
12
12
 
@@ -17,7 +17,7 @@ See the website, [http://opalrb.org](http://opalrb.org).
17
17
  Build the runtime, tests and dependencies:
18
18
 
19
19
  ```
20
- rake opal parser
20
+ rake opal
21
21
  ```
22
22
 
23
23
  Run tests using phantom.js runner:
@@ -26,6 +26,47 @@ Run tests using phantom.js runner:
26
26
  rake test
27
27
  ```
28
28
 
29
+ Alternatively, after building, you can open `spec/index.html` in any
30
+ web browser.
31
+
32
+ ## Code Overview
33
+
34
+ ### core
35
+
36
+ The `core` directory holds the opal runtime and corelib implemented in
37
+ ruby and javascript. These are built using `rake opal`, as above.
38
+
39
+ ### lib
40
+
41
+ The `lib` directory holds the opal parser/compiler used to compile ruby
42
+ into javascript. It is also built ready for the browser into `opal-parser.js`
43
+ to allow compilation in any javascript environment.
44
+
45
+ ### spec
46
+
47
+ * **core** contains rubyspecs that apply to opal.
48
+ * **language** applicable specs from rubyspec for testing language semantics
49
+ * **opal** tests for extra methods/features in opal not found in standard ruby
50
+ * **lib** specs for opal lib (parser, erb\_parser, grammar etc)
51
+
29
52
  ## License
30
53
 
31
- Opal is released under the MIT license.
54
+ Copyright (C) 2012 by Adam Beynon
55
+
56
+ Permission is hereby granted, free of charge, to any person obtaining a copy
57
+ of this software and associated documentation files (the "Software"), to deal
58
+ in the Software without restriction, including without limitation the rights
59
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
60
+ copies of the Software, and to permit persons to whom the Software is
61
+ furnished to do so, subject to the following conditions:
62
+
63
+ The above copyright notice and this permission notice shall be included in
64
+ all copies or substantial portions of the Software.
65
+
66
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
67
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
68
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
69
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
70
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
71
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
72
+ THE SOFTWARE.
data/Rakefile CHANGED
@@ -1,37 +1,17 @@
1
- require 'fileutils'
2
- require 'bundler'
3
- Bundler.setup
1
+ require 'bundler/setup'
4
2
 
5
- require 'opal'
6
3
  require 'opal/version'
7
4
  require 'opal/rake_task'
8
5
 
9
6
  Opal::RakeTask.new do |t|
10
7
  t.dependencies = %w(opal-spec)
11
8
  t.files = [] # we handle this by Opal.runtime instead
12
- end
13
-
14
- desc "Build opal-parser ready for browser"
15
- task :parser do
16
- puts " * build/opal-parser.js"
17
- File.open('build/opal-parser.js', 'w+') do |o|
18
- o.puts Opal::Builder.new(:files => %w(racc.rb strscan.rb), :dir => 'core').build
19
- o.puts Opal.build_gem('opal')
20
- o.puts File.read('core/browser.js')
21
- end
9
+ t.parser = true # we want to also build opal-parser.js (used in specs)
22
10
  end
23
11
 
24
12
  desc "Run tests"
25
13
  task :test do
26
- src = %w(build/opal.js build/opal-spec.js build/opal-parser.js build/specs.js)
27
- out = 'build/phantom_runner.js'
28
-
29
- File.open(out, 'w+') do |o|
30
- src.each { |s| o.write File.read(s) }
31
- o.write "Opal.require('opal-spec/autorun');"
32
- end
33
-
34
- sh "phantomjs build/phantom_runner.js"
14
+ sh "phantomjs vendor/opal_spec_runner.js spec/index.html"
35
15
  end
36
16
 
37
17
  task :default => :test
@@ -67,74 +47,4 @@ def gzip(str)
67
47
  i.close_write
68
48
  return i.read
69
49
  end
70
- end
71
-
72
- namespace :docs do
73
- desc "Clone repo"
74
- task :clone do
75
- if File.exists? 'gh-pages'
76
- Dir.chdir('gh-pages') { sh 'git pull origin gh-pages' }
77
- else
78
- FileUtils.mkdir_p 'gh-pages'
79
- Dir.chdir('gh-pages') do
80
- sh 'git clone git@github.com:/adambeynon/opal.git .'
81
- sh 'git checkout gh-pages'
82
- end
83
- end
84
- end
85
-
86
- desc "Build docs"
87
- task :build do
88
- require 'redcarpet'
89
- require 'albino'
90
-
91
- klass = Class.new(Redcarpet::Render::HTML) do
92
- def block_code(code, language)
93
- Albino.new(code, language || :text).colorize
94
- end
95
- end
96
-
97
- markdown = Redcarpet::Markdown.new(klass, :fenced_code_blocks => true)
98
-
99
- File.open('gh-pages/index.html', 'w+') do |o|
100
- puts " * index.html"
101
- o.write File.read('docs/pre.html')
102
- o.write markdown.render(File.read "docs/index.md")
103
- o.write File.read('docs/post.html')
104
- end
105
-
106
- %w(try).each do |src|
107
- puts " * src.md"
108
- File.open("gh-pages/#{src}.html", 'w+') do |out|
109
- out.write File.read("docs/pre.html")
110
- out.write File.read("docs/#{src}.html")
111
- out.write File.read("docs/post.html")
112
- end
113
- end
114
-
115
- %w(opal opal-parser).each do |src|
116
- puts " * #{src}.js"
117
- FileUtils.cp "build/#{src}.js", "gh-pages/#{src}.js"
118
- end
119
- end
120
-
121
- task :server do
122
- require 'rack'
123
- server = Class.new(Rack::Server) do
124
- def app
125
- Rack::Directory.new('gh-pages')
126
- end
127
- end
128
-
129
- server.new.start
130
- end
131
-
132
- desc "commit and push"
133
- task :push do
134
- Dir.chdir('gh-pages') do
135
- sh "git add ."
136
- sh "git commit -a -m \"Documentation update #{Time.new}\""
137
- sh "git push origin gh-pages"
138
- end
139
- end
140
- end
50
+ end
data/core/array.rb CHANGED
@@ -7,18 +7,14 @@ class Array < `Array`
7
7
 
8
8
  def self.[](*objects)
9
9
  %x{
10
- var result = #{allocate};
11
-
12
- result.splice.apply(result, [0, 0].concat(objects));
13
-
14
- return result;
10
+ return objects;
15
11
  }
16
12
  end
17
13
 
18
14
  def self.new(size, obj = nil)
19
- arr = allocate
20
-
21
15
  %x{
16
+ var arr = [];
17
+
22
18
  if (size && size._isArray) {
23
19
  for (var i = 0; i < size.length; i++) {
24
20
  arr[i] = size[i];
@@ -29,9 +25,9 @@ class Array < `Array`
29
25
  arr[i] = obj;
30
26
  }
31
27
  }
32
- }
33
28
 
34
- arr
29
+ return arr;
30
+ }
35
31
  end
36
32
 
37
33
  def &(other)
@@ -223,8 +219,6 @@ class Array < `Array`
223
219
  end
224
220
 
225
221
  def collect(&block)
226
- return enum_for :collect unless block_given?
227
-
228
222
  %x{
229
223
  var result = [];
230
224
 
@@ -241,8 +235,6 @@ class Array < `Array`
241
235
  end
242
236
 
243
237
  def collect!(&block)
244
- return enum_for :collect! unless block_given?
245
-
246
238
  %x{
247
239
  for (var i = 0, length = #{self}.length, val; i < length; i++) {
248
240
  if ((val = block.call(__context, #{self}[i])) === __breaker) {
@@ -351,8 +343,6 @@ class Array < `Array`
351
343
  end
352
344
 
353
345
  def delete_if(&block)
354
- return enum_for :delete_if unless block_given?
355
-
356
346
  %x{
357
347
  for (var i = 0, length = #{self}.length, value; i < length; i++) {
358
348
  if ((value = block.call(__context, #{self}[i])) === __breaker) {
@@ -376,8 +366,6 @@ class Array < `Array`
376
366
  end
377
367
 
378
368
  def drop_while(&block)
379
- return enum_for :drop_while unless block_given?
380
-
381
369
  %x{
382
370
  for (var i = 0, length = #{self}.length, value; i < length; i++) {
383
371
  if ((value = block(__context, #{self}[i])) === __breaker) {
@@ -396,8 +384,6 @@ class Array < `Array`
396
384
  alias dup clone
397
385
 
398
386
  def each(&block)
399
- return enum_for :each unless block_given?
400
-
401
387
  `for (var i = 0, length = #{self}.length; i < length; i++) {`
402
388
  yield `#{self}[i]`
403
389
  `}`
@@ -406,8 +392,6 @@ class Array < `Array`
406
392
  end
407
393
 
408
394
  def each_index(&block)
409
- return enum_for :each_index unless block_given?
410
-
411
395
  `for (var i = 0, length = #{self}.length; i < length; i++) {`
412
396
  yield `i`
413
397
  `}`
@@ -416,8 +400,6 @@ class Array < `Array`
416
400
  end
417
401
 
418
402
  def each_with_index(&block)
419
- return enum_for :each_with_index unless block_given?
420
-
421
403
  `for (var i = 0, length = #{self}.length; i < length; i++) {`
422
404
  yield `#{self}[i]`, `i`
423
405
  `}`
@@ -533,9 +515,6 @@ class Array < `Array`
533
515
 
534
516
  def index(object, &block)
535
517
  %x{
536
- if (block === nil && object == null) {
537
- return #{enum_for :index};
538
- }
539
518
  if (block !== nil) {
540
519
  for (var i = 0, length = #{self}.length, value; i < length; i++) {
541
520
  if ((value = block.call(__context, '', #{self}[i])) === __breaker) {
@@ -560,8 +539,6 @@ class Array < `Array`
560
539
  end
561
540
 
562
541
  def inject(initial, &block)
563
- return enum_for :inject unless block_given?
564
-
565
542
  %x{
566
543
  var result, i;
567
544
 
@@ -640,7 +617,6 @@ class Array < `Array`
640
617
  end
641
618
 
642
619
  def keep_if(&block)
643
- return enum_for :keep_if unless block_given?
644
620
  %x{
645
621
  for (var i = 0, length = #{self}.length, value; i < length; i++) {
646
622
  if ((value = block(__context, #{self}[i])) === __breaker) {
@@ -729,8 +705,6 @@ class Array < `Array`
729
705
  end
730
706
 
731
707
  def reject(&block)
732
- return enum_for :reject unless block_given?
733
-
734
708
  %x{
735
709
  var result = [];
736
710
 
@@ -748,8 +722,6 @@ class Array < `Array`
748
722
  end
749
723
 
750
724
  def reject!(&block)
751
- return enum_for :reject! unless block_given?
752
-
753
725
  %x{
754
726
  var original = #{self}.length;
755
727
 
@@ -778,9 +750,7 @@ class Array < `Array`
778
750
  }
779
751
  end
780
752
 
781
- def reverse
782
- `#{self}.reverse()`
783
- end
753
+ alias_native :reverse, :reverse
784
754
 
785
755
  def reverse!
786
756
  %x{
@@ -791,16 +761,12 @@ class Array < `Array`
791
761
  end
792
762
 
793
763
  def reverse_each(&block)
794
- return enum_for :reverse_each unless block_given?
795
-
796
764
  reverse.each &block
797
765
 
798
766
  self
799
767
  end
800
768
 
801
769
  def rindex(object, &block)
802
- return enum_for :rindex unless block_given?
803
-
804
770
  %x{
805
771
  if (block !== nil) {
806
772
  for (var i = #{self}.length - 1, value; i >= 0; i--) {
@@ -826,8 +792,6 @@ class Array < `Array`
826
792
  end
827
793
 
828
794
  def select(&block)
829
- return enum_for :select unless block_given?
830
-
831
795
  %x{
832
796
  var result = [];
833
797
 
@@ -848,7 +812,6 @@ class Array < `Array`
848
812
  end
849
813
 
850
814
  def select!(&block)
851
- return enum_for :select! unless block_given?
852
815
  %x{
853
816
  var original = #{self}.length;
854
817
 
@@ -872,7 +835,13 @@ class Array < `Array`
872
835
  end
873
836
 
874
837
  def shift(count)
875
- `count == null ? #{self}.shift() : #{self}.splice(0, count)`
838
+ %x{
839
+ if (#{self}.length === 0) {
840
+ return nil;
841
+ }
842
+
843
+ return count == null ? #{self}.shift() : #{self}.splice(0, count)
844
+ }
876
845
  end
877
846
 
878
847
  alias size length
@@ -902,8 +871,6 @@ class Array < `Array`
902
871
  end
903
872
 
904
873
  def take_while(&block)
905
- return enum_for :take_while unless block_given?
906
-
907
874
  %x{
908
875
  var result = [];
909
876
 
data/core/basic_object.rb CHANGED
@@ -8,13 +8,7 @@ class BasicObject
8
8
 
9
9
  def __send__(symbol, *args, &block)
10
10
  %x{
11
- var meth = #{self}['$' + symbol];
12
-
13
- if (!meth) {
14
- return #{ method_missing symbol };
15
- }
16
-
17
- return meth.apply(#{self}, args);
11
+ return #{self}['$' + symbol].apply(#{self}, args);
18
12
  }
19
13
  end
20
14
 
@@ -23,7 +17,7 @@ class BasicObject
23
17
  alias eql? ==
24
18
  alias equal? ==
25
19
 
26
- def instance_eval(string, &block)
20
+ def instance_eval(string=undefined, &block)
27
21
  %x{
28
22
  if (block === nil) {
29
23
  no_block_given();
@@ -42,8 +36,4 @@ class BasicObject
42
36
  return block.apply(#{self}, args);
43
37
  }
44
38
  end
45
-
46
- def method_missing(symbol, *args)
47
- raise NoMethodError, "undefined method `#{symbol}` for #{inspect}"
48
- end
49
39
  end