opal 0.3.2 → 0.3.6

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 (203) hide show
  1. data/README.md +85 -127
  2. data/bin/opal +3 -3
  3. data/lib/core.rb +114 -0
  4. data/{gems/core/lib → lib}/core/array.rb +68 -187
  5. data/{gems/core/lib → lib}/core/basic_object.rb +22 -5
  6. data/lib/core/class.rb +38 -0
  7. data/{gems/core/lib → lib}/core/dir.rb +1 -1
  8. data/lib/core/enumerable.rb +33 -0
  9. data/{gems/core/lib → lib}/core/error.rb +1 -4
  10. data/{gems/core/lib → lib}/core/false_class.rb +0 -0
  11. data/{gems/core/lib → lib}/core/file.rb +11 -3
  12. data/{gems/core/lib → lib}/core/hash.rb +12 -0
  13. data/{gems/core/lib → lib}/core/kernel.rb +114 -86
  14. data/lib/core/match_data.rb +33 -0
  15. data/lib/core/module.rb +97 -0
  16. data/{gems/core/lib → lib}/core/nil_class.rb +0 -0
  17. data/{gems/core/lib → lib}/core/numeric.rb +5 -0
  18. data/{gems/core/lib → lib}/core/object.rb +0 -0
  19. data/{gems/core/lib → lib}/core/proc.rb +13 -3
  20. data/{gems/core/lib → lib}/core/range.rb +10 -0
  21. data/{gems/core/lib → lib}/core/regexp.rb +33 -1
  22. data/{gems/core/lib → lib}/core/string.rb +25 -10
  23. data/{gems/core/lib → lib}/core/symbol.rb +3 -3
  24. data/{gems/core/lib → lib}/core/top_self.rb +0 -0
  25. data/{gems/core/lib → lib}/core/true_class.rb +0 -0
  26. data/lib/dev.rb +169 -0
  27. data/{gems/ospec/lib → lib}/ospec.rb +1 -0
  28. data/lib/ospec/autorun.rb +8 -0
  29. data/{gems/ospec/lib → lib}/ospec/dsl.rb +2 -2
  30. data/{gems/ospec/lib → lib}/ospec/example.rb +0 -0
  31. data/{gems/ospec/lib → lib}/ospec/example/before_and_after_hooks.rb +0 -0
  32. data/{gems/ospec/lib → lib}/ospec/example/errors.rb +0 -0
  33. data/{gems/ospec/lib → lib}/ospec/example/example_group.rb +0 -0
  34. data/{gems/ospec/lib → lib}/ospec/example/example_group_factory.rb +0 -3
  35. data/{gems/ospec/lib → lib}/ospec/example/example_group_hierarchy.rb +4 -3
  36. data/{gems/ospec/lib → lib}/ospec/example/example_group_methods.rb +1 -1
  37. data/{gems/ospec/lib → lib}/ospec/example/example_group_proxy.rb +3 -2
  38. data/{gems/ospec/lib → lib}/ospec/example/example_methods.rb +1 -1
  39. data/{gems/ospec/lib → lib}/ospec/example/example_proxy.rb +7 -7
  40. data/{gems/ospec/lib → lib}/ospec/expectations.rb +0 -0
  41. data/{gems/ospec/lib → lib}/ospec/expectations/errors.rb +0 -0
  42. data/{gems/ospec/lib → lib}/ospec/expectations/fail_with.rb +4 -3
  43. data/{gems/ospec/lib → lib}/ospec/expectations/handler.rb +6 -0
  44. data/lib/ospec/helpers/scratch.rb +18 -0
  45. data/{gems/ospec/lib → lib}/ospec/matchers.rb +2 -2
  46. data/{gems/ospec/lib → lib}/ospec/matchers/be.rb +0 -0
  47. data/{gems/ospec/lib → lib}/ospec/matchers/generated_descriptions.rb +0 -0
  48. data/{gems/ospec/lib → lib}/ospec/matchers/operator_matcher.rb +2 -0
  49. data/lib/ospec/matchers/raise_error.rb +38 -0
  50. data/lib/ospec/runner.rb +90 -0
  51. data/{gems/ospec/lib → lib}/ospec/runner/example_group_runner.rb +10 -13
  52. data/lib/ospec/runner/formatter/html_formatter.rb +139 -0
  53. data/{gems/ospec/lib → lib}/ospec/runner/formatter/terminal_formatter.rb +0 -0
  54. data/{gems/ospec/lib → lib}/ospec/runner/options.rb +1 -3
  55. data/{gems/ospec/lib → lib}/ospec/runner/reporter.rb +0 -9
  56. data/lib/racc/parser.rb +165 -0
  57. data/lib/strscan.rb +52 -0
  58. data/{lib → opal_lib}/opal.rb +2 -0
  59. data/opal_lib/opal/build_methods.rb +51 -0
  60. data/opal_lib/opal/builder.rb +164 -0
  61. data/opal_lib/opal/bundle.rb +70 -0
  62. data/opal_lib/opal/command.rb +68 -0
  63. data/{lib → opal_lib}/opal/context.rb +21 -9
  64. data/{lib → opal_lib}/opal/context/console.rb +0 -2
  65. data/opal_lib/opal/context/file_system.rb +34 -0
  66. data/{lib → opal_lib}/opal/context/loader.rb +26 -8
  67. data/opal_lib/opal/gem.rb +84 -0
  68. data/{lib → opal_lib}/opal/rake/builder_task.rb +2 -2
  69. data/opal_lib/opal/rake/spec_task.rb +32 -0
  70. data/{lib → opal_lib}/opal/ruby/nodes.rb +730 -109
  71. data/{lib → opal_lib}/opal/ruby/parser.rb +90 -23
  72. data/opal_lib/opal/ruby/ruby_parser.rb +4862 -0
  73. data/opal_lib/opal/ruby/ruby_parser.y +1454 -0
  74. data/opal_lib/opal/version.rb +4 -0
  75. data/runtime/class.js +359 -0
  76. data/runtime/debug.js +84 -0
  77. data/runtime/fs.js +199 -0
  78. data/runtime/init.js +558 -0
  79. data/runtime/loader.js +351 -0
  80. data/runtime/module.js +109 -0
  81. data/runtime/post.js +10 -0
  82. data/runtime/pre.js +7 -0
  83. data/runtime/runtime.js +351 -0
  84. metadata +88 -175
  85. data/.gitignore +0 -7
  86. data/Changelog +0 -31
  87. data/LICENSE +0 -75
  88. data/Rakefile +0 -86
  89. data/gems/core/README.md +0 -14
  90. data/gems/core/Rakefile +0 -8
  91. data/gems/core/core.gemspec +0 -13
  92. data/gems/core/lib/core.rb +0 -34
  93. data/gems/core/lib/core/class.rb +0 -31
  94. data/gems/core/lib/core/module.rb +0 -100
  95. data/gems/core/lib/core/vm.rb +0 -16
  96. data/gems/core/spec/core/array/append_spec.rb +0 -30
  97. data/gems/core/spec/core/array/assoc_spec.rb +0 -29
  98. data/gems/core/spec/core/array/at_spec.rb +0 -37
  99. data/gems/core/spec/core/array/clear_spec.rb +0 -22
  100. data/gems/core/spec/core/array/collect_bang_spec.rb +0 -27
  101. data/gems/core/spec/core/array/collect_spec.rb +0 -27
  102. data/gems/core/spec/core/array/compact_spec.rb +0 -41
  103. data/gems/core/spec/core/array/concat_spec.rb +0 -15
  104. data/gems/core/spec/core/array/constructor_spec.rb +0 -14
  105. data/gems/core/spec/core/array/each_spec.rb +0 -9
  106. data/gems/core/spec/core/array/element_reference_spec.rb +0 -4
  107. data/gems/core/spec/core/array/first_spec.rb +0 -35
  108. data/gems/core/spec/core/array/include_spec.rb +0 -9
  109. data/gems/core/spec/core/array/join_spec.rb +0 -6
  110. data/gems/core/spec/core/array/last_spec.rb +0 -51
  111. data/gems/core/spec/core/array/length_spec.rb +0 -6
  112. data/gems/core/spec/core/array/map_spec.rb +0 -33
  113. data/gems/core/spec/core/array/reverse_spec.rb +0 -6
  114. data/gems/core/spec/core/builtin_constants/builtin_constants_spec.rb +0 -7
  115. data/gems/core/spec/core/false/and_spec.rb +0 -10
  116. data/gems/core/spec/core/false/inspect_spec.rb +0 -6
  117. data/gems/core/spec/core/false/or_spec.rb +0 -10
  118. data/gems/core/spec/core/false/to_s_spec.rb +0 -6
  119. data/gems/core/spec/core/false/xor_spec.rb +0 -10
  120. data/gems/core/spec/core/file/join_spec.rb +0 -19
  121. data/gems/core/spec/core/hash/assoc_spec.rb +0 -32
  122. data/gems/core/spec/core/kernel/instance_eval_spec.rb +0 -0
  123. data/gems/core/spec/core/kernel/loop_spec.rb +0 -24
  124. data/gems/core/spec/core/kernel/raise_spec.rb +0 -0
  125. data/gems/core/spec/core/module/attr_accessor_spec.rb +0 -28
  126. data/gems/core/spec/core/number/lt_spec.rb +0 -12
  127. data/gems/core/spec/core/string/sub_spec.rb +0 -24
  128. data/gems/core/spec/core/true/and_spec.rb +0 -10
  129. data/gems/core/spec/core/true/inspect_spec.rb +0 -6
  130. data/gems/core/spec/core/true/or_spec.rb +0 -10
  131. data/gems/core/spec/core/true/to_s_spec.rb +0 -6
  132. data/gems/core/spec/core/true/xor_spec.rb +0 -10
  133. data/gems/core/spec/language/and_spec.rb +0 -61
  134. data/gems/core/spec/language/array_spec.rb +0 -68
  135. data/gems/core/spec/language/block_spec.rb +0 -38
  136. data/gems/core/spec/language/break_spec.rb +0 -36
  137. data/gems/core/spec/language/case_spec.rb +0 -103
  138. data/gems/core/spec/language/def_spec.rb +0 -21
  139. data/gems/core/spec/language/eigenclass_spec.rb +0 -60
  140. data/gems/core/spec/language/file_spec.rb +0 -13
  141. data/gems/core/spec/language/fixtures/block.rb +0 -21
  142. data/gems/core/spec/language/fixtures/super.rb +0 -293
  143. data/gems/core/spec/language/hash_spec.rb +0 -29
  144. data/gems/core/spec/language/if_spec.rb +0 -54
  145. data/gems/core/spec/language/loop_spec.rb +0 -11
  146. data/gems/core/spec/language/metaclass_spec.rb +0 -21
  147. data/gems/core/spec/language/method_spec.rb +0 -124
  148. data/gems/core/spec/language/next_spec.rb +0 -25
  149. data/gems/core/spec/language/or_spec.rb +0 -34
  150. data/gems/core/spec/language/redo_spec.rb +0 -24
  151. data/gems/core/spec/language/regexp_spec.rb +0 -26
  152. data/gems/core/spec/language/rescue_spec.rb +0 -20
  153. data/gems/core/spec/language/return_spec.rb +0 -47
  154. data/gems/core/spec/language/string_spec.rb +0 -25
  155. data/gems/core/spec/language/super_spec.rb +0 -32
  156. data/gems/core/spec/language/until_spec.rb +0 -157
  157. data/gems/core/spec/language/variables_spec.rb +0 -155
  158. data/gems/core/spec/language/while_spec.rb +0 -163
  159. data/gems/core/spec/spec_helper.rb +0 -5
  160. data/gems/core_fs/README.md +0 -19
  161. data/gems/dev/Rakefile +0 -5
  162. data/gems/dev/lib/dev.js +0 -99
  163. data/gems/dev/lib/dev/generator.js +0 -1264
  164. data/gems/dev/lib/dev/parser.js +0 -979
  165. data/gems/dev/lib/dev/ruby_parser.js +0 -1088
  166. data/gems/dev/lib/dev/ruby_parser.y +0 -1267
  167. data/gems/dev/lib/dev/string_scanner.js +0 -38
  168. data/gems/dev/tools/racc2js/README.md +0 -39
  169. data/gems/dev/tools/racc2js/math_parser.js +0 -222
  170. data/gems/dev/tools/racc2js/math_parser.rb +0 -133
  171. data/gems/dev/tools/racc2js/math_parser.y +0 -28
  172. data/gems/dev/tools/racc2js/parser.js +0 -218
  173. data/gems/dev/tools/racc2js/racc2js.rb +0 -153
  174. data/gems/json/README.md +0 -4
  175. data/gems/json/json.gemspec +0 -14
  176. data/gems/json/lib/json.rb +0 -64
  177. data/gems/json/lib/json/ext.rb +0 -51
  178. data/gems/json/lib/json/json2.js +0 -481
  179. data/gems/ospec/README.md +0 -0
  180. data/gems/ospec/lib/ospec/autorun.rb +0 -3
  181. data/gems/ospec/lib/ospec/runner.rb +0 -40
  182. data/gems/ospec/lib/ospec/runner/formatter/html_formatter.rb +0 -91
  183. data/gems/ospec/ospec.gemspec +0 -0
  184. data/gems/rquery/README.md +0 -9
  185. data/gems/rquery/lib/rquery.rb +0 -10
  186. data/gems/rquery/lib/rquery/ajax.rb +0 -4
  187. data/gems/rquery/lib/rquery/css.rb +0 -96
  188. data/gems/rquery/lib/rquery/document.rb +0 -25
  189. data/gems/rquery/lib/rquery/element.rb +0 -292
  190. data/gems/rquery/lib/rquery/event.rb +0 -108
  191. data/gems/rquery/lib/rquery/jquery.js +0 -8177
  192. data/gems/rquery/lib/rquery/request.rb +0 -138
  193. data/gems/rquery/lib/rquery/response.rb +0 -49
  194. data/gems/rquery/rquery.gemspec +0 -16
  195. data/lib/opal.js +0 -1597
  196. data/lib/opal/builder.rb +0 -117
  197. data/lib/opal/bundle.rb +0 -131
  198. data/lib/opal/command.rb +0 -11
  199. data/lib/opal/context/file_system.rb +0 -19
  200. data/lib/opal/gem.rb +0 -153
  201. data/lib/opal/ruby/ruby_parser.rb +0 -5170
  202. data/lib/opal/ruby/ruby_parser.y +0 -1298
  203. data/opal.gemspec +0 -15
