opal 0.3.43 → 0.3.44

Sign up to get free protection for your applications and to get access to all the features.
Files changed (98) hide show
  1. data/CHANGELOG.md +19 -0
  2. data/README.md +1 -0
  3. data/Rakefile +29 -19
  4. data/examples/native/Gemfile +3 -0
  5. data/examples/native/README.md +17 -0
  6. data/examples/native/app/app.rb +38 -0
  7. data/examples/native/config.ru +8 -0
  8. data/examples/native/index.html.erb +12 -0
  9. data/lib/opal/lexer.rb +5 -1
  10. data/lib/opal/parser.rb +36 -10
  11. data/lib/opal/processor.rb +10 -9
  12. data/lib/opal/server.rb +17 -7
  13. data/lib/opal/target_scope.rb +2 -2
  14. data/lib/opal/version.rb +1 -1
  15. data/opal/opal-browser/script_loader.rb +8 -13
  16. data/opal/opal.rb +23 -5
  17. data/opal/opal/array.rb +128 -14
  18. data/opal/opal/boolean.rb +1 -1
  19. data/opal/opal/class.rb +92 -18
  20. data/opal/opal/enumerable.rb +90 -0
  21. data/opal/opal/error.rb +1 -1
  22. data/opal/opal/hash.rb +2 -2
  23. data/opal/opal/kernel.rb +3 -3
  24. data/opal/opal/numeric.rb +1 -1
  25. data/opal/opal/proc.rb +1 -1
  26. data/opal/opal/regexp.rb +31 -31
  27. data/opal/opal/runtime.js +181 -69
  28. data/opal/opal/string.rb +561 -40
  29. data/opal/opal/time.rb +1 -1
  30. data/opal/rbconfig.rb +17 -3
  31. data/opal/strscan.rb +41 -5
  32. data/spec/opal/class/new_spec.rb +27 -0
  33. data/spec/opal/native_spec.rb +127 -0
  34. data/spec/ospec/runner.rb +0 -2
  35. data/spec/parser/strscan/get_byte_spec.rb +29 -0
  36. data/spec/parser/strscan/skip_spec.rb +40 -0
  37. data/spec/rubyspec/core/array/each_spec.rb +1 -1
  38. data/spec/rubyspec/core/array/intersection_spec.rb +5 -0
  39. data/spec/rubyspec/core/array/max_spec.rb +32 -0
  40. data/spec/rubyspec/core/array/min_spec.rb +32 -0
  41. data/spec/rubyspec/core/array/minus_spec.rb +7 -5
  42. data/spec/rubyspec/core/array/multiply_spec.rb +1 -1
  43. data/spec/rubyspec/core/array/plus_spec.rb +1 -1
  44. data/spec/rubyspec/core/array/pop_spec.rb +1 -1
  45. data/spec/rubyspec/core/array/push_spec.rb +1 -1
  46. data/spec/rubyspec/core/array/rassoc_spec.rb +1 -1
  47. data/spec/rubyspec/core/array/reject_spec.rb +2 -2
  48. data/spec/rubyspec/core/array/reverse_each_spec.rb +2 -2
  49. data/spec/rubyspec/core/array/rindex_spec.rb +2 -2
  50. data/spec/rubyspec/core/array/select_spec.rb +1 -1
  51. data/spec/rubyspec/core/array/shift_spec.rb +1 -1
  52. data/spec/rubyspec/core/array/slice_spec.rb +1 -1
  53. data/spec/rubyspec/core/array/sort_spec.rb +15 -15
  54. data/spec/rubyspec/core/array/to_a_spec.rb +1 -1
  55. data/spec/rubyspec/core/array/to_ary_spec.rb +1 -1
  56. data/spec/rubyspec/core/array/uniq_spec.rb +1 -1
  57. data/spec/rubyspec/core/array/unshift_spec.rb +1 -1
  58. data/spec/rubyspec/core/array/zip_spec.rb +1 -1
  59. data/spec/rubyspec/core/enumerable/select_spec.rb +4 -1
  60. data/spec/rubyspec/core/module/const_defined_spec.rb +86 -0
  61. data/spec/rubyspec/core/module/const_get_spec.rb +55 -3
  62. data/spec/rubyspec/core/module/const_set_spec.rb +2 -2
  63. data/spec/rubyspec/core/module/constants_spec.rb +49 -0
  64. data/spec/rubyspec/core/regexp/match_spec.rb +66 -1
  65. data/spec/rubyspec/core/string/center_spec.rb +71 -0
  66. data/spec/rubyspec/core/string/chomp_spec.rb +6 -1
  67. data/spec/rubyspec/core/string/clone_spec.rb +8 -0
  68. data/spec/rubyspec/core/string/dup_spec.rb +8 -0
  69. data/spec/rubyspec/core/string/end_with_spec.rb +5 -1
  70. data/spec/rubyspec/core/string/gsub_spec.rb +15 -1
  71. data/spec/rubyspec/core/string/lines_spec.rb +9 -0
  72. data/spec/rubyspec/core/string/ljust_spec.rb +17 -0
  73. data/spec/rubyspec/core/string/match_spec.rb +25 -3
  74. data/spec/rubyspec/core/string/rindex_spec.rb +50 -0
  75. data/spec/rubyspec/core/string/rjust_spec.rb +17 -0
  76. data/spec/rubyspec/core/string/scan_spec.rb +66 -0
  77. data/spec/rubyspec/core/string/sub_spec.rb +17 -1
  78. data/spec/rubyspec/core/string/tr_s_spec.rb +31 -0
  79. data/spec/rubyspec/core/string/tr_spec.rb +31 -0
  80. data/spec/rubyspec/fixtures/constants.rb +6 -0
  81. data/spec/rubyspec/language/class_spec.rb +4 -8
  82. data/spec/rubyspec/language/numbers_spec.rb +10 -4
  83. data/spec/rubyspec/language/predefined_spec.rb +69 -2
  84. data/spec/rubyspec/library/rbconfig/config_spec.rb +47 -0
  85. data/spec/rubyspec/spec_helper.rb +6 -0
  86. metadata +46 -25
  87. data/opal/opal-eventable.rb +0 -26
  88. data/opal/opal/native.rb +0 -115
  89. data/spec/opal/eventable_spec.rb +0 -75
  90. data/spec/opal/native/element_reference_spec.rb +0 -40
  91. data/spec/opal/native/equal_spec.rb +0 -17
  92. data/spec/opal/native/fixtures/classes.rb +0 -27
  93. data/spec/opal/native/global_spec.rb +0 -12
  94. data/spec/opal/native/initialize_spec.rb +0 -8
  95. data/spec/opal/native/method_missing_spec.rb +0 -53
  96. data/spec/opal/native/to_native_spec.rb +0 -8
  97. data/spec/rubyspec/core/string/demodulize_spec.rb +0 -10
  98. data/spec/rubyspec/core/string/underscore_spec.rb +0 -17
