opal 0.5.5 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (257) hide show
  1. checksums.yaml +4 -4
  2. data/.travis.yml +40 -9
  3. data/CHANGELOG.md +349 -0
  4. data/Gemfile +7 -8
  5. data/README.md +25 -3
  6. data/Rakefile +4 -2
  7. data/bin/opal +1 -1
  8. data/examples/rack/Gemfile +3 -0
  9. data/examples/rack/app/application.rb +13 -0
  10. data/examples/rack/app/user.rb +21 -0
  11. data/examples/rack/config.ru +7 -0
  12. data/examples/rack/index.html.erb +10 -0
  13. data/examples/sinatra/Gemfile +4 -0
  14. data/examples/sinatra/app/application.rb +7 -0
  15. data/examples/sinatra/config.ru +21 -0
  16. data/lib/mspec/opal/rake_task.rb +29 -8
  17. data/lib/mspec/opal/runner.rb +5 -4
  18. data/lib/opal.rb +1 -0
  19. data/lib/opal/builder.rb +0 -28
  20. data/lib/opal/cli.rb +0 -14
  21. data/lib/opal/compiler.rb +12 -11
  22. data/lib/opal/fragment.rb +8 -1
  23. data/lib/opal/nodes/array.rb +1 -1
  24. data/lib/opal/nodes/base.rb +4 -0
  25. data/lib/opal/nodes/call.rb +6 -2
  26. data/lib/opal/nodes/call_special.rb +1 -1
  27. data/lib/opal/nodes/class.rb +2 -2
  28. data/lib/opal/nodes/constants.rb +3 -1
  29. data/lib/opal/nodes/helpers.rb +23 -14
  30. data/lib/opal/nodes/if.rb +16 -9
  31. data/lib/opal/nodes/literal.rb +37 -5
  32. data/lib/opal/nodes/logic.rb +7 -1
  33. data/lib/opal/nodes/module.rb +2 -2
  34. data/lib/opal/nodes/scope.rb +13 -2
  35. data/lib/opal/nodes/top.rb +9 -0
  36. data/lib/opal/nodes/variables.rb +5 -2
  37. data/lib/opal/parser.rb +306 -71
  38. data/lib/opal/parser/grammar.rb +2667 -2775
  39. data/lib/opal/parser/grammar.y +177 -233
  40. data/lib/opal/parser/lexer.rb +511 -427
  41. data/lib/opal/parser/sexp.rb +15 -3
  42. data/lib/opal/source_map.rb +8 -4
  43. data/lib/opal/sprockets.rb +4 -0
  44. data/lib/opal/sprockets/cache_key_fix.rb +17 -0
  45. data/lib/opal/sprockets/environment.rb +21 -0
  46. data/lib/opal/sprockets/erb.rb +30 -0
  47. data/lib/opal/sprockets/processor.rb +127 -0
  48. data/lib/opal/sprockets/server.rb +166 -0
  49. data/lib/opal/util.rb +29 -0
  50. data/lib/opal/version.rb +1 -1
  51. data/opal.gemspec +1 -1
  52. data/opal/corelib/array.rb +106 -187
  53. data/opal/corelib/array/inheritance.rb +113 -0
  54. data/opal/corelib/basic_object.rb +6 -2
  55. data/opal/corelib/boolean.rb +4 -0
  56. data/opal/corelib/class.rb +2 -0
  57. data/opal/corelib/complex.rb +3 -0
  58. data/opal/corelib/enumerable.rb +75 -8
  59. data/opal/corelib/enumerator.rb +2 -0
  60. data/opal/corelib/error.rb +23 -23
  61. data/opal/corelib/hash.rb +5 -5
  62. data/opal/corelib/helpers.rb +51 -16
  63. data/opal/corelib/io.rb +7 -24
  64. data/opal/corelib/kernel.rb +23 -11
  65. data/opal/corelib/module.rb +44 -47
  66. data/opal/corelib/nil_class.rb +4 -0
  67. data/opal/corelib/numeric.rb +101 -15
  68. data/opal/corelib/range.rb +2 -0
  69. data/opal/corelib/rational.rb +3 -0
  70. data/opal/corelib/regexp.rb +36 -17
  71. data/opal/corelib/runtime.js +22 -7
  72. data/opal/corelib/string.rb +213 -110
  73. data/opal/corelib/string/inheritance.rb +78 -0
  74. data/opal/corelib/struct.rb +8 -0
  75. data/opal/corelib/time.rb +54 -42
  76. data/opal/corelib/variables.rb +24 -0
  77. data/opal/opal.rb +5 -27
  78. data/spec/cli/compiler_spec.rb +136 -0
  79. data/spec/cli/dependency_resolver_spec.rb +40 -0
  80. data/spec/cli/lexer_spec.rb +110 -0
  81. data/spec/cli/parser/alias_spec.rb +26 -0
  82. data/spec/cli/parser/and_spec.rb +13 -0
  83. data/spec/cli/parser/attrasgn_spec.rb +28 -0
  84. data/spec/cli/parser/begin_spec.rb +42 -0
  85. data/spec/cli/parser/block_spec.rb +12 -0
  86. data/spec/cli/parser/break_spec.rb +17 -0
  87. data/spec/cli/parser/call_spec.rb +139 -0
  88. data/spec/cli/parser/class_spec.rb +35 -0
  89. data/spec/cli/parser/comments_spec.rb +11 -0
  90. data/spec/cli/parser/def_spec.rb +61 -0
  91. data/spec/cli/parser/if_spec.rb +26 -0
  92. data/spec/cli/parser/iter_spec.rb +59 -0
  93. data/spec/cli/parser/lambda_spec.rb +64 -0
  94. data/spec/cli/parser/literal_spec.rb +113 -0
  95. data/spec/cli/parser/masgn_spec.rb +37 -0
  96. data/spec/cli/parser/module_spec.rb +27 -0
  97. data/spec/cli/parser/not_spec.rb +27 -0
  98. data/spec/cli/parser/op_asgn1_spec.rb +23 -0
  99. data/spec/cli/parser/op_asgn2_spec.rb +23 -0
  100. data/spec/cli/parser/or_spec.rb +13 -0
  101. data/spec/cli/parser/return_spec.rb +17 -0
  102. data/spec/cli/parser/sclass_spec.rb +21 -0
  103. data/spec/cli/parser/string_spec.rb +269 -0
  104. data/spec/cli/parser/super_spec.rb +20 -0
  105. data/spec/cli/parser/undef_spec.rb +15 -0
  106. data/spec/cli/parser/unless_spec.rb +13 -0
  107. data/spec/cli/parser/variables_spec.rb +92 -0
  108. data/spec/cli/parser/while_spec.rb +15 -0
  109. data/spec/cli/parser/yield_spec.rb +20 -0
  110. data/spec/cli/spec_helper.rb +31 -11
  111. data/spec/opal/core/array/set_range_to_array_spec.rb +7 -0
  112. data/spec/opal/core/date_spec.rb +122 -0
  113. data/spec/opal/core/language/predefined_spec.rb +1 -1
  114. data/spec/opal/core/runtime/operator_call_spec.rb +13 -0
  115. data/spec/opal/core/runtime/truthy_spec.rb +23 -0
  116. data/spec/opal/filters/bugs/array.rb +96 -87
  117. data/spec/opal/filters/bugs/basic_object.rb +9 -0
  118. data/spec/opal/filters/bugs/class.rb +16 -0
  119. data/spec/opal/filters/bugs/enumerable.rb +54 -0
  120. data/spec/opal/filters/bugs/language.rb +37 -3
  121. data/spec/opal/filters/bugs/math.rb +93 -0
  122. data/spec/opal/filters/bugs/nil.rb +7 -0
  123. data/spec/opal/filters/bugs/numeric.rb +19 -0
  124. data/spec/opal/filters/bugs/opal.rb +12 -0
  125. data/spec/opal/filters/bugs/regexp.rb +0 -2
  126. data/spec/opal/filters/bugs/string.rb +317 -19
  127. data/spec/opal/filters/bugs/struct.rb +29 -0
  128. data/spec/opal/filters/bugs/time.rb +130 -9
  129. data/spec/opal/filters/unsupported/encoding.rb +52 -4
  130. data/spec/opal/filters/unsupported/enumerator.rb +0 -3
  131. data/spec/opal/filters/unsupported/integer_size.rb +7 -0
  132. data/spec/opal/filters/unsupported/method_added.rb +10 -0
  133. data/spec/opal/filters/unsupported/mutable_strings.rb +299 -1
  134. data/spec/opal/filters/unsupported/private_constants.rb +30 -0
  135. data/spec/opal/filters/unsupported/private_methods.rb +16 -0
  136. data/spec/opal/filters/unsupported/random.rb +4 -0
  137. data/spec/opal/filters/unsupported/tainted.rb +53 -0
  138. data/spec/opal/filters/unsupported/trusted.rb +5 -0
  139. data/spec/opal/rubyspecs +167 -234
  140. data/spec/opal/spec_helper.rb +3 -0
  141. data/spec/opal/stdlib/promise/error_spec.rb +15 -0
  142. data/spec/opal/stdlib/promise/rescue_spec.rb +35 -0
  143. data/spec/opal/stdlib/promise/then_spec.rb +54 -0
  144. data/spec/opal/stdlib/promise/trace_spec.rb +35 -0
  145. data/spec/opal/stdlib/promise/value_spec.rb +15 -0
  146. data/spec/opal/stdlib/promise/when_spec.rb +34 -0
  147. data/stdlib/base64.rb +152 -0
  148. data/stdlib/date.rb +82 -49
  149. data/{opal/corelib → stdlib}/encoding.rb +3 -1
  150. data/stdlib/erb.rb +0 -1
  151. data/stdlib/json.rb +10 -26
  152. data/stdlib/math.rb +370 -0
  153. data/stdlib/native.rb +40 -33
  154. data/stdlib/opal-parser.rb +7 -4
  155. data/stdlib/promise.rb +292 -0
  156. data/stdlib/strscan.rb +1 -1
  157. data/stdlib/template.rb +1 -3
  158. data/stdlib/time.rb +9 -0
  159. metadata +143 -204
  160. data/doc/compiler.md +0 -42
  161. data/doc/compiler_options.md +0 -5
  162. data/doc/examples/node_http_server.rb +0 -49
  163. data/doc/external_libraries.md +0 -9
  164. data/doc/generated_javascript.md +0 -272
  165. data/doc/home.md +0 -17
  166. data/doc/method_missing.md +0 -58
  167. data/doc/static_applications.md +0 -60
  168. data/doc/using_ruby_from_javascript.md +0 -18
  169. data/doc/using_sprockets.md +0 -65
  170. data/spec/opal/core/numeric/abs_spec.rb +0 -12
  171. data/spec/opal/core/numeric/downto_spec.rb +0 -19
  172. data/spec/opal/core/numeric/equal_value_spec.rb +0 -9
  173. data/spec/opal/core/numeric/even_spec.rb +0 -21
  174. data/spec/opal/core/numeric/magnitude_spec.rb +0 -12
  175. data/spec/opal/core/numeric/odd_spec.rb +0 -21
  176. data/spec/opal/core/string/chop_spec.rb +0 -10
  177. data/spec/opal/core/string/chr_spec.rb +0 -13
  178. data/spec/opal/core/string/clone_spec.rb +0 -8
  179. data/spec/opal/core/string/comparison_spec.rb +0 -13
  180. data/spec/opal/core/string/dup_spec.rb +0 -8
  181. data/spec/opal/core/string/element_reference_spec.rb +0 -96
  182. data/spec/opal/core/string/fixtures/classes.rb +0 -49
  183. data/spec/opal/core/string/format_spec.rb +0 -9
  184. data/spec/opal/core/string/freeze_spec.rb +0 -15
  185. data/spec/opal/core/string/gsub_spec.rb +0 -31
  186. data/spec/opal/core/string/lines_spec.rb +0 -9
  187. data/spec/opal/core/string/ljust_spec.rb +0 -32
  188. data/spec/opal/core/string/lstrip_spec.rb +0 -7
  189. data/spec/opal/core/string/match_spec.rb +0 -49
  190. data/spec/opal/core/string/next_spec.rb +0 -10
  191. data/spec/opal/core/string/ord_spec.rb +0 -9
  192. data/spec/opal/core/string/partition_spec.rb +0 -10
  193. data/spec/opal/core/string/rindex_spec.rb +0 -50
  194. data/spec/opal/core/string/rjust_spec.rb +0 -32
  195. data/spec/opal/core/string/rstrip_spec.rb +0 -7
  196. data/spec/opal/core/string/scan_spec.rb +0 -66
  197. data/spec/opal/core/string/slice_spec.rb +0 -74
  198. data/spec/opal/core/string/split_spec.rb +0 -5
  199. data/spec/opal/core/string/strip_spec.rb +0 -6
  200. data/spec/opal/core/string/sub_spec.rb +0 -38
  201. data/spec/opal/core/string/succ_spec.rb +0 -10
  202. data/spec/opal/core/string/sum_spec.rb +0 -5
  203. data/spec/opal/core/string/to_f_spec.rb +0 -14
  204. data/spec/opal/core/string/to_i_spec.rb +0 -25
  205. data/spec/opal/core/string/tr_s_spec.rb +0 -31
  206. data/spec/opal/core/string/tr_spec.rb +0 -31
  207. data/spec/opal/filters/bugs/parser.rb +0 -10
  208. data/spec/opal/filters/unsupported/immutable_strings.rb +0 -24
  209. data/spec/opal/filters/unsupported/string_subclasses.rb +0 -8
  210. data/spec/opal/parser/alias_spec.rb +0 -26
  211. data/spec/opal/parser/and_spec.rb +0 -13
  212. data/spec/opal/parser/array_spec.rb +0 -22
  213. data/spec/opal/parser/attrasgn_spec.rb +0 -28
  214. data/spec/opal/parser/begin_spec.rb +0 -42
  215. data/spec/opal/parser/block_spec.rb +0 -12
  216. data/spec/opal/parser/break_spec.rb +0 -17
  217. data/spec/opal/parser/call_spec.rb +0 -131
  218. data/spec/opal/parser/class_spec.rb +0 -35
  219. data/spec/opal/parser/const_spec.rb +0 -13
  220. data/spec/opal/parser/cvar_spec.rb +0 -11
  221. data/spec/opal/parser/def_spec.rb +0 -61
  222. data/spec/opal/parser/false_spec.rb +0 -17
  223. data/spec/opal/parser/file_spec.rb +0 -7
  224. data/spec/opal/parser/gvar_spec.rb +0 -13
  225. data/spec/opal/parser/hash_spec.rb +0 -17
  226. data/spec/opal/parser/heredoc_spec.rb +0 -30
  227. data/spec/opal/parser/iasgn_spec.rb +0 -9
  228. data/spec/opal/parser/if_spec.rb +0 -26
  229. data/spec/opal/parser/int_spec.rb +0 -13
  230. data/spec/opal/parser/iter_spec.rb +0 -59
  231. data/spec/opal/parser/ivar_spec.rb +0 -9
  232. data/spec/opal/parser/lambda_spec.rb +0 -64
  233. data/spec/opal/parser/lasgn_spec.rb +0 -8
  234. data/spec/opal/parser/line_spec.rb +0 -8
  235. data/spec/opal/parser/lvar_spec.rb +0 -38
  236. data/spec/opal/parser/masgn_spec.rb +0 -37
  237. data/spec/opal/parser/module_spec.rb +0 -27
  238. data/spec/opal/parser/nil_spec.rb +0 -17
  239. data/spec/opal/parser/not_spec.rb +0 -27
  240. data/spec/opal/parser/nth_ref_spec.rb +0 -13
  241. data/spec/opal/parser/op_asgn1_spec.rb +0 -23
  242. data/spec/opal/parser/op_asgn2_spec.rb +0 -23
  243. data/spec/opal/parser/or_spec.rb +0 -13
  244. data/spec/opal/parser/parse_spec.rb +0 -66
  245. data/spec/opal/parser/regexp_spec.rb +0 -16
  246. data/spec/opal/parser/return_spec.rb +0 -17
  247. data/spec/opal/parser/sclass_spec.rb +0 -21
  248. data/spec/opal/parser/self_spec.rb +0 -17
  249. data/spec/opal/parser/str_spec.rb +0 -107
  250. data/spec/opal/parser/string_spec.rb +0 -8
  251. data/spec/opal/parser/super_spec.rb +0 -20
  252. data/spec/opal/parser/true_spec.rb +0 -17
  253. data/spec/opal/parser/undef_spec.rb +0 -15
  254. data/spec/opal/parser/unless_spec.rb +0 -13
  255. data/spec/opal/parser/while_spec.rb +0 -15
  256. data/spec/opal/parser/xstr_spec.rb +0 -116
  257. data/spec/opal/parser/yield_spec.rb +0 -20