@@ -0,0 +1,33 @@
1
+ class MatchData
2
+
3
+ def inspect
4
+ "#<MatchData #{`self.$data[0]`.inspect}>"
5
+ end
6
+
7
+ def to_s
8
+ `return self.$data[0];`
9
+ end
10
+
11
+ def length
12
+ `return self.$data.length;`
13
+ end
14
+
15
+ def size
16
+ `return self.$data.length;`
17
+ end
18
+
19
+ def to_a
20
+ `return [].slice.call(self.$data, 0);`
21
+ end
22
+
23
+ def [](index)
24
+ `var length = self.$data.length;
25
+
26
+ if (index < 0) index += length;
27
+
28
+ if (index >= length || index < 0) return nil;
29
+
30
+ return self.$data[index];`
31
+ end
32
+ end
33
+
@@ -0,0 +1,97 @@
1
+ # Implements the core functionality of modules. This is inherited from
2
+ # by instances of {Class}, so these methods are also available to
3
+ # classes.
4
+ class Module
5
+
6
+ def name
7
+ `return self.__classid__;`
8
+ end
9
+
10
+ def ===(obj)
11
+ obj.kind_of? self
12
+ end
13
+
14
+ def define_method(method_id, &block)
15
+ raise LocalJumpError, "no block given" unless block_given?
16
+ `$rb.dm(self, #{method_id.to_s}.toString(), block)`
17
+ nil
18
+ end
19
+
20
+ def attr_accessor(*attrs)
21
+ attr_reader *attrs
22
+ attr_writer *attrs
23
+ end
24
+
25
+ def attr_reader(*attrs)
26
+ attrs.each do |a|
27
+ method_id = a.to_s
28
+ `$rb.dm(self, method_id, function(self) {
29
+ var iv = self['@' + method_id];
30
+ return iv == undefined ? nil : iv;
31
+ });`
32
+ end
33
+ nil
34
+ end
35
+
36
+ def attr_writer(*attrs)
37
+ attrs.each do |a|
38
+ method_id = a.to_s
39
+ `$rb.dm(self, method_id + '=', function(self, val) {
40
+ return self['@' + method_id] = val;
41
+ });`
42
+ end
43
+ nil
44
+ end
45
+
46
+ def alias_method(new_name, old_name)
47
+ `$rb.alias_method(self, #{new_name.to_s}, #{old_name.to_s});`
48
+ self
49
+ end
50
+
51
+ def instance_methods
52
+ `return self.$methods;`
53
+ end
54
+
55
+ def ancestors
56
+ `var ary = [], parent = self;
57
+
58
+ while (parent) {
59
+ if (parent.$flags & $rb.FL_SINGLETON) {
60
+ // nothing?
61
+ }
62
+ else {
63
+ ary.push(parent);
64
+ }
65
+
66
+ parent = parent.$super;
67
+ }
68
+
69
+ return ary;`
70
+ end
71
+
72
+ def to_s
73
+ `return self.__classid__;`
74
+ end
75
+
76
+ def const_set(id, value)
77
+ `return $rb.cs(self, #{id.to_s}, value);`
78
+ end
79
+
80
+ def class_eval(str = nil, &block)
81
+ if block_given?
82
+ `block(self)`
83
+ else
84
+ raise "need to compile str"
85
+ end
86
+ end
87
+
88
+ def module_eval(str = nil, &block)
89
+ class_eval str, &block
90
+ end
91
+
92
+ def extend(mod)
93
+ `$rb.extend_module(self, mod)`
94
+ nil
95
+ end
96
+ end
97
+
File without changes
@@ -326,6 +326,7 @@ class Numeric
326
326
  #
