opal 0.4.3 → 0.4.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (303) hide show
  1. data/.rspec +3 -0
  2. data/.travis.yml +0 -4
  3. data/README.md +6 -0
  4. data/Rakefile +30 -6
  5. data/bin/opal +6 -82
  6. data/corelib/{opal/array.rb → array.rb} +40 -56
  7. data/corelib/{opal/basic_object.rb → basic_object.rb} +2 -2
  8. data/corelib/{opal/boolean.rb → boolean.rb} +0 -8
  9. data/corelib/class.rb +47 -0
  10. data/corelib/{opal/comparable.rb → comparable.rb} +0 -0
  11. data/corelib/{opal/enumerable.rb → enumerable.rb} +208 -141
  12. data/corelib/{opal/enumerator.rb → enumerator.rb} +3 -3
  13. data/corelib/{opal/error.rb → error.rb} +1 -0
  14. data/corelib/{opal/hash.rb → hash.rb} +67 -56
  15. data/corelib/io.rb +39 -0
  16. data/corelib/{opal/kernel.rb → kernel.rb} +35 -31
  17. data/corelib/{opal/class.rb → module.rb} +29 -57
  18. data/corelib/native.rb +148 -0
  19. data/corelib/{opal/nil_class.rb → nil_class.rb} +2 -10
  20. data/corelib/{opal/numeric.rb → numeric.rb} +39 -14
  21. data/corelib/opal.rb +42 -25
  22. data/corelib/{opal/proc.rb → proc.rb} +3 -3
  23. data/corelib/{opal/range.rb → range.rb} +5 -1
  24. data/corelib/{opal/regexp.rb → regexp.rb} +0 -0
  25. data/corelib/{opal/runtime.js → runtime.js} +96 -188
  26. data/corelib/{opal/string.rb → string.rb} +20 -15
  27. data/corelib/struct.rb +139 -0
  28. data/corelib/{opal/time.rb → time.rb} +0 -0
  29. data/lib/opal.rb +4 -0
  30. data/lib/opal/cli.rb +79 -32
  31. data/lib/opal/cli_options.rb +91 -0
  32. data/lib/opal/erb.rb +41 -8
  33. data/lib/opal/grammar.rb +24 -10
  34. data/lib/opal/grammar.y +15 -4
  35. data/lib/opal/grammar_helpers.rb +4 -0
  36. data/lib/opal/lexer.rb +214 -143
  37. data/lib/opal/parser.rb +596 -562
  38. data/lib/opal/require_parser.rb +1 -1
  39. data/lib/opal/source_map.rb +15 -30
  40. data/lib/opal/target_scope.rb +24 -5
  41. data/lib/opal/version.rb +1 -1
  42. data/mri_spec/cli_spec.rb +18 -0
  43. data/mri_spec/fixtures/opal_file.rb +2 -0
  44. data/mri_spec/spec_helper.rb +17 -0
  45. data/opal.gemspec +5 -2
  46. data/spec/{rubyspec/filters → filters}/bugs/alias.rb +0 -0
  47. data/spec/{rubyspec/filters → filters}/bugs/ancestors.rb +0 -0
  48. data/spec/{rubyspec/filters → filters}/bugs/array.rb +0 -0
  49. data/spec/filters/bugs/array/combination.rb +11 -0
  50. data/spec/filters/bugs/array/count.rb +3 -0
  51. data/spec/filters/bugs/array/delete_if.rb +3 -0
  52. data/spec/filters/bugs/array/drop.rb +3 -0
  53. data/spec/filters/bugs/array/drop_while.rb +5 -0
  54. data/spec/filters/bugs/array/eql.rb +3 -0
  55. data/spec/filters/bugs/array/flatten.rb +9 -0
  56. data/spec/filters/bugs/array/minus.rb +5 -0
  57. data/spec/filters/bugs/array/multipliy.rb +9 -0
  58. data/spec/filters/bugs/array/new.rb +3 -0
  59. data/spec/filters/bugs/array/pop.rb +6 -0
  60. data/spec/filters/bugs/array/rassoc.rb +4 -0
  61. data/spec/filters/bugs/array/rindex.rb +6 -0
  62. data/spec/filters/bugs/array/select.rb +3 -0
  63. data/spec/filters/bugs/array/shift.rb +7 -0
  64. data/spec/filters/bugs/array/shuffle.rb +11 -0
  65. data/spec/filters/bugs/array/slice.rb +7 -0
  66. data/spec/filters/bugs/array/take.rb +3 -0
  67. data/spec/filters/bugs/array/to_a.rb +3 -0
  68. data/spec/filters/bugs/array/try_convert.rb +7 -0
  69. data/spec/filters/bugs/array/uniq.rb +10 -0
  70. data/spec/filters/bugs/array/zip.rb +4 -0
  71. data/spec/{rubyspec/filters → filters}/bugs/array_delete.rb +1 -0
  72. data/spec/{rubyspec/filters → filters}/bugs/array_fetch.rb +0 -0
  73. data/spec/{rubyspec/filters → filters}/bugs/array_first.rb +0 -0
  74. data/spec/{rubyspec/filters → filters}/bugs/array_flatten.rb +0 -0
  75. data/spec/{rubyspec/filters → filters}/bugs/array_intersection.rb +0 -0
  76. data/spec/{rubyspec/filters → filters}/bugs/array_join.rb +0 -0
  77. data/spec/{rubyspec/filters → filters}/bugs/break.rb +0 -0
  78. data/spec/filters/bugs/case.rb +4 -0
  79. data/spec/{rubyspec/filters → filters}/bugs/coerce_integer.rb +0 -0
  80. data/spec/filters/bugs/enumerable_sort_by.rb +3 -0
  81. data/spec/{rubyspec/filters → filters}/bugs/kernel/instance_variables.rb +0 -0
  82. data/spec/filters/bugs/kernel/rand.rb +4 -0
  83. data/spec/filters/bugs/language/array.rb +3 -0
  84. data/spec/filters/bugs/language/block.rb +6 -0
  85. data/spec/filters/bugs/language/break.rb +3 -0
  86. data/spec/{rubyspec/filters → filters}/bugs/language/class.rb +3 -0
  87. data/spec/{rubyspec/filters → filters}/bugs/language/class_variables.rb +0 -0
  88. data/spec/filters/bugs/language/def.rb +27 -0
  89. data/spec/filters/bugs/language/defined.rb +3 -0
  90. data/spec/filters/bugs/language/ensure.rb +4 -0
  91. data/spec/filters/bugs/language/execution.rb +4 -0
  92. data/spec/filters/bugs/language/for.rb +18 -0
  93. data/spec/filters/bugs/language/if.rb +13 -0
  94. data/spec/filters/bugs/language/loop.rb +4 -0
  95. data/spec/filters/bugs/language/metaclass.rb +14 -0
  96. data/spec/filters/bugs/language/module.rb +6 -0
  97. data/spec/filters/bugs/language/next.rb +3 -0
  98. data/spec/filters/bugs/language/or.rb +3 -0
  99. data/spec/filters/bugs/language/order.rb +4 -0
  100. data/spec/filters/bugs/language/precedence.rb +10 -0
  101. data/spec/filters/bugs/language/proc.rb +24 -0
  102. data/spec/filters/bugs/language/redo.rb +5 -0
  103. data/spec/filters/bugs/language/rescue.rb +9 -0
  104. data/spec/filters/bugs/language/retry.rb +5 -0
  105. data/spec/filters/bugs/language/send.rb +10 -0
  106. data/spec/filters/bugs/language/super.rb +9 -0
  107. data/spec/filters/bugs/language/until.rb +8 -0
  108. data/spec/filters/bugs/language/variables.rb +37 -0
  109. data/spec/filters/bugs/language/while.rb +6 -0
  110. data/spec/filters/bugs/language/yield.rb +5 -0
  111. data/spec/{rubyspec/filters → filters}/bugs/module/class_variables.rb +0 -0
  112. data/spec/filters/bugs/module/method_defined.rb +6 -0
  113. data/spec/filters/bugs/parser.rb +10 -0
  114. data/spec/{rubyspec/filters → filters}/bugs/public_methods.rb +1 -0
  115. data/spec/filters/bugs/return.rb +7 -0
  116. data/spec/filters/bugs/singleton/instance.rb +4 -0
  117. data/spec/filters/bugs/source_map.rb +3 -0
  118. data/spec/filters/bugs/string/center.rb +4 -0
  119. data/spec/filters/bugs/string/lines.rb +3 -0
  120. data/spec/{rubyspec/filters → filters}/mspec/mocks.rb +0 -0
  121. data/spec/{rubyspec/filters → filters}/mspec/ruby_exe.rb +0 -0
  122. data/spec/{rubyspec/filters → filters}/mspec/should_receive.rb +0 -0
  123. data/spec/{rubyspec/filters → filters}/parser/block_args.rb +0 -0
  124. data/spec/{rubyspec/filters → filters}/unsupported/array_subclasses.rb +1 -0
  125. data/spec/filters/unsupported/frozen.rb +32 -0
  126. data/spec/filters/unsupported/immutable_strings.rb +3 -0
  127. data/spec/filters/unsupported/tainted.rb +17 -0
  128. data/spec/filters/unsupported/trusted.rb +15 -0
  129. data/spec/opal/class/constants_spec.rb +7 -0
  130. data/spec/opal/erb/erb_spec.rb +7 -1
  131. data/spec/opal/erb/inline_block.opalerb +3 -0
  132. data/spec/opal/hash/allocate_spec.rb +16 -0
  133. data/spec/opal/hash/new_spec.rb +10 -0
  134. data/spec/opal/hash/to_s_spec.rb +9 -0
  135. data/spec/opal/kernel/instance_variable_defined_spec.rb +15 -0
  136. data/spec/opal/kernel/rand_spec.rb +5 -5
  137. data/spec/opal/kernel/respond_to_spec.rb +14 -1
  138. data/spec/opal/language/block_spec.rb +13 -0
  139. data/spec/opal/language/rescue_spec.rb +27 -0
  140. data/spec/opal/language/return_spec.rb +38 -0
  141. data/spec/opal/language/singleton_class_spec.rb +13 -0
  142. data/spec/opal/language/super_spec.rb +99 -0
  143. data/spec/opal/language/variables_spec.rb +20 -0
  144. data/spec/opal/module/attr_accessor_spec.rb +8 -0
  145. data/spec/opal/module/constants_spec.rb +2 -2
  146. data/spec/opal/native/alias_native_spec.rb +18 -0
  147. data/spec/opal/native/each_spec.rb +14 -0
  148. data/spec/opal/native/element_reference_spec.rb +14 -0
  149. data/spec/opal/native/method_missing_spec.rb +39 -0
  150. data/spec/opal/native/new_spec.rb +19 -0
  151. data/spec/opal/native/nil_spec.rb +14 -0
  152. data/spec/opal/runtime2/class_hierarchy_spec.rb +2 -2
  153. data/spec/opal/source_map_spec.rb +3 -7
  154. data/spec/ospec/main.rb.erb +2 -5
  155. data/spec/parser/alias_spec.rb +4 -4
  156. data/spec/parser/and_spec.rb +2 -2
  157. data/spec/parser/array_spec.rb +6 -6
  158. data/spec/parser/attrasgn_spec.rb +8 -8
  159. data/spec/parser/begin_spec.rb +11 -11
  160. data/spec/parser/block_spec.rb +3 -3
  161. data/spec/parser/break_spec.rb +4 -4
  162. data/spec/parser/call_spec.rb +50 -48
  163. data/spec/parser/class_spec.rb +2 -2
  164. data/spec/parser/const_spec.rb +1 -1
  165. data/spec/parser/cvar_spec.rb +1 -1
  166. data/spec/parser/def_spec.rb +5 -5
  167. data/spec/parser/gvar_spec.rb +2 -2
  168. data/spec/parser/hash_spec.rb +4 -4
  169. data/spec/parser/iasgn_spec.rb +3 -3
  170. data/spec/parser/if_spec.rb +6 -6
  171. data/spec/parser/iter_spec.rb +6 -6
  172. data/spec/parser/lambda_spec.rb +5 -5
  173. data/spec/parser/lasgn_spec.rb +2 -2
  174. data/spec/parser/line_spec.rb +2 -2
  175. data/spec/parser/lvar_spec.rb +5 -5
  176. data/spec/parser/masgn_spec.rb +1 -1
  177. data/spec/parser/module_spec.rb +2 -2
  178. data/spec/parser/not_spec.rb +4 -4
  179. data/spec/parser/op_asgn1_spec.rb +2 -2
  180. data/spec/parser/op_asgn2_spec.rb +2 -2
  181. data/spec/parser/or_spec.rb +2 -2
  182. data/spec/parser/parse_spec.rb +66 -0
  183. data/spec/parser/parser_spec.rb +32 -38
  184. data/spec/parser/regexp_spec.rb +4 -4
  185. data/spec/parser/return_spec.rb +4 -4
  186. data/spec/parser/sclass_spec.rb +4 -4
  187. data/spec/parser/str_spec.rb +3 -3
  188. data/spec/parser/super_spec.rb +6 -6
  189. data/spec/parser/undef_spec.rb +3 -3
  190. data/spec/parser/unless_spec.rb +4 -4
  191. data/spec/parser/while_spec.rb +3 -3
  192. data/spec/parser/xstr_spec.rb +3 -3
  193. data/spec/parser/yield_spec.rb +6 -6
  194. data/spec/rubyspec/core/array/drop_spec.rb +1 -1
  195. data/spec/rubyspec/core/array/keep_if_spec.rb +1 -3
  196. data/spec/rubyspec/core/array/length_spec.rb +1 -3
  197. data/spec/rubyspec/core/array/map_spec.rb +2 -6
  198. data/spec/rubyspec/core/array/minus_spec.rb +3 -3
  199. data/spec/rubyspec/core/array/multiply_spec.rb +14 -16
  200. data/spec/rubyspec/core/array/new_spec.rb +3 -5
  201. data/spec/rubyspec/core/array/plus_spec.rb +2 -2
  202. data/spec/rubyspec/core/array/pop_spec.rb +4 -4
  203. data/spec/rubyspec/core/array/rassoc_spec.rb +2 -2
  204. data/spec/rubyspec/core/array/reject_spec.rb +2 -6
  205. data/spec/rubyspec/core/array/replace_spec.rb +1 -3
  206. data/spec/rubyspec/core/array/reverse_each_spec.rb +1 -3
  207. data/spec/rubyspec/core/array/reverse_spec.rb +4 -4
  208. data/spec/rubyspec/core/array/rindex_spec.rb +1 -1
  209. data/spec/rubyspec/core/array/select_spec.rb +3 -7
  210. data/spec/rubyspec/core/array/shared/collect.rb +7 -0
  211. data/spec/rubyspec/core/array/shared/index.rb +1 -1
  212. data/spec/rubyspec/core/array/shared/keep_if.rb +3 -0
  213. data/spec/rubyspec/core/array/shared/length.rb +3 -0
  214. data/spec/rubyspec/core/array/shared/replace.rb +3 -0
  215. data/spec/rubyspec/core/array/shared/slice.rb +3 -0
  216. data/spec/rubyspec/core/array/shuffle_spec.rb +1 -1
  217. data/spec/rubyspec/core/array/size_spec.rb +1 -3
  218. data/spec/rubyspec/core/array/slice_spec.rb +4 -6
  219. data/spec/rubyspec/core/array/sort_spec.rb +2 -2
  220. data/spec/rubyspec/core/array/to_a_spec.rb +1 -1
  221. data/spec/rubyspec/core/array/try_convert_spec.rb +7 -7
  222. data/spec/rubyspec/core/array/uniq_spec.rb +7 -7
  223. data/spec/rubyspec/core/array/zip_spec.rb +1 -1
  224. data/spec/rubyspec/core/enumerable/collect_spec.rb +24 -0
  225. data/spec/rubyspec/core/enumerable/count_spec.rb +35 -19
  226. data/spec/rubyspec/core/enumerable/find_spec.rb +5 -0
  227. data/spec/rubyspec/core/hash/each_pair_spec.rb +7 -7
  228. data/spec/rubyspec/core/hash/each_spec.rb +13 -7
  229. data/spec/rubyspec/core/module/method_defined_spec.rb +4 -4
  230. data/spec/rubyspec/core/module/public_method_defined_spec.rb +19 -0
  231. data/spec/rubyspec/core/module/remove_const_spec.rb +23 -0
  232. data/spec/rubyspec/core/numeric/step_spec.rb +14 -0
  233. data/spec/rubyspec/core/string/center_spec.rb +43 -65
  234. data/spec/rubyspec/core/string/lines_spec.rb +1 -1
  235. data/spec/rubyspec/core/string/ljust_spec.rb +25 -9
  236. data/spec/rubyspec/core/string/rjust_spec.rb +26 -10
  237. data/spec/rubyspec/core/struct/fixtures/classes.rb +26 -0
  238. data/spec/rubyspec/core/struct/initialize_spec.rb +11 -0
  239. data/spec/rubyspec/core/struct/new_spec.rb +24 -0
  240. data/spec/rubyspec/fixtures/constants.rb +7 -0
  241. data/spec/rubyspec/language/break_spec.rb +1 -1
  242. data/spec/rubyspec/language/case_spec.rb +30 -30
  243. data/spec/rubyspec/language/def_spec.rb +34 -34
  244. data/spec/rubyspec/language/defined_spec.rb +2 -2
  245. data/spec/rubyspec/language/ensure_spec.rb +2 -2
  246. data/spec/rubyspec/language/execution_spec.rb +2 -2
  247. data/spec/rubyspec/language/for_spec.rb +17 -17
  248. data/spec/rubyspec/language/hash_spec.rb +3 -3
  249. data/spec/rubyspec/language/if_spec.rb +11 -11
  250. data/spec/rubyspec/language/loop_spec.rb +3 -3
  251. data/spec/rubyspec/language/metaclass_spec.rb +14 -14
  252. data/spec/rubyspec/language/module_spec.rb +6 -6
  253. data/spec/rubyspec/language/next_spec.rb +5 -5
  254. data/spec/rubyspec/language/not_spec.rb +1 -1
  255. data/spec/rubyspec/language/or_spec.rb +1 -1
  256. data/spec/rubyspec/language/order_spec.rb +3 -5
  257. data/spec/rubyspec/language/precedence_spec.rb +19 -19
  258. data/spec/rubyspec/language/proc_spec.rb +29 -29
  259. data/spec/rubyspec/language/redo_spec.rb +3 -5
  260. data/spec/rubyspec/language/rescue_spec.rb +7 -7
  261. data/spec/rubyspec/language/retry_spec.rb +2 -2
  262. data/spec/rubyspec/language/return_spec.rb +12 -12
  263. data/spec/rubyspec/language/send_spec.rb +9 -9
  264. data/spec/rubyspec/language/singleton_class_spec.rb +3 -4
  265. data/spec/rubyspec/language/super_spec.rb +7 -11
  266. data/spec/rubyspec/language/until_spec.rb +10 -10
  267. data/spec/rubyspec/language/variables_spec.rb +59 -67
  268. data/spec/rubyspec/language/versions/array_1.9.rb +1 -1
  269. data/spec/rubyspec/language/versions/block_1.9.rb +0 -0
  270. data/spec/rubyspec/language/versions/break_1.9.rb +0 -0
  271. data/spec/rubyspec/language/versions/literal_lambda_1.9.rb +12 -12
  272. data/spec/rubyspec/language/versions/send_1.9.rb +23 -23
  273. data/spec/rubyspec/language/versions/symbol_1.9.rb +2 -2
  274. data/spec/rubyspec/language/versions/variables_1.9.rb +1 -1
  275. data/spec/rubyspec/language/while_spec.rb +4 -4
  276. data/spec/rubyspec/language/yield_spec.rb +3 -3
  277. data/spec/rubyspec/library/erb/util/html_escape_spec.rb +10 -0
  278. data/spec/rubyspec/library/singleton/clone_spec.rb +8 -0
  279. data/spec/rubyspec/library/singleton/dup_spec.rb +8 -0
  280. data/spec/rubyspec/library/singleton/fixtures/classes.rb +18 -0
  281. data/spec/rubyspec/library/singleton/instance_spec.rb +30 -0
  282. data/spec/spec_helper.rb +214 -0
  283. data/stdlib/buffer.rb +40 -0
  284. data/stdlib/buffer/array.rb +66 -0
  285. data/stdlib/buffer/view.rb +70 -0
  286. data/stdlib/erb.rb +11 -20
  287. data/stdlib/forwardable.rb +71 -0
  288. data/stdlib/json.rb +78 -0
  289. data/stdlib/ostruct.rb +69 -0
  290. data/stdlib/rbconfig.rb +1 -0
  291. data/stdlib/singleton.rb +40 -0
  292. data/stdlib/stringio.rb +173 -0
  293. data/stdlib/template.rb +44 -0
  294. metadata +285 -79
  295. data/corelib/opal/native.rb +0 -31
  296. data/spec/opal/class/bridge_class_spec.rb +0 -39
  297. data/spec/opal/native_spec.rb +0 -209
  298. data/spec/ospec/runner.rb +0 -223
  299. data/spec/rubyspec/filters/bugs/block_args.rb +0 -3
  300. data/spec/rubyspec/filters/bugs/case.rb +0 -8
  301. data/spec/rubyspec/filters/bugs/language/module.rb +0 -3
  302. data/spec/rubyspec/filters/unsupported/frozen.rb +0 -4
  303. data/spec/rubyspec/filters/unsupported/tainted.rb +0 -7