@@ -0,0 +1,78 @@
1
+ class String
2
+ def self.inherited(klass)
3
+ replace = Class.new(String::Wrapper)
4
+
5
+ %x{
6
+ klass._proto = replace._proto;
7
+ klass._proto._klass = klass;
8
+ klass._alloc = replace._alloc;
9
+ klass.__parent = #{String::Wrapper};
10
+
11
+ klass.$allocate = replace.$allocate;
12
+ klass.$new = replace.$new;
13
+ }
14
+ end
15
+ end
16
+
17
+ class String::Wrapper
18
+ def self.allocate(string = "")
19
+ obj = super()
20
+ `obj.literal = string`
21
+ obj
22
+ end
23
+
24
+ def self.new(*args, &block)
25
+ obj = allocate
26
+ obj.initialize(*args, &block)
27
+ obj
28
+ end
29
+
30
+ def self.[](*objects)
31
+ allocate(objects)
32
+ end
33
+
34
+ def initialize(string = '')
35
+ @literal = string
36
+ end
37
+
38
+ def method_missing(*args, &block)
39
+ result = @literal.__send__(*args, &block)
40
+
41
+ if `result._isString != null`
42
+ if `result == #@literal`
43
+ self
44
+ else
45
+ self.class.allocate(result)
46
+ end
47
+ else
48
+ result
49
+ end
50
+ end
51
+
52
+ def initialize_copy(other)
53
+ @literal = `other.literal`.clone
54
+ end
55
+
56
+ def respond_to?(name, *)
57
+ super || @literal.respond_to?(name)
58
+ end
59
+
60
+ def ==(other)
61
+ @literal == other
62
+ end
63
+
64
+ alias eql? ==
65
+ alias === ==
66
+
67
+ def to_s
68
+ @literal
69
+ end
70
+
71
+ def to_str
72
+ self
73
+ end
74
+
75
+ def inspect
76
+ @literal.inspect
77
+ end
78
+ end
@@ -49,6 +49,10 @@ class Struct
49
49
  }