data/opal/opal/time.rb CHANGED
@@ -3,7 +3,7 @@ short_days = %w[Sun Mon Tue Wed Thu Fri Sat]
3
3
  short_months = %w[Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec]
4
4
  long_months = %w[January Febuary March April May June July August September October November December]
5
5
 
6
- class Time < `Date`
6
+ class Time
7
7
  include Comparable
8
8
 
9
9
  def self.at(seconds, frac = 0)
data/opal/rbconfig.rb CHANGED
@@ -1,5 +1,19 @@
1
+ module RbConfig
2
+ versions = RUBY_VERSION.split('.')
3
+ CONFIG = {
4
+ 'ruby_version' => RUBY_VERSION,
5
+ 'MAJOR' => versions[0],
6
+ 'MINOR' => versions[1],
7
+ 'TEENY' => versions[2],
8
+ 'RUBY' => RUBY_ENGINE,
9
+ 'RUBY_INSTALL_NAME' => RUBY_ENGINE,
10
+ 'RUBY_SO_NAME' => RUBY_ENGINE,
11
+ 'target_os' => 'ECMA-262',
12
+ 'host_os' => 'ECMA-262',
13
+ 'PATH_SEPARATOR' => ':'
14
+ }
15
+ end
16
+
17
+ # required for mspec it would appear
1
18
  RUBY_NAME = 'opal'
2
19
  RUBY_EXE = 'opal'
3
-
4
- RUBY_VERSION = '1.9.3'
5
-
data/opal/strscan.rb CHANGED
@@ -5,7 +5,7 @@ class StringScanner
5
5
  def initialize(string)
6
6
  @string = string
7
7
  @pos = 0
8
- @matched = ''
8
+ @matched = nil
9
9
  @working = string
10
10
  end
11
11
 
