opal 0.3.11 → 0.3.15

Sign up to get free protection for your applications and to get access to all the features.
Files changed (282) hide show
  1. data/.gitignore +13 -0
  2. data/Gemfile +10 -0
  3. data/LICENSE +20 -0
  4. data/README.md +11 -116
  5. data/Rakefile +126 -0
  6. data/bin/opal +1 -2
  7. data/docs/spec_runner.html +16 -0
  8. data/index.html +434 -0
  9. data/lib/opal.rb +14 -15
  10. data/lib/opal/builder.rb +46 -148
  11. data/lib/opal/command.rb +45 -115
  12. data/lib/opal/context.rb +139 -78
  13. data/lib/opal/dependency_builder.rb +34 -0
  14. data/lib/opal/environment.rb +92 -0
  15. data/lib/opal/parser/grammar.rb +4915 -0
  16. data/lib/opal/{parser.y → parser/grammar.y} +430 -284
  17. data/lib/opal/parser/lexer.rb +1329 -0
  18. data/lib/opal/parser/parser.rb +1460 -0
  19. data/lib/opal/parser/scope.rb +140 -0
  20. data/lib/opal/parser/sexp.rb +17 -0
  21. data/lib/opal/version.rb +2 -1
  22. data/opal.gemspec +23 -0
  23. data/opal.js +3149 -4162
  24. data/runtime/README.md +25 -0
  25. data/runtime/corelib/alpha.rb +10 -0
  26. data/runtime/corelib/array.rb +962 -0
  27. data/runtime/corelib/basic_object.rb +66 -0
  28. data/runtime/corelib/boolean.rb +44 -0
  29. data/runtime/corelib/class.rb +43 -0
  30. data/runtime/corelib/comparable.rb +25 -0
  31. data/runtime/corelib/complex.rb +2 -0
  32. data/runtime/corelib/dir.rb +29 -0
  33. data/runtime/corelib/enumerable.rb +316 -0
  34. data/runtime/corelib/enumerator.rb +80 -0
  35. data/runtime/corelib/error.rb +25 -0
  36. data/runtime/corelib/file.rb +80 -0
  37. data/runtime/corelib/hash.rb +503 -0
  38. data/runtime/corelib/io.rb +44 -0
  39. data/runtime/corelib/kernel.rb +237 -0
  40. data/runtime/corelib/load_order +29 -0
  41. data/runtime/corelib/match_data.rb +37 -0
  42. data/runtime/corelib/module.rb +171 -0
  43. data/runtime/corelib/native.rb +50 -0
  44. data/runtime/corelib/nil_class.rb +47 -0
  45. data/runtime/corelib/numeric.rb +219 -0
  46. data/runtime/corelib/object.rb +21 -0
  47. data/runtime/corelib/proc.rb +42 -0
  48. data/runtime/corelib/range.rb +38 -0
  49. data/runtime/corelib/rational.rb +16 -0
  50. data/runtime/corelib/regexp.rb +63 -0
  51. data/runtime/corelib/string.rb +185 -0
  52. data/runtime/corelib/struct.rb +97 -0
  53. data/runtime/corelib/time.rb +196 -0
  54. data/runtime/corelib/top_self.rb +7 -0
  55. data/runtime/gemlib/alpha.rb +5 -0
  56. data/runtime/gemlib/kernel.rb +17 -0
  57. data/runtime/gemlib/load_order +2 -0
  58. data/runtime/kernel/class.js +256 -0
  59. data/runtime/kernel/debug.js +42 -0
  60. data/runtime/kernel/init.js +114 -0
  61. data/runtime/kernel/load_order +5 -0
  62. data/runtime/kernel/loader.js +151 -0
  63. data/runtime/kernel/runtime.js +414 -0
  64. data/runtime/spec/README.md +34 -0
  65. data/runtime/spec/core/array/allocate_spec.rb +15 -0
  66. data/runtime/spec/core/array/append_spec.rb +31 -0
  67. data/runtime/spec/core/array/assoc_spec.rb +29 -0
  68. data/runtime/spec/core/array/at_spec.rb +38 -0
  69. data/runtime/spec/core/array/clear_spec.rb +22 -0
  70. data/runtime/spec/core/array/collect_spec.rb +3 -0
  71. data/runtime/spec/core/array/compact_spec.rb +42 -0
  72. data/runtime/spec/core/array/concat_spec.rb +21 -0
  73. data/runtime/spec/core/array/constructor_spec.rb +24 -0
  74. data/runtime/spec/core/array/count_spec.rb +11 -0
  75. data/runtime/spec/core/array/delete_at_spec.rb +31 -0
  76. data/runtime/spec/core/array/delete_if_spec.rb +24 -0
  77. data/runtime/spec/core/array/delete_spec.rb +26 -0
  78. data/runtime/spec/core/array/each_index_spec.rb +33 -0
  79. data/runtime/spec/core/array/each_spec.rb +11 -0
  80. data/runtime/spec/core/array/element_reference_spec.rb +136 -0
  81. data/runtime/spec/core/array/element_set_spec.rb +7 -0
  82. data/runtime/spec/core/array/empty_spec.rb +10 -0
  83. data/runtime/spec/core/array/eql_spec.rb +3 -0
  84. data/runtime/spec/core/array/equal_value_spec.rb +3 -0
  85. data/runtime/spec/core/array/fetch_spec.rb +26 -0
  86. data/runtime/spec/core/array/first_spec.rb +54 -0
  87. data/runtime/spec/core/array/fixtures/classes.rb +8 -0
  88. data/runtime/spec/core/array/flatten_spec.rb +41 -0
  89. data/runtime/spec/core/array/include_spec.rb +20 -0
  90. data/runtime/spec/core/array/insert_spec.rb +59 -0
  91. data/runtime/spec/core/array/last_spec.rb +57 -0
  92. data/runtime/spec/core/array/length_spec.rb +3 -0
  93. data/runtime/spec/core/array/map_spec.rb +3 -0
  94. data/runtime/spec/core/array/plus_spec.rb +16 -0
  95. data/runtime/spec/core/array/pop_spec.rb +79 -0
  96. data/runtime/spec/core/array/push_spec.rb +19 -0
  97. data/runtime/spec/core/array/rassoc_spec.rb +12 -0
  98. data/runtime/spec/core/array/reject_spec.rb +54 -0
  99. data/runtime/spec/core/array/replace_spec.rb +3 -0
  100. data/runtime/spec/core/array/reverse_each_spec.rb +18 -0
  101. data/runtime/spec/core/array/reverse_spec.rb +9 -0
  102. data/runtime/spec/core/array/shared/collect.rb +53 -0
  103. data/runtime/spec/core/array/shared/eql.rb +19 -0
  104. data/runtime/spec/core/array/shared/length.rb +6 -0
  105. data/runtime/spec/core/array/shared/replace.rb +31 -0
  106. data/runtime/spec/core/class/new_spec.rb +19 -0
  107. data/runtime/spec/core/enumerable/all_spec.rb +102 -0
  108. data/runtime/spec/core/enumerable/any_spec.rb +115 -0
  109. data/runtime/spec/core/enumerable/collect_spec.rb +3 -0
  110. data/runtime/spec/core/enumerable/count_spec.rb +29 -0
  111. data/runtime/spec/core/enumerable/detect_spec.rb +3 -0
  112. data/runtime/spec/core/enumerable/find_spec.rb +3 -0
  113. data/runtime/spec/core/enumerable/fixtures/classes.rb +26 -0
  114. data/runtime/spec/core/enumerable/shared/collect.rb +12 -0
  115. data/runtime/spec/core/enumerable/shared/entries.rb +7 -0
  116. data/runtime/spec/core/enumerable/shared/find.rb +49 -0
  117. data/runtime/spec/core/enumerable/to_a_spec.rb +7 -0
  118. data/runtime/spec/core/false/and_spec.rb +11 -0
  119. data/runtime/spec/core/false/inspect_spec.rb +7 -0
  120. data/runtime/spec/core/false/or_spec.rb +11 -0
  121. data/runtime/spec/core/false/to_s_spec.rb +7 -0
  122. data/runtime/spec/core/false/xor_spec.rb +11 -0
  123. data/runtime/spec/core/hash/allocate_spec.rb +15 -0
  124. data/runtime/spec/core/hash/assoc_spec.rb +29 -0
  125. data/runtime/spec/core/hash/clear_spec.rb +21 -0
  126. data/runtime/spec/core/hash/clone_spec.rb +12 -0
  127. data/runtime/spec/core/hash/default_spec.rb +6 -0
  128. data/runtime/spec/core/hash/delete_if_spec.rb +15 -0
  129. data/runtime/spec/core/hash/element_reference_spec.rb +16 -0
  130. data/runtime/spec/core/hash/element_set_spec.rb +8 -0
  131. data/runtime/spec/core/hash/new_spec.rb +13 -0
  132. data/runtime/spec/core/matchdata/to_a_spec.rb +7 -0
  133. data/runtime/spec/core/nil/and_spec.rb +12 -0
  134. data/runtime/spec/core/nil/inspect_spec.rb +8 -0
  135. data/runtime/spec/core/nil/nil_spec.rb +8 -0
  136. data/runtime/spec/core/nil/or_spec.rb +12 -0
  137. data/runtime/spec/core/nil/to_a_spec.rb +8 -0
  138. data/runtime/spec/core/nil/to_f_spec.rb +12 -0
  139. data/runtime/spec/core/nil/to_i_spec.rb +12 -0
  140. data/runtime/spec/core/nil/to_s_spec.rb +8 -0
  141. data/runtime/spec/core/nil/xor_spec.rb +12 -0
  142. data/runtime/spec/core/numeric/equal_value_spec.rb +11 -0
  143. data/runtime/spec/core/object/is_a_spec.rb +2 -0
  144. data/runtime/spec/core/object/shared/kind_of.rb +0 -0
  145. data/runtime/spec/core/regexp/match_spec.rb +23 -0
  146. data/runtime/spec/core/regexp/shared/match.rb +11 -0
  147. data/runtime/spec/core/symbol/to_proc_spec.rb +8 -0
  148. data/runtime/spec/core/true/and_spec.rb +11 -0
  149. data/runtime/spec/core/true/inspect_spec.rb +7 -0
  150. data/runtime/spec/core/true/or_spec.rb +11 -0
  151. data/runtime/spec/core/true/to_s_spec.rb +7 -0
  152. data/runtime/spec/core/true/xor_spec.rb +11 -0
  153. data/runtime/spec/language/alias_spec.rb +25 -0
  154. data/runtime/spec/language/and_spec.rb +62 -0
  155. data/runtime/spec/language/array_spec.rb +68 -0
  156. data/runtime/spec/language/block_spec.rb +105 -0
  157. data/runtime/spec/language/break_spec.rb +49 -0
  158. data/runtime/spec/language/case_spec.rb +165 -0
  159. data/runtime/spec/language/defined_spec.rb +80 -0
  160. data/runtime/spec/language/ensure_spec.rb +82 -0
  161. data/runtime/spec/language/fixtures/block.rb +19 -0
  162. data/runtime/spec/language/fixtures/break.rb +39 -0
  163. data/runtime/spec/language/fixtures/defined.rb +9 -0
  164. data/runtime/spec/language/fixtures/ensure.rb +37 -0
  165. data/runtime/spec/language/fixtures/next.rb +46 -0
  166. data/runtime/spec/language/fixtures/send.rb +36 -0
  167. data/runtime/spec/language/fixtures/super.rb +43 -0
  168. data/runtime/spec/language/hash_spec.rb +43 -0
  169. data/runtime/spec/language/if_spec.rb +278 -0
  170. data/runtime/spec/language/loop_spec.rb +32 -0
  171. data/runtime/spec/language/next_spec.rb +128 -0
  172. data/runtime/spec/language/or_spec.rb +65 -0
  173. data/runtime/spec/language/predefined_spec.rb +21 -0
  174. data/runtime/spec/language/regexp/interpolation_spec.rb +9 -0
  175. data/runtime/spec/language/regexp_spec.rb +7 -0
  176. data/runtime/spec/language/send_spec.rb +105 -0
  177. data/runtime/spec/language/string_spec.rb +4 -0
  178. data/runtime/spec/language/super_spec.rb +18 -0
  179. data/runtime/spec/language/symbol_spec.rb +41 -0
  180. data/runtime/spec/language/undef_spec.rb +16 -0
  181. data/runtime/spec/language/unless_spec.rb +44 -0
  182. data/runtime/spec/language/until_spec.rb +137 -0
  183. data/runtime/spec/language/variables_spec.rb +28 -0
  184. data/runtime/spec/language/versions/hash_1.9.rb +20 -0
  185. data/runtime/spec/language/while_spec.rb +175 -0
  186. data/runtime/spec/library/stringscanner/scan_spec.rb +36 -0
  187. data/runtime/spec/opal/forwardable/def_instance_delegator_spec.rb +49 -0
  188. data/runtime/spec/opal/opal/defined_spec.rb +15 -0
  189. data/runtime/spec/opal/opal/function_spec.rb +11 -0
  190. data/runtime/spec/opal/opal/native_spec.rb +16 -0
  191. data/runtime/spec/opal/opal/null_spec.rb +10 -0
  192. data/runtime/spec/opal/opal/number_spec.rb +11 -0
  193. data/runtime/spec/opal/opal/object_spec.rb +16 -0
  194. data/runtime/spec/opal/opal/string_spec.rb +11 -0
  195. data/runtime/spec/opal/opal/typeof_spec.rb +9 -0
  196. data/runtime/spec/opal/opal/undefined_spec.rb +10 -0
  197. data/runtime/spec/opal/true/case_compare_spec.rb +12 -0
  198. data/runtime/spec/opal/true/class_spec.rb +10 -0
  199. data/runtime/spec/spec_helper.rb +25 -0
  200. data/runtime/stdlib/base64.rb +91 -0
  201. data/runtime/stdlib/date.rb +4 -0
  202. data/{stdlib → runtime/stdlib}/dev.rb +0 -0
  203. data/runtime/stdlib/forwardable.rb +33 -0
  204. data/runtime/stdlib/optparse.rb +0 -0
  205. data/runtime/stdlib/pp.rb +6 -0
  206. data/{stdlib → runtime/stdlib}/racc/parser.rb +0 -0
  207. data/runtime/stdlib/rbconfig.rb +0 -0
  208. data/runtime/stdlib/si.rb +17 -0
  209. data/runtime/stdlib/strscan.rb +53 -0
  210. data/runtime/stdlib/uri.rb +111 -0
  211. data/runtime/stdlib/uri/common.rb +1014 -0
  212. data/runtime/stdlib/uri/ftp.rb +261 -0
  213. data/runtime/stdlib/uri/generic.rb +1599 -0
  214. data/runtime/stdlib/uri/http.rb +106 -0
  215. data/runtime/stdlib/uri/https.rb +22 -0
  216. data/runtime/stdlib/uri/ldap.rb +260 -0
  217. data/runtime/stdlib/uri/ldaps.rb +20 -0
  218. data/runtime/stdlib/uri/mailto.rb +280 -0
  219. data/spec/builder/build_source_spec.rb +52 -0
  220. data/spec/builder/fixtures/build_source/adam.rb +0 -0
  221. data/spec/builder/fixtures/build_source/bar/a.rb +0 -0
  222. data/spec/builder/fixtures/build_source/bar/wow/b.rb +0 -0
  223. data/spec/builder/fixtures/build_source/bar/wow/cow/c.rb +0 -0
  224. data/spec/builder/fixtures/build_source/beynon.rb +0 -0
  225. data/spec/builder/fixtures/build_source/charles.js +0 -0
  226. data/spec/builder/fixtures/build_source/foo/a.rb +0 -0
  227. data/spec/builder/fixtures/build_source/foo/b.rb +0 -0
  228. data/spec/builder/fixtures/build_source/foo/x.js +0 -0
  229. data/spec/builder/fixtures/build_source/foo/y.js +0 -0
  230. data/spec/builder/output_path_spec.rb +50 -0
  231. data/spec/grammar/alias_spec.rb +26 -0
  232. data/spec/grammar/and_spec.rb +13 -0
  233. data/spec/grammar/array_spec.rb +22 -0
  234. data/spec/grammar/attrasgn_spec.rb +28 -0
  235. data/spec/grammar/begin_spec.rb +38 -0
  236. data/spec/grammar/block_spec.rb +12 -0
  237. data/spec/grammar/break_spec.rb +17 -0
  238. data/spec/grammar/call_spec.rb +58 -0
  239. data/spec/grammar/class_spec.rb +35 -0
  240. data/spec/grammar/const_spec.rb +13 -0
  241. data/spec/grammar/cvar_spec.rb +11 -0
  242. data/spec/grammar/def_spec.rb +60 -0
  243. data/spec/grammar/false_spec.rb +17 -0
  244. data/spec/grammar/file_spec.rb +7 -0
  245. data/spec/grammar/gvar_spec.rb +13 -0
  246. data/spec/grammar/hash_spec.rb +17 -0
  247. data/spec/grammar/iasgn_spec.rb +9 -0
  248. data/spec/grammar/if_spec.rb +26 -0
  249. data/spec/grammar/iter_spec.rb +59 -0
  250. data/spec/grammar/ivar_spec.rb +9 -0
  251. data/spec/grammar/lasgn_spec.rb +8 -0
  252. data/spec/grammar/line_spec.rb +8 -0
  253. data/spec/grammar/lvar_spec.rb +38 -0
  254. data/spec/grammar/module_spec.rb +27 -0
  255. data/spec/grammar/nil_spec.rb +17 -0
  256. data/spec/grammar/not_spec.rb +27 -0
  257. data/spec/grammar/op_asgn1_spec.rb +23 -0
  258. data/spec/grammar/op_asgn2_spec.rb +23 -0
  259. data/spec/grammar/or_spec.rb +13 -0
  260. data/spec/grammar/return_spec.rb +17 -0
  261. data/spec/grammar/sclass_spec.rb +20 -0
  262. data/spec/grammar/self_spec.rb +17 -0
  263. data/spec/grammar/str_spec.rb +96 -0
  264. data/spec/grammar/super_spec.rb +20 -0
  265. data/spec/grammar/true_spec.rb +17 -0
  266. data/spec/grammar/undef_spec.rb +15 -0
  267. data/spec/grammar/unless_spec.rb +13 -0
  268. data/spec/grammar/while_spec.rb +15 -0
  269. data/spec/grammar/xstr_spec.rb +116 -0
  270. data/spec/grammar/yield_spec.rb +20 -0
  271. data/spec/spec_helper.rb +9 -0
  272. metadata +346 -21
  273. data/lib/opal/bundle.rb +0 -34
  274. data/lib/opal/lexer.rb +0 -902
  275. data/lib/opal/nodes.rb +0 -2150
  276. data/lib/opal/parser.rb +0 -4894
  277. data/lib/opal/rake/bundle_task.rb +0 -63
  278. data/opal-parser.js +0 -8343
  279. data/stdlib/strscan.rb +0 -52
  280. data/templates/init/Rakefile +0 -7
  281. data/templates/init/index.html +0 -17
  282. data/templates/init/lib/__NAME__.rb +0 -2