50
50
  end
51
51
 
52
+ class << self
53
+ alias [] new
54
+ end
55
+
52
56
  include Enumerable
53
57
 
54
58
  def initialize(*args)
@@ -89,12 +93,14 @@ class Struct
89
93
  return enum_for :each unless block_given?
90
94
 
91
95
  members.each { |name| yield self[name] }
96
+ self
92
97
  end
93
98
 
94
99
  def each_pair
95
100
  return enum_for :each_pair unless block_given?
96
101
 
97
102
  members.each { |name| yield name, self[name] }
103
+ self
98
104
  end
99
105
 
100
106
  def eql?(other)
@@ -130,4 +136,6 @@ class Struct
130
136
 
131
137
  result
132
138
  end
139
+
140
+ alias to_s inspect
133
141
  end
@@ -1,3 +1,5 @@
1
+ require 'corelib/comparable'
2
+
1
3
  class Time
2
4
  include Comparable
3
5
 
@@ -5,7 +7,7 @@ class Time
5
7
  var days_of_week = #{%w[Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sunday]},
6
8
  short_days = #{%w[Sun Mon Tue Wed Thu Fri Sat]},
7
9
  short_months = #{%w[Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec]},
8
- long_months = #{%w[January Febuary March April May June July August September October November December]};
10
+ long_months = #{%w[January February March April May June July August September October November December]};
9
11
  }
