opal 0.3.6 → 0.3.9

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 (98) hide show
  1. data/README.md +69 -97
  2. data/bin/opal +2 -2
  3. data/{lib/core → corelib}/array.rb +85 -56
  4. data/corelib/boolean.rb +20 -0
  5. data/corelib/class.rb +58 -0
  6. data/{lib → corelib}/core.rb +2 -50
  7. data/corelib/dir.rb +22 -0
  8. data/{lib/core → corelib}/enumerable.rb +0 -0
  9. data/corelib/error.rb +19 -0
  10. data/{lib/core → corelib}/file.rb +7 -9
  11. data/{lib/core → corelib}/hash.rb +104 -144
  12. data/{lib/core → corelib}/kernel.rb +38 -44
  13. data/corelib/load_order +21 -0
  14. data/{lib/core → corelib}/match_data.rb +0 -0
  15. data/{lib/core → corelib}/module.rb +12 -8
  16. data/{lib/core → corelib}/nil_class.rb +2 -2
  17. data/{lib/core → corelib}/numeric.rb +37 -100
  18. data/corelib/object.rb +37 -0
  19. data/{lib/core → corelib}/proc.rb +3 -3
  20. data/corelib/range.rb +27 -0
  21. data/{lib/core → corelib}/regexp.rb +1 -1
  22. data/{lib/core → corelib}/string.rb +16 -107
  23. data/{lib/core → corelib}/top_self.rb +0 -0
  24. data/lib/opal.rb +7 -0
  25. data/lib/opal/browserify.rb +34 -0
  26. data/{opal_lib → lib}/opal/builder.rb +70 -24
  27. data/lib/opal/command.rb +52 -0
  28. data/lib/opal/context.rb +197 -0
  29. data/{opal_lib/opal/ruby/parser.rb → lib/opal/lexer.rb} +20 -4
  30. data/{opal_lib/opal/ruby → lib/opal}/nodes.rb +238 -127
  31. data/lib/opal/parser.rb +4894 -0
  32. data/{opal_lib/opal/ruby/ruby_parser.y → lib/opal/parser.y} +38 -18
  33. data/lib/rbp.rb +2 -0
  34. data/lib/rbp/package.rb +49 -0
  35. data/runtime/class.js +216 -189
  36. data/runtime/fs.js +2 -2
  37. data/runtime/init.js +242 -244
  38. data/runtime/loader.js +78 -99
  39. data/runtime/module.js +34 -40
  40. data/runtime/post.js +2 -2
  41. data/runtime/pre.js +1 -1
  42. data/runtime/runtime.js +129 -135
  43. data/{lib → stdlib}/dev.rb +10 -10
  44. data/{lib → stdlib}/racc/parser.rb +0 -6
  45. data/{lib → stdlib}/strscan.rb +4 -4
  46. metadata +57 -105
  47. data/lib/core/basic_object.rb +0 -51
  48. data/lib/core/class.rb +0 -38
  49. data/lib/core/dir.rb +0 -26
  50. data/lib/core/error.rb +0 -75
  51. data/lib/core/false_class.rb +0 -81
  52. data/lib/core/object.rb +0 -6
  53. data/lib/core/range.rb +0 -27
  54. data/lib/core/symbol.rb +0 -42
  55. data/lib/core/true_class.rb +0 -41
  56. data/lib/ospec.rb +0 -7
  57. data/lib/ospec/autorun.rb +0 -8
  58. data/lib/ospec/dsl.rb +0 -15
  59. data/lib/ospec/example.rb +0 -11
  60. data/lib/ospec/example/before_and_after_hooks.rb +0 -56
  61. data/lib/ospec/example/errors.rb +0 -17
  62. data/lib/ospec/example/example_group.rb +0 -12
  63. data/lib/ospec/example/example_group_factory.rb +0 -18
  64. data/lib/ospec/example/example_group_hierarchy.rb +0 -21
  65. data/lib/ospec/example/example_group_methods.rb +0 -100
  66. data/lib/ospec/example/example_group_proxy.rb +0 -15
  67. data/lib/ospec/example/example_methods.rb +0 -46
  68. data/lib/ospec/example/example_proxy.rb +0 -18
  69. data/lib/ospec/expectations.rb +0 -19
  70. data/lib/ospec/expectations/errors.rb +0 -8
  71. data/lib/ospec/expectations/fail_with.rb +0 -9
  72. data/lib/ospec/expectations/handler.rb +0 -33
  73. data/lib/ospec/helpers/scratch.rb +0 -18
  74. data/lib/ospec/matchers.rb +0 -24
  75. data/lib/ospec/matchers/be.rb +0 -1
  76. data/lib/ospec/matchers/generated_descriptions.rb +0 -20
  77. data/lib/ospec/matchers/operator_matcher.rb +0 -54
  78. data/lib/ospec/matchers/raise_error.rb +0 -38
  79. data/lib/ospec/runner.rb +0 -90
  80. data/lib/ospec/runner/example_group_runner.rb +0 -41
  81. data/lib/ospec/runner/formatter/html_formatter.rb +0 -139
  82. data/lib/ospec/runner/formatter/terminal_formatter.rb +0 -48
  83. data/lib/ospec/runner/options.rb +0 -34
  84. data/lib/ospec/runner/reporter.rb +0 -82
  85. data/opal_lib/opal.rb +0 -16
  86. data/opal_lib/opal/build_methods.rb +0 -51
  87. data/opal_lib/opal/bundle.rb +0 -70
  88. data/opal_lib/opal/command.rb +0 -68
  89. data/opal_lib/opal/context.rb +0 -81
  90. data/opal_lib/opal/context/console.rb +0 -10
  91. data/opal_lib/opal/context/file_system.rb +0 -34
  92. data/opal_lib/opal/context/loader.rb +0 -135
  93. data/opal_lib/opal/gem.rb +0 -84
  94. data/opal_lib/opal/rake/builder_task.rb +0 -44
  95. data/opal_lib/opal/rake/spec_task.rb +0 -32
  96. data/opal_lib/opal/ruby/ruby_parser.rb +0 -4862
  97. data/opal_lib/opal/version.rb +0 -4
  98. data/runtime/debug.js +0 -84
