opal 0.4.2 → 0.4.3

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.
Files changed (93) hide show
  1. data/.travis.yml +5 -0
  2. data/README.md +42 -2
  3. data/Rakefile +7 -6
  4. data/bin/opal +2 -2
  5. data/{opal → corelib}/opal.rb +21 -21
  6. data/{opal → corelib}/opal/array.rb +37 -24
  7. data/{opal → corelib}/opal/basic_object.rb +0 -0
  8. data/{opal → corelib}/opal/boolean.rb +0 -0
  9. data/{opal → corelib}/opal/class.rb +45 -22
  10. data/{opal → corelib}/opal/comparable.rb +0 -0
  11. data/{opal → corelib}/opal/enumerable.rb +0 -0
  12. data/{opal → corelib}/opal/enumerator.rb +0 -0
  13. data/{opal → corelib}/opal/error.rb +16 -12
  14. data/{opal → corelib}/opal/hash.rb +2 -2
  15. data/{opal → corelib}/opal/kernel.rb +16 -14
  16. data/{opal → corelib}/opal/native.rb +0 -0
  17. data/{opal → corelib}/opal/nil_class.rb +1 -1
  18. data/{opal → corelib}/opal/numeric.rb +0 -0
  19. data/{opal → corelib}/opal/proc.rb +0 -0
  20. data/{opal → corelib}/opal/range.rb +2 -2
  21. data/{opal → corelib}/opal/regexp.rb +0 -0
  22. data/{opal → corelib}/opal/runtime.js +92 -126
  23. data/{opal → corelib}/opal/string.rb +14 -13
  24. data/{opal → corelib}/opal/time.rb +0 -0
  25. data/lib/opal.rb +9 -8
  26. data/lib/opal/builder.rb +81 -0
  27. data/lib/opal/erb.rb +17 -0
  28. data/lib/opal/grammar.rb +33 -30
  29. data/lib/opal/grammar.y +18 -15
  30. data/lib/opal/grammar_helpers.rb +5 -5
  31. data/lib/opal/lexer.rb +3 -2
  32. data/lib/opal/parser.rb +58 -41
  33. data/lib/opal/require_parser.rb +77 -0
  34. data/lib/opal/version.rb +1 -1
  35. data/opal.gemspec +1 -1
  36. data/{config.ru → spec/config.ru} +0 -0
  37. data/spec/opal/array/to_json_spec.rb +3 -1
  38. data/spec/opal/boolean/to_json_spec.rb +3 -1
  39. data/spec/opal/erb/erb_spec.rb +25 -0
  40. data/spec/opal/erb/quoted.opalerb +1 -0
  41. data/spec/opal/erb/simple.opalerb +1 -0
  42. data/spec/opal/json/parse_spec.rb +3 -1
  43. data/spec/opal/kernel/to_json_spec.rb +3 -1
  44. data/spec/opal/nil/to_json_spec.rb +3 -1
  45. data/spec/opal/string/to_json_spec.rb +3 -1
  46. data/spec/parser/int_spec.rb +13 -0
  47. data/spec/parser/regexp_spec.rb +16 -0
  48. data/spec/parser/str_spec.rb +11 -0
  49. data/spec/rubyspec/core/array/plus_spec.rb +7 -2
  50. data/spec/rubyspec/core/hash/to_json_spec.rb +3 -1
  51. data/spec/rubyspec/core/numeric/to_json_spec.rb +3 -1
  52. data/spec/rubyspec/language/defined_spec.rb +9 -0
  53. data/spec/rubyspec/library/stringscanner/pos_spec.rb +20 -0
  54. data/{opal → stdlib}/date.rb +0 -0
  55. data/stdlib/erb.rb +26 -0
  56. data/{opal → stdlib}/fileutils.rb +0 -0
  57. data/{opal → stdlib}/iconv.rb +0 -0
  58. data/{opal/opal → stdlib}/json.rb +0 -0
  59. data/{opal → stdlib}/observer.rb +0 -0
  60. data/{opal → stdlib}/opal-browser/local_storage.rb +0 -0
  61. data/{opal → stdlib}/opal-browser/script_loader.rb +1 -1
  62. data/{opal → stdlib}/opal-parser.js.erb +1 -1
  63. data/{opal → stdlib}/opal-source-maps.js.erb +0 -0
  64. data/{opal → stdlib}/pp.rb +0 -0
  65. data/{opal → stdlib}/racc.rb +0 -0
  66. data/{opal → stdlib}/rbconfig.rb +0 -0
  67. data/{opal → stdlib}/source_map.rb +1 -1
  68. data/{opal → stdlib}/source_map/generator.rb +0 -0
  69. data/{opal → stdlib}/source_map/parser.rb +0 -0
  70. data/{opal → stdlib}/source_map/vlq.rb +0 -0
  71. data/{opal → stdlib}/strscan.rb +15 -0
  72. data/{opal → stdlib}/yaml.rb +0 -0
  73. metadata +87 -77
  74. checksums.yaml +0 -7
  75. data/CHANGELOG.md +0 -176
  76. data/examples/native/Gemfile +0 -3
  77. data/examples/native/README.md +0 -17
  78. data/examples/native/app/app.rb +0 -57
  79. data/examples/native/config.ru +0 -8
  80. data/examples/native/index.html.erb +0 -12
  81. data/examples/rack/Gemfile +0 -3
  82. data/examples/rack/README.md +0 -22
  83. data/examples/rack/app/app.rb +0 -5
  84. data/examples/rack/config.ru +0 -20
  85. data/examples/rack/index.html +0 -12
  86. data/examples/server/Gemfile +0 -3
  87. data/examples/server/README.md +0 -22
  88. data/examples/server/app/app.rb +0 -18
  89. data/examples/server/config.ru +0 -10
  90. data/examples/server/index.html.erb +0 -10
  91. data/opal/opal-template.rb +0 -33
  92. data/spec/opal/class/_inherited_spec.rb +0 -32
  93. data/spec/opal/class/new_spec.rb +0 -27