@@ -0,0 +1,5 @@
1
+ runtime
2
+ class
3
+ loader
4
+ debug
5
+ init
@@ -0,0 +1,151 @@
1
+ opal.main = function(id) {
2
+ opal.gvars.$0 = find_lib(id);
3
+
4
+ try {
5
+ top_self.m$require(null, id);
6
+
7
+ opal.do_at_exit();
8
+ }
9
+ catch (e) {
10
+ // this is defined in debug.js
11
+ if (opal.backtrace) {
12
+ opal.backtrace(e);
13
+ }
14
+ }
15
+ };
16
+
17
+ /**
18
+ * Register a standard file. This can be used to register non-lib files.
19
+ * For example, specs can be registered here so they are available.
20
+ *
21
+ * NOTE: Files should be registered as a full path with given factory.
22
+ *
23
+ * Usage:
24
+ *
25
+ * opal.file('/spec/foo.rb': function() {
26
+ * // ...
27
+ * });
28
+ */
29
+ opal.file = function(file, factory) {
30
+ FACTORIES[file] = factory;
31
+ };
32
+
33
+ /**
34
+ * Register a lib.
35
+ *
36
+ * Usage:
37
+ *
38
+ * opal.lib('my_lib', function() {
39
+ * // ...
40
+ * });
41
+ *
42
+ * opal.lib('my_lib/foo', function() {
43
+ * // ...
44
+ * });
45
+ */
46
+ opal.lib = function(lib, factory) {
47
+ var file = '/lib/' + lib + '.rb';
48
+ FACTORIES[file] = factory;
49
+ LIBS[lib] = file;
50
+ };
51
+
52
+ FACTORIES = {};
53
+ LIBS = {};
54
+ LOADER_PATHS = ['', '/lib'];
55
+ LOADER_CACHE = {};
56
+
57
+ function find_lib(id) {
58
+ var lib = '/lib/' + id;
59
+
60
+ // try to load a lib path first - i.e. something in our load path
61
+ if (FACTORIES[lib + '.rb']) {
62
+ return lib + '.rb';
63
+ }
64
+
65
+ // next, incase our require() has a ruby extension..
66
+ if (FACTORIES[lib]) {
67
+ return lib;
68
+ }
69
+
70
+ // check if id is full path..
71
+ if (FACTORIES[id]) {
72
+ return id;
73
+ }
74
+
75
+ // full path without '.rb'
76
+ if (FACTORIES[id + '.rb']) {
77
+ return id + '.rb';
78
+ }
79
+
80
+ // check in current working directory.
81
+ var in_cwd = FS_CWD + '/' + id;
82
+
83
+ if (FACTORIES[in_cwd]) {
84
+ return in_cwd;
85
+ }
86
+ };
87
+
88
+ // Split to dirname, basename and extname
89
+ var PATH_RE = /^(.+\/(?!$)|\/)?((?:.+?)?(\.[^.]*)?)$/;
90
+
91
+ // Current working directory
92
+ var FS_CWD = '/';
93
+
94
+ // Turns a glob string into a regexp
95
+ function fs_glob_to_regexp(glob) {
96
+ var parts = glob.split(''),
97
+ length = parts.length,
98
+ result = '';
99
+
100
+ var opt_group_stack = 0;
101
+
102
+ for (var i = 0; i < length; i++) {
103
+ var cur = parts[i];
104
+
105
+ switch (cur) {
106
+ case '*':
107
+ if (parts[i + 1] === '*' && parts[i + 2] === '/') {
108
+ result += '.*';
109
+ i += 2;
110
+ }
111
+ else {
112
+ result += '[^/]*';
113
+ }
114
+ break;
115
+
116
+ case '.':
117
+ result += '\\';
118
+ result += cur;
119
+ break;
120
+
121
+ case ',':
122
+ if (opt_group_stack) {
123
+ result += '|';
124
+ }
125
+ else {
126
+ result += ',';
127
+ }
128
+ break;
129
+
130
+ case '{':
131
+ result += '(';
132
+ opt_group_stack++;
133
+ break;
134
+
135
+ case '}':
136
+ if (opt_group_stack) {
137
+ result += ')';
138
+ opt_group_stack--;
139
+ }
140
+ else {
141
+ result += '}'
142
+ }
143
+ break;
144
+
145
+ default:
146
+ result += cur;
147
+ }
148
+ }
149
+
150
+ return new RegExp('^' + result + '$');
151
+ };
@@ -0,0 +1,414 @@
1
+ var opal = this.opal = {};
2
+
3
+ // Minify common function calls
4
+ var ArrayProto = Array.prototype,
5
+ ObjectProto = Object.prototype,
6
+ $slice = ArrayProto.slice,
7
+ hasOwnProperty = ObjectProto.hasOwnProperty;
8
+
9
+ // Types - also added to bridged objects
10
+ var T_CLASS = 0x0001,
11
+ T_MODULE = 0x0002,
12
+ T_OBJECT = 0x0004,
13
+ T_BOOLEAN = 0x0008,
14
+ T_STRING = 0x0010,
15
+ T_ARRAY = 0x0020,
16
+ T_NUMBER = 0x0040,
17
+ T_PROC = 0x0080,
18
+ T_HASH = 0x0100,
19
+ T_RANGE = 0x0200,
20
+ T_ICLASS = 0x0400,
21
+ FL_SINGLETON = 0x0800;
22
+
23
+ // Generates unique id for every ruby object
24
+ var unique_id = 0;
25
+
26
+ function define_attr(klass, name, getter, setter) {
27
+ if (getter) {
28
+ define_method(klass, mid_to_jsid(name), function() {
29
+ var res = this[name];
30
+
31
+ return res == null ? nil : res;
32
+ });
33
+ }
34
+
35
+ if (setter) {
36
+ define_method(klass, mid_to_jsid(name + '='), function(block, val) {
37
+ return this[name] = val;
38
+ });
39
+ }
40
+ }
41
+
42
+ function define_attr_bridge(klass, target, name, getter, setter) {
43
+ if (getter) {
44
+ define_method(klass, mid_to_jsid(name), function() {
45
+ var res = target[name];
46
+
47
+ return res == null ? nil : res;
48
+ });
49
+ }
50
+
51
+ if (setter) {
52
+ define_method(klass, mid_to_jsid(name + '='), function (block, val) {
53
+ return target[name] = val;
54
+ });
55
+ }
56
+ }
57
+
58
+ /**
59
+ * Hash constructor
60
+ */
61
+ function Hash() {
62
+ var args = $slice.call(arguments),
63
+ assocs = {},
64
+ key;
65
+
66
+ this.map = assocs;
67
+ this.none = nil;
68
+ this.proc = nil;
69
+
70
+ for (var i = 0, length = args.length; i < length; i++) {
71
+ key = args[i];
72
+ assocs[key.m$hash()] = [key, args[++i]];
73
+ }
74
+
75
+ return this;
76
+ };
77
+
78
+ // Returns new hash with values passed from ruby
79
+ opal.hash = Hash;
80
+
81
+ // Find function body for the super call
82
+ function find_super(klass, callee, mid) {
83
+ var cur_method;
84
+
85
+ while (klass) {
86
+ if (klass.$m[mid]) {
87
+ if (klass.$m[mid] == callee) {
88
+ cur_method = klass.$m[mid];
89
+ break;
90
+ }
91
+ }
92
+ klass = klass.$s;
93
+ }
94
+
95
+ if (!(klass && cur_method)) { return null; }
96
+
97
+ klass = klass.$s;
98
+
99
+ while (klass) {
100
+ if (klass.$m[mid]) {
101
+ return klass.$m[mid];
102
+ }
103
+
104
+ klass = klass.$s;
105
+ }
106
+ }
107
+
108
+ // Jump return - return in proc body
109
+ opal.jump = function(value, func) {
110
+ throw new Error('jump return');
111
+ };
112
+
113
+ // Get constant with given id
114
+ opal.const_get = function(const_table, id) {
115
+ if (const_table[id]) {
116
+ return const_table[id];
117
+ }
118
+
119
+ raise(RubyNameError, 'uninitialized constant ' + id);
120
+ };
121
+
122
+ // Set constant with given id
123
+ opal.const_set = function(base, id, val) {
124
+ if (base.$flags & T_OBJECT) {
125
+ base = class_real(base.$klass);
126
+ }
127
+
128
+ return base.$const[id] = val;
129
+ };
130
+
131
+ // Table holds all class variables
132
+ opal.cvars = {};
133
+
134
+ // Array of all procs to be called at_exit
135
+ var end_procs = [];
136
+
137
+ // Call exit blocks in reverse order
138
+ opal.do_at_exit = function() {
139
+ var proc;
140
+
141
+ while (proc = end_procs.pop()) {
142
+ proc.call(proc.$S);
143
+ }
144
+ };
145
+
146
+ // Globals table
147
+ opal.gvars = {};
148
+
149
+ // Define a method alias
150
+ opal.alias = function(klass, new_name, old_name) {
151
+ new_name = mid_to_jsid(new_name);
152
+ old_name = mid_to_jsid(old_name);
153
+
154
+ var body = klass.$allocator.prototype[old_name];
155
+
156
+ if (!body) {
157
+ raise(RubyNameError, "undefined method `" + old_name + "' for class `" + klass.__classid__ + "'");
158
+ }
159
+
160
+ define_method(klass, new_name, body);
161
+ return nil;
162
+ };
163
+
164
+ // method missing yielder - used in debug mode to call method_missing.
165
+ opal.mm = function(jsid) {
166
+ var mid = jsid_to_mid(jsid);
167
+ return function(block) {
168
+ var args = $slice.call(arguments, 1);
169
+ args.unshift(mid);
170
+ args.unshift(block);
171
+ return this.m$method_missing.apply(this, args);
172
+ };
173
+ }
174
+
175
+ // Actually define methods
176
+ var define_method = opal.defn = function(klass, id, body) {
177
+ // If an object, make sure to use its class
178
+ if (klass.$flags & T_OBJECT) {
179
+ klass = klass.$klass;
180
+ }
181
+
182
+ // super uses this
183
+ if (!body.$rbName) {
184
+ body.$rbName = id;
185
+ }
186
+
187
+ klass.$allocator.prototype[id] = body;
188
+ klass.$m[id] = 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_method(includee, id, body);
197
+ }
198
+ }
199
+
200
+ // Add method to toll-free prototypes as well
201
+ if (klass.$bridge_prototype) {
202
+ klass.$bridge_prototype[id] = body;
203
+ }
204
+
205
+
206
+ return nil;
207
+ }
208
+
209
+ function define_method_bridge(klass, target, id, name) {
210
+ define_method(klass, id, function() {
211
+ return target.apply(this, $slice.call(arguments, 1));
212
+ });
213
+ }
214
+
215
+ function string_inspect(self) {
216
+ /* borrowed from json2.js, see file for license */
217
+ var cx = /[\u0000\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,
218
+ escapable = /[\\\"\x00-\x1f\x7f-\x9f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,
219
+ meta = {
220
+ '\b': '\\b',
221
+ '\t': '\\t',
222
+ '\n': '\\n',
223
+ '\f': '\\f',
224
+ '\r': '\\r',
225
+ '"' : '\\"',
226
+ '\\': '\\\\'
227
+ };
228
+
229
+ escapable.lastIndex = 0;
230
+
231
+ return escapable.test(self) ? '"' + self.replace(escapable, function(a) {
232
+ var c = meta[a];
233
+
234
+ return typeof c === 'string' ? c :
235
+ '\\u' + ('0000' + a.charCodeAt(0).toString(16)).slice(-4);
236
+ }) + '"' : '"' + self + '"';
237
+ };
238
+
239
+ // Fake yielder used when no block given
240
+ opal.no_proc = function() {
241
+ raise(RubyLocalJumpError, "no block given");
242
+ };
243
+
244
+ // Create a new Range instance
245
+ opal.range = function(beg, end, exc) {
246
+ var range = new RubyRange.$allocator();
247
+ range.begin = beg;
248
+ range.end = end;
249
+ range.exclude = exc;
250
+
251
+ return range;
252
+ };
253
+
254
+
255
+ function define_module(base, id) {
256
+ var module;
257
+
258
+ module = boot_module();
259
+ module.__classid__ = (base === RubyObject ? id : base.__classid__ + '::' + id)
260
+
261
+ make_metaclass(module, RubyModule);
262
+
263
+ module.$flags = T_MODULE;
264
+ module.$included_in = [];
265
+
266
+ var const_alloc = function() {};
267
+ var const_scope = const_alloc.prototype = new base.$const.alloc();
268
+ module.$const = const_scope;
269
+ const_scope.alloc = const_alloc;
270
+
271
+ base.$const[id] = module;
272
+
273
+ return module;
274
+ }
275
+
276
+ function include_module(klass, module) {
277
+ if (!klass.$included_modules) {
278
+ klass.$included_modules = [];
279
+ }
280
+
281
+ if (klass.$included_modules.indexOf(module) != -1) {
282
+ return;
283
+ }
284
+
285
+ klass.$included_modules.push(module);
286
+
287
+ if (!module.$included_in) {
288
+ module.$included_in = [];
289
+ }
290
+
291
+ module.$included_in.push(klass);
292
+
293
+ var module_proto = module.$allocator.prototype;
294
+ for (var method in module_proto) {
295
+ if (hasOwnProperty.call(module_proto, method)) {
296
+ if (!klass.$allocator.prototype[method]) {
297
+ define_method(klass, method, module_proto[method]);
298
+ }
299
+ }
300
+ }
301
+ }
302
+
303
+ // opal define class. 0: regular, 1: module, 2: shift class.
304
+ opal.klass = function(base, superklass, id, body, type) {
305
+ var klass;
306
+
307
+ switch (type) {
308
+ case 0:
309
+ if (base.$flags & T_OBJECT) {
310
+ base = class_real(base.$klass);
311
+ }
312
+
313
+ if (superklass === nil) {
314
+ superklass = RubyObject;
315
+ }
316
+
317
+ if (base.$const.hasOwnProperty(id)) {
318
+ klass = base.$const[id];
319
+ }
320
+ else {
321
+ klass = define_class(base, id, superklass);
322
+ }
323
+
324
+ break;
325
+
326
+ case 1:
327
+ if (base.$flags & T_OBJECT) {
328
+ base = class_real(base.$klass);
329
+ }
330
+
331
+ if (base.$const.hasOwnProperty(id)) {
332
+ klass = base.$const[id];
333
+ }
334
+ else {
335
+ klass = define_module(base, id);
336
+ }
337
+
338
+ break;
339
+
340
+ case 2:
341
+ klass = singleton_class(base);
342
+ break;
343
+ }
344
+
345
+ return body.call(klass);
346
+ };
347
+
348
+ opal.slice = $slice;
349
+
350
+ opal.defs = function(base, id, body) {
351
+ return define_method(singleton_class(base), id, body);
352
+ };
353
+
354
+ // Undefine one or more methods
355
+ opal.undef = function(klass) {
356
+ var args = $slice.call(arguments, 1);
357
+
358
+ for (var i = 0, length = args.length; i < length; i++) {
359
+ var mid = args[i], id = mid_to_jsid[mid];
360
+
361
+ delete klass.$m_tbl[id];
362
+ }
363
+ };
364
+
365
+ // Calls a super method.
366
+ opal.zuper = function(callee, self, args) {
367
+ var mid = callee.$rbName,
368
+ func = find_super(self.$klass, callee, mid);
369
+
370
+ if (!func) {
371
+ raise(RubyNoMethodError, "super: no superclass method `" + mid + "'"
372
+ + " for " + self.$m.inspect(self, 'inspect'));
373
+ }
374
+
375
+ args.unshift(null);
376
+ return func.apply(self, args);
377
+ };
378
+
379
+ function mid_to_jsid(mid) {
380
+ if (method_names[mid]) {
381
+ return method_names[mid];
382
+ }
383
+
384
+ return 'm$' + mid.replace('!', '$b').replace('?', '$p').replace('=', '$e');
385
+ }
386
+
387
+ function jsid_to_mid(jsid) {
388
+ if (reverse_method_names[jsid]) {
389
+ return reverse_method_names[jsid];
390
+ }
391
+
392
+ jsid = jsid.substr(2); // remove 'm$'
393
+
394
+ return jsid.replace('$b', '!').replace('$p', '?').replace('$e', '=');
395
+ }
396
+
397
+ // Raise a new exception using exception class and message
398
+ function raise(exc, str) {
399
+ throw exc.m$new(null, str);
400
+ }
401
+
402
+ opal.arg_error = function(given, expected) {
403
+ raise(RubyArgError, 'wrong number of arguments(' + given + ' for ' + expected + ')');
404
+ };
405
+
406
+ // Inspect object or class
407
+ function inspect_object(obj) {
408
+ if (obj.$flags & T_OBJECT) {
409
+ return "#<" + class_real(obj.$klass).__classid__ + ":0x" + (obj.$id * 400487).toString(16) + ">";
410
+ }
411
+ else {
412
+ return obj.__classid__;
413
+ }
414
+ }