327
327
  # @return [Numeric] returns the receiver
328
328
  def times
329
+ raise "no block given" unless block_given?
329
330
  `for (var i = 0; i < self; i++) {
330
331
  #{yield `i`};
331
332
  }
@@ -406,5 +407,9 @@ class Numeric
406
407
  def to_i
407
408
  `return parseInt(self);`
408
409
  end
410
+
411
+ def self.allocate
412
+ raise RuntimeError, "cannot instantiate instance of Numeric class"
413
+ end
409
414
  end
410
415
 
File without changes
@@ -30,7 +30,9 @@
30
30
  class Proc
31
31
 
32
32
  def self.new(&block)
33
- raise "No block given" unless block_given?
33
+ raise ArgumentError,
34
+ "tried to create Proc object without a block" unless block_given?
35
+
34
36
  block
35
37
  end
36
38
 
@@ -39,7 +41,15 @@ class Proc
39
41
  end
40
42
 
41
43
  def call(*args)
42
- # `args.unshift(self.$self);
43
- `return self.apply(self.$self, args);`
44
+ `args.unshift(self.$proc[0]); return self.apply(null, args);`
45
+ end
46
+
47
+ def to_s
48
+ `return "#<Proc:0x" + (self.$hash() * 400487).toString(16) + (self.$lambda ? ' (lambda)' : '') + ">";`
49
+ end
50
+
51
+ def lambda?
52
+ `return self.$fn.$lambda ? Qtrue : Qfalse;`
44
53
  end
45
54
  end
55
+
@@ -1,5 +1,15 @@
1
1
  class Range
2
2
 
3
+ def begin
4
+ `return self.$beg;`
5
+ end
6
+
7
+ alias_method :first, :begin
8
+
9
+ def end
10
+ `return self.$end;`
11
+ end
12
+
3
13
  def to_s
4
14
  `var str = #{`self.$beg`.to_s};
5
15
  var str2 = #{`self.$end`.to_s};
@@ -18,10 +18,23 @@
18
18
  # particular browsers documentation might point to differences
19
19
  # discovered. The majority of regexp syntax is typically the same.
20
20
  class Regexp
21
+
22
+ def self.escape(s)
23
+ s
24
+ end
25
+
26
+ def self.new(s)
27
+ `return new RegExp(s);`
28
+ end
29
+
21
30
  def inspect
22
31
  `return self.toString();`
23
32
  end
24
33
 
34
+ def to_s
35
+ `return self.source;`
36
+ end
37
+
25
38
  def ==(other)
26
39
  `return self.toString() === other.toString() ? Qtrue : Qfalse;`
27
40
  end
@@ -30,8 +43,27 @@ class Regexp
30
43
  self == other
31
44
  end
32
45
 
46
+ # Match - matches the regular expression against the given string. If
47
+ # the string matches, the index of the match is returned. Otherwise,
48
+ # `nil` is returned to imply no match.
49
+ #
50
+ # @param [String] str The string to match
51
+ # @return [Numeric, nil]
52
+ def =~(str)
53
+ `var result = self.exec(str);
54
+ $rb.X = result;
55
+
56
+ if (result) {
57
+ return result.index;
58
+ }
59
+ else {
60
+ return nil;
61
+ }`
62
+ end
63
+
33
64
  def match(pattern)
34
- nil
65
+ self =~ pattern
66
+ $~
35
67
  end
36
68
  end
37
69
 
@@ -126,8 +126,8 @@ class String
126
126
  `return self.toLowerCase();`
127
127
  end
128
128
 
129
- def to_s
130
- self
129
+ def upcase
130
+ `return self.toUpperCase();`
131
131
  end
132
132
 
133
133
  # Returns a printable version of `self`, surrounded with quotation marks, with
@@ -172,6 +172,10 @@ class String
172
172
  `return self.length;`
173
173
  end
174
174
 
175
+ def to_i
176
+ `return parseInt(self);`
177
+ end
178
+
175
179
  # Returns the corresponding symbol for the receiver.
176
180
  #
177
181
  # @example
@@ -185,11 +189,11 @@ class String
185
189
  #
186
190
  # @return [Symbol]
187
191
  def to_sym
188
- `return VM.Y(self);`
192
+ `return $rb.Y(self);`
189
193
  end
190
194
 
191
195
  def intern
192
- `return VM.Y(self);`
196
+ `return $rb.Y(self);`
193
197
  end
194
198
 
195
199
  # Returns a new string with the characters from `self` in reverse order.
@@ -204,18 +208,26 @@ class String
204
208
  `return self.split('').reverse().join('');`
205
209
  end
206
210
 
211
+ def succ
212
+ `return String.fromCharCode(self.charCodeAt(0));`
213
+ end
214
+
215
+ def [](idx)
216
+ `return self.substr(idx, idx + 1);`
217
+ end
218
+
207
219
  def sub(pattern)
208
220
  `return self.replace(pattern, function(str) {
209
221
  return #{yield `str`};
210
222
  });`
211
223
  end
212
224
 
213
- def gsub(pattern)
225
+ def gsub(pattern, replace)
214
226
  `var r = pattern.toString();
