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
data/index.html DELETED
@@ -1,434 +0,0 @@
1
- <!DOCTYPE HTML>
2
- <html>
3
- <head>
4
- <meta http-equiv="content-type" content="text/html; charset=utf-8" />
5
-
6
- <title>Opal</title>
7
- <style>
8
- body {
9
- line-height: 22px;
10
- font-size: 14px;
11
- font-family: Helvetica Neue, Helvetica, Arial, sans-serif;
12
- }
13
-
14
- .content {
15
- padding: 50px 50px 50px 50px;
16
- width: 650px;
17
- }
18
-
19
- p {
20
- width: 650px;
21
- }
22
-
23
- pre, code {
24
- font-family: "Bitstream Vera Sans Mono", Monaco, "Lucida Console", monospace;
25
- font-size: 12px;
26
- }
27
-
28
- pre {
29
- line-height: 17px;
30
- color: #444444;
31
- white-space: pre;
32
- padding: 3px 0px 3px 12px;
33
- margin: 0px 0px 8px;
34
-
35
- background: #FAFAFA;
36
- -webkit-box-shadow: rgba(0,0,0,0.07) 0 1px 2px inset;
37
- -webkit-border-radius: 3px;
38
- -moz-border-radius: 3px;
39
- border-radius: 3px;
40
- border: 1px solid #DDDDDD;
41
- }
42
-
43
- code {
44
- background-color: #FAFAFA;
45
- padding: 1px;
46
- color: #444444;
47
- border: 1px solid #DEDEDE;
48
- }
49
-
50
- pre code {
51
- border: none;
52
- padding: 0px;
53
- }
54
-
55
- a {
56
- color: #212121;
57
- }
58
- </style>
59
- </head>
60
- <body>
61
- <div class="content">
62
- <h1 id='opal'>Opal</h1>
63
-
64
- <p>
65
- <strong>Opal is a ruby to javascript compiler</strong>. Opal aims to
66
- take ruby files and generate efficient javascript that maintains rubys
67
- features. Opal will, by default, generate fast and efficient code in
68
- preference to keeping all ruby features.
69
- </p>
70
-
71
- <p>
72
- Opal comes with an implementation of the ruby corelib, written in ruby,
73
- that uses a bundled runtime (written in javascript) that tie all the
74
- features together. Whenever possible Opal bridges to native javascript
75
- features under the hood. The Opal gem includes the compiler used to
76
- convert ruby sources into javascript.
77
- </p>
78
-
79
- <p>
80
- Opal is <a href='http://github.com/adambeynon/opal'>hosted on github</a>,
81
- and there is a Freenode IRC channel at <code>#opal</code>.
82
- </p>
83
-
84
- <h2 id='downloads'>Downloads</h2>
85
-
86
- <p>
87
- The Opal runtime and corelib are distributed here, and are required to
88
- run any code generated by opal.
89
- </p>
90
-
91
- <p>
92
- <strong>Production</strong> version to be used in real apps/sites.
93
- </p>
94
-
95
- <p>
96
- <a href='http://adambeynon.github.com/opal/opal.js'>
97
- Production version 0.3.15
98
- </a>
99
- <em>(120kb Uncompressed, 13.7kb Minified And Gzipped)</em>
100
- </p>
101
-
102
- <p>
103
- <strong>Debug</strong> version adds a lot of additional debug code.
104
- See the debug section below.
105
- </p>
106
-
107
- <p>
108
- <a href='http://adambeynon.github.com/opal/opal.debug.js'>
109
- Debug version 0.3.15
110
- </a>
111
- <em>(183kb Uncompressed)</em>
112
- </p>
113
-
114
- <h2 id='installation'>Installation</h2>
115
-
116
- <p>Opal comes distributed as a gem, so either install with:</p>
117
-
118
- <pre><code>gem install opal</code></pre>
119
-
120
- <p>Or add to your Gemfile:</p>
121
-
122
- <pre><code>gem &quot;opal&quot;</code></pre>
123
-
124
- <h2 id='usage'>Usage</h2>
125
-
126
- <p>
127
- Opal provides an <code>opal</code> command which can be used to
128
- compile ruby sources into javascript. These can then be used in
129
- any browser/javascript environment. Opal also comes with the
130
- ability to run files direcly using
131
- <a href='http://github.com/cowboyd/therubyracer'>therubyracer</a>,
132
- which is a gem to embed v8 into ruby. This also allows Opal to provide
133
- a REPL to run ruby code directly against the Opal runtime.
134
- </p>
135
-
136
- <h3 id='repl'>REPL</h3>
137
-
138
- <p>To run the ruby repl, simply run:</p>
139
-
140
- <pre><code>opal</code></pre>
141
-
142
- <p>from the command line. To exit, type <code>exit</code>.</p>
143
-
144
- <h3 id='running_a_ruby_file'>Running a ruby file</h3>
145
-
146
- <p>
147
- Opal can compile/eval any ruby file inside the v8 context by simply
148
- passing a path to run:
149
- </p>
150
-
151
- <pre><code>opal path/to/file.rb</code></pre>
152
-
153
- <p>Try running some code with <code>puts</code> statements.</p>
154
-
155
- <h3 id='compiling_ruby_sources'>Compiling Ruby Sources</h3>
156
-
157
- <p>If you have a ruby source <code>foo.rb</code>:</p>
158
-
159
- <pre>
160
- # foo.rb
161
-
162
- puts "Wow, running ruby"
163
- </pre>
164
-
165
- <p>Compile it into <code>foo.js</code> using:</p>
166
-
167
- <pre>
168
- opal -c foo.rb
169
- </pre>
170
-
171
- <p>
172
- The generated code includes a wrapper which will register the
173
- file with opal. Opal maintains an internal file system to make
174
- <code>require()</code> work as expected. You will see this in
175
- the output:
176
- </p>
177
-
178
- <pre>
179
- // foo.js
180
- opal.file('foo.rb', function() {
181
- // compiled ruby code
182
- });
183
- </pre>
184
-
185
- <p>
186
- This file is not enough to run by itself. It depends on
187
- <code>opal.js</code> which can be downloaded above. Finally,
188
- compiling a ruby source creates a wrapper around the code which
189
- will register the file with the opal runtime. To actually load
190
- this code, you can use the <code>main()</code> function:
191
- </p>
192
-
193
- <pre>
194
- opal.main('foo');
195
- </pre>
196
-
197
- <p>These files can be added to a HTML page:</p>
198
-
199
- <pre>
200
- &lt;!DOCTYPE html&gt;
201
- &lt;html&gt;
202
- &lt;head&gt;
203
- &lt;script src="opal.js"&gt;&lt;/script&gt;
204
- &lt;script src="foo.js"&gt;&lt;/script&gt;
205
- &lt;script&gt;
206
- opal.main('foo');
207
- &lt;/script&gt;
208
- &lt;/head&gt;
209
- &lt;body&gt;
210
-
211
- &lt;/body&gt;
212
- &lt;/html&gt;
213
- </pre>
214
-
215
- <p>Open this file and check out the browsers' console.</p>
216
-
217
- <h2 id='generated_code'>Generated Code</h2>
218
-
219
- <p>
220
- The Opal compiler is a source-to-source compiler; it reads in ruby
221
- code and ouputs javascript code. The generated code makes use of native
222
- javascript features when possible, and all code is output to the same
223
- line number as the source. This, along with correctly indented output,
224
- makes debugging very easy.
225
- </p>
226
-
227
- <h3 id='ruby_literals'>Ruby Literals</h3>
228
-
229
- <h4 id='literals'>Literals</h4>
230
-
231
- <p>
232
- <strong>self</strong> is always compiled to <code>this</code> in
233
- javascript which makes the generated code a lot cleaner to use. All
234
- methods, blocks, classes, modules and top level code correctly have
235
- their <code>this</code> value set.
236
- </p>
237
-
238
- <p>
239
- <strong>true</strong> and <strong>false</strong> are also compiled into
240
- their native javascript equivalents. This makes interacting with
241
- external libraries a lot easier as there is no need to convert to
242
- special ruby values.
243
- </p>
244
-
245
- <p>
246
- <strong>nil</strong> is compiled into a special ruby object (an instance
247
- of NilClass). A real object is used (instead of null and undefined) as
248
- this allows nil to receive method calls which is a crucial ruby feature
249
- which Opal maintains.
250
- </p>
251
-
252
- <h4 id='strings'>Strings</h4>
253
-
254
- <p>
255
- Ruby strings are compiled directly into javascript strings, for
256
- performance as well as readability. This has the side affect that Opal
257
- does not support mutable strings - all strings are immutable.
258
- </p>
259
-
260
- <h4 id='symbols'>Symbols</h4>
261
-
262
- <p>
263
- For performance reasons, Symbols compile into the string equivalents.
264
- Opal supports the symbol syntax(es), but does not have a real Symbol
265
- class. The Symbol constant is just an alias of String. Strings and
266
- Symbols can be used in Opal interchangeably.
267
- </p>
268
-
269
- <h4 id='numbers'>Numbers</h4>
270
-
271
- <p>
272
- In Opal there is a single class for all numbers; <code>Numeric</code>.
273
- To keep Opal as performant as possible, native javascript strings are
274
- used. This has the side effect that all numbers must be an instance of
275
- a single class. Most relevant methods from <code>Integer</code>,
276
- <code>Float</code> and <code>Numeric</code> are implemented on this
277
- class.
278
- </p>
279
-
280
- <h4 id='arrays'>Arrays</h4>
281
-
282
- <p>Ruby arrays compile straight into javascript array literals.</p>
283
-
284
- <h4 id='hash'>Hash</h4>
285
-
286
- <p>
287
- There is a special constructor available inside generated sources,
288
- <code>$hash</code> which is used to create hash instances.
289
- </p>
290
-
291
- <h4 id='range'>Range</h4>
292
-
293
- <p>
294
- Similarly to hashes, the <code>$range</code> constructor can be used
295
- to create new range instances.
296
- </p>
297
-
298
- <h3 id='ruby_methods'>Ruby Methods</h3>
299
-
300
- <p>
301
- A ruby method is just a function in the generated code. These functions
302
- are added to the constructor&#8217;s prototypes so they are called just
303
- like any other javascript function. All ruby methods are defined with
304
- an <code>m$</code> prefix which isolates them from any javascript
305
- function/property on the receiver.
306
- </p>
307
-
308
- <h4 id='method_calls'>Method Calls</h4>
309
-
310
- <p>
311
- All arguments are added to regular javascript function calls with the
312
- addition of a block argument. If a block is given, then it will be the
313
- first argument in the call. If no block is given then the first argument
314
- will be null. This block argument is invisible to the ruby code.
315
- </p>
316
-
317
- <p>The following ruby code:</p>
318
-
319
- <pre>
320
- do_something 1, 2, 3
321
- self.length
322
- [1, 2, 3].push 5
323
- </pre>
324
-
325
- <p>Will therefore compile into the following easy to read javascript:</p>
326
-
327
- <pre>
328
- this.m$do_something(null, 1, 2, 3);
329
- this.m$length();
330
- [1, 2, 3].m$push(null, 5);
331
- </pre>
332
-
333
- <p>
334
- There are of course some special characters valid as ruby names that are
335
- not valid as javascript identifiers. These are specially encoded to keep
336
- the generated javascript sane:
337
- </p>
338
-
339
- <pre>
340
- this.loaded? # =&gt; this.m$loaded$p()
341
- this.load! # =&gt; this.m$load$b()
342
- this.loaded = true # =&gt; this.m$loaded$e(null, true)
343
- </pre>
344
-
345
- <p>Call arguments with splats are also supported.</p>
346
-
347
- <pre>
348
- this.push *[1, 2, 3]
349
- # =&gt; this.m$push.apply(this, [null].concat([1, 2, 3])
350
- </pre>
351
-
352
- <p>When a block argument is given, it will be added as the first call arg.</p>
353
-
354
- <pre>
355
- describe &quot;some test&quot; do; ... end
356
- # =&gt; this.m$describe(function() { ... }, &quot;some test&quot;)
357
- </pre>
358
-
359
- <h4 id='method_definitions'>Method Definitions</h4>
360
-
361
- <p>
362
- Methods are implemented as regular javascript functions. Assuming the
363
- following method definition defined inside a class body:
364
- </p>
365
-
366
- <pre>
367
- def to_s
368
- inspect
369
- end
370
- </pre>
371
-
372
- <p>
373
- This would generate the following javascript (<code>$proto</code> will
374
- be explained in the Class documentation):
375
- </p>
376
-
377
- <pre>
378
- $proto.m$to_s = function() {
379
- return this.m$inspect();
380
- };
381
- </pre>
382
-
383
- <p>
384
- The defined name retains the <code>m$</code> prefix outlined above,
385
- and the <code>self</code> value for the method is <code>this</code>,
386
- which will be the receiver.
387
- </p>
388
-
389
- <p>Normal arguments, splat args and optional args are all supported:</p>
390
-
391
- <pre>
392
- def norm(a, b, c)
393
-
394
- end
395
-
396
- def opt(a, b = 100)
397
-
398
- end
399
-
400
- def rest(a, *b)
401
-
402
- end
403
- </pre>
404
-
405
- <p>
406
- The generated code includes an empty block definition
407
- <code>$block</code> which will not be used as this method does not
408
- yield to a block:
409
- </p>
410
-
411
- <pre>
412
- $proto.m$norm = function($block, a, b, c) {
413
- return nil;
414
- };
415
-
416
- $proto.m$opt = function($block, a, b) {
417
- if (b === undefined) b = 10;
418
- return nil;
419
- };
420
-
421
- $proto.m$rest = function($block, a, b) {
422
- b = Array.prototype.slice.call(arguments, 2);
423
- return nil;
424
- };
425
- </pre>
426
-
427
- <h2 id="change_log">Change Log</h2>
428
-
429
- <h3>0.3.15</h3>
430
-
431
- <p>Initial Release.</p>
432
- </div>
433
- </body>
434
- </html>
data/lib/opal/command.rb DELETED
@@ -1,73 +0,0 @@
1
- require 'optparse'
2
- require 'fileutils'
3
- require 'opal/builder'
4
- require 'opal/version'
5
-
6
- module Opal
7
- class Command
8
-
9
- def initialize(args)
10
- options = {}
11
- if ARGV.first == 'init'
12
- options[:init] = true
13
- ARGV.shift
14
- end
15
-
16
- OptionParser.new do |opts|
17
- opts.on('-c', '--compile', 'Compile ruby') do |c|
18
- options[:compile] = c
19
- end
20
-
21
- opts.on('-o', '--out [DIR]', 'Output directory') do |o|
22
- options[:out] = o || ''
23
- end
24
-
25
- opts.on('-j', '--join [OUT]', 'Join out') do |j|
26
- options[:join] = j || ''
27
- end
28
-
29
- opts.on('-d', '--debug', 'Debug mode') do |d|
30
- options[:debug] = true
31
- end
32
-
33
- opts.on_tail("-v", "--version", "Show version") do
34
- puts Opal::VERSION
35
- exit
36
- end
37
- end.parse!
38
-
39
- if options[:init]
40
- init options
41
- elsif options[:compile]
42
- build options
43
- else
44
- run options
45
- end
46
- end
47
-
48
- def run(options)
49
- if ARGV.empty?
50
- Context.new.start_repl
51
- elsif File.exists? ARGV.first
52
- Context.runner ARGV.first
53
- else
54
- puts "#{ARGV.first} does not exist"
55
- end
56
- end
57
-
58
- def init(options)
59
- out = options[:out]
60
- src = options[:debug] ? Opal.runtime_debug_code : Opal.runtime_code
61
- out ||= (options[:debug] ? 'opal.debug.js' : 'opal.js')
62
-
63
- FileUtils.mkdir_p File.dirname(out)
64
- File.open(out, 'w+') { |o| o.write src }
65
-
66
- puts "Wrote Opal to #{out}#{options[:debug] && ' (debug)'}"
67
- end
68
-
69
- def build(options)
70
- Builder.new(ARGV, options).build
71
- end
72
- end # Command
73
- end