opal 0.3.16 → 0.3.17

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 (251) hide show
  1. data/.gitignore +2 -0
  2. data/CHANGELOG.md +12 -0
  3. data/Gemfile +8 -7
  4. data/README.md +21 -13
  5. data/Rakefile +64 -78
  6. data/bin/opal +18 -29
  7. data/core/alpha.rb +2 -9
  8. data/core/array.rb +106 -50
  9. data/core/basic_object.rb +10 -8
  10. data/core/boolean.rb +2 -0
  11. data/core/class.rb +25 -13
  12. data/core/comparable.rb +6 -6
  13. data/core/enumerable.rb +90 -94
  14. data/core/enumerator.rb +3 -3
  15. data/core/hash.rb +86 -46
  16. data/core/kernel.rb +55 -39
  17. data/core/load_order +2 -5
  18. data/core/match_data.rb +1 -1
  19. data/core/module.rb +45 -20
  20. data/core/nil_class.rb +6 -2
  21. data/core/numeric.rb +20 -10
  22. data/core/proc.rb +2 -2
  23. data/core/range.rb +72 -13
  24. data/core/regexp.rb +5 -3
  25. data/core/runtime.js +228 -287
  26. data/core/string.rb +345 -37
  27. data/core/top_self.rb +1 -1
  28. data/lib/opal.rb +13 -91
  29. data/lib/opal/builder.rb +47 -120
  30. data/lib/opal/builder_task.rb +74 -0
  31. data/lib/opal/{parser/grammar.rb → grammar.rb} +1094 -1083
  32. data/lib/opal/{parser/grammar.y → grammar.y} +7 -0
  33. data/lib/opal/{parser/lexer.rb → lexer.rb} +32 -11
  34. data/lib/opal/{parser/parser.rb → parser.rb} +232 -238
  35. data/lib/opal/{parser/scope.rb → scope.rb} +72 -9
  36. data/lib/opal/version.rb +2 -3
  37. data/opal.gemspec +1 -2
  38. data/{core_spec → spec}/core/array/allocate_spec.rb +1 -3
  39. data/{core_spec → spec}/core/array/append_spec.rb +1 -4
  40. data/{core_spec → spec}/core/array/assoc_spec.rb +1 -4
  41. data/{core_spec → spec}/core/array/at_spec.rb +1 -3
  42. data/{core_spec → spec}/core/array/clear_spec.rb +1 -3
  43. data/spec/core/array/clone_spec.rb +15 -0
  44. data/{core_spec/core/array/shared/collect.rb → spec/core/array/collect_spec.rb} +1 -1
  45. data/{core_spec → spec}/core/array/compact_spec.rb +1 -3
  46. data/{core_spec → spec}/core/array/concat_spec.rb +1 -3
  47. data/{core_spec → spec}/core/array/constructor_spec.rb +9 -3
  48. data/{core_spec → spec}/core/array/count_spec.rb +1 -3
  49. data/{core_spec → spec}/core/array/delete_at_spec.rb +1 -4
  50. data/{core_spec → spec}/core/array/delete_if_spec.rb +1 -4
  51. data/{core_spec → spec}/core/array/delete_spec.rb +1 -4
  52. data/{core_spec → spec}/core/array/each_index_spec.rb +1 -4
  53. data/{core_spec → spec}/core/array/each_spec.rb +1 -4
  54. data/{core_spec → spec}/core/array/element_reference_spec.rb +74 -4
  55. data/{core_spec → spec}/core/array/empty_spec.rb +1 -4
  56. data/{core_spec/core/array/shared/eql.rb → spec/core/array/eql_spec.rb} +1 -1
  57. data/{core_spec → spec}/core/array/fetch_spec.rb +1 -4
  58. data/{core_spec → spec}/core/array/first_spec.rb +1 -3
  59. data/{core_spec → spec}/core/array/flatten_spec.rb +1 -3
  60. data/{core_spec → spec}/core/array/include_spec.rb +1 -3
  61. data/{core_spec → spec}/core/array/insert_spec.rb +1 -4
  62. data/{core_spec → spec}/core/array/last_spec.rb +1 -4
  63. data/{core_spec/core/array/shared/length.rb → spec/core/array/length_spec.rb} +1 -1
  64. data/spec/core/array/map_spec.rb +53 -0
  65. data/{core_spec → spec}/core/array/plus_spec.rb +1 -4
  66. data/{core_spec → spec}/core/array/pop_spec.rb +1 -4
  67. data/{core_spec → spec}/core/array/push_spec.rb +1 -4
  68. data/{core_spec → spec}/core/array/rassoc_spec.rb +1 -4
  69. data/{core_spec → spec}/core/array/reject_spec.rb +1 -4
  70. data/{core_spec/core/array/shared/replace.rb → spec/core/array/replace_spec.rb} +1 -1
  71. data/{core_spec → spec}/core/array/reverse_each_spec.rb +1 -4
  72. data/{core_spec → spec}/core/array/reverse_spec.rb +1 -4
  73. data/spec/core/array/size_spec.rb +6 -0
  74. data/spec/core/array/to_ary_spec.rb +6 -0
  75. data/spec/core/array/uniq_spec.rb +22 -0
  76. data/spec/core/array/zip_spec.rb +20 -0
  77. data/{core_spec → spec}/core/class/new_spec.rb +1 -4
  78. data/{core_spec → spec}/core/enumerable/all_spec.rb +1 -4
  79. data/{core_spec → spec}/core/enumerable/any_spec.rb +1 -4
  80. data/{core_spec/core/enumerable/shared/collect.rb → spec/core/enumerable/collect_spec.rb} +1 -1
  81. data/{core_spec → spec}/core/enumerable/count_spec.rb +2 -5
  82. data/{core_spec → spec}/core/enumerable/fixtures/classes.rb +1 -2
  83. data/{core_spec → spec}/core/false/and_spec.rb +1 -3
  84. data/{core_spec → spec}/core/false/inspect_spec.rb +1 -3
  85. data/{core_spec → spec}/core/false/or_spec.rb +1 -3
  86. data/{core_spec → spec}/core/false/to_s_spec.rb +1 -3
  87. data/{core_spec → spec}/core/false/xor_spec.rb +1 -3
  88. data/{core_spec → spec}/core/hash/allocate_spec.rb +1 -3
  89. data/spec/core/hash/assoc_spec.rb +25 -0
  90. data/{core_spec → spec}/core/hash/clear_spec.rb +1 -3
  91. data/{core_spec → spec}/core/hash/clone_spec.rb +1 -3
  92. data/{core_spec → spec}/core/hash/default_spec.rb +1 -3
  93. data/{core_spec → spec}/core/hash/delete_if_spec.rb +1 -3
  94. data/{core_spec → spec}/core/hash/element_reference_spec.rb +1 -3
  95. data/{core_spec → spec}/core/hash/element_set_spec.rb +1 -3
  96. data/spec/core/hash/merge_spec.rb +37 -0
  97. data/{core_spec → spec}/core/hash/new_spec.rb +1 -3
  98. data/{core_spec → spec}/core/matchdata/to_a_spec.rb +1 -3
  99. data/{core_spec → spec}/core/nil/and_spec.rb +1 -4
  100. data/{core_spec → spec}/core/nil/inspect_spec.rb +1 -4
  101. data/{core_spec → spec}/core/nil/nil_spec.rb +1 -4
  102. data/{core_spec → spec}/core/nil/or_spec.rb +1 -4
  103. data/{core_spec → spec}/core/nil/to_a_spec.rb +1 -4
  104. data/{core_spec → spec}/core/nil/to_f_spec.rb +1 -4
  105. data/{core_spec → spec}/core/nil/to_i_spec.rb +1 -4
  106. data/{core_spec → spec}/core/nil/to_s_spec.rb +1 -4
  107. data/{core_spec → spec}/core/nil/xor_spec.rb +1 -4
  108. data/{core_spec → spec}/core/numeric/equal_value_spec.rb +1 -3
  109. data/{core_spec → spec}/core/regexp/match_spec.rb +10 -3
  110. data/{core_spec → spec}/core/symbol/to_proc_spec.rb +1 -3
  111. data/{core_spec → spec}/core/true/and_spec.rb +1 -3
  112. data/{core_spec → spec}/core/true/inspect_spec.rb +1 -3
  113. data/{core_spec → spec}/core/true/or_spec.rb +1 -3
  114. data/{core_spec → spec}/core/true/to_s_spec.rb +1 -3
  115. data/{core_spec → spec}/core/true/xor_spec.rb +1 -3
  116. data/spec/index.html +11 -0
  117. data/{core_spec → spec}/language/alias_spec.rb +1 -3
  118. data/{core_spec → spec}/language/and_spec.rb +1 -4
  119. data/{core_spec → spec}/language/array_spec.rb +1 -4
  120. data/{core_spec → spec}/language/block_spec.rb +20 -3
  121. data/{core_spec → spec}/language/break_spec.rb +40 -3
  122. data/{core_spec → spec}/language/case_spec.rb +1 -4
  123. data/{core_spec → spec}/language/defined_spec.rb +9 -3
  124. data/{core_spec → spec}/language/ensure_spec.rb +38 -3
  125. data/{core_spec → spec}/language/hash_spec.rb +1 -3
  126. data/{core_spec → spec}/language/if_spec.rb +1 -4
  127. data/{core_spec → spec}/language/loop_spec.rb +1 -3
  128. data/spec/language/metaclass_spec.rb +13 -0
  129. data/{core_spec → spec}/language/next_spec.rb +47 -3
  130. data/{core_spec → spec}/language/or_spec.rb +1 -4
  131. data/{core_spec → spec}/language/predefined_spec.rb +1 -3
  132. data/{core_spec/language/regexp/interpolation_spec.rb → spec/language/regexp_spec.rb} +6 -3
  133. data/{core_spec → spec}/language/send_spec.rb +38 -4
  134. data/spec/language/singleton_class_spec.rb +31 -0
  135. data/spec/language/super_spec.rb +188 -0
  136. data/{core_spec → spec}/language/symbol_spec.rb +1 -3
  137. data/{core_spec → spec}/language/undef_spec.rb +1 -3
  138. data/{core_spec → spec}/language/unless_spec.rb +1 -4
  139. data/{core_spec → spec}/language/until_spec.rb +1 -3
  140. data/{core_spec → spec}/language/variables_spec.rb +1 -3
  141. data/{core_spec → spec}/language/while_spec.rb +1 -3
  142. data/{spec → test}/builder/build_source_spec.rb +0 -0
  143. data/{spec → test}/builder/fixtures/build_source/adam.rb +0 -0
  144. data/{spec → test}/builder/fixtures/build_source/bar/a.rb +0 -0
  145. data/{spec → test}/builder/fixtures/build_source/bar/wow/b.rb +0 -0
  146. data/{spec → test}/builder/fixtures/build_source/bar/wow/cow/c.rb +0 -0
  147. data/{spec → test}/builder/fixtures/build_source/beynon.rb +0 -0
  148. data/{spec → test}/builder/fixtures/build_source/charles.js +0 -0
  149. data/{spec → test}/builder/fixtures/build_source/foo/a.rb +0 -0
  150. data/{spec → test}/builder/fixtures/build_source/foo/b.rb +0 -0
  151. data/{spec → test}/builder/fixtures/build_source/foo/x.js +0 -0
  152. data/{spec → test}/builder/fixtures/build_source/foo/y.js +0 -0
  153. data/{spec → test}/grammar/alias_spec.rb +0 -0
  154. data/{spec → test}/grammar/and_spec.rb +0 -0
  155. data/{spec → test}/grammar/array_spec.rb +0 -0
  156. data/{spec → test}/grammar/attrasgn_spec.rb +0 -0
  157. data/{spec → test}/grammar/begin_spec.rb +0 -0
  158. data/{spec → test}/grammar/block_spec.rb +0 -0
  159. data/{spec → test}/grammar/break_spec.rb +0 -0
  160. data/{spec → test}/grammar/call_spec.rb +0 -0
  161. data/{spec → test}/grammar/class_spec.rb +0 -0
  162. data/{spec → test}/grammar/const_spec.rb +0 -0
  163. data/{spec → test}/grammar/cvar_spec.rb +0 -0
  164. data/{spec → test}/grammar/def_spec.rb +0 -0
  165. data/{spec → test}/grammar/false_spec.rb +0 -0
  166. data/{spec → test}/grammar/file_spec.rb +0 -0
  167. data/{spec → test}/grammar/gvar_spec.rb +0 -0
  168. data/{spec → test}/grammar/hash_spec.rb +0 -0
  169. data/{spec → test}/grammar/iasgn_spec.rb +0 -0
  170. data/{spec → test}/grammar/if_spec.rb +0 -0
  171. data/{spec → test}/grammar/iter_spec.rb +0 -0
  172. data/{spec → test}/grammar/ivar_spec.rb +0 -0
  173. data/{spec → test}/grammar/lasgn_spec.rb +0 -0
  174. data/{spec → test}/grammar/line_spec.rb +0 -0
  175. data/{spec → test}/grammar/lvar_spec.rb +0 -0
  176. data/{spec → test}/grammar/masgn_spec.rb +0 -0
  177. data/{spec → test}/grammar/module_spec.rb +0 -0
  178. data/{spec → test}/grammar/nil_spec.rb +0 -0
  179. data/{spec → test}/grammar/not_spec.rb +0 -0
  180. data/{spec → test}/grammar/op_asgn1_spec.rb +0 -0
  181. data/{spec → test}/grammar/op_asgn2_spec.rb +0 -0
  182. data/{spec → test}/grammar/or_spec.rb +0 -0
  183. data/{spec → test}/grammar/return_spec.rb +0 -0
  184. data/{spec → test}/grammar/sclass_spec.rb +0 -0
  185. data/{spec → test}/grammar/self_spec.rb +0 -0
  186. data/{spec → test}/grammar/str_spec.rb +0 -0
  187. data/{spec → test}/grammar/super_spec.rb +0 -0
  188. data/{spec → test}/grammar/true_spec.rb +0 -0
  189. data/{spec → test}/grammar/undef_spec.rb +0 -0
  190. data/{spec → test}/grammar/unless_spec.rb +0 -0
  191. data/{spec → test}/grammar/while_spec.rb +0 -0
  192. data/{spec → test}/grammar/xstr_spec.rb +0 -0
  193. data/{spec → test}/grammar/yield_spec.rb +0 -0
  194. data/{spec → test}/spec_helper.rb +0 -0
  195. metadata +330 -264
  196. data/core/debug.js +0 -59
  197. data/core/debug.rb +0 -35
  198. data/core/dir.rb +0 -90
  199. data/core/file.rb +0 -83
  200. data/core/gemlib.rb +0 -30
  201. data/core/io.rb +0 -44
  202. data/core_spec/README.md +0 -34
  203. data/core_spec/core/array/collect_spec.rb +0 -3
  204. data/core_spec/core/array/element_set_spec.rb +0 -7
  205. data/core_spec/core/array/eql_spec.rb +0 -3
  206. data/core_spec/core/array/equal_value_spec.rb +0 -3
  207. data/core_spec/core/array/fixtures/classes.rb +0 -8
  208. data/core_spec/core/array/length_spec.rb +0 -3
  209. data/core_spec/core/array/map_spec.rb +0 -3
  210. data/core_spec/core/array/replace_spec.rb +0 -3
  211. data/core_spec/core/enumerable/collect_spec.rb +0 -3
  212. data/core_spec/core/enumerable/detect_spec.rb +0 -3
  213. data/core_spec/core/enumerable/find_spec.rb +0 -3
  214. data/core_spec/core/enumerable/first_spec.rb +0 -3
  215. data/core_spec/core/enumerable/shared/entries.rb +0 -7
  216. data/core_spec/core/enumerable/shared/find.rb +0 -49
  217. data/core_spec/core/enumerable/shared/take.rb +0 -31
  218. data/core_spec/core/enumerable/to_a_spec.rb +0 -7
  219. data/core_spec/core/hash/assoc_spec.rb +0 -29
  220. data/core_spec/core/object/is_a_spec.rb +0 -2
  221. data/core_spec/core/object/shared/kind_of.rb +0 -0
  222. data/core_spec/core/regexp/shared/match.rb +0 -11
  223. data/core_spec/language/fixtures/block.rb +0 -19
  224. data/core_spec/language/fixtures/break.rb +0 -39
  225. data/core_spec/language/fixtures/defined.rb +0 -9
  226. data/core_spec/language/fixtures/ensure.rb +0 -37
  227. data/core_spec/language/fixtures/next.rb +0 -46
  228. data/core_spec/language/fixtures/send.rb +0 -36
  229. data/core_spec/language/fixtures/super.rb +0 -43
  230. data/core_spec/language/regexp_spec.rb +0 -7
  231. data/core_spec/language/string_spec.rb +0 -4
  232. data/core_spec/language/super_spec.rb +0 -18
  233. data/core_spec/language/versions/hash_1.9.rb +0 -20
  234. data/core_spec/opal/opal/defined_spec.rb +0 -15
  235. data/core_spec/opal/opal/function_spec.rb +0 -11
  236. data/core_spec/opal/opal/native_spec.rb +0 -16
  237. data/core_spec/opal/opal/null_spec.rb +0 -10
  238. data/core_spec/opal/opal/number_spec.rb +0 -11
  239. data/core_spec/opal/opal/object_spec.rb +0 -16
  240. data/core_spec/opal/opal/string_spec.rb +0 -11
  241. data/core_spec/opal/opal/typeof_spec.rb +0 -9
  242. data/core_spec/opal/opal/undefined_spec.rb +0 -10
  243. data/core_spec/opal/true/case_compare_spec.rb +0 -12
  244. data/core_spec/opal/true/class_spec.rb +0 -10
  245. data/core_spec/release_runner.html +0 -17
  246. data/core_spec/runner.html +0 -16
  247. data/core_spec/spec_helper.rb +0 -23
  248. data/lib/opal/context.rb +0 -269
  249. data/lib/opal/dependency_builder.rb +0 -133
  250. data/lib/opal/environment.rb +0 -87
  251. data/lib/opal/parser/sexp.rb +0 -17
