opal 0.3.15 → 0.3.16

Sign up to get free protection for your applications and to get access to all the features.
Files changed (236) hide show
  1. data/.gitignore +1 -1
  2. data/Gemfile +8 -2
  3. data/README.md +1 -1
  4. data/Rakefile +51 -57
  5. data/bin/opal +50 -2
  6. data/{runtime/corelib → core}/alpha.rb +6 -1
  7. data/{runtime/corelib → core}/array.rb +38 -42
  8. data/{runtime/corelib → core}/basic_object.rb +6 -17
  9. data/core/boolean.rb +40 -0
  10. data/core/class.rb +69 -0
  11. data/{runtime/corelib → core}/comparable.rb +0 -0
  12. data/core/debug.js +59 -0
  13. data/core/debug.rb +35 -0
  14. data/core/dir.rb +90 -0
  15. data/core/enumerable.rb +331 -0
  16. data/core/enumerator.rb +126 -0
  17. data/core/error.rb +40 -0
  18. data/{runtime/corelib → core}/file.rb +6 -3
  19. data/core/gemlib.rb +30 -0
  20. data/{runtime/corelib → core}/hash.rb +37 -35
  21. data/{runtime/corelib → core}/io.rb +0 -0
  22. data/{runtime/corelib → core}/kernel.rb +26 -21
  23. data/{runtime/corelib → core}/load_order +2 -5
  24. data/{runtime/corelib → core}/match_data.rb +2 -4
  25. data/core/module.rb +204 -0
  26. data/{runtime/corelib → core}/nil_class.rb +1 -7
  27. data/{runtime/corelib → core}/numeric.rb +9 -13
  28. data/core/object.rb +17 -0
  29. data/{runtime/corelib → core}/proc.rb +2 -13
  30. data/{runtime/corelib → core}/range.rb +19 -7
  31. data/{runtime/corelib → core}/rational.rb +0 -0
  32. data/{runtime/corelib → core}/regexp.rb +2 -6
  33. data/core/runtime.js +672 -0
  34. data/{runtime/corelib → core}/string.rb +25 -9
  35. data/{runtime/corelib → core}/struct.rb +0 -0
  36. data/core/time.rb +111 -0
  37. data/{runtime/corelib → core}/top_self.rb +0 -0
  38. data/{runtime/spec → core_spec}/README.md +0 -0
  39. data/{runtime/spec → core_spec}/core/array/allocate_spec.rb +0 -0
  40. data/{runtime/spec → core_spec}/core/array/append_spec.rb +0 -0
  41. data/{runtime/spec → core_spec}/core/array/assoc_spec.rb +0 -0
  42. data/{runtime/spec → core_spec}/core/array/at_spec.rb +0 -0
  43. data/{runtime/spec → core_spec}/core/array/clear_spec.rb +0 -0
  44. data/{runtime/spec → core_spec}/core/array/collect_spec.rb +0 -0
  45. data/{runtime/spec → core_spec}/core/array/compact_spec.rb +0 -0
  46. data/{runtime/spec → core_spec}/core/array/concat_spec.rb +0 -0
  47. data/{runtime/spec → core_spec}/core/array/constructor_spec.rb +0 -0
  48. data/{runtime/spec → core_spec}/core/array/count_spec.rb +0 -0
  49. data/{runtime/spec → core_spec}/core/array/delete_at_spec.rb +0 -0
  50. data/{runtime/spec → core_spec}/core/array/delete_if_spec.rb +0 -0
  51. data/{runtime/spec → core_spec}/core/array/delete_spec.rb +0 -0
  52. data/{runtime/spec → core_spec}/core/array/each_index_spec.rb +0 -0
  53. data/{runtime/spec → core_spec}/core/array/each_spec.rb +0 -0
  54. data/{runtime/spec → core_spec}/core/array/element_reference_spec.rb +0 -0
  55. data/{runtime/spec → core_spec}/core/array/element_set_spec.rb +0 -0
  56. data/{runtime/spec → core_spec}/core/array/empty_spec.rb +0 -0
  57. data/{runtime/spec → core_spec}/core/array/eql_spec.rb +0 -0
  58. data/{runtime/spec → core_spec}/core/array/equal_value_spec.rb +0 -0
  59. data/{runtime/spec → core_spec}/core/array/fetch_spec.rb +0 -0
  60. data/{runtime/spec → core_spec}/core/array/first_spec.rb +0 -0
  61. data/{runtime/spec → core_spec}/core/array/fixtures/classes.rb +0 -0
  62. data/{runtime/spec → core_spec}/core/array/flatten_spec.rb +0 -0
  63. data/{runtime/spec → core_spec}/core/array/include_spec.rb +0 -0
  64. data/{runtime/spec → core_spec}/core/array/insert_spec.rb +0 -0
  65. data/{runtime/spec → core_spec}/core/array/last_spec.rb +0 -0
  66. data/{runtime/spec → core_spec}/core/array/length_spec.rb +0 -0
  67. data/{runtime/spec → core_spec}/core/array/map_spec.rb +0 -0
  68. data/{runtime/spec → core_spec}/core/array/plus_spec.rb +0 -0
  69. data/{runtime/spec → core_spec}/core/array/pop_spec.rb +0 -0
  70. data/{runtime/spec → core_spec}/core/array/push_spec.rb +0 -0
  71. data/{runtime/spec → core_spec}/core/array/rassoc_spec.rb +0 -0
  72. data/{runtime/spec → core_spec}/core/array/reject_spec.rb +0 -0
  73. data/{runtime/spec → core_spec}/core/array/replace_spec.rb +0 -0
  74. data/{runtime/spec → core_spec}/core/array/reverse_each_spec.rb +0 -0
  75. data/{runtime/spec → core_spec}/core/array/reverse_spec.rb +0 -0
  76. data/{runtime/spec → core_spec}/core/array/shared/collect.rb +0 -0
  77. data/{runtime/spec → core_spec}/core/array/shared/eql.rb +0 -0
  78. data/{runtime/spec → core_spec}/core/array/shared/length.rb +0 -0
  79. data/{runtime/spec → core_spec}/core/array/shared/replace.rb +0 -0
  80. data/{runtime/spec → core_spec}/core/class/new_spec.rb +0 -0
  81. data/{runtime/spec → core_spec}/core/enumerable/all_spec.rb +0 -0
  82. data/{runtime/spec → core_spec}/core/enumerable/any_spec.rb +0 -0
  83. data/{runtime/spec → core_spec}/core/enumerable/collect_spec.rb +0 -0
  84. data/{runtime/spec → core_spec}/core/enumerable/count_spec.rb +0 -0
  85. data/{runtime/spec → core_spec}/core/enumerable/detect_spec.rb +0 -0
  86. data/{runtime/spec → core_spec}/core/enumerable/find_spec.rb +0 -0
  87. data/core_spec/core/enumerable/first_spec.rb +3 -0
  88. data/{runtime/spec → core_spec}/core/enumerable/fixtures/classes.rb +0 -0
  89. data/{runtime/spec → core_spec}/core/enumerable/shared/collect.rb +0 -0
  90. data/{runtime/spec → core_spec}/core/enumerable/shared/entries.rb +0 -0
  91. data/{runtime/spec → core_spec}/core/enumerable/shared/find.rb +0 -0
  92. data/core_spec/core/enumerable/shared/take.rb +31 -0
  93. data/{runtime/spec → core_spec}/core/enumerable/to_a_spec.rb +0 -0
  94. data/{runtime/spec → core_spec}/core/false/and_spec.rb +0 -0
  95. data/{runtime/spec → core_spec}/core/false/inspect_spec.rb +0 -0
  96. data/{runtime/spec → core_spec}/core/false/or_spec.rb +0 -0
  97. data/{runtime/spec → core_spec}/core/false/to_s_spec.rb +0 -0
  98. data/{runtime/spec → core_spec}/core/false/xor_spec.rb +0 -0
  99. data/{runtime/spec → core_spec}/core/hash/allocate_spec.rb +0 -0
  100. data/{runtime/spec → core_spec}/core/hash/assoc_spec.rb +0 -0
  101. data/{runtime/spec → core_spec}/core/hash/clear_spec.rb +0 -0
  102. data/{runtime/spec → core_spec}/core/hash/clone_spec.rb +0 -0
  103. data/{runtime/spec → core_spec}/core/hash/default_spec.rb +0 -0
  104. data/{runtime/spec → core_spec}/core/hash/delete_if_spec.rb +0 -0
  105. data/{runtime/spec → core_spec}/core/hash/element_reference_spec.rb +0 -0
  106. data/{runtime/spec → core_spec}/core/hash/element_set_spec.rb +0 -0
  107. data/{runtime/spec → core_spec}/core/hash/new_spec.rb +0 -0
  108. data/{runtime/spec → core_spec}/core/matchdata/to_a_spec.rb +0 -0
  109. data/{runtime/spec → core_spec}/core/nil/and_spec.rb +0 -0
  110. data/{runtime/spec → core_spec}/core/nil/inspect_spec.rb +0 -0
  111. data/{runtime/spec → core_spec}/core/nil/nil_spec.rb +0 -0
  112. data/{runtime/spec → core_spec}/core/nil/or_spec.rb +0 -0
  113. data/{runtime/spec → core_spec}/core/nil/to_a_spec.rb +0 -0
  114. data/{runtime/spec → core_spec}/core/nil/to_f_spec.rb +0 -0
  115. data/{runtime/spec → core_spec}/core/nil/to_i_spec.rb +0 -0
  116. data/{runtime/spec → core_spec}/core/nil/to_s_spec.rb +0 -0
  117. data/{runtime/spec → core_spec}/core/nil/xor_spec.rb +0 -0
  118. data/{runtime/spec → core_spec}/core/numeric/equal_value_spec.rb +0 -0
  119. data/{runtime/spec → core_spec}/core/object/is_a_spec.rb +0 -0
  120. data/{runtime/spec → core_spec}/core/object/shared/kind_of.rb +0 -0
  121. data/{runtime/spec → core_spec}/core/regexp/match_spec.rb +0 -0
  122. data/{runtime/spec → core_spec}/core/regexp/shared/match.rb +0 -0
  123. data/{runtime/spec → core_spec}/core/symbol/to_proc_spec.rb +0 -0
  124. data/{runtime/spec → core_spec}/core/true/and_spec.rb +0 -0
  125. data/{runtime/spec → core_spec}/core/true/inspect_spec.rb +0 -0
  126. data/{runtime/spec → core_spec}/core/true/or_spec.rb +0 -0
  127. data/{runtime/spec → core_spec}/core/true/to_s_spec.rb +0 -0
  128. data/{runtime/spec → core_spec}/core/true/xor_spec.rb +0 -0
  129. data/{runtime/spec → core_spec}/language/alias_spec.rb +0 -0
  130. data/{runtime/spec → core_spec}/language/and_spec.rb +0 -0
  131. data/{runtime/spec → core_spec}/language/array_spec.rb +0 -0
  132. data/{runtime/spec → core_spec}/language/block_spec.rb +0 -0
  133. data/{runtime/spec → core_spec}/language/break_spec.rb +0 -0
  134. data/{runtime/spec → core_spec}/language/case_spec.rb +0 -0
  135. data/{runtime/spec → core_spec}/language/defined_spec.rb +0 -0
  136. data/{runtime/spec → core_spec}/language/ensure_spec.rb +0 -0
  137. data/{runtime/spec → core_spec}/language/fixtures/block.rb +0 -0
  138. data/{runtime/spec → core_spec}/language/fixtures/break.rb +0 -0
  139. data/{runtime/spec → core_spec}/language/fixtures/defined.rb +0 -0
  140. data/{runtime/spec → core_spec}/language/fixtures/ensure.rb +0 -0
  141. data/{runtime/spec → core_spec}/language/fixtures/next.rb +0 -0
  142. data/{runtime/spec → core_spec}/language/fixtures/send.rb +0 -0
  143. data/{runtime/spec → core_spec}/language/fixtures/super.rb +0 -0
  144. data/{runtime/spec → core_spec}/language/hash_spec.rb +0 -0
  145. data/{runtime/spec → core_spec}/language/if_spec.rb +0 -0
  146. data/{runtime/spec → core_spec}/language/loop_spec.rb +0 -0
  147. data/{runtime/spec → core_spec}/language/next_spec.rb +0 -0
  148. data/{runtime/spec → core_spec}/language/or_spec.rb +0 -0
  149. data/{runtime/spec → core_spec}/language/predefined_spec.rb +0 -0
  150. data/{runtime/spec → core_spec}/language/regexp/interpolation_spec.rb +8 -0
  151. data/{runtime/spec → core_spec}/language/regexp_spec.rb +0 -0
  152. data/{runtime/spec → core_spec}/language/send_spec.rb +0 -0
  153. data/{runtime/spec → core_spec}/language/string_spec.rb +0 -0
  154. data/{runtime/spec → core_spec}/language/super_spec.rb +0 -0
  155. data/{runtime/spec → core_spec}/language/symbol_spec.rb +0 -0
  156. data/{runtime/spec → core_spec}/language/undef_spec.rb +0 -0
  157. data/{runtime/spec → core_spec}/language/unless_spec.rb +0 -0
  158. data/{runtime/spec → core_spec}/language/until_spec.rb +0 -0
  159. data/core_spec/language/variables_spec.rb +112 -0
  160. data/{runtime/spec → core_spec}/language/versions/hash_1.9.rb +0 -0
  161. data/{runtime/spec → core_spec}/language/while_spec.rb +0 -0
  162. data/{runtime/spec → core_spec}/opal/opal/defined_spec.rb +0 -0
  163. data/{runtime/spec → core_spec}/opal/opal/function_spec.rb +0 -0
  164. data/{runtime/spec → core_spec}/opal/opal/native_spec.rb +0 -0
  165. data/{runtime/spec → core_spec}/opal/opal/null_spec.rb +0 -0
  166. data/{runtime/spec → core_spec}/opal/opal/number_spec.rb +0 -0
  167. data/{runtime/spec → core_spec}/opal/opal/object_spec.rb +0 -0
  168. data/{runtime/spec → core_spec}/opal/opal/string_spec.rb +0 -0
  169. data/{runtime/spec → core_spec}/opal/opal/typeof_spec.rb +0 -0
  170. data/{runtime/spec → core_spec}/opal/opal/undefined_spec.rb +0 -0
  171. data/{runtime/spec → core_spec}/opal/true/case_compare_spec.rb +0 -0
  172. data/{runtime/spec → core_spec}/opal/true/class_spec.rb +0 -0
  173. data/{docs/spec_runner.html → core_spec/release_runner.html} +4 -3
  174. data/core_spec/runner.html +16 -0
  175. data/core_spec/spec_helper.rb +23 -0
  176. data/lib/opal.rb +85 -2
  177. data/lib/opal/builder.rb +129 -46
  178. data/lib/opal/context.rb +47 -26
  179. data/lib/opal/dependency_builder.rb +113 -14
  180. data/lib/opal/environment.rb +40 -45
  181. data/lib/opal/parser/grammar.rb +2296 -2254
  182. data/lib/opal/parser/grammar.y +27 -8
  183. data/lib/opal/parser/lexer.rb +12 -3
  184. data/lib/opal/parser/parser.rb +117 -30
  185. data/lib/opal/parser/scope.rb +2 -2
  186. data/lib/opal/version.rb +1 -1
  187. data/opal.gemspec +3 -8
  188. data/spec/grammar/masgn_spec.rb +37 -0
  189. metadata +177 -227
  190. data/index.html +0 -434
  191. data/lib/opal/command.rb +0 -73
  192. data/runtime/README.md +0 -25
  193. data/runtime/corelib/boolean.rb +0 -44
  194. data/runtime/corelib/class.rb +0 -43
  195. data/runtime/corelib/complex.rb +0 -2
  196. data/runtime/corelib/dir.rb +0 -29
  197. data/runtime/corelib/enumerable.rb +0 -316
  198. data/runtime/corelib/enumerator.rb +0 -80
  199. data/runtime/corelib/error.rb +0 -25
  200. data/runtime/corelib/module.rb +0 -171
  201. data/runtime/corelib/native.rb +0 -50
  202. data/runtime/corelib/object.rb +0 -21
  203. data/runtime/corelib/time.rb +0 -196
  204. data/runtime/gemlib/alpha.rb +0 -5
  205. data/runtime/gemlib/kernel.rb +0 -17
  206. data/runtime/gemlib/load_order +0 -2
  207. data/runtime/kernel/class.js +0 -256
  208. data/runtime/kernel/debug.js +0 -42
  209. data/runtime/kernel/init.js +0 -114
  210. data/runtime/kernel/load_order +0 -5
  211. data/runtime/kernel/loader.js +0 -151
  212. data/runtime/kernel/runtime.js +0 -414
  213. data/runtime/spec/language/variables_spec.rb +0 -28
  214. data/runtime/spec/library/stringscanner/scan_spec.rb +0 -36
  215. data/runtime/spec/opal/forwardable/def_instance_delegator_spec.rb +0 -49
  216. data/runtime/spec/spec_helper.rb +0 -25
  217. data/runtime/stdlib/base64.rb +0 -91
  218. data/runtime/stdlib/date.rb +0 -4
  219. data/runtime/stdlib/dev.rb +0 -171
  220. data/runtime/stdlib/forwardable.rb +0 -33
  221. data/runtime/stdlib/optparse.rb +0 -0
  222. data/runtime/stdlib/pp.rb +0 -6
  223. data/runtime/stdlib/racc/parser.rb +0 -159
  224. data/runtime/stdlib/rbconfig.rb +0 -0
  225. data/runtime/stdlib/si.rb +0 -17
  226. data/runtime/stdlib/strscan.rb +0 -53
  227. data/runtime/stdlib/uri.rb +0 -111
  228. data/runtime/stdlib/uri/common.rb +0 -1014
  229. data/runtime/stdlib/uri/ftp.rb +0 -261
  230. data/runtime/stdlib/uri/generic.rb +0 -1599
  231. data/runtime/stdlib/uri/http.rb +0 -106
  232. data/runtime/stdlib/uri/https.rb +0 -22
  233. data/runtime/stdlib/uri/ldap.rb +0 -260
  234. data/runtime/stdlib/uri/ldaps.rb +0 -20
  235. data/runtime/stdlib/uri/mailto.rb +0 -280
  236. data/spec/builder/output_path_spec.rb +0 -50
@@ -1,151 +0,0 @@
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
- };
@@ -1,414 +0,0 @@
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
- }