10
12
 
11
13
  def self.at(seconds, frac = 0)
@@ -34,7 +36,7 @@ class Time
34
36
  return new Date(year, month - 1, day, hour, minute, second);
35
37
 
36
38
  case 7:
37
- #{raise NotImplementedError};
39
+ return new Date(year, month - 1, day, hour, minute, second);
38
40
 
39
41
  default:
40
42
  return new Date();
@@ -95,28 +97,13 @@ class Time
95
97
  raise TypeError, 'missing year (got nil)' if year.nil?
96
98
 
97
99
  %x{
98
- switch (arguments.length) {
99
- case 1:
100
- return new Date(Date.UTC(year, 0));
101
-
102
- case 2:
103
- return new Date(Date.UTC(year, month - 1));
104
-
105
- case 3:
106
- return new Date(Date.UTC(year, month - 1, day));
107
-
108
- case 4:
109
- return new Date(Date.UTC(year, month - 1, day, hour));
110
-
111
- case 5:
112
- return new Date(Date.UTC(year, month - 1, day, hour, minute));
113
-
114
- case 6:
115
- return new Date(Date.UTC(year, month - 1, day, hour, minute, second));
116
-
117
- case 7:
118
- #{raise NotImplementedError};
100
+ if (month > 12 || day > 31 || hour > 24 || minute > 59 || second > 59) {
101
+ #{raise ArgumentError};
119
102
  }
103
+
104
+ var date = new Date(Date.UTC(year, (month || 1) - 1, (day || 1), (hour || 0), (minute || 0), (second || 0)));
105
+ date.tz_offset = 0
106
+ return date;
120
107
  }
121
108
  end
122
109
 
@@ -136,7 +123,11 @@ class Time
136
123
 
137
124
  other = Opal.coerce_to other, Integer, :to_int
138
125
 
139
- `new Date(self.getTime() + (other * 1000))`
126
+ %x{
127
+ var result = new Date(self.getTime() + (other * 1000));
128
+ result.tz_offset = self.tz_offset;
129
+ return result;
130
+ }
140
131
  end
141
132
 
142
133
  def -(other)
@@ -145,7 +136,11 @@ class Time
145
136
  else
146
137
  other = Opal.coerce_to other, Integer, :to_int
147
138
 
148
- `new Date(self.getTime() - (other * 1000))`
139
+ %x{
140
+ var result = new Date(self.getTime() - (other * 1000));
141
+ result.tz_offset = self.tz_offset;
142
+ return result;
143
+ }
149
144
  end
150
145
  end
151
146
 
@@ -157,6 +152,12 @@ class Time
157
152
  `#{to_f} === #{other.to_f}`
158
153
  end
159
154
 
155
+ def asctime
156
+ strftime '%a %b %e %H:%M:%S %Y'
157
+ end
158
+
159
+ alias ctime asctime
160
+
160
161
  def day
161
162
  `self.getDate()`
162
163
  end
@@ -186,7 +187,11 @@ class Time
186
187
  end
187
188
 
188
189
  def inspect
189
- `self.toString()`
190
+ if utc?
191
+ strftime '%Y-%m-%d %H:%M:%S UTC'
192
+ else
193
+ strftime '%Y-%m-%d %H:%M:%S %z'
194
+ end
190
195
  end
191
196
 
192
197
  alias mday day
@@ -227,10 +232,10 @@ class Time
227
232
  result = string.match(/[A-Z]{3,4}/)[0];
228
233
  }