data/core/runtime.js CHANGED
@@ -1,9 +1,10 @@
1
- var opal = this.opal = {};
2
- opal.global = this;
1
+ var Opal = this.Opal = {};
2
+
3
+ Opal.global = this;
3
4
 
4
5
  // Minify common function calls
5
- var hasOwnProperty = Object.prototype.hasOwnProperty,
6
- $slice = Array.prototype.slice;
6
+ var __hasOwn = Object.prototype.hasOwnProperty,
7
+ __slice = Array.prototype.slice;
7
8
 
8
9
  // Types - also added to bridged objects
9
10
  var T_CLASS = 0x0001,
@@ -22,40 +23,13 @@ var T_CLASS = 0x0001,
22
23
  // Generates unique id for every ruby object
23
24
  var unique_id = 0;
24
25
 
25
- // Find function body for the super call
26
- function find_super(klass, callee, mid) {
27
- var cur_method;
28
-
29
- while (klass) {
30
- if (klass.$m[mid]) {
31
- if (klass.$m[mid] == callee) {
32
- cur_method = klass.$m[mid];
33
- break;
34
- }
35
- }
36
- klass = klass.$s;
37
- }
38
-
39
- if (!(klass && cur_method)) { return null; }
40
-
41
- klass = klass.$s;
42
-
43
- while (klass) {
44
- if (klass.$m[mid]) {
45
- return klass.$m[mid];
46
- }
47
-
48
- klass = klass.$s;
49
- }
50
- }
51
-
52
26
  // Jump return - return in proc body