@@ -2,6 +2,7 @@ class String
2
2
  include Comparable
3
3
 
4
4
  `def._isString = true`
5
+ `var native_string = "".constructor;`
5
6
 
6
7
  def self.try_convert(what)
7
8
  what.to_str
@@ -11,7 +12,7 @@ class String
11
12
 
12
13
  def self.new(str = '')
13
14
  %x{
14
- return new String(str)
15
+ return new native_string(str)
15
16
  }
16
17
  end
17
18
 
@@ -75,7 +76,7 @@ class String
75
76
  end
76
77
 
77
78
  def ==(other)
78
- `other == String(#{self})`
79
+ `other == native_string(#{self})`
79
80
  end
80
81
 
81
82
  alias === ==
@@ -429,7 +430,7 @@ class String
429
430
  }
430
431
 
431
432
  var initial = #{self}.substr(0, #{self}.length - 1);
432
- var last = String.fromCharCode(#{self}.charCodeAt(#{self}.length - 1) + 1);
433
+ var last = native_string.fromCharCode(#{self}.charCodeAt(#{self}.length - 1) + 1);
433
434
 
434
435
  return initial + last;
435
436
  }
@@ -455,8 +456,8 @@ class String
455
456
  # TODO handle case where search is regexp
456
457
  def rindex(search, offset = undefined)
457
458
  %x{
458
- var search_type = (search == null ? Opal.NilClass : search.$class());
459
- if (search_type != String && search_type != RegExp) {
459
+ var search_type = (search == null ? Opal.NilClass : search.constructor);
460
+ if (search_type != native_string && search_type != RegExp) {
460
461
  var msg = "type mismatch: " + search_type + " given";
461
462
  #{raise TypeError.new(`msg`)};
462
463
  }
@@ -471,7 +472,7 @@ class String
471
472
  offset = #{self}.length + offset;
472
473
  }
473
474
 
474
- if (search_type == String) {
475
+ if (search_type == native_string) {
475
476
  result = #{self}.lastIndexOf(search, offset);
476
477
  }
477
478
  else {
@@ -482,7 +483,7 @@ class String
482
483
  }
483
484
  }
484
485
  else {
485
- if (search_type == String) {
486
+ if (search_type == native_string) {
486
487
  result = #{self}.lastIndexOf(search);
487
488
  }
488
489
  else {
@@ -645,7 +646,7 @@ class String
645
646
  return $1 ? $0.toUpperCase() : $0.toLowerCase();
646
647
  });
647
648
 
648
- if (#{self}.constructor === String) {
649
+ if (#{self}.constructor === native_string) {
649
650
  return str;
650
651
  }
651
652
 
@@ -750,7 +751,7 @@ class String
750
751
  var start = last_from.charCodeAt(0) + 1;
751
752
  var end = char.charCodeAt(0);
752
753
  for (var c = start; c < end; c++) {
753
- from_chars_expanded.push(String.fromCharCode(c));
754
+ from_chars_expanded.push(native_string.fromCharCode(c));
754
755
  }
755
756
  from_chars_expanded.push(char);
756
757
  in_range = null;
@@ -796,7 +797,7 @@ class String
796
797
  var start = last_from.charCodeAt(0) + 1;
797
798
  var end = char.charCodeAt(0);
798
799
  for (var c = start; c < end; c++) {
799
- to_chars_expanded.push(String.fromCharCode(c));
800
+ to_chars_expanded.push(native_string.fromCharCode(c));
800
801
  }
801
802
  to_chars_expanded.push(char);
802
803
  in_range = null;
@@ -885,7 +886,7 @@ class String
885
886
  var start = last_from.charCodeAt(0) + 1;
886
887
  var end = char.charCodeAt(0);
887
888
  for (var c = start; c < end; c++) {
888
- from_chars_expanded.push(String.fromCharCode(c));
889
+ from_chars_expanded.push(native_string.fromCharCode(c));
889
890
  }
890
891
  from_chars_expanded.push(char);
891
892
  in_range = null;
@@ -931,7 +932,7 @@ class String
931
932
  var start = last_from.charCodeAt(0) + 1;
932
933
  var end = char.charCodeAt(0);
933
934
  for (var c = start; c < end; c++) {
934
- to_chars_expanded.push(String.fromCharCode(c));
935
+ to_chars_expanded.push(native_string.fromCharCode(c));
935
936
  }
936
937
  to_chars_expanded.push(char);
937
938
  in_range = null;
@@ -1012,7 +1013,7 @@ class MatchData < Array
1012
1013
 
1013
1014
  def self.new(regexp, match_groups)
1014
1015
  %x{
1015
- var instance = new Opal.MatchData;
1016
+ var instance = new Opal.MatchData._alloc;
1016
1017
  for (var i = 0, len = match_groups.length; i < len; i++) {
1017
1018
  var group = match_groups[i];
1018
1019
  if (group == undefined) {
File without changes
data/lib/opal.rb CHANGED
@@ -1,4 +1,7 @@
1
1
  require 'opal/parser'
2
+ require 'opal/require_parser'
3
+ require 'opal/builder'
4
+ require 'opal/erb'
2
5
  require 'opal/version'
3
6
 
4
7
  # Opal is a ruby to javascript compiler, with a runtime for running
@@ -15,26 +18,24 @@ module Opal
15
18
  Parser.new.parse(source, options)
16
19
  end
17
20
 
18
- # Returns the path to the opal corelib. Used by Opal::Processor to load
19
- # opal runtime and core lib.
20
- #
21
- # @return [String]
22
21
  def self.core_dir
23
- File.expand_path('../../opal', __FILE__)
22
+ File.expand_path('../../corelib', __FILE__)
23
+ end
24
+
25
+ def self.std_dir
26
+ File.expand_path('../../stdlib', __FILE__)
24
27
  end
25
28
 
26
29
  # Add a file path to opals load path. Any gem containing ruby code that Opal
27
30
  # has access to should add a load path through this method. Load paths added
28
31
  # here should only be paths which contain code targeted at being compiled by
29
32
  # Opal.
30
- #
31
- # @param [String] path file path to add
32
33
  def self.append_path(path)
33
34
  paths << path
34
35
  end
35
36
 
36
37
  # Private, don't add to these directly (use .append_path instead).
37
38
  def self.paths
38
- @paths ||= [core_dir]
39
+ @paths ||= [core_dir, std_dir]
39
40
  end
40
41
  end
@@ -0,0 +1,81 @@
1
+ require 'opal/require_parser'
2
+ require 'erb'
3
+
4
+ module Opal
5
+ class Builder
6
+
7
+ BUILDERS = { ".rb" => :build_ruby, ".js" => :build_js, ".erb" => :build_erb }
8
+
9
+ def self.build(name)
10
+ Builder.new.build name
11
+ end
12
+
13
+ def initialize
14
+ @paths = Opal.paths.clone
15
+ end
16
+
17
+ def append_path(path)
18
+ @paths << path
19
+ end
20
+
21
+ def build(path)
22
+ @segments = []
23
+ @handled = {}
24
+
25
+ require_asset path
26
+
27
+ @segments.join
28
+ end
29
+
30
+ def require_asset(path)
31
+ location = find_asset path
32
+
33
+ build_asset location
34
+ end
35
+
36
+ def find_asset(path)
37
+ file_types = %w[.rb .js .js.erb]
38
+
39
+ @paths.each do |root|
40
+ file_types.each do |type|
41
+ test = File.join root, "#{path}#{type}"
42
+
43
+ if File.exist? test
44
+ return test
45
+ end
46
+ end
47
+ end
48
+
49
+ raise "Could not find asset: #{path}"
50
+ end
51
+
52
+ def build_asset(path)
53
+ ext = File.extname path
54
+
55
+ unless builder = BUILDERS[ext]
56
+ raise "Unknown builder for #{ext}"
57
+ end
58
+
59
+ @segments << __send__(builder, path)
60
+ end
61
+
62
+ def build_ruby(path)
63
+ parser = RequireParser.new
64
+ result = parser.parse File.read(path)
65
+
66
+ parser.requires.each do |r|
67
+ require_asset r
68
+ end
69
+
70
+ result
71
+ end
72
+
73
+ def build_js(path)
74
+ File.read(path)
75
+ end
76
+
77
+ def build_erb(path)
78
+ ::ERB.new(File.read(path)).result binding
79
+ end
80
+ end
81
+ end
data/lib/opal/erb.rb ADDED
@@ -0,0 +1,17 @@
1
+ require 'opal/parser'
2
+
3
+ module Opal
4
+ module ERB
5
+ def self.parse(str, name='(erb)')
6
+ body = str.gsub('"', '\\"').gsub(/<%=([\s\S]+?)%>/) do
7
+ inner = $1.gsub(/\\'/, "'").gsub(/\\"/, '"')
8
+ "\")\nout.<<(#{ inner })\nout.<<(\""
9
+ end.gsub(/<%([\s\S]+?)%>/) do
10
+ "\")\n#{ $1 }\nout.<<(\""
11
+ end
12
+
13
+ code = "ERB.new('#{name}') do |out|\nout.<<(\"#{ body }\")\nout.join\nend\n"
14
+ Opal.parse code
15
+ end
16
+ end
17
+ end
data/lib/opal/grammar.rb CHANGED
@@ -1967,7 +1967,7 @@ racc_reduce_table = [
1967
1967
  1, 181, :_reduce_93,
1968
1968
  1, 181, :_reduce_94,
1969
1969
  1, 149, :_reduce_95,
1970
- 1, 149, :_reduce_96,
1970
+ 1, 149, :_reduce_none,
1971
1971
  1, 150, :_reduce_97,
1972
1972
  3, 150, :_reduce_98,
1973
1973
  1, 182, :_reduce_none,
@@ -2243,8 +2243,8 @@ racc_reduce_table = [
2243
2243
  1, 253, :_reduce_369,
2244
2244
  2, 143, :_reduce_370,
2245
2245
  1, 143, :_reduce_none,
2246
- 1, 198, :_reduce_372,
2247
- 1, 198, :_reduce_373,
2246
+ 1, 198, :_reduce_none,
2247
+ 1, 198, :_reduce_none,
2248
2248
  1, 198, :_reduce_none,
2249
2249
  1, 199, :_reduce_375,
2250
2250
  1, 256, :_reduce_none,
@@ -2277,14 +2277,14 @@ racc_reduce_table = [
2277
2277
  1, 264, :_reduce_403,
2278
2278
  1, 264, :_reduce_none,
2279
2279
  2, 184, :_reduce_405,
2280
- 1, 184, :_reduce_none,
2280
+ 1, 184, :_reduce_406,
2281
2281
  1, 267, :_reduce_none,
2282
2282
  1, 267, :_reduce_none,
2283
2283
  1, 267, :_reduce_none,
2284
2284
  1, 267, :_reduce_none,
2285
2285
  3, 255, :_reduce_411,
2286
- 1, 254, :_reduce_none,
2287
- 1, 254, :_reduce_none,
2286
+ 1, 254, :_reduce_412,
2287
+ 1, 254, :_reduce_413,
2288
2288
  2, 254, :_reduce_none,
2289
2289
  2, 254, :_reduce_none,
2290
2290
  1, 178, :_reduce_416,
@@ -3261,16 +3261,12 @@ def _reduce_94(val, _values, result)
3261
3261
  end
3262
3262
 
3263
3263
  def _reduce_95(val, _values, result)
3264
- result = s(:lit, val[0].intern)
3264
+ result = s(:sym, val[0].intern)
3265
3265
 
3266
3266
  result
3267
3267
  end
3268
3268
 
3269
- def _reduce_96(val, _values, result)
3270
- result = s(:lit, val[0])
3271
-
3272
- result
3273
- end
3269
+ # reduce 96 omitted
3274
3270
 
3275
3271
  def _reduce_97(val, _values, result)
3276
3272
  result = s(:undef, val[0])
@@ -3517,16 +3513,19 @@ end
3517
3513
 
3518
3514
  def _reduce_188(val, _values, result)
3519
3515
  result = new_call val[1], :"+@", s(:arglist)
3520
- result = val[1] if val[1][0] == :lit and Numeric === val[1][1]
3516
+ result = val[1] if [:int, :float].include? val[1][0]
3521
3517
 
3522
3518
  result
3523
3519
  end
3524
3520
 
3525
3521
  def _reduce_189(val, _values, result)
3526
3522
  result = new_call val[1], :"-@", s(:arglist)
3527
- if val[1][0] == :lit and Numeric === val[1][1]
3523
+ if val[1][0] == :int
3528
3524
  val[1][1] = -val[1][1]
3529
3525
  result = val[1]
3526
+ elsif val[1][0] == :float
3527
+ val[1][1] = -val[1][1].to_f
3528
+ result = val[1]
3530
3529
  end
3531
3530
 
3532
3531
  result
@@ -4513,17 +4512,9 @@ end
4513
4512
 
4514
4513
  # reduce 371 omitted
4515
4514
 
4516
- def _reduce_372(val, _values, result)
4517
- result = s(:lit, val[0])
4518
-
4519
- result
4520
- end
4515
+ # reduce 372 omitted
4521
4516
 
4522
- def _reduce_373(val, _values, result)
4523
- result = s(:lit, val[0])
4524
-
4525
- result
4526
- end
4517
+ # reduce 373 omitted
4527
4518
 
4528
4519
  # reduce 374 omitted
4529
4520
 
@@ -4707,13 +4698,17 @@ end
4707
4698
  # reduce 404 omitted
4708
4699
 
4709
4700
  def _reduce_405(val, _values, result)
4710
- result = val[1].intern
4701
+ result = s(:sym, val[1].intern)
4711
4702
  @lex_state = :expr_end
4712
4703
 
4713
4704
  result
4714
4705
  end
4715
4706
 
4716
- # reduce 406 omitted
4707
+ def _reduce_406(val, _values, result)
4708
+ result = s(:sym, val[0].intern)
4709
+
4710
+ result
4711
+ end
4717
4712
 
4718
4713
  # reduce 407 omitted
4719
4714
 
@@ -4729,9 +4724,17 @@ def _reduce_411(val, _values, result)
4729
4724
  result
4730
4725
  end
4731
4726
 
4732
- # reduce 412 omitted
4727
+ def _reduce_412(val, _values, result)
4728
+ result = s(:int, val[0])
4729
+
4730
+ result
4731
+ end
4733
4732
 
4734
- # reduce 413 omitted
4733
+ def _reduce_413(val, _values, result)
4734
+ result = s(:float, val[0])
4735
+
4736
+ result
4737
+ end
4735
4738
 
4736
4739
  # reduce 414 omitted
4737
4740
 
@@ -4798,7 +4801,7 @@ def _reduce_425(val, _values, result)
4798
4801
  end
4799
4802
 
4800
4803
  def _reduce_426(val, _values, result)
4801
- result = s(:lit, @line)
4804
+ result = s(:int, @line)
4802
4805
 
4803
4806
  result
4804
4807
  end
@@ -5057,7 +5060,7 @@ def _reduce_471(val, _values, result)
5057
5060
  end
5058
5061
 
5059
5062
  def _reduce_472(val, _values, result)
5060
- result = [s(:lit, val[0].intern), val[1]]
5063
+ result = [s(:sym, val[0].intern), val[1]]
5061
5064
 
5062
5065
  result
5063
5066
  end
data/lib/opal/grammar.y CHANGED
@@ -382,12 +382,9 @@ fname:
382
382
  fitem:
383
383
  fname
384
384
  {
385
- result = s(:lit, val[0].intern)
385
+ result = s(:sym, val[0].intern)
386
386
  }
387
387
  | symbol
388
- {
389
- result = s(:lit, val[0])
390
- }
391
388
 
392
389
  undef_list:
393
390
  fitem
@@ -480,14 +477,17 @@ arg:
480
477
  | '+@' arg
481
478
  {
482
479
  result = new_call val[1], :"+@", s(:arglist)
483
- result = val[1] if val[1][0] == :lit and Numeric === val[1][1]
480
+ result = val[1] if [:int, :float].include? val[1][0]
484
481
  }
485
482
  | '-@' arg
486
483
  {
487
484
  result = new_call val[1], :"-@", s(:arglist)
488
- if val[1][0] == :lit and Numeric === val[1][1]
485
+ if val[1][0] == :int
489
486
  val[1][1] = -val[1][1]
490
487
  result = val[1]
488
+ elsif val[1][0] == :float
489
+ val[1][1] = -val[1][1].to_f
490
+ result = val[1]
491
491
  end
492
492
  }
493
493
  | arg '|' arg
@@ -1221,13 +1221,7 @@ opt_ensure:
1221
1221
 
1222
1222
  literal:
1223
1223
  numeric
1224
- {
1225
- result = s(:lit, val[0])
1226
- }
1227
1224
  | symbol
1228
- {
1229
- result = s(:lit, val[0])
1230
- }
1231
1225
  | dsym
1232
1226
 
1233
1227
  strings:
@@ -1384,10 +1378,13 @@ string_dvar:
1384
1378
  symbol:
1385
1379
  SYMBOL_BEG sym
1386
1380
  {
1387
- result = val[1].intern
1381
+ result = s(:sym, val[1].intern)
1388
1382
  @lex_state = :expr_end
1389
1383
  }
1390
1384
  | SYMBOL
1385
+ {
1386
+ result = s(:sym, val[0].intern)
1387
+ }
1391
1388
 
1392
1389
  sym: fname
1393
1390
  | IVAR
@@ -1402,7 +1399,13 @@ dsym:
1402
1399
 
1403
1400
  numeric:
1404
1401
  INTEGER
1402
+ {
1403
+ result = s(:int, val[0])
1404
+ }
1405
1405
  | FLOAT
1406
+ {
1407
+ result = s(:float, val[0])
1408
+ }
1406
1409
  | '-@NUM' INTEGER =LOWEST
1407
1410
  | '-@NUM' FLOAT =LOWEST
1408
1411
 
@@ -1449,7 +1452,7 @@ variable:
1449
1452
  }
1450
1453
  | LINE
1451
1454
  {
1452
- result = s(:lit, @line)
1455
+ result = s(:int, @line)
1453
1456
  }
1454
1457
 
1455
1458
  var_ref:
@@ -1659,7 +1662,7 @@ assoc:
1659
1662
  }
1660
1663
  | LABEL arg_value
1661
1664
  {
1662
- result = [s(:lit, val[0].intern), val[1]]
1665
+ result = [s(:sym, val[0].intern), val[1]]
1663
1666
  }
1664
1667
 
1665
1668
  operation: