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
data/runtime/init.js ADDED
@@ -0,0 +1,558 @@
1
+
2
+ /**
3
+ Sets the constant value `val` on the given `klass` as `id`.
4
+
5
+ @param {RClass} klass
6
+ @param {String} id
7
+ @param {Object} val
8
+ @return {Object} returns the set value
9
+ */
10
+ function const_set(klass, id, val) {
11
+ klass.$c_prototype[id] = val;
12
+ klass.$const_table[id] = val;
13
+ return val;
14
+ }
15
+
16
+ /**
17
+ Lookup a constant named `id` on the `klass`. This will throw an error if
18
+ the constant cannot be found.
19
+
20
+ @param {RClass} klass
21
+ @param {String} id
22
+ */
23
+ function const_get(klass, id) {
24
+ if (klass.$c[id]) {
25
+ return (klass.$c[id]);
26
+ }
27
+
28
+ var parent = klass.$parent;
29
+
30
+ while (parent && parent != cObject) {
31
+ // while (parent) {
32
+ if (parent.$c[id] !== undefined) {
33
+ return parent.$c[id];
34
+ }
35
+
36
+ parent = parent.$parent;
37
+ }
38
+
39
+ raise(eNameError, 'uninitialized constant ' + id);
40
+ };
41
+
42
+ Rt.const_get = const_get;
43
+
44
+ /**
45
+ Returns true or false depending whether a constant named `id` is defined
46
+ on the receiver `klass`.
47
+
48
+ @param {RClass} klass
49
+ @param {String} id
50
+ @return {true, false}
51
+ */
52
+ function const_defined(klass, id) {
53
+ if (klass.$c[id]) {
54
+ return true;
55
+ }
56
+
57
+ return false;
58
+ };
59
+
60
+ /**
61
+ This table holds all the global variables accessible from ruby.
62
+
63
+ Entries are mapped by their global id => an object that contains the
64
+ given keys:
65
+
66
+ - name
67
+ - value
68
+ - getter
69
+ - setter
70
+ */
71
+ var global_tbl = {};
72
+
73
+ /**
74
+ Defines a hooked/global variable.
75
+
76
+ @param {String} name The global name (e.g. '$:')
77
+ @param {Function} getter The getter function to return the variable
78
+ @param {Function} setter The setter function used for setting the var
79
+ @return {null}
80
+ */
81
+ function define_hooked_variable(name, getter, setter) {
82
+ var entry = {
83
+ "name": name,
84
+ "value": Qnil,
85
+ "getter": getter,
86
+ "setter": setter
87
+ };
88
+
89
+ global_tbl[name] = entry;
90
+ };
91
+
92
+ /**
93
+ A default read only getter for a global variable. This will simply throw a
94
+ name error with the given id. This can be used for variables that should
95
+ not be altered.
96
+ */
97
+ function gvar_readonly_setter(id, value) {
98
+ raise(eNameError, id + " is a read-only variable");
99
+ };
100
+
101
+ /**
102
+ Retrieve a global variable. This will use the assigned getter.
103
+ */
104
+ function gvar_get(id) {
105
+ var entry = global_tbl[id];
106
+ if (!entry) { return Qnil; }
107
+ return entry.getter(id);
108
+ };
109
+
110
+ /**
111
+ Set a global. If not already set, then we assign basic getters and setters.
112
+ */
113
+ function gvar_set(id, value) {
114
+ var entry = global_tbl[id];
115
+ if (entry) { return entry.setter(id, value); }
116
+
117
+ define_hooked_variable(id,
118
+
119
+ function(id) {
120
+ return global_tbl[id].value;
121
+ },
122
+
123
+ function(id, value) {
124
+ return (global_tbl[id].value = value);
125
+ }
126
+ );
127
+
128
+ return gvar_set(id, value);
129
+ };
130
+
131
+ /**
132
+ Every object has a unique id. This count is used as the next id for the
133
+ next created object. Therefore, first ruby object has id 0, next has 1 etc.
134
+ */
135
+ var hash_yield = 0;
136
+
137
+ /**
138
+ Yield the next object id, updating the count, and returning it.
139
+ */
140
+ function yield_hash() {
141
+ return hash_yield++;
142
+ };
143
+
144
+ var cHash;
145
+
146
+ /**
147
+ Returns a new hash with values passed from the runtime.
148
+ */
149
+ Rt.H = function() {
150
+ var hash = new RObject(cHash), k, v, args = [].slice.call(arguments);
151
+ hash.$keys = [];
152
+ hash.$assocs = {};
153
+ hash.$default = Qnil;
154
+
155
+ for (var i = 0, ii = args.length; i < ii; i++) {
156
+ k = args[i];
157
+ v = args[i + 1];
158
+ i++;
159
+ hash.$keys.push(k);
160
+ hash.$assocs[k.$hash()] = v;
161
+ }
162
+
163
+ return hash;
164
+ };
165
+
166
+ var alias_method = Rt.alias_method = function(klass, new_name, old_name) {
167
+ var body = klass.$m_tbl[old_name];
168
+
169
+ if (!body) {
170
+ throw new Error("NameError: undefined method `" + old_name + "' for class `" + klass.__classid__ + "'");
171
+ }
172
+
173
+ define_raw_method(klass, new_name, body, body);
174
+ return Qnil;
175
+ };
176
+
177
+ /**
178
+ This does the main work, but does not call runtime methods like
179
+ singleton_method_added etc. define_method does that.
180
+
181
+ */
182
+ function define_raw_method(klass, private_name, private_body, public_body) {
183
+ var public_name = '$' + private_name;
184
+
185
+ klass.$m_tbl[private_name] = private_body;
186
+ klass.$m_tbl[public_name] = public_body;
187
+
188
+ klass.$method_table[private_name] = private_body;
189
+
190
+ var included_in = klass.$included_in, includee;
191
+
192
+ if (included_in) {
193
+ for (var i = 0, ii = included_in.length; i < ii; i++) {
194
+ includee = included_in[i];
195
+
196
+ define_raw_method(includee, private_name, private_body, public_body);
197
+ }
198
+ }
199
+ };
200
+
201
+ Rt.private_methods = function(klass, args) {
202
+ return;
203
+
204
+ if (args.length) {
205
+ var proto = klass.allocator.prototype;
206
+
207
+ for (var i = 0, ii = args.length; i < ii; i++) {
208
+ var arg = args[i].$m$to_s(), mid = 'm$' + arg;
209
+
210
+ // If method doesn't exist throw an error. Also check that if it
211
+ // does exist that it isnt just a method missing implementation.
212
+ if (!proto[mid] || proto[mid].$rbMM) {
213
+ raise(eNameError, "undefined method `" + arg +
214
+ "' for class `" + klass.__classid__ + "'");
215
+ }
216
+
217
+ // Set the public implementation to a function that just throws
218
+ // and error when called
219
+ klass.allocator.prototype[mid] = function() {
220
+ raise(eNoMethodError, "private method `" + arg + "' called for " +
221
+ this.$m$inspect());
222
+ }
223
+
224
+ // If this method is in the method_table then we must also set that.
225
+ // If not then we inherited this method from further up the chain,
226
+ // so we do not set it in our method table.
227
+ if (klass.$method_table[mid]) {
228
+ // set
229
+ }
230
+ }
231
+ }
232
+ else {
233
+ // no args - set klass mode to private
234
+ klass.$mode = FL_PRIVATE;
235
+ }
236
+ };
237
+
238
+ function define_alias(base, new_name, old_name) {
239
+ define_method(base, new_name, base.$m_tbl[old_name]);
240
+ return Qnil;
241
+ };
242
+
243
+ function obj_alloc(klass) {
244
+ var result = new RObject(klass);
245
+ return result;
246
+ };
247
+
248
+ /**
249
+ Raise the exception class with the given string message.
250
+ */
251
+ function raise(exc, str) {
252
+ if (str === undefined) {
253
+ str = exc;
254
+ exc = eException;
255
+ }
256
+
257
+ var exception = exc.$m['new'](exc, str);
258
+ raise_exc(exception);
259
+ };
260
+
261
+ Rt.raise = raise;
262
+
263
+ /**
264
+ Raise an exception instance (DO NOT pass strings to this)
265
+ */
266
+ function raise_exc(exc) {
267
+ throw exc;
268
+ };
269
+
270
+ Rt.raise_exc = raise_exc;
271
+
272
+ var cString, cSymbol;
273
+
274
+ /**
275
+ Returns a new ruby symbol with the given intern value. Symbols are made
276
+ using the new String() constructor, and just have its klass and method
277
+ table reassigned. This makes dealing with strings/symbols internally
278
+ easier as both can be used as a string within opal.
279
+
280
+ @param {String} intern Symbol value
281
+ @return {RSymbol} symbol
282
+ */
283
+ var intern = Rt.Y = function(intern) {
284
+ if (symbol_table.hasOwnProperty(intern)) {
285
+ return symbol_table[intern];
286
+ }
287
+
288
+ var res = new String(intern);
289
+ res.$klass = cSymbol;
290
+ res.$m = cSymbol.$m_tbl;
291
+ res.$flags = T_OBJECT | T_SYMBOL;
292
+ symbol_table[intern] = res;
293
+ return res;
294
+ };
295
+
296
+
297
+ var cIO, stdin, stdout, stderr;
298
+
299
+ function stdio_getter(id) {
300
+ switch (id) {
301
+ case "$stdout":
302
+ return stdout;
303
+ case "$stdin":
304
+ return stdin;
305
+ case "$stderr":
306
+ return stderr;
307
+ default:
308
+ raise(eRuntimeError, "stdout_setter being used for bad variable");
309
+ }
310
+ };
311
+
312
+ function stdio_setter(id, value) {
313
+ raise(eException, "stdio_setter cannot currently set stdio variables");
314
+
315
+ switch (id) {
316
+ case "$stdout":
317
+ return stdout = value;
318
+ case "$stdin":
319
+ return stdin = value;
320
+ case "$stderr":
321
+ return stderr = value;
322
+ default:
323
+ raise(eRuntimeError, "stdout_setter being used for bad variable: " + id);
324
+ }
325
+ };
326
+
327
+ Rt.re = function(re) {
328
+ var regexp = new cRegexp.allocator();
329
+ regexp.$re = re;
330
+ return regexp;
331
+ };
332
+
333
+ var cProc;
334
+
335
+ /**
336
+ Block passing - holds current block for runtime
337
+
338
+ f: function
339
+ p: proc
340
+ y: yield error
341
+ */
342
+ var block = Rt.P = {
343
+ f: null,
344
+ p: null,
345
+ y: function() {
346
+ throw new Error("LocalJumpError - no block given");
347
+ }
348
+ };
349
+
350
+ block.y.$proc = [block.y];
351
+
352
+ /**
353
+ Turns the given proc/function into a lambda. This is useful for the
354
+ Proc#lambda method, but also for blocks that are turned into
355
+ methods, in Module#define_method, for example. Lambdas and methods
356
+ from blocks are the same thing. Lambdas basically wrap the passed
357
+ block function and perform stricter arg checking to make sure the
358
+ right number of args are passed. Procs are liberal in their arg
359
+ checking, and simply turned ommited args into nil. Lambdas and
360
+ methods MUST check args and throw an error if the wrong number are
361
+ given. Also, lambdas/methods must catch return statements as lambdas
362
+ capture returns.
363
+
364
+ FIXME: wrap must detect if we are the receiver of a block, and fix
365
+ the block to send it to the proc.
366
+
367
+ FIXME: need to be strict on checking proc arity
368
+
369
+ FIXME: need to catch return statements which may be thrown.
370
+
371
+ @param {Function} proc The proc/function to lambdafy.
372
+ @return {Function} Wrapped lambda function.
373
+ */
374
+ Rt.lambda = function(proc) {
375
+ if (proc.$lambda) return proc;
376
+
377
+ var wrap = function() {
378
+ var args = Array.prototype.slice.call(arguments, 0);
379
+ return proc.apply(null, args);
380
+ };
381
+
382
+ wrap.$lambda = true;
383
+ wrap.$proc = proc.$proc;
384
+
385
+ return Rt.proc(wrap);
386
+ };
387
+
388
+ var cRange;
389
+
390
+ /**
391
+ Returns a new ruby range. G for ranGe.
392
+ */
393
+ Rt.G = function(beg, end, exc) {
394
+ var range = new RObject(cRange, T_OBJECT);
395
+ range.$beg = beg;
396
+ range.$end = end;
397
+ range.$exc = exc;
398
+ return range;
399
+ };
400
+
401
+ Rt.A = function(objs) {
402
+ var arr = new cArray.allocator();
403
+ arr.splice.apply(arr, [0, 0].concat(objs));
404
+ return arr;
405
+ };
406
+
407
+ var puts = function(str) {
408
+ console.log(str);
409
+ };
410
+
411
+ /**
412
+ Main init method. This is called once this file has fully loaded. It setups
413
+ all the core objects and classes and required runtime features.
414
+ */
415
+ function init() {
416
+ if (typeof OPAL_DEBUG != "undefined" && OPAL_DEBUG) {
417
+ init_debug();
418
+ }
419
+
420
+ var metaclass;
421
+
422
+ Rt.BasicObject = cBasicObject = boot_defrootclass('BasicObject');
423
+ Rt.Object = cObject = boot_defclass('Object', cBasicObject);
424
+ Rt.Module = cModule = boot_defclass('Module', cObject);
425
+ Rt.Class = cClass = boot_defclass('Class', cModule);
426
+
427
+ const_set(cObject, 'BasicObject', cBasicObject);
428
+
429
+ metaclass = make_metaclass(cBasicObject, cClass);
430
+ metaclass = make_metaclass(cObject, metaclass);
431
+ metaclass = make_metaclass(cModule, metaclass);
432
+ metaclass = make_metaclass(cClass, metaclass);
433
+
434
+ boot_defmetametaclass(cModule, metaclass);
435
+ boot_defmetametaclass(cObject, metaclass);
436
+ boot_defmetametaclass(cBasicObject, metaclass);
437
+
438
+ mKernel = Rt.Kernel = define_module('Kernel');
439
+
440
+ top_self = obj_alloc(cObject);
441
+ Rt.top = top_self;
442
+
443
+ cNilClass = define_class('NilClass', cObject);
444
+ Rt.Qnil = Qnil = obj_alloc(cNilClass);
445
+ Qnil.$r = false;
446
+
447
+ cTrueClass = define_class('TrueClass', cObject);
448
+ Rt.Qtrue = Qtrue = obj_alloc(cTrueClass);
449
+
450
+ cFalseClass = define_class('FalseClass', cObject);
451
+ Rt.Qfalse = Qfalse = obj_alloc(cFalseClass);
452
+ Qfalse.$r = false;
453
+
454
+ cArray = bridge_class(Array.prototype,
455
+ T_OBJECT | T_ARRAY, 'Array', cObject);
456
+
457
+ Function.prototype.$hash = function() {
458
+ return this.$id || (this.$id = yield_hash());
459
+ };
460
+
461
+ cHash = define_class('Hash', cObject);
462
+
463
+ cNumeric = bridge_class(Number.prototype,
464
+ T_OBJECT | T_NUMBER, 'Numeric', cObject);
465
+
466
+ cString = bridge_class(String.prototype,
467
+ T_OBJECT | T_STRING, 'String', cObject);
468
+
469
+ cSymbol = define_class('Symbol', cObject);
470
+
471
+ cProc = bridge_class(Function.prototype,
472
+ T_OBJECT | T_PROC, 'Proc', cObject);
473
+
474
+ Function.prototype.$hash = function() {
475
+ return this.$id || (this.$id = yield_hash());
476
+ };
477
+
478
+ cRange = define_class('Range', cObject);
479
+
480
+ cRegexp = bridge_class(RegExp.prototype,
481
+ T_OBJECT, 'Regexp', cObject);
482
+
483
+ cMatch = define_class('MatchData', cObject);
484
+ define_hooked_variable('$~', regexp_match_getter, gvar_readonly_setter);
485
+
486
+ eException = bridge_class(Error.prototype, T_OBJECT, 'Exception', cObject);
487
+
488
+ eStandardError = define_class("StandardError", eException);
489
+ eRuntimeError = define_class("RuntimeError", eException);
490
+ eLocalJumpError = define_class("LocalJumpError", eStandardError);
491
+ Rt.TypeError = eTypeError = define_class("TypeError", eStandardError);
492
+
493
+ eNameError = define_class("NameError", eStandardError);
494
+ eNoMethodError = define_class('NoMethodError', eNameError);
495
+ eArgError = define_class('ArgumentError', eStandardError);
496
+
497
+ eScriptError = define_class('ScriptError', eException);
498
+ eLoadError = define_class('LoadError', eScriptError);
499
+
500
+ eIndexError = define_class("IndexError", eStandardError);
501
+ eKeyError = define_class("KeyError", eIndexError);
502
+ eRangeError = define_class("RangeError", eStandardError);
503
+
504
+ eBreakInstance = new Error();
505
+ eBreakInstance['@message'] = "unexpected break";
506
+ block.b = eBreakInstance;
507
+ // dont need this anymore???
508
+ eBreakInstance.$keyword = 2;
509
+
510
+ eReturnInstance = new Error();
511
+ eReturnInstance.$klass = eLocalJumpError;
512
+ eReturnInstance.$keyword = 1;
513
+
514
+ eNextInstance = new Error();
515
+ eNextInstance.$klass = eLocalJumpError;
516
+ eNextInstance.$keyword = 3;
517
+
518
+ // need to do this after we make symbol
519
+ Rt.ds(cClass, 'new', class_s_new);
520
+
521
+ cIO = define_class('IO', cObject);
522
+ stdin = obj_alloc(cIO);
523
+ stdout = obj_alloc(cIO);
524
+ stderr = obj_alloc(cIO);
525
+
526
+ const_set(cObject, 'STDIN', stdin);
527
+ const_set(cObject, 'STDOUT', stdout);
528
+ const_set(cObject, 'STDERR', stderr);
529
+
530
+ define_hooked_variable('$stdin', stdio_getter, stdio_setter);
531
+ define_hooked_variable('$stdout', stdio_getter, stdio_setter);
532
+ define_hooked_variable('$stderr', stdio_getter, stdio_setter);
533
+
534
+ define_hooked_variable('$:', load_path_getter, gvar_readonly_setter);
535
+ define_hooked_variable('$LOAD_PATH', load_path_getter, gvar_readonly_setter);
536
+
537
+ Op.loader = new Loader(Op);
538
+ Op.cache = {};
539
+
540
+ // const_set(cObject, 'RUBY_ENGINE', Op.platform.engine);
541
+ const_set(cObject, 'RUBY_ENGINE', 'opal-gem');
542
+
543
+ puts = function(str) {
544
+ top_self.$m.puts(top_self, str);
545
+ };
546
+
547
+ };
548
+
549
+ /**
550
+ Symbol table. All symbols are stored here.
551
+ */
552
+ var symbol_table = { };
553
+
554
+ function class_s_new(cls, sup) {
555
+ var klass = define_class_id("AnonClass", sup || cObject);
556
+ return klass;
557
+ };
558
+