@@ -1,12 +1,11 @@
1
- class Class
2
- def self.new(sup = Object, &block)
1
+ class Module
2
+ def self.new(&block)
3
3
  %x{
4
- function AnonClass(){};
5
- var klass = Opal.boot(sup, AnonClass)
4
+ function AnonModule(){}
5
+ var klass = Opal.boot(Module, AnonModule);
6
6
  klass._name = nil;
7
- klass._scope = sup._scope;
8
-
9
- sup.$inherited(klass);
7
+ klass._scope = Module._scope;
8
+ klass._klass = Module;
10
9
 
11
10
  if (block !== nil) {
12
11
  var block_self = block._s;
@@ -19,10 +18,6 @@ class Class
19
18
  }
20
19
  end
21
20
 
22
- def self.bridge_class(name, constructor)
23
- `__opal.bridge(name, constructor)`
24
- end
25
-
26
21
  def ===(object)
27
22
  %x{
28
23
  if (object == null) {
@@ -43,14 +38,6 @@ class Class
43
38
  }
44
39
  end
45
40
 
46
- def allocate
47
- %x{
48
- var obj = new #{self}._alloc;
49
- obj._id = Opal.uid();
50
- return obj;
51
- }
52
- end
53
-
54
41
  def alias_method(newname, oldname)
55
42
  `#{self}._proto['$' + newname] = #{self}._proto['$' + oldname]`
56
43
  self
@@ -110,7 +97,7 @@ class Class
110
97
  //}
111
98
 
112
99
  if (klass._included_in) {
113
- __opal.donate(klass, methods.slice(), true);
100
+ $opal.donate(klass, methods.slice(), true);
114
101
  }
115
102
  }
116
103
 
@@ -175,13 +162,13 @@ class Class
175
162
  var name_re = /^[A-Z][A-Za-z0-9_]+$/;
176
163
  var scopes = [#{self}._scope];
177
164
  var own_only;
178
- if (#{self} === Opal.Class) {
165
+ if (#{self} === Opal.Class || #{self} === Opal.Module) {
179
166
  own_only = false;
180
167
  }
181
168
  else {
182
169
  own_only = true;
183
170
  var parent = #{self}._super;
184
- while (parent !== Opal.Object) {
171
+ while (parent && (parent !== Opal.Object)) {
185
172
  scopes.push(parent._scope);
186
173
  parent = parent._super;
187
174
  }
@@ -242,7 +229,7 @@ class Class
242
229
  return scopes[i][name];
243
230
  }
244
231
  }
245
-
232
+
246
233
  return #{const_missing name};
247
234
  }
248
235
  end
@@ -261,6 +248,11 @@ class Class
261
248
  end
262
249
  %x{
263
250
  #{self}._scope[name] = #{value};
251
+
252
+ if (value._isClass && value._name === nil) {
253
+ value._name = #{self.name} + '::' + name;
254
+ }
255
+
264
256
  return #{value}
265
257
  }
266
258
  end
@@ -272,7 +264,7 @@ class Class
272
264
  }
273
265
 
274
266
  if (block === nil) {
275
- no_block_given();
267
+ throw new Error("no block given");
276
268
  }
277
269
 
278
270
  var jsid = '$' + name;
@@ -281,7 +273,7 @@ class Class
281
273
  block._s = null;
282
274
 
283
275
  #{self}._proto[jsid] = block;
284
- __opal.donate(#{self}, [jsid]);
276
+ $opal.donate(#{self}, [jsid]);
285
277
 
286
278
  return null;
287
279
  }
@@ -327,13 +319,10 @@ class Class
327
319
  def included(mod)
328
320
  end
329
321
 
330
- def inherited(cls)
331
- end
332
-
333
322
  def module_eval(&block)
334
323
  %x{
335
324
  if (block === nil) {
336
- no_block_given();
325
+ throw new Error("no block given");
337
326
  }
338
327
 
339
328
  var block_self = block._s, result;
@@ -350,11 +339,8 @@ class Class
350
339
 
351
340
  def method_defined?(method)
352
341
  %x{
353
- if (typeof(#{self}._proto['$' + method]) === 'function') {
354
- return true;
355
- }
356
-
357
- return false;
342
+ var body = #{self}._proto['$' + method];
343
+ return (!!body) && !body.rb_stub;
358
344
  }
359
345
  end
360
346
 
@@ -374,34 +360,20 @@ class Class
374
360
  `#{self}._name`
375
361
  end
376
362
 
377
- def new(*args, &block)
378
- %x{
379
- if (#{self}._proto.$initialize) {
380
- var obj = new #{self}._alloc;
381
- obj._id = Opal.uid();
382
-
383
- obj.$initialize._p = block;
384
- obj.$initialize.apply(obj, args);
385
- return obj;
386
- }
387
- else {
388
- var cons = function() {};
389
- cons.prototype = #{self}.prototype;
390
- var obj = new cons;
391
- #{self}.apply(obj, args);
392
- return obj;
393
- }
394
- }
395
- end
396
-
397
363
  def public(*)
398
364
  end
399
365
 
400
366
  alias private public
401
367
  alias protected public
402
368
 
403
- def superclass
404
- `#{self}._super || nil`
369
+ alias public_method_defined? method_defined?
370
+
371
+ def remove_const(name)
372
+ %x{
373
+ var old = #{self}._scope[name];
374
+ delete #{self}._scope[name];
375
+ return old;
376
+ }
405
377
  end
406
378
 
407
379
  def to_s
@@ -409,7 +381,7 @@ class Class
409
381
  end
410
382
 
411
383
  def undef_method(symbol)
412
- `#{self}._proto['$' + symbol] = undefined`
384
+ `$opal.add_stub_for(#{self}._proto, "$" + symbol)`
413
385
  self
414
386
  end
415
387
  end
data/corelib/native.rb ADDED
@@ -0,0 +1,148 @@
1
+ module Kernel
2
+ def native?(value)
3
+ `value == null || !value._klass`
4
+ end
5
+
6
+ def Native(obj)
7
+ if native?(obj)
8
+ Native.new(obj)
9
+ else
10
+ obj
11
+ end
12
+ end
13
+ end
14
+
15
+ class Native < BasicObject
16
+ module Base
17
+ module Helpers
18
+ def alias_native(new, old)
19
+ define_method new do |*args|
20
+ Native.call(@native, old, *args)
21
+ end
22
+ end
23
+ end
24
+
25
+ def self.included(klass)
26
+ klass.instance_eval {
27
+ extend Helpers
28
+ }
29
+ end
30
+
31
+ def initialize(native)
32
+ unless native?(native)
33
+ raise ArgumentError, "the passed value isn't native"
34
+ end
35
+
36
+ @native = native
37
+ end
38
+
39
+ def to_n
40
+ @native
41
+ end
42
+ end
43
+
44
+ def self.try_convert(value)
45
+ %x{
46
+ if (#{native?(value)}) {
47
+ return #{value}.valueOf();
48
+ }
49
+ else if (#{value.respond_to? :to_n}) {
50
+ return #{value.to_n};
51
+ }
52
+ else {
53
+ return nil;
54
+ }
55
+ }
56
+ end
57
+
58
+ def self.convert(value)
59
+ native = try_convert(value)
60
+
61
+ if `#{native} === nil`
62
+ raise ArgumentError, "the passed value isn't a native"
63
+ end
64
+
65
+ native
66
+ end
67
+
68
+ def self.call(obj, key, *args, &block)
69
+ args << block if block
70
+
71
+ %x{
72
+ var prop = #{obj}[#{key}];
73
+
74
+ if (prop == null) {
75
+ return nil;
76
+ }
77
+ else if (prop instanceof Function) {
78
+ var result = prop.apply(#{obj}, #{args});
79
+
80
+ return result == null ? nil : result;
81
+ }
82
+ else if (#{native?(`prop`)}) {
83
+ return #{Native(`prop`)};
84
+ }
85
+ else {
86
+ return prop;
87
+ }
88
+ }
89
+ end
90
+
91
+ include Base
92
+
93
+ def nil?
94
+ `#@native == null`
95
+ end
96
+
97
+ def each
98
+ return Enumerator.new(self, :each) unless block_given?
99
+
100
+ %x{
101
+ for (var key in #@native) {
102
+ #{yield `key`, `#@native[key]`}
103
+ }
104
+ }
105
+
106
+ self
107
+ end
108
+
109
+ def [](key)
110
+ raise 'cannot get value from nil native' if nil?
111
+
112
+ %x{
113
+ var prop = #@native[key];
114
+
115
+ if (prop instanceof Function) {
116
+ return prop;
117
+ }
118
+ else {
119
+ return #{::Native.call(@native, key)}
120
+ }
121
+ }
122
+ end
123
+
124
+ def []=(key, value)
125
+ raise 'cannot set value on nil native' if nil?
126
+
127
+ native = Native.try_convert(value)
128
+
129
+ if `#{native} === nil`
130
+ `#@native[key] = #{value}`
131
+ else
132
+ `#@native[key] = #{native}`
133
+ end
134
+ end
135
+
136
+ def method_missing(mid, *args, &block)
137
+ raise 'cannot call method from nil native' if nil?
138
+
139
+ %x{
140
+ if (mid.charAt(mid.length - 1) === '=') {
141
+ return #{self[mid.slice(0, mid.length - 1)] = args[0]};
142
+ }
143
+ else {
144
+ return #{::Native.call(@native, mid, *args, &block)};
145
+ }
146
+ }
147
+ end
148
+ end
@@ -15,10 +15,6 @@ class NilClass
15
15
  `other === nil`
16
16
  end
17
17
 
18
- def as_json
19
- self
20
- end
21
-
22
18
  def dup
23
19
  raise TypeError
24
20
  end
@@ -40,7 +36,7 @@ class NilClass
40
36
  end
41
37
 
42
38
  def to_h
43
- `__opal.hash()`
39
+ `$opal.hash()`
44
40
  end
45
41
 
46
42
  def to_i
@@ -49,10 +45,6 @@ class NilClass
49
45
 
50
46
  alias to_f to_i
51
47
 
52
- def to_json
53
- 'null'
54
- end
55
-
56
48
  def to_n
57
49
  `null`
58
50
  end
@@ -62,7 +54,7 @@ class NilClass
62
54
  end
63
55
 
64
56
  def object_id
65
- `#{NilClass}._id || (#{NilClass}._id = Opal.uid())`
57
+ `#{NilClass}._id || (#{NilClass}._id = $opal.uid())`
66
58
  end
67
59
  alias hash object_id
68
60
  end
@@ -93,10 +93,6 @@ class Numeric
93
93
  `Math.abs(#{self})`
94
94
  end
95
95
 
96
- def as_json
97
- self
98
- end
99
-
100
96
  def ceil
101
97
  `Math.ceil(#{self})`
102
98
  end
@@ -108,8 +104,8 @@ class Numeric
108
104
  def downto(finish, &block)
109
105
  %x{
110
106
  for (var i = #{self}; i >= finish; i--) {
111
- if (block(i) === __breaker) {
112
- return __breaker.$v;
107
+ if (block(i) === $breaker) {
108
+ return $breaker.$v;
113
109
  }
114
110
  }
115
111
 
@@ -159,13 +155,34 @@ class Numeric
159
155
  `#{self} - 1`
160
156
  end
161
157
 
158
+ def step(limit, step = 1, &block)
159
+ %x{
160
+ var working = #{self};
161
+
162
+ if (step > 0) {
163
+ while (working <= limit) {
164
+ block(working);
165
+ working += step;
166
+ }
167
+ }
168
+ else {
169
+ while (working >= limit) {
170
+ block(working);
171
+ working += step;
172
+ }
173
+ }
174
+
175
+ return #{self};
176
+ }
177
+ end
178
+
162
179
  alias succ next
163
180
 
164
181
  def times(&block)
165
182
  %x{
166
183
  for (var i = 0; i < #{self}; i++) {
167
- if (block(i) === __breaker) {
168
- return __breaker.$v;
184
+ if (block(i) === $breaker) {
185
+ return $breaker.$v;
169
186
  }
170
187
  }
171
188
 
@@ -181,10 +198,6 @@ class Numeric
181
198
  `parseInt(#{self})`
182
199
  end
183
200
 
184
- def to_json
185
- `#{self}.toString()`
186
- end
187
-
188
201
  def to_s(base = 10)
189
202
  `#{self}.toString()`
190
203
  end
@@ -198,8 +211,8 @@ class Numeric
198
211
 
199
212
  %x{
200
213
  for (var i = #{self}; i <= finish; i++) {
201
- if (block(i) === __breaker) {
202
- return __breaker.$v;
214
+ if (block(i) === $breaker) {
215
+ return $breaker.$v;
203
216
  }
204
217
  }
205
218
 
@@ -218,3 +231,15 @@ class Numeric
218
231
  end
219
232
 
220
233
  Fixnum = Numeric
234
+
235
+ class Integer < Numeric
236
+ def self.===(other)
237
+ other.is_a?(Numeric) && `(other % 1) == 0`
238
+ end
239
+ end
240
+
241
+ class Float < Numeric
242
+ def self.===(other)
243
+ other.is_a?(Numeric) && `(other % 1) != 0`
244
+ end
245
+ end