215
227
  r = r.substr(1, r.lastIndexOf('/') - 1);
216
228
  r = new RegExp(r, 'g');
217
229
  return self.replace(pattern, function(str) {
218
- return #{yield `str`};
230
+ return replace;
219
231
  });`
220
232
  end
221
233
 
@@ -223,7 +235,7 @@ class String
223
235
  `return self.substr(start, finish);`
224
236
  end
225
237
 
226
- def split(split)
238
+ def split(split, limit = nil)
227
239
  `return self.split(split);`
228
240
  end
229
241
 
@@ -260,13 +272,16 @@ class String
260
272
  # obj is not a regexp, then it calls =~ on it, using the receiver as an
261
273
  # argument
262
274
  #
263
- # **TODO** passing a non regexp is not currently supported
275
+ # TODO passing a non regexp is not currently supported
264
276
  #
265
277
  # @param [Regexp, Objec] obj
266
278
  # @return [Numeric, nil]
267
279
  def =~(obj)
268
- obj.match self
269
- nil
280
+ `if (obj.$flags & $rb.T_STRING) {
281
+ $rb.raise(VM.TypeError, "type mismatch: String given");
282
+ }`
283
+
284
+ obj =~ self
270
285
  end
271
286
 
272
287
  # Case-insensitive version of {#<=>}
@@ -31,11 +31,11 @@ class Symbol
31
31
  `return ':' + self.toString();`
32
32
  end
33
33
 
34
- def to_s
35
- `return self.toString();`
34
+ def to_sym
35
+ self
36
36
  end
37
37
 
38
- def to_sym
38
+ def intern
39
39
  self
40
40
  end
41
41
  end
File without changes
File without changes
data/lib/dev.rb ADDED
@@ -0,0 +1,169 @@
1
+ require 'opal/ruby/parser'
2
+
3
+ module Opal
4
+
5
+ def self.compile(source)
6
+ res = Opal::RubyParser.new(source).parse!.generate_top :debug => true
7
+ res
8
+ end
9
+
10
+ def self.run_ruby_content(source, filename = "(opal)")
11
+ js = compile source
12
+ `var exec = new Function('$rb', 'self', '__FILE__', js);
13
+ return exec($rb, $rb.top, filename);`
14
+ end
15
+
16
+ # Load the ruby code at the remote url, parse and run it. This is typically
17
+ # used when loading a script tag of type text/ruby. The filename given in the
18
+ # tag is used as the actual filename
19
+ #
20
+ # @param [String] filename
21
+ def self.run_remote_content(filename)
22
+ `var xhr;
23
+
24
+ if (window.ActiveXObject)
25
+ xhr = new window.ActiveXObject('Microsoft.XMLHTTP');
26
+ else
27
+ xhr = new XMLHttpRequest();
28
+
29
+ xhr.open('GET', filename, true);
30
+ xhr.onreadystatechange = function() {
31
+ if (xhr.readyState == 4) {
32
+ if (xhr.status == 0 || xhr.status == 200) {
33
+ #{ run_ruby_content `xhr.responseText`, filename };
34
+ } else {
35
+ #{ raise "LoadError: Cannot load: #{filename}" };
36
+ }
37
+ }
38
+ };
39
+ xhr.send(null);`
40
+ nil
41
+ end
42
+
43
+ def self.run_script_tags
44
+ `var scripts = document.getElementsByTagName('script');
45
+
46
+ for (var i = 0, ii = scripts.length; i < ii; i++) {
47
+ var script = scripts[i];
48
+
49
+ if (script.type == "text/ruby") {
50
+ if (script.src) {
51
+ #{ run_remote_content `script.src` };
52
+ } else {
53
+ #{ run_ruby_content `script.innerHTML`, "(script-tag)" };
54
+ }
55
+ }
56
+ }`
57
+
58
+ nil
59
+ end
60
+ end
61
+
62
+ `opal.compile = function(source, options) {
63
+ console.log("need to compile some code");
64
+ return #{ Opal.compile `source` };
65
+ };`
66
+
67
+ `if (typeof window !== 'undefined') {
68
+ var runner = function() { #{ Opal.run_script_tags }; };
69
+
70
+ if (window.addEventListener) {
71
+ window.addEventListener('DOMContentLoaded', runner, false);
72
+ } else {
73
+ window.attachEvent('onload', runner);
74
+ }
75
+ }`
76
+
77
+ `var repl_running = false;
78
+
79
+ opal.browser_repl = function() {
80
+ if (repl_running) return;
81
+ repl_running = true;
82
+
83
+ var html = '<div id="opal-repl" style="position: fixed; width: 100%; height: '
84
+ + '230px; bottom: 0px; overflow: scroll; border-top: 4px solid'
85
+ + '#A5A5A5; left: 0px; padding: 4px; background-color: #E5E5E5;">'
86
+
87
+ + '<div id="opal-stdout" style="font-family: \'Bitstream Vera Sans'
88
+ + 'Mono\', \'Courier\', monospace; font-size: 12px"></div>'
89
+
90
+ + '<span style="float: left; display: block; font-family: \'Bitst'
91
+ + 'ream Vera Sans Mono\', \'Courier\', monospace; font-size: '
92
+ + '12px">&gt;&gt;&nbsp;</span>'
93
+
94
+ + '<input id="opal-stdin" type="text" style="position: relative;'
95
+ + 'float: left; right: 0px; width: 500px; font-family: \'Bit'
96
+ + 'stream Vera Sans Mono\', \'Courier\', monospace;'
97
+ + 'font-size: 12px; outline-width: 0; outline: none; border:'
98
+ + '0px; padding: 0px; margin: 0px; background: none" />'
99
+
100
+ + '</div>';
101
+
102
+ var host = document.createElement('div');
103
+ host.innerHTML = html;
104
+ document.body.appendChild(host);
105
+ var opal_repl = document.getElementById('opal-repl');
106
+
107
+ var stdout = document.getElementById('opal-stdout');
108
+ var stdin = document.getElementById('opal-stdin');
109
+ var history = [], history_idx = 0;
110
+ setTimeout(function() { stdin.focus(); }, 0);
111
+
112
+ var puts_content = function(str) {
113
+ var elem = document.createElement('div');
114
+ elem.textContent == null ? elem.innerText = str : elem.textContent = str;
115
+ stdout.appendChild(elem);
116
+ };
117
+
118
+ var stdin_keydown = function(evt) {
119
+ if (evt.keyCode == 13) {
120
+ var ruby = stdin.value;
121
+
122
+ history.push(stdin.value);
123
+ history_idx = history.length;
124
+ stdin.value = '';
125
+ puts_content(">> " + ruby);
126
+
127
+ try {
128
+ puts_content("=> " + #{Opal.run_ruby_content(`ruby`, '(irb)').inspect}.toString());
129
+ }
130
+ catch (err) {
131
+ // if (err.stack) puts_content(err.stack);
132
+ //else puts_content("=> " + err.message);
133
+ puts_content("=> " + err.$klass.__classid__ + ": " + err['@message']);
134
+ }
135
+
136
+ opal_repl.scrollTop = opal_repl.scrollHeight;
137
+ }
138
+ else if (evt.keyCode == 38) {
139
+ if (history_idx > 0) {
140
+ history_idx -= 1;
141
+ stdin.value = history[history_idx];
142
+ }
143
+ }
144
+ else if (evt.keyCode == 40) {
145
+ if (history_idx < history.length - 1) {
146
+ history_idx += 1;
147
+ stdin.value = history[history_idx];
148
+ }
149
+ }
150
+ };
151
+
152
+ if (stdin.addEventListener) {
153
+ stdin.addEventListener('keydown', stdin_keydown, false);
154
+ } else {
155
+ stdin.attachEvent('onkeydown', stdin_keydown);
156
+ }
157
+
158
+ #{
159
+ def $stdout.puts(*a)
160
+ `for (var i = 0, ii = a.length; i < ii; i ++) {
161
+ puts_content(#{`a[i]`.to_s}.toString());
162
+ }`
163
+ nil
164
+ end
165
+ };
166
+
167
+ puts_content("opal REPL! Type command then <enter>.");
168
+ };`
169
+