229
234
  else {
230
- result = string.match(/\\([^)]+\\)/)[0].match(/[A-Z]/g).join('');
235
+ result = string.match(/\([^)]+\)/)[0].match(/[A-Z]/g).join('');
231
236
  }
232
237
 
233
- if (result == "GMT" && /(GMT\\W*\\d{4})/.test(string)) {
238
+ if (result == "GMT" && /(GMT\W*\d{4})/.test(string)) {
234
239
  return RegExp.$1;
235
240
  }
236
241
  else {
@@ -239,13 +244,25 @@ class Time
239
244
  }
240
245
  end
241
246
 
247
+ def getgm
248
+ %x{
249
+ var result = new Date(self.getTime());
250
+ result.tz_offset = 0;
251
+ return result;
252
+ }
253
+ end
254
+
255
+ def gmt?
256
+ `self.tz_offset == 0`
257
+ end
258
+
242
259
  def gmt_offset
243
260
  `-self.getTimezoneOffset() * 60`
244
261
  end
245
262
 
246
263
  def strftime(format)
247
264
  %x{
248
- return format.replace(/%([\\-_#^0]*:{0,2})(\\d+)?([EO]*)(.)/g, function(full, flags, width, _, conv) {
265
+ return format.replace(/%([\-_#^0]*:{0,2})(\d+)?([EO]*)(.)/g, function(full, flags, width, _, conv) {
249
266
  var result = "",
250
267
  width = parseInt(width),
251
268
  zero = flags.indexOf('0') !== -1,
@@ -408,11 +425,11 @@ class Time
408
425
  break;
409
426
 
410
427
  case 'n':
411
- result += "\\n";
428
+ result += "\n";
412
429
  break;
413
430
 
414
431
  case 't':
415
- result += "\\t";
432
+ result += "\t";
416
433
  break;
417
434
 
418
435
  case '%':
@@ -497,6 +514,12 @@ class Time
497
514
  `self.getDay() === 2`
498
515
  end
499
516
 
517
+ alias utc? gmt?
518
+
519
+ def utc_offset
520
+ `self.getTimezoneOffset() * -60`
521
+ end
522
+
500
523
  def wday
501
524
  `self.getDay()`
502
525
  end
@@ -509,14 +532,3 @@ class Time
509
532
  `self.getFullYear()`
510
533
  end
511
534
  end
512
-
513
- # FIXME: move this to stdlib when the corelib has its own path
514
- class Time
515
- def self.parse(str)
516
- `new Date(Date.parse(str))`
517
- end
518
-
519
- def iso8601
520
- strftime('%FT%T%z')
521
- end
522
- end
@@ -0,0 +1,24 @@
1
+ # regexp matches
2
+ $& = $~ = $` = $' = nil
3
+
4
+ # stub library path
5
+ $: = []
6
+ $" = []
7
+
8
+ # split lines
9
+ $/ = "\n"
10
+ $, = nil
11
+
12
+ ARGV = []
13
+ ARGF = Object.new
14
+ ENV = {}
15
+
16
+ $VERBOSE = false
17
+ $DEBUG = false
18
+ $SAFE = 0
19
+
20
+ RUBY_PLATFORM = 'opal'
21
+ RUBY_ENGINE = 'opal'
22
+ RUBY_VERSION = '2.0.0'
23
+ RUBY_ENGINE_VERSION = '0.6.0'
24
+ RUBY_RELEASE_DATE = '2014-03-05'
@@ -12,11 +12,14 @@ require 'corelib/comparable'
12
12
  require 'corelib/enumerable'
13
13
  require 'corelib/enumerator'
14
14
  require 'corelib/array'
15
+ require 'corelib/array/inheritance'
15
16
  require 'corelib/hash'
16
17
  require 'corelib/string'
18
+ require 'corelib/string/inheritance'
17
19
  require 'corelib/match_data'
18
- require 'corelib/encoding'
19
20
  require 'corelib/numeric'
21
+ require 'corelib/complex'
22
+ require 'corelib/rational'
20
23
  require 'corelib/proc'
21
24
  require 'corelib/method'
22
25
  require 'corelib/range'
@@ -24,29 +27,4 @@ require 'corelib/time'
24
27
  require 'corelib/struct'
25
28
  require 'corelib/io'
26
29
  require 'corelib/main'
27
- require 'native'
28
-
29
- # regexp matches
30
- $& = $~ = $` = $' = nil
31
-
32
- # stub library path
33
- $: = []
34
- $" = []
35
-
36
- # split lines
37
- $/ = "\n"
38
- $, = " "
39
-
40
- ARGV = []
41
- ARGF = Object.new
42
- ENV = {}
43
-
44
- $VERBOSE = false
45
- $DEBUG = false
46
- $SAFE = 0
47
-
48
- RUBY_PLATFORM = 'opal'
49
- RUBY_ENGINE = 'opal'
50
- RUBY_VERSION = '1.9.3'
51
- RUBY_ENGINE_VERSION = '0.5.5'
52
- RUBY_RELEASE_DATE = '2013-11-25'
30
+ require 'corelib/variables'
@@ -0,0 +1,136 @@
1
+ require File.expand_path('../spec_helper', __FILE__)
2
+
3
+ describe Opal::Compiler do
4
+ it "should compile simple ruby values" do
5
+ expect_compiled("3.142").to include("return 3.142")
6
+ expect_compiled("123e1").to include("return 1230")
7
+ expect_compiled("123E+10").to include("return 1230000000000")
8
+ expect_compiled("false").to include("return false")
9
+ expect_compiled("true").to include("return true")
10
+ expect_compiled("nil").to include("return nil")
11
+ end
12
+
13
+ it "should compile ruby strings" do
14
+ expect_compiled('"hello world"').to include('return "hello world"')
15
+ expect_compiled('"hello #{100}"').to include('"hello "', '100')
16
+ end
17
+
18
+ it "should compile method calls" do
19
+ expect_compiled("self.inspect").to include("$inspect()")
20
+ expect_compiled("self.map { |a| a + 10 }").to include("$map")
21
+ end
22
+
23
+ it "should compile constant lookups" do
24
+ expect_compiled("Object").to include("scope.Object")
25
+ expect_compiled("Array").to include("scope.Array")
26
+ end
27
+
28
+ describe "class names" do
29
+ it "generates a named function for class using $ prefix" do
30
+ expect_compiled("class Foo; end").to include("function $Foo")
31
+ end
32
+ end
33
+
34
+ describe "debugger special method" do
35
+ it "generates debugger keyword in javascript" do
36
+ expect_compiled("debugger").to include("debugger")
37
+ expect_compiled("debugger").to_not include("$debugger")
38
+ end
39
+ end
40
+
41
+ describe "DATA special variable" do
42
+ it "is not a special case unless __END__ part present in source" do
43
+ expect_compiled("DATA").to include("DATA")
44
+ expect_compiled("DATA\n__END__").to_not include("DATA")
45
+ end
46
+
47
+ it "DATA gets compiled as a reference to special $__END__ variable" do
48
+ expect_compiled("a = DATA\n__END__").to include("a = $__END__")
49
+ end
50
+
51
+ it "causes the compiler to create a reference to special __END__ variable" do
52
+ expect_compiled("DATA\n__END__\nFord Perfect").to include("$__END__ = ")
53
+ end
54
+
55
+ it "does not create a reference to __END__ vairbale unless __END__ content present" do
56
+ expect_compiled("DATA").to_not include("$__END__ = ")
57
+ end
58
+ end
59
+
60
+ describe "escapes in x-strings" do
61
+ it "compiles the exscapes directly as appearing in x-strings" do
62
+ expect_compiled('`"hello\nworld"`').to include('"hello\nworld"')
63
+ expect_compiled('%x{"hello\nworld"}').to include('"hello\nworld"')
64
+ end
65
+ end
66
+
67
+ describe "pre-processed if conditions" do
68
+ it "compiles if blocks using RUBY_ENGINE/RUBY_PLATFORM == opal" do
69
+ expect_compiled(<<-RUBY).to include("should_compile_fine")
70
+ if RUBY_ENGINE == 'opal'
71
+ :should_compile_fine
72
+ end
73
+ RUBY
74
+
75
+ expect_compiled(<<-RUBY).to include("so_should_this")
76
+ if RUBY_PLATFORM == 'opal'
77
+ :so_should_this
78
+ end
79
+ RUBY
80
+ end
81
+
82
+ it "skips elsif/else parts" do
83
+ expect_compiled(<<-RUBY).to_not include("should_be_skipped")
84
+ if RUBY_PLATFORM == "opal"
85
+ :ok
86
+ else
87
+ :should_be_skipped
88
+ end
89
+ RUBY
90
+
91
+ result = expect_compiled(<<-RUBY)
92
+ if RUBY_ENGINE == 'opal'
93
+ :this_compiles
94
+ elsif false
95
+ :this_does_not_compile
96
+ else
97
+ :this_neither
98
+ end
99
+ RUBY
100
+
101
+ result.to_not include("this_does_not_compile", "this_neither")
102
+ end
103
+
104
+ it "generates if-code as normal without check" do
105
+ expect_compiled(<<-RUBY).to include("should_compile", "and_this")
106
+ if some_conditional
107
+ :should_compile
108
+ else
109
+ :and_this
110
+ end
111
+ RUBY
112
+ end
113
+ end
114
+
115
+ describe "pre-processed unless conditionals" do
116
+ it "skips over if using RUBY_ENGINE/RUBY_PLATFORM == 'opal'" do
117
+ expect_compiled(<<-RUBY).to_not include("should_not_compile")
118
+ unless RUBY_ENGINE == 'opal'
119
+ :should_not_compile
120
+ end
121
+ RUBY
122
+ end
123
+
124
+ it "generates unless code as normal if no check" do
125
+ expect_compiled(<<-RUBY).to include("this_should_compile")
126
+ unless this_is_true
127
+ :this_should_compile
128
+ end
129
+ RUBY
130
+ end
131
+ end
132
+
133
+ def expect_compiled(source)
134
+ expect(Opal::Compiler.new.compile source)
135
+ end
136
+ end