opal 0.3.11 → 0.3.15

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 (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
data/runtime/README.md ADDED
@@ -0,0 +1,25 @@
1
+ Opal runtime
2
+ ------------
3
+
4
+ This directory holds the opal runtime and corelib files.
5
+
6
+ ### kernel
7
+
8
+ This is the very core runtime, written in javascript, that sets up the class/object heirarchy etc.
9
+
10
+ ### corelib
11
+
12
+ This holds Opal's implementation of the ruby corelib, written in ruby with inline javascript.
13
+
14
+ ### spec
15
+
16
+ Specs taken from RubySpec to test the runtime/corelib. This is not complete, but it contains tests
17
+ for parts of Opal that have been implemented.
18
+
19
+ ### gemlib
20
+
21
+ Additional files used by opal when running on the command line (it gives opal proper file access).
22
+
23
+ ### stdlib
24
+
25
+ The parts of the ruby standard library that are implemented for opal. This is very incomplete.
@@ -0,0 +1,10 @@
1
+ # load path getters
2
+ $LOAD_PATH = $: = `LOADER_PATHS`
3
+
4
+ # regexp matches
5
+ $~ = nil
6
+
7
+ RUBY_ENGINE = 'opal-browser'
8
+ RUBY_PLATFORM = 'opal'
9
+ RUBY_VERSION = '1.9.2'
10
+ ARGV = []
@@ -0,0 +1,962 @@
1
+ class Array
2
+ include Enumerable
3
+
4
+ def self.[](*objects)
5
+ %x{
6
+ var result = #{allocate};
7
+
8
+ result.splice.apply(result, [0, 0].concat(objects));
9
+
10
+ return result;
11
+ }
12
+ end
13
+
14
+ def self.allocate
15
+ %x{
16
+ var array = [];
17
+ array.$klass = this;
18
+
19
+ return array;
20
+ }
21
+ end
22
+
23
+ def self.new(*a)
24
+ `[]`
25
+ end
26
+
27
+ def &(other)
28
+ %x{
29
+ var result = [],
30
+ seen = {};
31
+
32
+ for (var i = 0, length = this.length; i < length; i++) {
33
+ var item = this[i],
34
+ hash = item;
35
+
36
+ if (!seen[hash]) {
37
+ for (var j = 0, length2 = other.length; j < length2; j++) {
38
+ var item2 = other[j],
39
+ hash2 = item2;
40
+
41
+ if ((hash === hash2) && !seen[hash]) {
42
+ seen[hash] = true;
43
+
44
+ result.push(item);
45
+ }
46
+ }
47
+ }
48
+ }
49
+
50
+ return result;
51
+ }
52
+ end
53
+
54
+ def *(other)
55
+ %x{
56
+ if (#{Opal.string?(other)}) {
57
+ return this.join(other);
58
+ }
59
+
60
+ var result = [];
61
+
62
+ for (var i = 0, length = this.length; i < length; i++) {
63
+ result = result.concat(this);
64
+ }
65
+
66
+ return result;
67
+ }
68
+ end
69
+
70
+ def +(other)
71
+ `this.slice(0).concat(other.slice(0))`
72
+ end
73
+
74
+ def <<(object)
75
+ `this.push(object);`
76
+
77
+ self
78
+ end
79
+
80
+ def <=>(other)
81
+ %x{
82
+ if (#{self.hash} === #{other.hash}) {
83
+ return 0;
84
+ }
85
+
86
+ if (this.length != other.length) {
87
+ return (this.length > other.length) ? 1 : -1;
88
+ }
89
+
90
+ for (var i = 0, length = this.length, tmp; i < length; i++) {
91
+ if ((tmp = #{`this[i]` <=> `other[i]`}) !== 0) {
92
+ return tmp;
93
+ }
94
+ }
95
+
96
+ return 0;
97
+ }
98
+ end
99
+
100
+ def ==(other)
101
+ %x{
102
+ if (this.length !== other.length) {
103
+ return false;
104
+ }
105
+
106
+ for (var i = 0, length = this.length; i < length; i++) {
107
+ if (!#{`this[i]` == `other[i]`}) {
108
+ return false;
109
+ }
110
+ }
111
+
112
+ return true;
113
+ }
114
+ end
115
+
116
+ # TODO: does not yet work with ranges
117
+ def [](index, length = undefined)
118
+ %x{
119
+ var size = this.length;
120
+
121
+ if (index < 0) {
122
+ index += size;
123
+ }
124
+
125
+ if (length !== undefined) {
126
+ if (length < 0 || index > size || index < 0) {
127
+ return nil;
128
+ }
129
+
130
+ return this.slice(index, index + length);
131
+ }
132
+ else {
133
+ if (index >= size || index < 0) {
134
+ return nil;
135
+ }
136
+
137
+ return this[index];
138
+ }
139
+ }
140
+ end
141
+
142
+ # TODO: need to expand functionality
143
+ def []=(index, value)
144
+ %x{
145
+ var size = this.length;
146
+
147
+ if (index < 0) {
148
+ index += size;
149
+ }
150
+
151
+ return this[index] = value;
152
+ }
153
+ end
154
+
155
+ def assoc(object)
156
+ %x{
157
+ for (var i = 0, length = this.length, item; i < length; i++) {
158
+ if (item = this[i], item.length && #{`item[0]` == object}) {
159
+ return item;
160
+ }
161
+ }
162
+
163
+ return nil;
164
+ }
165
+ end
166
+
167
+ def at(index)
168
+ %x{
169
+ if (index < 0) {
170
+ index += this.length;
171
+ }
172
+
173
+ if (index < 0 || index >= this.length) {
174
+ return nil;
175
+ }
176
+
177
+ return this[index];
178
+ }
179
+ end
180
+
181
+ def clear
182
+ `this.splice(0);`
183
+
184
+ self
185
+ end
186
+
187
+ def clone
188
+ `this.slice(0)`
189
+ end
190
+
191
+ def collect(&block)
192
+ return enum_for :collect unless block_given?
193
+
194
+ %x{
195
+ var result = [];
196
+
197
+ for (var i = 0, length = this.length, value; i < length; i++) {
198
+ if ((value = $yield.call($context, null, this[i])) === $breaker) {
199
+ return $breaker.$v;
200
+ }
201
+
202
+ result.push(value);
203
+ }
204
+
205
+ return result;
206
+ }
207
+ end
208
+
209
+ def collect!(&block)
210
+ return enum_for :collect! unless block_given?
211
+
212
+ %x{
213
+ for (var i = 0, length = this.length, val; i < length; i++) {
214
+ if ((val = $yield.call($context, null, this[i])) === $breaker) {
215
+ return $breaker.$v;
216
+ }
217
+
218
+ this[i] = val;
219
+ }
220
+ }
221
+
222
+ self
223
+ end
224
+
225
+ def compact
226
+ %x{
227
+ var result = [];
228
+
229
+ for (var i = 0, length = this.length, item; i < length; i++) {
230
+ if ((item = this[i]) !== nil) {
231
+ result.push(item);
232
+ }
233
+ }
234
+
235
+ return result;
236
+ }
237
+ end
238
+
239
+ def compact!
240
+ %x{
241
+ var original = this.length;
242
+
243
+ for (var i = 0, length = this.length; i < length; i++) {
244
+ if (this[i] === nil) {
245
+ this.splice(i, 1);
246
+
247
+ length--;
248
+ i--;
249
+ }
250
+ }
251
+
252
+ return this.length === original ? nil : this;
253
+ }
254
+ end
255
+
256
+ def concat(other)
257
+ %x{
258
+ for (var i = 0, length = other.length; i < length; i++) {
259
+ this.push(other[i]);
260
+ }
261
+ }
262
+
263
+ self
264
+ end
265
+
266
+ def count(object = undefined)
267
+ %x{
268
+ if (object === undefined) {
269
+ return this.length;
270
+ }
271
+
272
+ var result = 0;
273
+
274
+ for (var i = 0, length = this.length; i < length; i++) {
275
+ if (#{`this[i]` == object}) {
276
+ result++;
277
+ }
278
+ }
279
+
280
+ return result;
281
+ }
282
+ end
283
+
284
+ def delete(object)
285
+ %x{
286
+ var original = this.length;
287
+
288
+ for (var i = 0, length = original; i < length; i++) {
289
+ if (#{`this[i]` == object}) {
290
+ this.splice(i, 1);
291
+
292
+ length--;
293
+ i--;
294
+ }
295
+ }
296
+
297
+ return this.length === original ? nil : object;
298
+ }
299
+ end
300
+
301
+ def delete_at(index)
302
+ %x{
303
+ if (index < 0) {
304
+ index += this.length;
305
+ }
306
+
307
+ if (index < 0 || index >= this.length) {
308
+ return nil;
309
+ }
310
+
311
+ var result = this[index];
312
+
313
+ this.splice(index, 1);
314
+
315
+ return result;
316
+ }
317
+ end
318
+
319
+ def delete_if(&block)
320
+ return enum_for :delete_if unless block_given?
321
+
322
+ %x{
323
+ for (var i = 0, length = this.length, value; i < length; i++) {
324
+ if ((value = $yield.call($context, null, this[i])) === $breaker) {
325
+ return $breaker.$v;
326
+ }
327
+
328
+ if (value !== false && value !== nil) {
329
+ this.splice(i, 1);
330
+
331
+ length--;
332
+ i--;
333
+ }
334
+ }
335
+ }
336
+
337
+ self
338
+ end
339
+
340
+ def drop(number)
341
+ `number > this.length ? [] : this.slice(number)`
342
+ end
343
+
344
+ def drop_while(&block)
345
+ return enum_for :drop_while unless block_given?
346
+
347
+ %x{
348
+ for (var i = 0, length = this.length, value; i < length; i++) {
349
+ if ((value = $yield.call($context, null, this[i])) === $breaker) {
350
+ return $breaker.$v;
351
+ }
352
+
353
+ if (value === false || value === nil) {
354
+ return this.slice(i);
355
+ }
356
+ }
357
+
358
+ return [];
359
+ }
360
+ end
361
+
362
+ def each(&block)
363
+ return enum_for :each unless block_given?
364
+
365
+ %x{
366
+ for (var i = 0, length = this.length; i < length; i++) {
367
+ if ($yield.call($context, null, this[i]) === $breaker) {
368
+ return $breaker.$v;
369
+ }
370
+ }
371
+ }
372
+
373
+ self
374
+ end
375
+
376
+ def each_index(&block)
377
+ return enum_for :each_index unless block_given?
378
+
379
+ %x{
380
+ for (var i = 0, length = this.length; i < length; i++) {
381
+ if ($yield.call($context, null, i) === $breaker) {
382
+ return $breaker.$v;
383
+ }
384
+ }
385
+ }
386
+
387
+ self
388
+ end
389
+
390
+ def each_with_index(&block)
391
+ return enum_for :each_with_index unless block_given?
392
+
393
+ %x{
394
+ for (var i = 0, length = this.length; i < length; i++) {
395
+ if ($yield.call($context, null, this[i], i) === $breaker) {
396
+ return $breaker.$v;
397
+ }
398
+ }
399
+ }
400
+
401
+ self
402
+ end
403
+
404
+ def empty?
405
+ `this.length === 0`
406
+ end
407
+
408
+ def fetch(index, defaults = undefined, &block)
409
+ %x{
410
+ var original = index;
411
+
412
+ if (index < 0) {
413
+ index += this.length;
414
+ }
415
+
416
+ if (index >= 0 && index < this.length) {
417
+ return this[index];
418
+ }
419
+
420
+ if (defaults !== undefined) {
421
+ return defaults;
422
+ }
423
+
424
+ if (block !== nil) {
425
+ return $yield.call($context, null, original);
426
+ }
427
+
428
+ raise(RubyIndexError, 'Array#fetch');
429
+ }
430
+ end
431
+
432
+ def first(count = undefined)
433
+ %x{
434
+ if (count !== undefined) {
435
+ return this.slice(0, count);
436
+ }
437
+
438
+ return this.length === 0 ? nil : this[0];
439
+ }
440
+ end
441
+
442
+ def flatten(level = undefined)
443
+ %x{
444
+ var result = [];
445
+
446
+ for (var i = 0, length = this.length, item; i < length; i++) {
447
+ item = this[i];
448
+
449
+ if (item.$flags & T_ARRAY) {
450
+ if (level === undefined) {
451
+ result = result.concat(#{`item`.flatten});
452
+ }
453
+ else if (level === 0) {
454
+ result.push(item);
455
+ }
456
+ else {
457
+ result = result.concat(#{`item`.flatten(`level - 1`)});
458
+ }
459
+ }
460
+ else {
461
+ result.push(item);
462
+ }
463
+ }
464
+
465
+ return result;
466
+ }
467
+ end
468
+
469
+ def flatten!(level = undefined)
470
+ %x{
471
+ var flattenable = false;
472
+
473
+ for (var i = 0, length = this.length; i < length; i++) {
474
+ if (this[i].$flags & T_ARRAY) {
475
+ flattenable = true;
476
+
477
+ break;
478
+ }
479
+ }
480
+
481
+ return flattenable ? #{replace flatten level} : nil;
482
+ }
483
+ end
484
+
485
+ def grep(pattern)
486
+ %x{
487
+ var result = [];
488
+
489
+ for (var i = 0, length = this.length, item; i < length; i++) {
490
+ item = this[i];
491
+
492
+ if (#{pattern === `item`}) {
493
+ result.push(item);
494
+ }
495
+ }
496
+
497
+ return result;
498
+ }
499
+ end
500
+
501
+ def hash
502
+ `this.$id || (this.$id = unique_id++)`
503
+ end
504
+
505
+ def include?(member)
506
+ %x{
507
+ for (var i = 0, length = this.length; i < length; i++) {
508
+ if (#{`this[i]` == member}) {
509
+ return true;
510
+ }
511
+ }
512
+
513
+ return false;
514
+ }
515
+ end
516
+
517
+ def index(object = undefined, &block)
518
+ return enum_for :index unless block_given? && object == undefined
519
+
520
+ %x{
521
+ if (block !== nil) {
522
+ for (var i = 0, length = this.length, value; i < length; i++) {
523
+ if ((value = $yield.call($context, null, this[i])) === $breaker) {
524
+ return $breaker.$v;
525
+ }
526
+
527
+ if (value !== false && value !== nil) {
528
+ return i;
529
+ }
530
+ }
531
+ }
532
+ else {
533
+ for (var i = 0, length = this.length; i < length; i++) {
534
+ if (#{`this[i]` == object}) {
535
+ return i;
536
+ }
537
+ }
538
+ }
539
+
540
+ return nil
541
+ }
542
+ end
543
+
544
+ def inject(initial = undefined, &block)
545
+ return enum_for :inject unless block_given?
546
+
547
+ %x{
548
+ var result, i;
549
+
550
+ if (initial === undefined) {
551
+ result = this[0];
552
+ i = 1;
553
+ }
554
+ else {
555
+ result = initial;
556
+ i = 0;
557
+ }
558
+
559
+ for (var length = this.length, value; i < length; i++) {
560
+ if ((value = $yield.call($context, null, result, this[i])) === $breaker) {
561
+ return $breaker.$v;
562
+ }
563
+
564
+ result = value;
565
+ }
566
+
567
+ return result;
568
+ }
569
+ end
570
+
571
+ def insert(index, *objects)
572
+ %x{
573
+ if (objects.length > 0) {
574
+ if (index < 0) {
575
+ index += this.length + 1;
576
+
577
+ if (index < 0) {
578
+ raise(RubyIndexError, index + ' is out of bounds');
579
+ }
580
+ }
581
+ if (index > this.length) {
582
+ for (var i = this.length; i < index; i++) {
583
+ this.push(nil);
584
+ }
585
+ }
586
+
587
+ this.splice.apply(this, [index, 0].concat(objects));
588
+ }
589
+ }
590
+
591
+ self
592
+ end
593
+
594
+ def inspect
595
+ %x{
596
+ var inspect = [];
597
+
598
+ for (var i = 0, length = this.length; i < length; i++) {
599
+ inspect.push(#{`this[i]`.inspect});
600
+ }
601
+
602
+ return '[' + inspect.join(', ') + ']';
603
+ }
604
+ end
605
+
606
+ def join(sep = '')
607
+ %x{
608
+ var result = [];
609
+
610
+ for (var i = 0, length = this.length; i < length; i++) {
611
+ result.push(#{`this[i]`.to_s});
612
+ }
613
+
614
+ return result.join(sep);
615
+ }
616
+ end
617
+
618
+ def keep_if(&block)
619
+ return enum_for :keep_if unless block_given?
620
+ %x{
621
+ for (var i = 0, length = this.length, value; i < length; i++) {
622
+ if ((value = $yield.call($context, null, this[i])) === $breaker) {
623
+ return $breaker.$v;
624
+ }
625
+
626
+ if (value === false || value === nil) {
627
+ this.splice(i, 1);
628
+
629
+ length--;
630
+ i--;
631
+ }
632
+ }
633
+ }
634
+
635
+ self
636
+ end
637
+
638
+ def last(count = undefined)
639
+ %x{
640
+ var length = this.length;
641
+
642
+ if (count === undefined) {
643
+ return length === 0 ? nil : this[length - 1];
644
+ }
645
+ else if (count < 0) {
646
+ raise(RubyArgError, 'negative count given');
647
+ }
648
+
649
+ if (count > length) {
650
+ count = length;
651
+ }
652
+
653
+ return this.slice(length - count, length);
654
+ }
655
+ end
656
+
657
+ def length
658
+ `this.length`
659
+ end
660
+
661
+ alias_method :map, :collect
662
+
663
+ alias_method :map!, :collect!
664
+
665
+ def pop(count = undefined)
666
+ %x{
667
+ var length = this.length;
668
+
669
+ if (count === undefined) {
670
+ return length === 0 ? nil : this.pop();
671
+ }
672
+
673
+ if (count < 0) {
674
+ raise(RubyArgError, 'negative count given');
675
+ }
676
+
677
+ return count > length ? this.splice(0) : this.splice(length - count, length);
678
+ }
679
+ end
680
+
681
+ def push(*objects)
682
+ %x{
683
+ for (var i = 0, length = objects.length; i < length; i++) {
684
+ this.push(objects[i]);
685
+ }
686
+ }
687
+
688
+ self
689
+ end
690
+
691
+ def rassoc(object)
692
+ %x{
693
+ for (var i = 0, length = this.length, item; i < length; i++) {
694
+ item = this[i];
695
+
696
+ if (item.length && item[1] !== undefined) {
697
+ if (#{`item[1]` == object}) {
698
+ return item;
699
+ }
700
+ }
701
+ }
702
+
703
+ return nil;
704
+ }
705
+ end
706
+
707
+ def reject(&block)
708
+ return enum_for :reject unless block_given?
709
+
710
+ %x{
711
+ var result = [];
712
+
713
+ for (var i = 0, length = this.length, value; i < length; i++) {
714
+ if ((value = $yield.call($context, null, this[i])) === $breaker) {
715
+ return $breaker.$v;
716
+ }
717
+
718
+ if (value === false || value === nil) {
719
+ result.push(this[i]);
720
+ }
721
+ }
722
+ return result;
723
+ }
724
+ end
725
+
726
+ def reject!(&block)
727
+ return enum_for :reject! unless block_given?
728
+
729
+ %x{
730
+ var original = this.length;
731
+
732
+ for (var i = 0, length = this.length, value; i < length; i++) {
733
+ if ((value = $yield.call($context, null, this[i])) === $breaker) {
734
+ return $breaker.$v;
735
+ }
736
+
737
+ if (value !== false && value !== nil) {
738
+ this.splice(i, 1);
739
+
740
+ length--;
741
+ i--;
742
+ }
743
+ }
744
+
745
+ return original === this.length ? nil : this;
746
+ }
747
+ end
748
+
749
+ def replace(other)
750
+ clear
751
+ concat other
752
+ end
753
+
754
+ def reverse
755
+ `this.reverse()`
756
+ end
757
+
758
+ def reverse!
759
+ replace(reverse)
760
+ end
761
+
762
+ def reverse_each(&block)
763
+ return enum_for :reverse_each unless block_given?
764
+
765
+ reverse.each &block
766
+
767
+ self
768
+ end
769
+
770
+ def rindex(object = undefined, &block)
771
+ return enum_for :rindex unless block_given? && object == undefined
772
+
773
+ %x{
774
+ if (block !== nil) {
775
+ for (var i = this.length - 1, value; i >= 0; i--) {
776
+ if ((value = $yield.call($context, null, this[i])) === $breaker) {
777
+ return $breaker.$v;
778
+ }
779
+
780
+ if (value !== false && value !== nil) {
781
+ return i;
782
+ }
783
+ }
784
+ }
785
+ else {
786
+ for (var i = this.length - 1; i >= 0; i--) {
787
+ if (#{`this[i]` == `object`}) {
788
+ return i;
789
+ }
790
+ }
791
+ }
792
+
793
+ return nil;
794
+ }
795
+ end
796
+
797
+ def select(&block)
798
+ return enum_for :select unless block_given?
799
+
800
+ %x{
801
+ var result = [];
802
+
803
+ for (var i = 0, length = this.length, item, value; i < length; i++) {
804
+ item = this[i];
805
+
806
+ if ((value = $yield.call($context, null, item)) === $breaker) {
807
+ return $breaker.$v;
808
+ }
809
+
810
+ if (value !== false && value !== nil) {
811
+ result.push(item);
812
+ }
813
+ }
814
+
815
+ return result;
816
+ }
817
+ end
818
+
819
+ def select!(&block)
820
+ return enum_for :select! unless block_given?
821
+ %x{
822
+ var original = this.length;
823
+
824
+ for (var i = 0, length = original, item, value; i < length; i++) {
825
+ item = this[i];
826
+
827
+ if ((value = $yield.call($context, null, item)) === $breaker) {
828
+ return $breaker.$v;
829
+ }
830
+
831
+ if (value === false || value === nil) {
832
+ this.splice(i, 1);
833
+
834
+ length--;
835
+ i--;
836
+ }
837
+ }
838
+
839
+ return this.length === original ? nil : this;
840
+ }
841
+ end
842
+
843
+ def shift(count = undefined)
844
+ `count === undefined ? this.shift() : this.splice(0, count)`
845
+ end
846
+
847
+ alias_method :size, :length
848
+
849
+ alias_method :slice, :[]
850
+
851
+ def slice!(index, length = undefined)
852
+ %x{
853
+ if (index < 0) {
854
+ index += this.length;
855
+ }
856
+
857
+ if (index < 0 || index >= this.length) {
858
+ return nil;
859
+ }
860
+
861
+ if (length !== undefined) {
862
+ return this.splice(index, index + length);
863
+ }
864
+
865
+ return this.splice(index, 1)[0];
866
+ }
867
+ end
868
+
869
+ def take(count)
870
+ `this.slice(0, count)`
871
+ end
872
+
873
+ def take_while(&block)
874
+ return enum_for :take_while unless block_given?
875
+
876
+ %x{
877
+ var result = [];
878
+
879
+ for (var i = 0, length = this.length, item, value; i < length; i++) {
880
+ item = this[i];
881
+
882
+ if ((value = $yield.call($context, null, item)) === $breaker) {
883
+ return $breaker.$v;
884
+ }
885
+
886
+ if (value === false || value === nil) {
887
+ return result;
888
+ }
889
+
890
+ result.push(item);
891
+ }
892
+
893
+ return result;
894
+ }
895
+ end
896
+
897
+ def to_a
898
+ self
899
+ end
900
+
901
+ alias_method :to_ary, :to_a
902
+
903
+ def to_native
904
+ map { |obj| Opal.object?(obj) ? obj.to_native : obj }
905
+ end
906
+
907
+ alias_method :to_s, :inspect
908
+
909
+ def uniq
910
+ %x{
911
+ var result = [],
912
+ seen = {};
913
+
914
+ for (var i = 0, length = this.length, item, hash; i < length; i++) {
915
+ item = this[i];
916
+ hash = #{item.hash};
917
+
918
+ if (!seen[hash]) {
919
+ seen[hash] = true;
920
+
921
+ result.push(item);
922
+ }
923
+ }
924
+
925
+ return result;
926
+ }
927
+ end
928
+
929
+ def uniq!
930
+ %x{
931
+ var original = this.length,
932
+ seen = {};
933
+
934
+ for (var i = 0, length = original, item, hash; i < length; i++) {
935
+ item = this[i];
936
+ hash = #{item.hash};
937
+
938
+ if (!seen[hash]) {
939
+ seen[hash] = true;
940
+ }
941
+ else {
942
+ this.splice(i, 1);
943
+
944
+ length--;
945
+ i--;
946
+ }
947
+ }
948
+
949
+ return this.length === original ? nil : this;
950
+ }
951
+ end
952
+
953
+ def unshift(*objects)
954
+ %x{
955
+ for (var i = 0, length = objects.length; i < length; i++) {
956
+ this.unshift(objects[i]);
957
+ }
958
+
959
+ return this;
960
+ }
961
+ end
962
+ end