data/runtime/fs.js CHANGED
@@ -102,7 +102,7 @@ Fs.extname = function(file_name) {
102
102
  };
103
103
 
104
104
  Fs.exist_p = function(path) {
105
- return opal.loader.factories[file_expand_path(path)] ? true : false;
105
+ return Op.loader.factories[fs_expand_path(path)] ? true : false;
106
106
  };
107
107
 
108
108
  /**
@@ -135,7 +135,7 @@ Fs.glob = function() {
135
135
  */
136
136
  function fs_glob_to_regexp(glob) {
137
137
  if (typeof glob !== 'string') {
138
- throw new Error("file_glob_to_regexp: glob must be a string");
138
+ raise(eException, "file_glob_to_regexp: glob must be a string");
139
139
  }
140
140
 
141
141
  // make sure absolute
data/runtime/init.js CHANGED
@@ -7,9 +7,8 @@
7
7
  @param {Object} val
8
8
  @return {Object} returns the set value
9
9
  */
10
- function const_set(klass, id, val) {
11
- klass.$c_prototype[id] = val;
12
- klass.$const_table[id] = val;
10
+ function rb_const_set(klass, id, val) {
11
+ klass.$c[id] = val;
13
12
  return val;
14
13
  }
15
14
 
@@ -20,15 +19,14 @@ function const_set(klass, id, val) {
20
19
  @param {RClass} klass
21
20
  @param {String} id
22
21
  */
23
- function const_get(klass, id) {
22
+ function rb_const_get(klass, id) {
24
23
  if (klass.$c[id]) {
25
24
  return (klass.$c[id]);
26
25
  }
27
26
 
28
27
  var parent = klass.$parent;
29
28
 
30
- while (parent && parent != cObject) {
31
- // while (parent) {
29
+ while (parent) {
32
30
  if (parent.$c[id] !== undefined) {
33
31
  return parent.$c[id];
34
32
  }
@@ -36,10 +34,10 @@ function const_get(klass, id) {
36
34
  parent = parent.$parent;
37
35
  }
38
36
 
39
- raise(eNameError, 'uninitialized constant ' + id);
37
+ rb_raise(rb_eNameError, 'uninitialized constant ' + id);
40
38
  };
41
39
 
42
- Rt.const_get = const_get;
40
+ Rt.const_get = rb_const_get;
43
41
 
44
42
  /**
45
43
  Returns true or false depending whether a constant named `id` is defined
@@ -49,7 +47,7 @@ Rt.const_get = const_get;
49
47
  @param {String} id
50
48
  @return {true, false}
51
49
  */
52
- function const_defined(klass, id) {
50
+ function rb_const_defined(klass, id) {
53
51
  if (klass.$c[id]) {
54
52
  return true;
55
53
  }
@@ -68,7 +66,7 @@ function const_defined(klass, id) {
68
66
  - getter
69
67
  - setter
70
68
  */
71
- var global_tbl = {};
69
+ var rb_global_tbl = {};
72
70
 
73
71
  /**
74
72
  Defines a hooked/global variable.
@@ -78,7 +76,7 @@ var global_tbl = {};
78
76
  @param {Function} setter The setter function used for setting the var
79
77
  @return {null}
80
78
  */
81
- function define_hooked_variable(name, getter, setter) {
79
+ function rb_define_hooked_variable(name, getter, setter) {
82
80
  var entry = {
83
81
  "name": name,
84
82
  "value": Qnil,
@@ -86,7 +84,7 @@ function define_hooked_variable(name, getter, setter) {
86
84
  "setter": setter
87
85
  };
88
86
 
89
- global_tbl[name] = entry;
87
+ rb_global_tbl[name] = entry;
90
88
  };
91
89
 
92
90
  /**
@@ -94,15 +92,15 @@ function define_hooked_variable(name, getter, setter) {
94
92
  name error with the given id. This can be used for variables that should
95
93
  not be altered.
96
94
  */
97
- function gvar_readonly_setter(id, value) {
98
- raise(eNameError, id + " is a read-only variable");
95
+ function rb_gvar_readonly_setter(id, value) {
96
+ rb_raise(rb_eNameError, id + " is a read-only variable");
99
97
  };
100
98
 
101
99
  /**
102
100
  Retrieve a global variable. This will use the assigned getter.
103
101
  */
104
- function gvar_get(id) {
105
- var entry = global_tbl[id];
102
+ function rb_gvar_get(id) {
103
+ var entry = rb_global_tbl[id];
106
104
  if (!entry) { return Qnil; }
107
105
  return entry.getter(id);
108
106
  };
@@ -110,67 +108,67 @@ function gvar_get(id) {
110
108
  /**
111
109
  Set a global. If not already set, then we assign basic getters and setters.
112
110
  */
113
- function gvar_set(id, value) {
114
- var entry = global_tbl[id];
111
+ function rb_gvar_set(id, value) {
112
+ var entry = rb_global_tbl[id];
115
113
  if (entry) { return entry.setter(id, value); }
116
114
 
117
- define_hooked_variable(id,
115
+ rb_define_hooked_variable(id,
118
116
 
119
117
  function(id) {
120
- return global_tbl[id].value;
118
+ return rb_global_tbl[id].value;
121
119
  },
122
120
 
123
121
  function(id, value) {
124
- return (global_tbl[id].value = value);
122
+ return (rb_global_tbl[id].value = value);
125
123
  }
126
124
  );
127
125
 
128
- return gvar_set(id, value);
126
+ return rb_gvar_set(id, value);
129
127
  };
130
128
 
131
129
  /**
132
130
  Every object has a unique id. This count is used as the next id for the
133
131
  next created object. Therefore, first ruby object has id 0, next has 1 etc.
134
132
  */
135
- var hash_yield = 0;
133
+ var rb_hash_yield = 0;
136
134
 
137
135
  /**
138
136
  Yield the next object id, updating the count, and returning it.
139
137
  */
140
- function yield_hash() {
141
- return hash_yield++;
138
+ function rb_yield_hash() {
139
+ return rb_hash_yield++;
142
140
  };
143
141
 
144
- var cHash;
142
+ var rb_cHash;
145
143
 
146
144
  /**
147
145
  Returns a new hash with values passed from the runtime.
148
146
  */
149
147
  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;
148
+ var hash = new rb_cHash.$a(), k, v, args = Array.prototype.slice.call(arguments);
149
+ var keys = hash.k = [];
150
+ var assocs = hash.a = {};
151
+ hash.d = Qnil;
154
152
 
155
153
  for (var i = 0, ii = args.length; i < ii; i++) {
156
154
  k = args[i];
157
155
  v = args[i + 1];
158
156
  i++;
159
- hash.$keys.push(k);
160
- hash.$assocs[k.$hash()] = v;
157
+ keys.push(k);
158
+ assocs[k.$h()] = v;
161
159
  }
162
160
 
163
161
  return hash;
164
162
  };
165
163
 
166
- var alias_method = Rt.alias_method = function(klass, new_name, old_name) {
167
- var body = klass.$m_tbl[old_name];
164
+ var rb_alias_method = Rt.alias_method = function(klass, new_name, old_name) {
165
+ var body = klass.$a.prototype['m$' + old_name];
168
166
 
169
167
  if (!body) {
170
- throw new Error("NameError: undefined method `" + old_name + "' for class `" + klass.__classid__ + "'");
168
+ rb_raise(rb_eNameError, "undefined method `" + old_name + "' for class `" + klass.__classid__ + "'");
171
169
  }
172
170
 
173
- define_raw_method(klass, new_name, body, body);
171
+ rb_define_raw_method(klass, 'm$' + new_name, body);
174
172
  return Qnil;
175
173
  };
176
174
 
@@ -179,13 +177,10 @@ var alias_method = Rt.alias_method = function(klass, new_name, old_name) {
179
177
  singleton_method_added etc. define_method does that.
180
178
 
181
179
  */
182
- function define_raw_method(klass, private_name, private_body, public_body) {
183
- var public_name = '$' + private_name;
180
+ function rb_define_raw_method(klass, name, body) {
184
181
 
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;
182
+ klass.$a.prototype[name] = body;
183
+ klass.$m[name] = body;
189
184
 
190
185
  var included_in = klass.$included_in, includee;
191
186
 
@@ -193,144 +188,162 @@ function define_raw_method(klass, private_name, private_body, public_body) {
193
188
  for (var i = 0, ii = included_in.length; i < ii; i++) {
194
189
  includee = included_in[i];
195
190
 
196
- define_raw_method(includee, private_name, private_body, public_body);
191
+ rb_define_raw_method(includee, name, body);
197
192
  }
198
193
  }
199
- };
200
-
201
- Rt.private_methods = function(klass, args) {
202
- return;
203
-
204
- if (args.length) {
205
- var proto = klass.allocator.prototype;
206
194
 
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
- }
195
+ // This class is toll free bridged, so add method to native
196
+ // prototype as well
197
+ if (klass.$bridge_prototype) {
198
+ klass.$bridge_prototype[name] = body;
199
+ }
216
200
 
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
- }
201
+ // If we are dealing with Object, then we need to donate to
202
+ // all of our bridged prototypes as well.
203
+ if (klass === rb_cObject) {
204
+ var bridged = rb_bridged_classes;
223
205
 
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
206
+ for (var i = 0, ii = bridged.length; i < ii; i++) {
207
+ // do not overwrite bridged methods' implementation
208
+ if (!bridged[i][name]) {
209
+ bridged[i][name] = body;
229
210
  }
230
211
  }
231
212
  }
232
- else {
233
- // no args - set klass mode to private
234
- klass.$mode = FL_PRIVATE;
235
- }
236
213
  };
237
214
 
238
- function define_alias(base, new_name, old_name) {
239
- define_method(base, new_name, base.$m_tbl[old_name]);
215
+ function rb_define_alias(base, new_name, old_name) {
216
+ rb_define_method(base, new_name, base.$m_tbl[old_name]);
240
217
  return Qnil;
241
218
  };
242
219
 
243
- function obj_alloc(klass) {
244
- var result = new RObject(klass);
245
- return result;
246
- };
247
-
248
220
  /**
249
221
  Raise the exception class with the given string message.
250
222
  */
251
- function raise(exc, str) {
223
+ function rb_raise(exc, str) {
252
224
  if (str === undefined) {
253
225
  str = exc;
254
- exc = eException;
226
+ exc = rb_eException;
255
227
  }
256
228
 
257
- var exception = exc.$m['new'](exc, str);
258
- raise_exc(exception);
229
+ var exception = exc.m$new(str);
230
+ rb_raise_exc(exception);
259
231
  };
260
232
 
261
- Rt.raise = raise;
233
+ Rt.raise = rb_raise;
262
234
 
263
235
  /**
264
236
  Raise an exception instance (DO NOT pass strings to this)
265
237
  */
266
- function raise_exc(exc) {
238
+ function rb_raise_exc(exc) {
267
239
  throw exc;
268
240
  };
269
241
 
270
- Rt.raise_exc = raise_exc;
242
+ var rb_cString;
271
243
 
272
- var cString, cSymbol;
273
244
 
274
245
  /**
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.
246
+ Exception classes. Some of these are used by runtime so they are here for
247
+ convenience.
248
+ */
249
+ var rb_eException, rb_eStandardError, rb_eLocalJumpError, rb_eNameError,
250
+ rb_eNoMethodError, rb_eArgError, rb_eScriptError, rb_eLoadError,
251
+ rb_eRuntimeError, rb_eTypeError, rb_eIndexError, rb_eKeyError,
252
+ rb_eRangeError;
253
+
254
+ var rb_eExceptionInstance;
279
255
 
280
- @param {String} intern Symbol value
281
- @return {RSymbol} symbol
256
+ /**
257
+ Standard jump exceptions to save re-creating them everytime they are needed
282
258
  */
283
- var intern = Rt.Y = function(intern) {
284
- if (symbol_table.hasOwnProperty(intern)) {
285
- return symbol_table[intern];
286
- }
259
+ var rb_eReturnInstance,
260
+ rb_eBreakInstance,
261
+ rb_eNextInstance;
262
+
263
+ /**
264
+ Ruby break statement with the given value. When no break value is needed, nil
265
+ should be passed here. An undefined/null value is not valid and will cause an
266
+ internal error.
287
267
 
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;
268
+ @param {RubyObject} value The break value.
269
+ */
270
+ Rt.B = function(value) {
271
+ rb_eBreakInstance.$value = value;
272
+ rb_raise_exc(eBreakInstance);
294
273
  };
295
274
 
275
+ /**
276
+ Ruby return, with the given value. The func is the reference function which
277
+ represents the method that this statement must return from.
278
+ */
279
+ Rt.R = function(value, func) {
280
+ rb_eReturnInstance.$value = value;
281
+ rb_eReturnInstance.$func = func;
282
+ throw rb_eReturnInstance;
283
+ };
296
284
 
297
- var cIO, stdin, stdout, stderr;
285
+ /**
286
+ Get global by id
287
+ */
288
+ Rt.gg = function(id) {
289
+ return rb_gvar_get(id);
290
+ };
298
291
 
299
- function stdio_getter(id) {
292
+ /**
293
+ Set global by id
294
+ */
295
+ Rt.gs = function(id, value) {
296
+ return rb_gvar_set(id, value);
297
+ };
298
+
299
+ function rb_regexp_match_getter(id) {
300
+ var matched = Rt.X;
301
+
302
+ if (matched) {
303
+ if (matched.$md) {
304
+ return matched.$md;
305
+ } else {
306
+ var res = new rb_cMatch.o$a();
307
+ res.$data = matched;
308
+ matched.$md = res;
309
+ return res;
310
+ }
311
+ } else {
312
+ return Qnil;
313
+ }
314
+ }
315
+
316
+ var rb_cIO, rb_stdin, rb_stdout, rb_stderr;
317
+
318
+ function rb_stdio_getter(id) {
300
319
  switch (id) {
301
320
  case "$stdout":
302
- return stdout;
321
+ return rb_stdout;
303
322
  case "$stdin":
304
- return stdin;
323
+ return rb_stdin;
305
324
  case "$stderr":
306
- return stderr;
325
+ return rb_stderr;
307
326
  default:
308
- raise(eRuntimeError, "stdout_setter being used for bad variable");
327
+ rb_raise(rb_eRuntimeError, "stdout_setter being used for bad variable");
309
328
  }
310
329
  };
311
330
 
312
- function stdio_setter(id, value) {
313
- raise(eException, "stdio_setter cannot currently set stdio variables");
331
+ function rb_stdio_setter(id, value) {
332
+ rb_raise(rb_eException, "stdio_setter cannot currently set stdio variables");
314
333
 
315
334
  switch (id) {
316
335
  case "$stdout":
317
- return stdout = value;
336
+ return rb_stdout = value;
318
337
  case "$stdin":
319
- return stdin = value;
338
+ return rb_stdin = value;
320
339
  case "$stderr":
321
- return stderr = value;
340
+ return rb_stderr = value;
322
341
  default:
323
- raise(eRuntimeError, "stdout_setter being used for bad variable: " + id);
342
+ rb_raise(rb_eRuntimeError, "stdout_setter being used for bad variable: " + id);
324
343
  }
325
344
  };
326
345
 
327
- Rt.re = function(re) {
328
- var regexp = new cRegexp.allocator();
329
- regexp.$re = re;
330
- return regexp;
331
- };
332
-
333
- var cProc;
346
+ var rb_cProc;
334
347
 
335
348
  /**
336
349
  Block passing - holds current block for runtime
@@ -339,15 +352,15 @@ var cProc;
339
352
  p: proc
340
353
  y: yield error
341
354
  */
342
- var block = Rt.P = {
355
+ var rb_block = Rt.P = {
343
356
  f: null,
344
357
  p: null,
345
358
  y: function() {
346
- throw new Error("LocalJumpError - no block given");
359
+ rb_raise(rb_eLocalJumpError, "no block given");
347
360
  }
348
361
  };
349
362
 
350
- block.y.$proc = [block.y];
363
+ rb_block.y.$self = rb_block.y;
351
364
 
352
365
  /**
353
366
  Turns the given proc/function into a lambda. This is useful for the
@@ -371,7 +384,7 @@ block.y.$proc = [block.y];
371
384
  @param {Function} proc The proc/function to lambdafy.
372
385
  @return {Function} Wrapped lambda function.
373
386
  */
374
- Rt.lambda = function(proc) {
387
+ function rb_make_lambda(proc) {
375
388
  if (proc.$lambda) return proc;
376
389
 
377
390
  var wrap = function() {
@@ -380,30 +393,28 @@ Rt.lambda = function(proc) {
380
393
  };
381
394
 
382
395
  wrap.$lambda = true;
383
- wrap.$proc = proc.$proc;
396
+ wrap.o$s = proc.o$s;
384
397
 
385
- return Rt.proc(wrap);
398
+ return proc;
386
399
  };
387
400
 
388
- var cRange;
401
+ var rb_cRange;
389
402
 
390
403
  /**
391
404
  Returns a new ruby range. G for ranGe.
392
405
  */
393
406
  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;
407
+ var range = rb_obj_alloc(cRange);
408
+ range.beg = beg;
409
+ range.end = end;
410
+ range.exc = exc;
398
411
  return range;
399
412
  };
400
413
 
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
-
414
+ /**
415
+ Print to console - this is overriden upon init so that it will print to
416
+ stdout
417
+ */
407
418
  var puts = function(str) {
408
419
  console.log(str);
409
420
  };
@@ -413,146 +424,133 @@ var puts = function(str) {
413
424
  all the core objects and classes and required runtime features.
414
425
  */
415
426
  function init() {
416
- if (typeof OPAL_DEBUG != "undefined" && OPAL_DEBUG) {
417
- init_debug();
418
- }
419
-
420
427
  var metaclass;
421
428
 
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);
429
+ // The *instances* of core objects..
430
+ rb_boot_Object = rb_boot_defclass('Object');
431
+ rb_boot_Module = rb_boot_defclass('Module', rb_boot_Object);
432
+ rb_boot_Class = rb_boot_defclass('Class', rb_boot_Module);
437
433
 
438
- mKernel = Rt.Kernel = define_module('Kernel');
434
+ // The *classes* of core objects..
435
+ rb_cObject = rb_boot_makemeta('Object', rb_boot_Object, rb_boot_Class);
436
+ rb_cModule = rb_boot_makemeta('Module', rb_boot_Module, rb_cObject.constructor);
437
+ rb_cClass = rb_boot_makemeta('Class', rb_boot_Class, rb_cModule.constructor);
439
438
 
440
- top_self = obj_alloc(cObject);
441
- Rt.top = top_self;
439
+ rb_boot_defmetameta(rb_cObject, rb_cClass);
440
+ rb_boot_defmetameta(rb_cModule, rb_cClass);
441
+ rb_boot_defmetameta(rb_cClass, rb_cClass);
442
442
 
443
- cNilClass = define_class('NilClass', cObject);
444
- Rt.Qnil = Qnil = obj_alloc(cNilClass);
445
- Qnil.$r = false;
443
+ // fix superclasses..
444
+ rb_cObject.$s = null;
445
+ rb_cModule.$s = rb_cObject;
446
+ rb_cClass.$s = rb_cModule;
446
447
 
447
- cTrueClass = define_class('TrueClass', cObject);
448
- Rt.Qtrue = Qtrue = obj_alloc(cTrueClass);
448
+ rb_const_set(rb_cObject, 'Object', rb_cObject);
449
+ rb_const_set(rb_cObject, 'Module', rb_cModule);
450
+ rb_const_set(rb_cObject, 'Class', rb_cClass);
449
451
 
450
- cFalseClass = define_class('FalseClass', cObject);
451
- Rt.Qfalse = Qfalse = obj_alloc(cFalseClass);
452
- Qfalse.$r = false;
452
+ rb_mKernel = rb_define_module('Kernel');
453
453
 
454
- cArray = bridge_class(Array.prototype,
455
- T_OBJECT | T_ARRAY, 'Array', cObject);
454
+ rb_top_self = new rb_cObject.$a();
455
+ Rt.top = rb_top_self;
456
456
 
457
- Function.prototype.$hash = function() {
458
- return this.$id || (this.$id = yield_hash());
457
+ rb_cNilClass = rb_define_class('NilClass', rb_cObject);
458
+ Rt.Qnil = Qnil = new rb_cNilClass.$a();
459
+ Qnil.toString = function() {
460
+ return "nil";
459
461
  };
460
462
 
461
- cHash = define_class('Hash', cObject);
463
+ rb_cBoolean = rb_bridge_class(Boolean.prototype, T_OBJECT, 'Boolean', rb_cObject);
462
464
 
463
- cNumeric = bridge_class(Number.prototype,
464
- T_OBJECT | T_NUMBER, 'Numeric', cObject);
465
+ rb_cArray = rb_bridge_class(Array.prototype, T_OBJECT | T_ARRAY, 'Array', rb_cObject);
466
+ // array instances all get standard properties for subclasses to work
467
+ var ary_proto = Array.prototype, ary_inst = rb_cArray.$a.prototype;
468
+ ary_inst.$f = T_ARRAY | T_OBJECT;
469
+ ary_inst.push = ary_proto.push;
470
+ ary_inst.pop = ary_proto.pop;
471
+ ary_inst.slice = ary_proto.slice;
472
+ ary_inst.splice = ary_proto.splice;
473
+ ary_inst.concat = ary_proto.concat;
474
+ ary_inst.shift = ary_proto.shift;
475
+ ary_inst.unshift = ary_proto.unshift;
476
+ ary_inst.length = 0;
465
477
 
466
- cString = bridge_class(String.prototype,
467
- T_OBJECT | T_STRING, 'String', cObject);
478
+ rb_cHash = rb_define_class('Hash', rb_cObject);
468
479
 
469
- cSymbol = define_class('Symbol', cObject);
480
+ rb_cNumeric = rb_bridge_class(Number.prototype,
481
+ T_OBJECT | T_NUMBER, 'Numeric', rb_cObject);
470
482
 
471
- cProc = bridge_class(Function.prototype,
472
- T_OBJECT | T_PROC, 'Proc', cObject);
483
+ rb_cString = rb_bridge_class(String.prototype,
484
+ T_OBJECT | T_STRING, 'String', rb_cObject);
473
485
 
474
- Function.prototype.$hash = function() {
475
- return this.$id || (this.$id = yield_hash());
476
- };
477
-
478
- cRange = define_class('Range', cObject);
486
+ rb_cProc = rb_bridge_class(Function.prototype,
487
+ T_OBJECT | T_PROC, 'Proc', rb_cObject);
479
488
 
480
- cRegexp = bridge_class(RegExp.prototype,
481
- T_OBJECT, 'Regexp', cObject);
489
+ rb_cRange = rb_define_class('Range', rb_cObject);
482
490
 
483
- cMatch = define_class('MatchData', cObject);
484
- define_hooked_variable('$~', regexp_match_getter, gvar_readonly_setter);
491
+ rb_cRegexp = rb_bridge_class(RegExp.prototype,
492
+ T_OBJECT, 'Regexp', rb_cObject);
485
493
 
486
- eException = bridge_class(Error.prototype, T_OBJECT, 'Exception', cObject);
494
+ rb_cMatch = rb_define_class('MatchData', rb_cObject);
495
+ rb_define_hooked_variable('$~', rb_regexp_match_getter, rb_gvar_readonly_setter);
487
496
 
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);
497
+ rb_eException = rb_bridge_class(Error.prototype,
498
+ T_OBJECT, 'Exception', rb_cObject);
492
499
 
493
- eNameError = define_class("NameError", eStandardError);
494
- eNoMethodError = define_class('NoMethodError', eNameError);
495
- eArgError = define_class('ArgumentError', eStandardError);
500
+ rb_eStandardError = rb_define_class("StandardError", rb_eException);
501
+ rb_eRuntimeError = rb_define_class("RuntimeError", rb_eException);
502
+ rb_eLocalJumpError = rb_define_class("LocalJumpError", rb_eStandardError);
503
+ rb_eTypeError = rb_define_class("TypeError", rb_eStandardError);
496
504
 
497
- eScriptError = define_class('ScriptError', eException);
498
- eLoadError = define_class('LoadError', eScriptError);
505
+ rb_eNameError = rb_define_class("NameError", rb_eStandardError);
506
+ rb_eNoMethodError = rb_define_class('NoMethodError', rb_eNameError);
507
+ rb_eArgError = rb_define_class('ArgumentError', rb_eStandardError);
499
508
 
500
- eIndexError = define_class("IndexError", eStandardError);
501
- eKeyError = define_class("KeyError", eIndexError);
502
- eRangeError = define_class("RangeError", eStandardError);
509
+ rb_eScriptError = rb_define_class('ScriptError', rb_eException);
510
+ rb_eLoadError = rb_define_class('LoadError', rb_eScriptError);
503
511
 
504
- eBreakInstance = new Error();
505
- eBreakInstance['@message'] = "unexpected break";
506
- block.b = eBreakInstance;
507
- // dont need this anymore???
508
- eBreakInstance.$keyword = 2;
512
+ rb_eIndexError = rb_define_class("IndexError", rb_eStandardError);
513
+ rb_eKeyError = rb_define_class("KeyError", rb_eIndexError);
514
+ rb_eRangeError = rb_define_class("RangeError", rb_eStandardError);
509
515
 
510
- eReturnInstance = new Error();
511
- eReturnInstance.$klass = eLocalJumpError;
512
- eReturnInstance.$keyword = 1;
516
+ rb_eBreakInstance = new Error('unexpected break');
517
+ rb_eBreakInstance.$k = rb_eLocalJumpError;
518
+ rb_block.b = rb_eBreakInstance;
513
519
 
514
- eNextInstance = new Error();
515
- eNextInstance.$klass = eLocalJumpError;
516
- eNextInstance.$keyword = 3;
520
+ rb_eReturnInstance = new Error('unexpected return');
521
+ rb_eReturnInstance.$k = rb_eLocalJumpError;
517
522
 
518
- // need to do this after we make symbol
519
- Rt.ds(cClass, 'new', class_s_new);
523
+ rb_eNextInstance = new Error('unexpected next');
524
+ rb_eNextInstance.$k = rb_eLocalJumpError;
520
525
 
521
- cIO = define_class('IO', cObject);
522
- stdin = obj_alloc(cIO);
523
- stdout = obj_alloc(cIO);
524
- stderr = obj_alloc(cIO);
526
+ rb_cIO = rb_define_class('IO', rb_cObject);
527
+ rb_stdin = new rb_cIO.$a();
528
+ rb_stdout = new rb_cIO.$a();
529
+ rb_stderr = new rb_cIO.$a();
525
530
 
526
- const_set(cObject, 'STDIN', stdin);
527
- const_set(cObject, 'STDOUT', stdout);
528
- const_set(cObject, 'STDERR', stderr);
531
+ rb_const_set(rb_cObject, 'STDIN', rb_stdin);
532
+ rb_const_set(rb_cObject, 'STDOUT', rb_stdout);
533
+ rb_const_set(rb_cObject, 'STDERR', rb_stderr);
529
534
 
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);
535
+ rb_define_hooked_variable('$stdin', rb_stdio_getter, rb_stdio_setter);
536
+ rb_define_hooked_variable('$stdout', rb_stdio_getter, rb_stdio_setter);
537
+ rb_define_hooked_variable('$stderr', rb_stdio_getter, rb_stdio_setter);
533
538
 
534
- define_hooked_variable('$:', load_path_getter, gvar_readonly_setter);
535
- define_hooked_variable('$LOAD_PATH', load_path_getter, gvar_readonly_setter);
539
+ rb_define_hooked_variable('$:', rb_load_path_getter, rb_gvar_readonly_setter);
540
+ rb_define_hooked_variable('$LOAD_PATH', rb_load_path_getter, rb_gvar_readonly_setter);
536
541
 
537
542
  Op.loader = new Loader(Op);
538
543
  Op.cache = {};
539
544
 
540
- // const_set(cObject, 'RUBY_ENGINE', Op.platform.engine);
541
- const_set(cObject, 'RUBY_ENGINE', 'opal-gem');
545
+ rb_const_set(rb_cObject, 'RUBY_ENGINE', PLATFORM_ENGINE);
546
+ rb_const_set(rb_cObject, 'RUBY_PLATFORM', PLATFORM_PLATFORM);
547
+ rb_const_set(rb_cObject, 'RUBY_VERSION', PLATFORM_VERSION);
548
+ rb_const_set(rb_cObject, 'ARGV', PLATFORM_ARGV);
549
+
550
+ Op.run(core_lib);
542
551
 
543
552
  puts = function(str) {
544
- top_self.$m.puts(top_self, str);
553
+ rb_stdout.m$puts(str);
545
554
  };
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
555
  };
558
556