53
- opal.jump = function(value, func) {
27
+ Opal.jump = function(value, func) {
54
28
  throw new Error('jump return');
55
29
  };
56
30
 
57
31
  // Get constant with given id
58
- opal.const_get = function(const_table, id) {
32
+ Opal.const_get = function(const_table, id) {
59
33
  if (const_table[id]) {
60
34
  return const_table[id];
61
35
  }
@@ -64,56 +38,35 @@ opal.const_get = function(const_table, id) {
64
38
  };
65
39
 
66
40
  // Table holds all class variables
67
- opal.cvars = {};
68
-
69
- // Array of all procs to be called at_exit
70
- var end_procs = [];
71
-
72
- // Call exit blocks in reverse order
73
- opal.do_at_exit = function() {
74
- var proc;
75
-
76
- while (proc = end_procs.pop()) {
77
- proc.call(proc.$S);
78
- }
79
- };
41
+ Opal.cvars = {};
80
42
 
81
43
  // Globals table
82
- opal.gvars = {};
44
+ Opal.gvars = {};
83
45
 
84
46
  // Define a method alias
85
- opal.alias = function(klass, new_name, old_name) {
47
+ Opal.alias = function(klass, new_name, old_name) {
86
48
  new_name = mid_to_jsid(new_name);
87
49
  old_name = mid_to_jsid(old_name);
88
50
 
89
- var body = klass.$proto[old_name];
51
+ var body = klass._proto[old_name];
90
52
 
91
53
  if (!body) {
92
- throw RubyNameError.$new("undefined method `" + old_name + "' for class `" + klass.$name + "'");
54
+ // throw RubyNameError.$new(null, "undefined method `" + old_name + "' for class `" + klass._name + "'");
55
+ throw new Error("undefined method `" + old_name + "' for class `" + klass._name + "'");
93
56
  }
94
57
 
95
58
  define_method(klass, new_name, body);
96
- return nil;
59
+ return null;
97
60
  };
98
61
 
99
- // method missing yielder - used in debug mode to call method_missing.
100
- opal.mm = function(jsid) {
101
- var mid = jsid_to_mid(jsid);
102
- return function() {
103
- var args = $slice.call(arguments);
104
- args.unshift(mid);
105
- return this.$method_missing.apply(this, args);
106
- };
107
- }
108
-
109
62
  // Actually define methods
110
- var define_method = opal.defn = function(klass, id, body) {
63
+ var define_method = Opal.defn = function(klass, id, body) {
111
64
  // If an object, make sure to use its class
112
- if (klass.$flags & T_OBJECT) {
113
- klass = klass.$klass;
65
+ if (klass._flags & T_OBJECT) {
66
+ klass = klass._klass;
114
67
  }
115
68
 
116
- klass.$allocator.prototype[id] = body;
69
+ klass._alloc.prototype[id] = body;
117
70
 
118
71
  var included_in = klass.$included_in, includee;
119
72
 
@@ -125,56 +78,51 @@ var define_method = opal.defn = function(klass, id, body) {
125
78
  }
126
79
  }
127
80
 
128
- if (klass.$bridge_prototype) {
129
- klass.$bridge_prototype[id] = body;
81
+ if (klass._bridge) {
82
+ klass._bridge[id] = body;
130
83
  }
131
84
 
132
85
 
133
- return nil;
86
+ return null;
134
87
  }
135
88
 
136
- // Fake yielder used when no block given
137
- opal.no_proc = function() {
138
- throw RubyLocalJumpError.$new('no block given');
139
- };
140
-
141
89
  function define_module(base, id) {
142
90
  var module;
143
91
 
144
92
  module = boot_module();
145
- module.$name = (base === RubyObject ? id : base.$name + '::' + id)
93
+ module._name = (base === RubyObject ? id : base._name + '::' + id)
146
94
 
147
95
  make_metaclass(module, RubyModule);
148
96
 
149
- module.$flags = T_MODULE;
97
+ module._flags = T_MODULE;
150
98
  module.$included_in = [];
151
99
 
152
100
  var const_alloc = function() {};
153
- var const_scope = const_alloc.prototype = new base.$const.alloc();
154
- module.$const = const_scope;
101
+ var const_scope = const_alloc.prototype = new base._scope.alloc();
102
+ module._scope = const_scope;
155
103
  const_scope.alloc = const_alloc;
156
104
 
157
- base.$const[id] = module;
105
+ base._scope[id] = module;
158
106
 
159
107
  return module;
160
108
  }
161
109
 
162
- // opal define class. 0: regular, 1: module, 2: shift class.
163
- opal.klass = function(base, superklass, id, body, type) {
110
+ // Opal define class. 0: regular, 1: module, 2: shift class.
111
+ Opal.klass = function(base, superklass, id, body, type) {
164
112
  var klass;
165
113
 
166
114
  switch (type) {
167
115
  case 0:
168
- if (base.$flags & T_OBJECT) {
169
- base = class_real(base.$klass);
116
+ if (base._flags & T_OBJECT) {
117
+ base = class_real(base._klass);
170
118
  }
171
119
 
172
- if (superklass === nil) {
120
+ if (superklass === null) {
173
121
  superklass = RubyObject;
174
122
  }
175
123
 
176
- if (hasOwnProperty.call(base.$const, id)) {
177
- klass = base.$const[id];
124
+ if (__hasOwn.call(base._scope, id)) {
125
+ klass = base._scope[id];
178
126
  }
179
127
  else {
180
128
  klass = define_class(base, id, superklass);
@@ -183,12 +131,12 @@ opal.klass = function(base, superklass, id, body, type) {
183
131
  break;
184
132
 
185
133
  case 1:
186
- if (base.$flags & T_OBJECT) {
187
- base = class_real(base.$klass);
134
+ if (base._flags & T_OBJECT) {
135
+ base = class_real(base._klass);
188
136
  }
189
137
 
190
- if (hasOwnProperty.call(base.$const, id)) {
191
- klass = base.$const[id];
138
+ if (__hasOwn.call(base._scope, id)) {
139
+ klass = base._scope[id];
192
140
  }
193
141
  else {
194
142
  klass = define_module(base, id);
@@ -197,22 +145,22 @@ opal.klass = function(base, superklass, id, body, type) {
197
145
  break;
198
146
 
199
147
  case 2:
200
- klass = singleton_class(base);
148
+ klass = base.$singleton_class();
201
149
  break;
202
150
  }
203
151
 
204
152
  return body.call(klass);
205
153
  };
206
154
 
207
- opal.slice = $slice;
155
+ Opal.slice = __slice;
208
156
 
209
- opal.defs = function(base, id, body) {
210
- return define_method(singleton_class(base), id, body);
157
+ Opal.defs = function(base, id, body) {
158
+ return define_method(base.$singleton_class(), id, body);
211
159
  };
212
160
 
213
161
  // Undefine one or more methods
214
- opal.undef = function(klass) {
215
- var args = $slice.call(arguments, 1);
162
+ Opal.undef = function(klass) {
163
+ var args = __slice.call(arguments, 1);
216
164
 
217
165
  for (var i = 0, length = args.length; i < length; i++) {
218
166
  var mid = args[i], id = mid_to_jsid[mid];
@@ -222,19 +170,76 @@ opal.undef = function(klass) {
222
170
  };
223
171
 
224
172
  // Calls a super method.
225
- opal.zuper = function(callee, self, args) {
226
- var mid = callee.$rbName,
227
- func = find_super(self.$klass, callee, mid);
173
+ Opal.zuper = function(callee, jsid, self, args) {
174
+ var func = find_super(self._klass, callee, jsid);
228
175
 
229
176
  if (!func) {
230
- throw RubyNoMethodError.$new("super: no superclass method `" + mid + "'"
231
- + " for " + self.$inspect());
177
+ throw RubyNoMethodError.$new(null, "super: no superclass method `" +
178
+ jsid_to_mid(jsid) + "'" + " for " + self.$inspect());
232
179
  }
233
180
 
234
181
  return func.apply(self, args);
235
182
  };
236
183
 
237
- var mid_to_jsid = opal.mid_to_jsid = function(mid) {
184
+ // dynamic super (inside block)
185
+ Opal.dsuper = function(scopes, defn, jsid, self, args) {
186
+ var method, scope = scopes[0];
187
+
188
+ for (var i = 0, length = scopes.length; i < length; i++) {
189
+ if (scope.o$jsid) {
190
+ jsid = scope.o$jsid;
191
+ method = scope;
192
+ break;
193
+ }
194
+ }
195
+
196
+ if (method) {
197
+ // one of the nested blocks was define_method'd
198
+ return Opal.zuper(method, jsid, self, args);
199
+ }
200
+ else if (defn) {
201
+ // blocks not define_method'd, but they were enclosed by a real method
202
+ return Opal.zuper(defn, jsid, self, args);
203
+ }
204
+
205
+ // if we get here then we were inside a nest of just blocks, and none have
206
+ // been defined as a method
207
+ throw RubyNoMethodError.$new(null, "super: cannot call super when not in method");
208
+ }
209
+
210
+ // Find function body for the super call
211
+ function find_super(klass, callee, mid) {
212
+ var cur_method;
213
+
214
+ while (klass) {
215
+ if (klass._proto.hasOwnProperty(mid)) {
216
+ if (klass._proto[mid] === callee) {
217
+ cur_method = klass._proto[mid];
218
+ break;
219
+ }
220
+ }
221
+ klass = klass._super;
222
+ }
223
+
224
+ if (!(klass && cur_method)) { return null; }
225
+
226
+ klass = klass._super;
227
+
228
+ while (klass) {
229
+ if (klass._proto.hasOwnProperty(mid)) {
230
+ // make sure our found method isnt the same - this can happen if this
231
+ // newly found method is from a module and we are now looking at the
232
+ // module it came from.
233
+ if (klass._proto[mid] !== callee) {
234
+ return klass._proto[mid];
235
+ }
236
+ }
237
+
238
+ klass = klass._super;
239
+ }
240
+ }
241
+
242
+ var mid_to_jsid = Opal.mid_to_jsid = function(mid) {
238
243
  if (method_names[mid]) {
239
244
  return method_names[mid];
240
245
  }
@@ -242,7 +247,7 @@ var mid_to_jsid = opal.mid_to_jsid = function(mid) {
242
247
  return '$' + mid.replace('!', '$b').replace('?', '$p').replace('=', '$e');
243
248
  }
244
249
 
245
- var jsid_to_mid = opal.jsid_to_mid = function(jsid) {
250
+ var jsid_to_mid = Opal.jsid_to_mid = function(jsid) {
246
251
  if (reverse_method_names[jsid]) {
247
252
  return reverse_method_names[jsid];
248
253
  }
@@ -252,14 +257,14 @@ var jsid_to_mid = opal.jsid_to_mid = function(jsid) {
252
257
  return jsid.replace('$b', '!').replace('$p', '?').replace('$e', '=');
253
258
  }
254
259
 
255
- opal.arg_error = function(given, expected) {
256
- throw RubyArgError.$new('wrong number of arguments(' + given + ' for ' + expected + ')');
260
+ Opal.arg_error = function(given, expected) {
261
+ throw RubyArgError.$new(null, 'wrong number of arguments(' + given + ' for ' + expected + ')');
257
262
  };
258
263
 
259
264
  // Boot a base class (makes instances).
260
265
  function boot_defclass(superklass) {
261
266
  var cls = function() {
262
- this.$id = unique_id++;
267
+ this._id = unique_id++;
263
268
 
264
269
  return this;
265
270
  };
@@ -272,7 +277,7 @@ function boot_defclass(superklass) {
272
277
  }
273
278
 
274
279
  cls.prototype.constructor = cls;
275
- cls.prototype.$flags = T_OBJECT;
280
+ cls.prototype._flags = T_OBJECT;
276
281
 
277
282
  return cls;
278
283
  }
@@ -280,7 +285,7 @@ function boot_defclass(superklass) {
280
285
  // Boot actual (meta classes) of core objects.
281
286
  function boot_makemeta(id, klass, superklass) {
282
287
  var meta = function() {
283
- this.$id = unique_id++;
288
+ this._id = unique_id++;
284
289
 
285
290
  return this;
286
291
  };
@@ -292,15 +297,15 @@ function boot_makemeta(id, klass, superklass) {
292
297
 
293
298
  var proto = meta.prototype;
294
299
  proto.$included_in = [];
295
- proto.$allocator = klass;
296
- proto.$flags = T_CLASS;
297
- proto.$name = id;
298
- proto.$s = superklass;
300
+ proto._alloc = klass;
301
+ proto._flags = T_CLASS;
302
+ proto._name = id;
303
+ proto._super = superklass;
299
304
  proto.constructor = meta;
300
305
 
301
306
  var result = new meta();
302
- klass.prototype.$klass = result;
303
- result.$proto = klass.prototype;
307
+ klass.prototype._klass = result;
308
+ result._proto = klass.prototype;
304
309
 
305
310
  return result;
306
311
  }
@@ -309,23 +314,23 @@ function boot_makemeta(id, klass, superklass) {
309
314
  function boot_class(superklass) {
310
315
  // instances
311
316
  var cls = function() {
312
- this.$id = unique_id++;
317
+ this._id = unique_id++;
313
318
 
314
319
  return this;
315
320
  };
316
321
 
317
322
  var ctor = function() {};
318
- ctor.prototype = superklass.$allocator.prototype;
323
+ ctor.prototype = superklass._alloc.prototype;
319
324
 
320
325
  cls.prototype = new ctor();
321
326
 
322
327
  var proto = cls.prototype;
323
328
  proto.constructor = cls;
324
- proto.$flags = T_OBJECT;
329
+ proto._flags = T_OBJECT;
325
330
 
326
331
  // class itself
327
332
  var meta = function() {
328
- this.$id = unique_id++;
333
+ this._id = unique_id++;
329
334
 
330
335
  return this;
331
336
  };
@@ -336,15 +341,15 @@ function boot_class(superklass) {
336
341
  meta.prototype = new mtor();
337
342
 
338
343
  proto = meta.prototype;
339
- proto.$allocator = cls;
340
- proto.$flags = T_CLASS;
344
+ proto._alloc = cls;
345
+ proto._flags = T_CLASS;
341
346
  proto.constructor = meta;
342
- proto.$s = superklass;
347
+ proto._super = superklass;
343
348
 
344
349
  var result = new meta();
345
- cls.prototype.$klass = result;
346
-
347
- result.$proto = cls.prototype;
350
+ cls.prototype._klass = result;
351
+
352
+ result._proto = cls.prototype;
348
353
 
349
354
  return result;
350
355
  }
@@ -354,33 +359,33 @@ function boot_module() {
354
359
  // can be a regular object
355
360
  var module_cons = function(){};
356
361
  var module_inst = module_cons.prototype;
357
-
362
+
358
363
  // Module itself
359
364
  var meta = function() {
360
- this.$id = unique_id++;
365
+ this._id = unique_id++;
361
366
  return this;
362
367
  };
363
-
368
+
364
369
  var mtor = function(){};
365
370
  mtor.prototype = RubyModule.constructor.prototype;
366
371
  meta.prototype = new mtor();
367
-
372
+
368
373
  var proto = meta.prototype;
369
- proto.$allocator = module_cons;
370
- proto.$flags = T_MODULE;
374
+ proto._alloc = module_cons;
375
+ proto._flags = T_MODULE;
371
376
  proto.constructor = meta;
372
- proto.$s = RubyModule;
373
-
377
+ proto._super = null;
378
+
374
379
  var module = new meta();
375
- module.$proto = module_inst;
376
-
380
+ module._proto = module_inst;
381
+
377
382
  return module;
378
383
  }
379
384
 
380
385
  // Get actual class ignoring singleton classes and iclasses.
381
386
  function class_real(klass) {
382
- while (klass.$flags & FL_SINGLETON) {
383
- klass = klass.$s;
387
+ while (klass._flags & FL_SINGLETON) {
388
+ klass = klass._super;
384
389
  }
385
390
 
386
391
  return klass;
@@ -388,21 +393,23 @@ function class_real(klass) {
388
393
 
389
394
  // Make metaclass for the given class
390
395
  function make_metaclass(klass, superklass) {
391
- if (klass.$flags & T_CLASS) {
392
- if ((klass.$flags & T_CLASS) && (klass.$flags & FL_SINGLETON)) {
396
+ if (klass._flags & T_CLASS) {
397
+ if ((klass._flags & T_CLASS) && (klass._flags & FL_SINGLETON)) {
393
398
  throw RubyException.$new('too much meta: return klass?');
394
399
  }
395
400
  else {
396
- var class_id = "#<Class:" + klass.$name + ">",
401
+ var class_id = "#<Class:" + klass._name + ">",
397
402
  meta = boot_class(superklass);
398
403
 
399
- meta.$name = class_id;
400
- meta.$allocator.prototype = klass.constructor.prototype;
401
- meta.$flags |= FL_SINGLETON;
404
+ meta._name = class_id;
405
+ meta._alloc.prototype = klass.constructor.prototype;
406
+ meta._proto = meta._alloc.prototype;
407
+ meta._flags |= FL_SINGLETON;
408
+ meta._klass = RubyClass;
402
409
 
403
- klass.$klass = meta;
410
+ klass._klass = meta;
404
411
 
405
- meta.$const = klass.$const;
412
+ meta._scope = klass._scope;
406
413
  meta.__attached__ = klass;
407
414
 
408
415
  return meta;
@@ -414,20 +421,20 @@ function make_metaclass(klass, superklass) {
414
421
  }
415
422
 
416
423
  function make_singleton_class(obj) {
417
- var orig_class = obj.$klass,
418
- class_id = "#<Class:#<" + orig_class.$name + ":" + orig_class.$id + ">>";
424
+ var orig_class = obj._klass,
425
+ class_id = "#<Class:#<" + orig_class._name + ":" + orig_class._id + ">>";
419
426
 
420
427
  klass = boot_class(orig_class);
421
- klass.$name = class_id;
428
+ klass._name = class_id;
422
429
 
423
- klass.$flags |= FL_SINGLETON;
424
- klass.$bridge_prototype = obj;
430
+ klass._flags |= FL_SINGLETON;
431
+ klass._bridge = obj;
425
432
 
426
- obj.$klass = klass;
433
+ obj._klass = klass;
427
434
 
428
435
  klass.__attached__ = obj;
429
436
 
430
- klass.$klass = class_real(orig_class).$k;
437
+ klass._klass = class_real(orig_class)._klass;
431
438
 
432
439
  return klass;
433
440
  }
@@ -436,13 +443,13 @@ function bridge_class(constructor, flags, id) {
436
443
  var klass = define_class(RubyObject, id, RubyObject),
437
444
  prototype = constructor.prototype;
438
445
 
439
- klass.$allocator = constructor;
440
- klass.$proto = prototype;
446
+ klass._alloc = constructor;
447
+ klass._proto = prototype;
441
448
 
442
449
  bridged_classes.push(klass);
443
450
 
444
- prototype.$klass = klass;
445
- prototype.$flags = flags;
451
+ prototype._klass = klass;
452
+ prototype._flags = flags;
446
453
 
447
454
  return klass;
448
455
  }
@@ -451,19 +458,19 @@ function bridge_class(constructor, flags, id) {
451
458
  function define_class(base, id, superklass) {
452
459
  var klass;
453
460
 
454
- var class_id = (base === RubyObject ? id : base.$name + '::' + id);
461
+ var class_id = (base === RubyObject ? id : base._name + '::' + id);
455
462
 
456
463
  klass = boot_class(superklass);
457
- klass.$name = class_id;
464
+ klass._name = class_id;
458
465
 
459
- make_metaclass(klass, superklass.$klass);
466
+ make_metaclass(klass, superklass._klass);
460
467
 
461
468
  var const_alloc = function() {};
462
- var const_scope = const_alloc.prototype = new base.$const.alloc();
463
- klass.$const = const_scope;
469
+ var const_scope = const_alloc.prototype = new base._scope.alloc();
470
+ klass._scope = const_scope;
464
471
  const_scope.alloc = const_alloc;
465
472
 
466
- base.$const[id] = klass;
473
+ base._scope[id] = klass;
467
474
 
468
475
  if (superklass.$inherited) {
469
476
  superklass.$inherited(klass);
@@ -472,112 +479,25 @@ function define_class(base, id, superklass) {
472
479
  return klass;
473
480
  }
474
481
 
475
- // Get singleton class of obj
476
- function singleton_class(obj) {
477
- var klass;
478
-
479
- if (obj.$flags & T_OBJECT) {
480
- if ((obj.$flags & T_NUMBER) || (obj.$flags & T_STRING)) {
481
- throw RubyTypeError.$new("can't define singleton");
482
- }
483
- }
482
+ function define_iclass(klass, module) {
483
+ var sup = klass._super;
484
484
 
485
- if ((obj.$klass.$flags & FL_SINGLETON) && obj.$klass.__attached__ == obj) {
486
- klass = obj.$klass;
487
- }
488
- else {
489
- var class_id = obj.$klass.$name;
485
+ var iclass = {};
486
+ iclass._proto = module._proto;
487
+ iclass._super = sup;
488
+ iclass._flags = T_ICLASS;
489
+ iclass._klass = module;
490
+ iclass._name = module._name;
490
491
 
491
- klass = make_metaclass(obj, obj.$klass);
492
- }
492
+ klass._super = iclass;
493
493
 
494
- return klass;
494
+ return iclass;
495
495
  }
496
496
 
497
- opal.main = function(id) {
498
- opal.gvars.$0 = find_lib(id);
499
-
500
- try {
501
- top_self.$require(id);
502
-
503
- opal.do_at_exit();
504
- }
505
- catch (e) {
506
- // this is defined in debug.js
507
- console.log(e.$klass.$name + ': ' + e.message);
508
- console.log("\t" + e.$backtrace().join("\n\t"));
509
- }
510
- };
511
-
512
- /**
513
- * Register a standard file. This can be used to register non-lib files.
514
- * For example, specs can be registered here so they are available.
515
- *
516
- * NOTE: Files should be registered as a full path with given factory.
517
- *
518
- * Usage:
519
- *
520
- * opal.file('/spec/foo.rb': function() {
521
- * // ...
522
- * });
523
- */
524
- opal.file = function(file, factory) {
525
- FACTORIES[file] = factory;
526
- };
527
-
528
- /**
529
- * Register a lib.
530
- *
531
- * Usage:
532
- *
533
- * opal.lib('my_lib', function() {
534
- * // ...
535
- * });
536
- *
537
- * opal.lib('my_lib/foo', function() {
538
- * // ...
539
- * });
540
- */
541
- opal.lib = function(lib, factory) {
542
- var file = '/lib/' + lib + '.rb';
543
- FACTORIES[file] = factory;
544
- LIBS[lib] = file;
545
- };
546
-
547
- var FACTORIES = {},
548
- FEATURES = [],
549
- LIBS = {},
550
- LOADER_PATHS = ['', '/lib'],
551
- LOADER_CACHE = {};
552
-
553
- function find_lib(id) {
554
- var path;
555
-
556
- // try to load a lib path first - i.e. something in our load path
557
- if (path = LIBS[id]) return path;
558
-
559
- // find '/opal/x' style libs
560
- if (path = LIBS['opal/' + id]) return path;
561
-
562
- // next, incase our require() has a ruby extension..
563
- if (FACTORIES['/lib/' +id]) return '/lib/' + id;
564
-
565
- // check if id is full path..
566
- if (FACTORIES[id]) return id;
567
-
568
- // full path without '.rb'
569
- if (FACTORIES[id + '.rb']) return id + '.rb';
570
-
571
- // check in current working directory.
572
- var in_cwd = FS_CWD + '/' + id;
573
-
574
- if (FACTORIES[in_cwd]) return in_cwd;
575
- };
576
-
577
- // Current working directory
578
- var FS_CWD = '/';
579
-
580
- // Turns a glob string into a regexp
497
+ // Handling requires
498
+ function require_handler(path) {
499
+ throw new Error('Cannot require ' + path);
500
+ }
581
501
 
582
502
  // Initialization
583
503
  // --------------
@@ -593,20 +513,24 @@ var RubyModule = boot_makemeta('Module', BootModule, RubyObject.constructor);
593
513
  var RubyClass = boot_makemeta('Class', BootClass, RubyModule.constructor);
594
514
 
595
515
  // Fix boot classes to use meta class
596
- RubyObject.$klass = RubyClass;
597
- RubyModule.$klass = RubyClass;
598
- RubyClass.$klass = RubyClass;
516
+ RubyObject._klass = RubyClass;
517
+ RubyModule._klass = RubyClass;
518
+ RubyClass._klass = RubyClass;
599
519
 
600
520
  // fix superclasses
601
- RubyObject.$s = null;
602
- RubyModule.$s = RubyObject;
603
- RubyClass.$s = RubyModule;
604
-
605
- opal.Object = RubyObject;
606
- opal.Module = RubyModule;
607
- opal.Class = RubyClass;
608
-
609
- // make object act like a module
521
+ RubyObject._super = null;
522
+ RubyModule._super = RubyObject;
523
+ RubyClass._super = RubyModule;
524
+
525
+ Opal.Object = RubyObject;
526
+ Opal.Module = RubyModule;
527
+ Opal.Class = RubyClass;
528
+
529
+ // Make object act like a module. Internally, `Object` gets included
530
+ // into all the bridged classes. This is because the native prototypes
531
+ // for these bridged classes need to get all the `Object` methods as
532
+ // well. This allows `Object` to just donate its instance methods to
533
+ // the bridged classes using the exact same method that modules use.
610
534
  var bridged_classes = RubyObject.$included_in = [];
611
535
 
612
536
  // Top level Object scope (used by object and top_self).
@@ -614,27 +538,43 @@ var top_const_alloc = function(){};
614
538
  var top_const_scope = top_const_alloc.prototype;
615
539
  top_const_scope.alloc = top_const_alloc;
616
540
 
617
- RubyObject.$const = opal.constants = top_const_scope;
541
+ RubyObject._scope = Opal.constants = top_const_scope;
618
542
 
619
543
  var module_const_alloc = function(){};
620
544
  var module_const_scope = new top_const_alloc();
621
545
  module_const_scope.alloc = module_const_alloc;
622
- RubyModule.$const = module_const_scope;
546
+ RubyModule._scope = module_const_scope;
623
547
 
624
548
  var class_const_alloc = function(){};
625
549
  var class_const_scope = new top_const_alloc();
626
550
  class_const_scope.alloc = class_const_alloc;
627
- RubyClass.$const = class_const_scope;
628
-
629
- RubyObject.$const.BasicObject = RubyObject;
630
- RubyObject.$const.Object = RubyObject;
631
- RubyObject.$const.Module = RubyModule;
632
- RubyObject.$const.Class = RubyClass;
551
+ RubyClass._scope = class_const_scope;
552
+
553
+ RubyObject._scope.BasicObject = RubyObject;
554
+ RubyObject._scope.Object = RubyObject;
555
+ RubyObject._scope.Module = RubyModule;
556
+ RubyObject._scope.Class = RubyClass;
557
+
558
+ // Every ruby object (except natives) will have their #to_s method aliased
559
+ // to the native .toString() function so that accessing ruby objects from
560
+ // javascript will return a nicer string format. This is also used when
561
+ // interpolating objects into strings as the js engine will call toString
562
+ // which in turn calls #to_s.
563
+ //
564
+ // This is also used as the hashing function. In ruby, #hash should return
565
+ // an integer. This is not possible in Opal as strings cannot be mutable
566
+ // and can not therefore have unique integer hashes. Seeing as strings or
567
+ // symbols are used more often as hash keys, this role is changed in Opal
568
+ // so that hash values should be strings, and this function makes the #to_s
569
+ // value for an object the default.
570
+ RubyObject._proto.toString = function() {
571
+ return this.$to_s();
572
+ };
633
573
 
634
- var top_self = opal.top = new RubyObject.$allocator();
574
+ var top_self = Opal.top = new RubyObject._alloc();
635
575
 
636
576
  var RubyNilClass = define_class(RubyObject, 'NilClass', RubyObject);
637
- var nil = opal.nil = new RubyNilClass.$allocator();
577
+ Opal.nil = new RubyNilClass._alloc();
638
578
 
639
579
  bridge_class(Array, T_OBJECT | T_ARRAY, 'Array');
640
580
  bridge_class(Number, T_OBJECT | T_NUMBER, 'Numeric');
@@ -646,6 +586,7 @@ bridge_class(RegExp, T_OBJECT, 'Regexp');
646
586
 
647
587
  var RubyMatch = define_class(RubyObject, 'MatchData', RubyObject);
648
588
  var RubyRange = define_class(RubyObject, 'Range', RubyObject);
589
+ RubyRange._proto._flags = T_OBJECT | T_RANGE;
649
590
 
650
591
  var RubyException = bridge_class(Error, T_OBJECT, 'Exception');
651
592
  var RubyStandardError = define_class(RubyObject, 'StandardError', RubyException);
@@ -662,11 +603,11 @@ var RubyKeyError = define_class(RubyObject, 'KeyError', RubyIndexError);
662
603
  var RubyRangeError = define_class(RubyObject, 'RangeError', RubyStandardError);
663
604
  var RubyNotImplError = define_class(RubyObject, 'NotImplementedError', RubyException);
664
605
 
665
- RubyException.$allocator.prototype.toString = function() {
666
- return this.$klass.$name + ': ' + this.message;
606
+ RubyException._alloc.prototype.toString = function() {
607
+ return this._klass._name + ': ' + this.message;
667
608
  };
668
609
 
669
- var breaker = opal.breaker = new Error('unexpected break');
670
- breaker.$klass = RubyLocalJumpError;
610
+ var breaker = Opal.breaker = new Error('unexpected break');
611
+ breaker._klass = RubyLocalJumpError;
671
612
  breaker.$t = function() { throw this; };
672
613