@@ -15,9 +15,7 @@ class StringScanner
15
15
  result = regex.exec(#@working);
16
16
 
17
17
  if (result == null) {
18
- #@matched = '';
19
-
20
- return nil;
18
+ return this.matched = nil;
21
19
  }
22
20
  else if (typeof(result) === 'object') {
23
21
  #@pos += result[0].length;
@@ -58,4 +56,42 @@ class StringScanner
58
56
  def eos?
59
57
  `#@working.length === 0`
60
58
  end
61
- end
59
+
60
+ def skip(re)
61
+ %x{
62
+ re = new RegExp('^' + re.source)
63
+ var result = re.exec(#@working);
64
+
65
+ if (result == null) {
66
+ return this.matched = nil;
67
+ }
68
+ else {
69
+ var match_str = result[0];
70
+ var match_len = match_str.length;
71
+ this.matched = match_str;
72
+ this.pos += match_len;
73
+ this.working = this.working.substring(match_len);
74
+ return match_len;
75
+ }
76
+ }
77
+ end
78
+
79
+ def get_byte()
80
+ %x{
81
+ var result = nil;
82
+ if (this.pos < this.string.length) {
83
+ this.pos += 1;
84
+ result = this.matched = this.working.substring(0, 1);
85
+ this.working = this.working.substring(1);
86
+ }
87
+ else {
88
+ this.matched = nil;
89
+ }
90
+
91
+ return result;
92
+ }
93
+ end
94
+
95
+ # not exactly, but for now...
96
+ alias getch get_byte
97
+ end
@@ -0,0 +1,27 @@
1
+ require 'spec_helper'
2
+
3
+
4
+ %x{
5
+ function JavascriptClass(a, b, c) {
6
+ this.bar = "foo";
7
+ this.args = [a, b, c];
8
+ }
9
+ }
10
+
11
+ describe "Class#new" do
12
+ before do
13
+ @js_class = `JavascriptClass`
14
+ end
15
+
16
+ it "returns a new instance of a class" do
17
+ Object.new.class.should == Object
18
+ end
19
+
20
+ it "can create new instances of native javascript classes" do
21
+ @js_class.new.bar.should == "foo"
22
+ end
23
+
24
+ it "passed in arguments to native class constructor" do
25
+ @js_class.new(1, 2, 3).args.should == [1, 2, 3]
26
+ end
27
+ end
@@ -0,0 +1,127 @@
1
+ require 'spec_helper'
2
+
3
+ %x{
4
+ var obj = {
5
+ property: 42,
6
+
7
+ simple: function() {
8
+ return 'foo';
9
+ },
10
+
11
+ context_check: function() {
12
+ return this === obj;
13
+ },
14
+
15
+ check_args: function(a, b, c) {
16
+ return [a, b, c];
17
+ },
18
+
19
+ array: [1, 2, 3, 4],
20
+
21
+ child_object: {
22
+ grand_child: 100
23
+ }
24
+ };
25
+ }
26
+
27
+ NATIVE_OBJECT_SPEC = `obj`
28
+
29
+ describe "Native objects" do
30
+ before do
31
+ @obj = NATIVE_OBJECT_SPEC
32
+ end
33
+
34
+ describe "accessing a null/undefined property" do
35
+ it "returns nil" do
36
+ @obj.doesnt_exist.should == nil
37
+ end
38
+ end
39
+
40
+ describe "with a mid ending with '='" do
41
+ it "sets the value on the property" do
42
+ @obj.set_property = 100
43
+ @obj.set_property.should == 100
44
+
45
+ @obj.set_property = [1, 2, 3]
46
+ @obj.set_property.should == [1, 2, 3]
47
+ end
48
+ end
49
+
50
+ describe "accessing a property" do
51
+ it "returns values from the native object" do
52
+ @obj.property.should == 42
53
+ end
54
+
55
+ it "returns an array without wrapping" do
56
+ @obj.array.should == [1, 2, 3, 4]
57
+ end
58
+
59
+ end
60
+
61
+ describe "accessing a function property" do
62
+ it "forwards methods to wrapped object as native function calls" do
63
+ @obj.simple.should == "foo"
64
+ end
65
+
66
+ it "calls functions with native object as context" do
67
+ @obj.context_check.should be_true
68
+ end
69
+
70
+ it "passes each argument to native function" do
71
+ @obj.check_args(1, 2, 3).should == [1, 2, 3]
72
+ end
73
+
74
+ it "tries to convert each argument with to_native if defined" do
75
+ obj, obj2, obj3 = Object.new, Object.new, Object.new
76
+ def obj.to_native; "foo"; end
77
+ def obj2.to_native; 42; end
78
+
79
+ @obj.check_args(obj, obj2, obj3).should == ["foo", 42, obj3]
80
+ end
81
+ end
82
+
83
+ describe "#[]" do
84
+ before do
85
+ @native = %x{
86
+ {
87
+ foo: 'FOO',
88
+
89
+ str: 'LOL',
90
+ num: 42,
91
+ on: true,
92
+ off: false
93
+ };
94
+ }
95
+ end
96
+
97
+ it "returns a value from the native object" do
98
+ @native['foo'].should == "FOO"
99
+ end
100
+
101
+ it "returns nil for a key not existing on native object" do
102
+ @native['bar'].should be_nil
103
+ end
104
+
105
+ it "returns direct values for strings, numerics and booleans" do
106
+ @native['str'].should == 'LOL'
107
+ @native['num'].should == 42
108
+ @native['on'].should == true
109
+ @native['off'].should == false
110
+ end
111
+ end
112
+
113
+ describe "==" do
114
+ it "returns true if the wrapped objects are `===` to each other" do
115
+ %x{
116
+ var obj1 = {}, obj2 = {};
117
+ }
118
+
119
+ a = `obj1`
120
+ b = `obj1`
121
+ c = `obj2`
122
+
123
+ (a == b).should be_true
124
+ (a == c).should be_false
125
+ end
126
+ end
127
+ end
data/spec/ospec/runner.rb CHANGED
@@ -2,8 +2,6 @@
2
2
  require 'date'
3
3
  require 'observer'
4
4
 
5
- ENV = {}
6
-
7
5
  class OSpecFilter
8
6
  def self.main
9
7
  @main ||= self.new
@@ -0,0 +1,29 @@
1
+ describe "StringScanner#get_byte" do
2
+ before :each do
3
+ @s = StringScanner.new("abc")
4
+ end
5
+
6
+ it "should return the next byte in string" do
7
+ @s.get_byte.should == "a"
8
+ end
9
+
10
+ it "should set matched to next byte in string" do
11
+ @s.matched.should be_nil
12
+ @s.get_byte
13
+ @s.matched.should == "a"
14
+ end
15
+
16
+ it "should advance position by 1" do
17
+ @s.pos.should == 0
18
+ @s.get_byte
19
+ @s.pos.should == 1
20
+ end
21
+
22
+ it "should return nil if beyond end of string" do
23
+ @s.get_byte.should == "a"
24
+ @s.get_byte.should == "b"
25
+ @s.get_byte.should == "c"
26
+ @s.get_byte.should be_nil
27
+ @s.matched.should be_nil
28
+ end
29
+ end
@@ -0,0 +1,40 @@
1
+ describe "StringScanner#skip" do
2
+ before :each do
3
+ @s = StringScanner.new("This is a test")
4
+ end
5
+
6
+ it "returns the number of characters skipped when matched" do
7
+ @s.skip(/This/).should == 4
8
+ end
9
+
10
+ it "matches strictly at position" do
11
+ @s.skip(/This/)
12
+ @s.skip(/is/).should be_nil
13
+ end
14
+
15
+ it "returns nil when not matched" do
16
+ @s.skip(/foo/).should be_nil
17
+ end
18
+
19
+ it "advances the scan pointer when matched" do
20
+ @s.skip(/This/)
21
+ @s.pos.should == 4
22
+ end
23
+
24
+ it "does not advance the scan pointer when not matched" do
25
+ @s.skip(/foo/)
26
+ @s.pos.should == 0
27
+ end
28
+
29
+ it "captures the matched string when matched" do
30
+ @s.skip(/This/)
31
+ @s.matched.should == "This"
32
+ end
33
+
34
+ it "clears the matched string when not matched" do
35
+ @s.skip(/This/)
36
+ @s.matched.should == "This"
37
+ @s.skip(/foo/)
38
+ @s.matched.should be_nil
39
+ end
40
+ end
@@ -14,7 +14,7 @@ describe "Array#each" do
14
14
  a.should == [1, 2, 3]
15
15
  end
16
16
 
17
- it "yields each element to a block that takes multiple arguments" do
17
+ it "yields each nested element to a block that takes multiple arguments" do
18
18
  a = [[1, 2], :a, [3, 4]]
19
19
  b = []
20
20
 
@@ -70,4 +70,9 @@ describe "Array#&" do
70
70
  it "does not call to_ary on array subclasses" do
71
71
  ([5, 6] & ArraySpecs::ToAryArray[1, 2, 5, 6]).should == [5, 6]
72
72
  end
73
+
74
+ it "should match native string and object string that are equivalent" do
75
+ # "value".to_sym forces creation of an Opal.String
76
+ (["value".to_sym] & ["value"]).should == ["value"]
77
+ end
73
78
  end
@@ -0,0 +1,32 @@
1
+ require File.expand_path('../../../spec_helper', __FILE__)
2
+
3
+ describe "Array#max" do
4
+ it "should return max of values in numeric array" do
5
+ [1, 2].max.should == 2
6
+ end
7
+
8
+ it "should return max of values in string array" do
9
+ ["1", "2"].max.should == "2"
10
+ end
11
+
12
+ it "should return nil if array is empty" do
13
+ [].max.should be_nil
14
+ end
15
+
16
+ it "should raise ArgumentError if array contains a non-Comparable value" do
17
+ lambda { [true, false].max }.should raise_error(ArgumentError)
18
+ end
19
+
20
+ it "should use block when specified to compare values" do
21
+ [1, 2, 3].max {|candidate, current| candidate <=> current }.should == 3
22
+ end
23
+
24
+ it "should use break value from block when block is specified" do
25
+ [1, 2, 3].max {|candidate, current| break 5 }.should == 5
26
+ end
27
+
28
+ it "should allow block to compare non-Comparable values" do
29
+ [true, false].max {|candidate, current| candidate ? 1 : -1 }.should == true
30
+ [true, false].max {|candidate, current| candidate ? -1 : 1 }.should == false
31
+ end
32
+ end
@@ -0,0 +1,32 @@
1
+ require File.expand_path('../../../spec_helper', __FILE__)
2
+
3
+ describe "Array#min" do
4
+ it "should return min of values in numeric array" do
5
+ [1, 2].min.should == 1
6
+ end
7
+
8
+ it "should return min of values in string array" do
9
+ ["1", "2"].min.should == "1"
10
+ end
11
+
12
+ it "should return nil if array is empty" do
13
+ [].min.should be_nil
14
+ end
15
+
16
+ it "should raise ArgumentError if array contains a non-Comparable value" do
17
+ lambda { [true, false].min }.should raise_error(ArgumentError)
18
+ end
19
+
20
+ it "should use block when specified to compare values" do
21
+ [1, 2, 3].min {|candidate, current| candidate <=> current }.should == 1
22
+ end
23
+
24
+ it "should use break value from block when block is specified" do
25
+ [1, 2, 3].max {|candidate, current| break -5 }.should == -5
26
+ end
27
+
28
+ it "should allow block to compare non-Comparable values" do
29
+ [true, false].min {|candidate, current| candidate ? 1 : -1 }.should == false
30
+ [true, false].min {|candidate, current| candidate ? -1 : 1 }.should == true
31
+ end
32
+ end
@@ -13,7 +13,7 @@ describe "Array#-" do
13
13
  end
14
14
 
15
15
  ruby_bug "#", "1.8.6.277" do
16
- pending "properly handles recursive arrays" do
16
+ it "properly handles recursive arrays" do
17
17
  empty = ArraySpecs.empty_recursive_array
18
18
  (empty - empty).should == []
19
19
 
@@ -24,14 +24,16 @@ describe "Array#-" do
24
24
  end
25
25
  end
26
26
 
27
- pending "tries to convert the passed arguments to Arrays using #to_ary" do
27
+ it "tries to convert the passed arguments to Arrays using #to_ary" do
28
28
  obj = mock('[2,3,3,4]')
29
- obj.should_receive(:to_ary).and_return([2, 3, 3, 4])
29
+ def obj.to_ary; [2,3,3,4] ; end
30
30
  ([1, 1, 2, 2, 3, 4] - obj).should == [1, 1]
31
31
  end
32
32
 
33
- pending "raises a TypeError if the argument cannot be coerced to an Array by calling #to_ary" do
34
- obj = mock('not an array')
33
+ it "raises a TypeError if the argument cannot be coerced to an Array by calling #to_ary" do
34
+ obj = 'not an array'
35
+ lambda { [1, 2, 3] - obj }.should raise_error(TypeError)
36
+ obj = mock("123")
35
37
  lambda { [1, 2, 3] - obj }.should raise_error(TypeError)
36
38
  end
37
39