opal 0.3.6 → 0.3.9

Sign up to get free protection for your applications and to get access to all the features.
Files changed (98) hide show
  1. data/README.md +69 -97
  2. data/bin/opal +2 -2
  3. data/{lib/core → corelib}/array.rb +85 -56
  4. data/corelib/boolean.rb +20 -0
  5. data/corelib/class.rb +58 -0
  6. data/{lib → corelib}/core.rb +2 -50
  7. data/corelib/dir.rb +22 -0
  8. data/{lib/core → corelib}/enumerable.rb +0 -0
  9. data/corelib/error.rb +19 -0
  10. data/{lib/core → corelib}/file.rb +7 -9
  11. data/{lib/core → corelib}/hash.rb +104 -144
  12. data/{lib/core → corelib}/kernel.rb +38 -44
  13. data/corelib/load_order +21 -0
  14. data/{lib/core → corelib}/match_data.rb +0 -0
  15. data/{lib/core → corelib}/module.rb +12 -8
  16. data/{lib/core → corelib}/nil_class.rb +2 -2
  17. data/{lib/core → corelib}/numeric.rb +37 -100
  18. data/corelib/object.rb +37 -0
  19. data/{lib/core → corelib}/proc.rb +3 -3
  20. data/corelib/range.rb +27 -0
  21. data/{lib/core → corelib}/regexp.rb +1 -1
  22. data/{lib/core → corelib}/string.rb +16 -107
  23. data/{lib/core → corelib}/top_self.rb +0 -0
  24. data/lib/opal.rb +7 -0
  25. data/lib/opal/browserify.rb +34 -0
  26. data/{opal_lib → lib}/opal/builder.rb +70 -24
  27. data/lib/opal/command.rb +52 -0
  28. data/lib/opal/context.rb +197 -0
  29. data/{opal_lib/opal/ruby/parser.rb → lib/opal/lexer.rb} +20 -4
  30. data/{opal_lib/opal/ruby → lib/opal}/nodes.rb +238 -127
  31. data/lib/opal/parser.rb +4894 -0
  32. data/{opal_lib/opal/ruby/ruby_parser.y → lib/opal/parser.y} +38 -18
  33. data/lib/rbp.rb +2 -0
  34. data/lib/rbp/package.rb +49 -0
  35. data/runtime/class.js +216 -189
  36. data/runtime/fs.js +2 -2
  37. data/runtime/init.js +242 -244
  38. data/runtime/loader.js +78 -99
  39. data/runtime/module.js +34 -40
  40. data/runtime/post.js +2 -2
  41. data/runtime/pre.js +1 -1
  42. data/runtime/runtime.js +129 -135
  43. data/{lib → stdlib}/dev.rb +10 -10
  44. data/{lib → stdlib}/racc/parser.rb +0 -6
  45. data/{lib → stdlib}/strscan.rb +4 -4
  46. metadata +57 -105
  47. data/lib/core/basic_object.rb +0 -51
  48. data/lib/core/class.rb +0 -38
  49. data/lib/core/dir.rb +0 -26
  50. data/lib/core/error.rb +0 -75
  51. data/lib/core/false_class.rb +0 -81
  52. data/lib/core/object.rb +0 -6
  53. data/lib/core/range.rb +0 -27
  54. data/lib/core/symbol.rb +0 -42
  55. data/lib/core/true_class.rb +0 -41
  56. data/lib/ospec.rb +0 -7
  57. data/lib/ospec/autorun.rb +0 -8
  58. data/lib/ospec/dsl.rb +0 -15
  59. data/lib/ospec/example.rb +0 -11
  60. data/lib/ospec/example/before_and_after_hooks.rb +0 -56
  61. data/lib/ospec/example/errors.rb +0 -17
  62. data/lib/ospec/example/example_group.rb +0 -12
  63. data/lib/ospec/example/example_group_factory.rb +0 -18
  64. data/lib/ospec/example/example_group_hierarchy.rb +0 -21
  65. data/lib/ospec/example/example_group_methods.rb +0 -100
  66. data/lib/ospec/example/example_group_proxy.rb +0 -15
  67. data/lib/ospec/example/example_methods.rb +0 -46
  68. data/lib/ospec/example/example_proxy.rb +0 -18
  69. data/lib/ospec/expectations.rb +0 -19
  70. data/lib/ospec/expectations/errors.rb +0 -8
  71. data/lib/ospec/expectations/fail_with.rb +0 -9
  72. data/lib/ospec/expectations/handler.rb +0 -33
  73. data/lib/ospec/helpers/scratch.rb +0 -18
  74. data/lib/ospec/matchers.rb +0 -24
  75. data/lib/ospec/matchers/be.rb +0 -1
  76. data/lib/ospec/matchers/generated_descriptions.rb +0 -20
  77. data/lib/ospec/matchers/operator_matcher.rb +0 -54
  78. data/lib/ospec/matchers/raise_error.rb +0 -38
  79. data/lib/ospec/runner.rb +0 -90
  80. data/lib/ospec/runner/example_group_runner.rb +0 -41
  81. data/lib/ospec/runner/formatter/html_formatter.rb +0 -139
  82. data/lib/ospec/runner/formatter/terminal_formatter.rb +0 -48
  83. data/lib/ospec/runner/options.rb +0 -34
  84. data/lib/ospec/runner/reporter.rb +0 -82
  85. data/opal_lib/opal.rb +0 -16
  86. data/opal_lib/opal/build_methods.rb +0 -51
  87. data/opal_lib/opal/bundle.rb +0 -70
  88. data/opal_lib/opal/command.rb +0 -68
  89. data/opal_lib/opal/context.rb +0 -81
  90. data/opal_lib/opal/context/console.rb +0 -10
  91. data/opal_lib/opal/context/file_system.rb +0 -34
  92. data/opal_lib/opal/context/loader.rb +0 -135
  93. data/opal_lib/opal/gem.rb +0 -84
  94. data/opal_lib/opal/rake/builder_task.rb +0 -44
  95. data/opal_lib/opal/rake/spec_task.rb +0 -32
  96. data/opal_lib/opal/ruby/ruby_parser.rb +0 -4862
  97. data/opal_lib/opal/version.rb +0 -4
  98. data/runtime/debug.js +0 -84
@@ -3,36 +3,28 @@
3
3
  # {BasicObject}.
4
4
  module Kernel
5
5
 
6
-
7
6
  def instance_variable_defined?(name)
8
7
  `name = #{name.to_s};
9
- return self[name] == undefined ? Qfalse : Qtrue;`
8
+ return self[name.substr(1)] != undefined;`
10
9
  end
11
10
 
12
11
  def instance_variable_get(name)
13
12
  `name = #{name.to_s};
14
- return self[name] == undefined ? nil : self[name];`
13
+ return self[name.substr(1)] == undefined ? nil : self[name.substr(1)];`
15
14
  end
16
15
 
17
16
  def instance_variable_set(name, value)
18
17
  `name = #{name.to_s};
19
- return self[name] = value;`
20
- end
21
-
22
- # Returns `true` if a block was given to the current method, `false`
23
- # otherwise.
24
- #
25
- # @NOTE: In opal, this is actually a fake method. block_given? is inlined
26
- # for efficiency and implementation details.
27
- #
28
- # @return [true, false]
29
- def block_given?
30
- false
18
+ return self[name.substr(1)] = value;`
31
19
  end
32
20
 
33
21
  # raw object flags (used by runtime)
34
22
  def __flags__
35
- `return self.$flags;`
23
+ `return self.$f;`
24
+ end
25
+
26
+ def hash
27
+ `return self.$h();`
36
28
  end
37
29
 
38
30
  def to_a
@@ -46,22 +38,20 @@ module Kernel
46
38
  end
47
39
 
48
40
  def kind_of?(klass)
49
- `var search = self.$klass;
41
+ `var search = self.$k;
50
42
 
51
43
  while (search) {
52
44
  if (search == klass) {
53
- return Qtrue;
45
+ return true;
54
46
  }
55
47
 
56
48
  search = search.$super;
57
49
  }
58
50
 
59
- return Qfalse;`
51
+ return false;`
60
52
  end
61
53
 
62
- def is_a?(klass)
63
- kind_of? klass
64
- end
54
+ alias_method :is_a?, :kind_of?
65
55
 
66
56
  def nil?
67
57
  false
@@ -82,13 +72,14 @@ module Kernel
82
72
  # @param [String, Symbol] method_id
83
73
  # @return [Boolean]
84
74
  def respond_to?(method_id)
85
- `var method = self['m$' + #{`method_id`.to_s}];
75
+ `var method = self['m$' + method_id];
76
+
86
77
 
87
- if (method && !method.$rbMM) {
88
- return Qtrue;
78
+ if (method ) {
79
+ return true;
89
80
  }
90
81
 
91
- return Qfalse;`
82
+ return false;`
92
83
  end
93
84
 
94
85
  def ===(other)
@@ -96,27 +87,25 @@ module Kernel
96
87
  end
97
88
 
98
89
  def send(method_id, *args, &block)
99
- `var method = self.$m[#{method_id.to_s}];
90
+ `var method = self['m$' + method_id];
100
91
 
101
92
  if ($B.f == arguments.callee) {
102
93
  $B.f = method;
103
94
  }
104
95
 
105
- args.unshift(self);
106
-
107
96
  return method.apply(self, args);`
108
97
  end
109
98
 
110
99
  def class
111
- `return $rb.class_real(self.$klass);`
100
+ `return rb_class_real(self.$k);`
112
101
  end
113
102
 
114
103
  def singleton_class
115
- `return $rb.singleton_class(self);`
104
+ `return rb_singleton_class(self);`
116
105
  end
117
106
 
118
107
  def methods
119
- `return self.$klass.$methods;`
108
+ `return self.$k.$methods;`
120
109
  end
121
110
 
122
111
  # Returns a random number. If max is `nil`, then the result is 0. Otherwise
@@ -139,11 +128,11 @@ module Kernel
139
128
  end
140
129
 
141
130
  def __id__
142
- `return self.$hash();`
131
+ `return self.$h();`
143
132
  end
144
133
 
145
134
  def object_id
146
- `return self.$hash();`
135
+ `return self.$h();`
147
136
  end
148
137
 
149
138
  # Returns a simple string representation of the receiver object. The id shown in the string
@@ -152,7 +141,7 @@ module Kernel
152
141
  #
153
142
  # FIXME: proper hex output needed
154
143
  def to_s
155
- "#<#{`$rb.class_real(self.$klass)`}:0x#{`(self.$hash() * 400487).toString(16)`}>"
144
+ "#<#{`rb_class_real(self.$k)`}:0x#{`(self.$h() * 400487).toString(16)`}>"
156
145
  end
157
146
 
158
147
  def inspect
@@ -160,7 +149,7 @@ module Kernel
160
149
  end
161
150
 
162
151
  def const_set(name, value)
163
- `return rb_const_set($rb.class_real(self.$klass), name, value);`
152
+ `return rb_const_set(rb_class_real(self.$k), name, value);`
164
153
  end
165
154
 
166
155
  def const_defined?(name)
@@ -172,13 +161,10 @@ module Kernel
172
161
  end
173
162
 
174
163
  def extend(mod)
175
- `$rb.extend_module($rb.singleton_class(self), mod);`
164
+ `rb_extend_module(rb_singleton_class(self), mod);`
176
165
  nil
177
166
  end
178
167
 
179
- # @group Private methods
180
- private
181
-
182
168
  # Raises an exception. If given a string, this method will raise a
183
169
  # RuntimeError with the given string as a message. Otherwise, if the first
184
170
  # parameter is a subclass of Exception, then the method will raise a new
@@ -201,16 +187,16 @@ module Kernel
201
187
  def raise(exception, string = nil)
202
188
  `var msg = nil, exc;
203
189
 
204
- if (typeof exception == 'string') {
190
+ if (exception.$f & T_STRING) {
205
191
  msg = exception;
206
192
  exc = #{RuntimeError.new `msg`};
207
- } else if (#{`exception`.kind_of? Exception}.$r) {
193
+ } else if (#{`exception`.kind_of? Exception}) {
208
194
  exc = exception;
209
195
  } else {
210
196
  if (string != nil) msg = string;
211
197
  exc = #{`exception`.new `msg`};
212
198
  }
213
- $rb.raise_exc(exc);`
199
+ rb_raise_exc(exc);`
214
200
  end
215
201
 
216
202
  alias_method :fail, :raise
@@ -232,6 +218,14 @@ module Kernel
232
218
  return self;`
233
219
  end
234
220
 
221
+ # Executed in reverse order
222
+ def at_exit(&proc)
223
+ raise ArgumentError, "called without a block" unless block_given?
224
+ `rb_end_procs.push(proc);`
225
+
226
+ proc
227
+ end
228
+
235
229
  # Simple equivalent to `Proc.new`. Returns a new proc from the given block.
236
230
  #
237
231
  # @example
@@ -249,7 +243,7 @@ module Kernel
249
243
  def lambda(&block)
250
244
  raise ArgumentError,
251
245
  "tried to create Proc object without a block" unless block_given?
252
- `return $rb.lambda(block);`
246
+ `return rb_make_lambda(block);`
253
247
  end
254
248
 
255
249
  # @endgroup
@@ -0,0 +1,21 @@
1
+ core
2
+ object
3
+ module
4
+ class
5
+ kernel
6
+ top_self
7
+ nil_class
8
+ boolean
9
+ enumerable
10
+ array
11
+ numeric
12
+ hash
13
+ error
14
+ string
15
+ proc
16
+ range
17
+ regexp
18
+ match_data
19
+ file
20
+ dir
21
+
File without changes
@@ -25,8 +25,8 @@ class Module
25
25
  def attr_reader(*attrs)
26
26
  attrs.each do |a|
27
27
  method_id = a.to_s
28
- `$rb.dm(self, method_id, function(self) {
29
- var iv = self['@' + method_id];
28
+ `$rb.dm(self, method_id, function() {
29
+ var iv = this[method_id];
30
30
  return iv == undefined ? nil : iv;
31
31
  });`
32
32
  end
@@ -36,8 +36,8 @@ class Module
36
36
  def attr_writer(*attrs)
37
37
  attrs.each do |a|
38
38
  method_id = a.to_s
39
- `$rb.dm(self, method_id + '=', function(self, val) {
40
- return self['@' + method_id] = val;
39
+ `$rb.dm(self, method_id + '=', function(val) {
40
+ return this[method_id] = val;
41
41
  });`
42
42
  end
43
43
  nil
@@ -48,6 +48,10 @@ class Module
48
48
  self
49
49
  end
50
50
 
51
+ def public_instance_methods(include_super = true)
52
+ `return self.$methods;`
53
+ end
54
+
51
55
  def instance_methods
52
56
  `return self.$methods;`
53
57
  end
@@ -56,7 +60,7 @@ class Module
56
60
  `var ary = [], parent = self;
57
61
 
58
62
  while (parent) {
59
- if (parent.$flags & $rb.FL_SINGLETON) {
63
+ if (parent.$f & FL_SINGLETON) {
60
64
  // nothing?
61
65
  }
62
66
  else {
@@ -74,12 +78,12 @@ class Module
74
78
  end
75
79
 
76
80
  def const_set(id, value)
77
- `return $rb.cs(self, #{id.to_s}, value);`
81
+ `return rb_const_set(self, #{id.to_s}, value);`
78
82
  end
79
83
 
80
84
  def class_eval(str = nil, &block)
81
85
  if block_given?
82
- `block(self)`
86
+ `block.call(self)`
83
87
  else
84
88
  raise "need to compile str"
85
89
  end
@@ -90,7 +94,7 @@ class Module
90
94
  end
91
95
 
92
96
  def extend(mod)
93
- `$rb.extend_module(self, mod)`
97
+ `rb_extend_module(self, mod)`
94
98
  nil
95
99
  end
96
100
  end
@@ -42,11 +42,11 @@ class NilClass
42
42
  end
43
43
 
44
44
  def |(other)
45
- `return other.$r ? Qtrue : Qfalse;`
45
+ `return other != false && other != nil;`
46
46
  end
47
47
 
48
48
  def ^(other)
49
- `return other.$r ? Qtrue : Qfalse;`
49
+ `return other != false && other != nil;`
50
50
  end
51
51
  end
52
52
 
@@ -39,28 +39,41 @@
39
39
  # for these purposes, but it is easily done. This approach will still
40
40
  # not allow for literals to be used to make these subclass instances.
41
41
  class Numeric
42
- # Unary Plus - Returns the receivers value
43
- #
44
- # @example
45
- #
46
- # +5
47
- # # => 5
48
- #
49
- # @return [Numeric] receiver
50
- def +@
51
- `return self;`
42
+
43
+ def +(other)
44
+ `return self + other;`
52
45
  end
53
46
 
54
- # Unary minus - returns the receiver's value, negated.
55
- #
56
- # @example
57
- #
58
- # -5
59
- # # => -5
60
- #
61
- # @return [Numeric] result
62
- def -@
63
- `return -self;`
47
+ def -(other)
48
+ `return self - other;`
49
+ end
50
+
51
+ def *(other)
52
+ `return self * other;`
53
+ end
54
+
55
+ def /(other)
56
+ `return self / other;`
57
+ end
58
+
59
+ def ==(other)
60
+ `return self.valueOf() === other.valueOf();`
61
+ end
62
+
63
+ def <(other)
64
+ `return self < other;`
65
+ end
66
+
67
+ def <=(other)
68
+ `return self <= other;`
69
+ end
70
+
71
+ def >(other)
72
+ `return self > other;`
73
+ end
74
+
75
+ def >=(other)
76
+ `return self >= other;`
64
77
  end
65
78
 
66
79
  # Returns `self` modulo `other`. See `divmod` for more information.
@@ -83,14 +96,6 @@ class Numeric
83
96
  `return self & num2;`
84
97
  end
85
98
 
86
- # Performs multiplication
87
- #
88
- # @param [Numeric] other number to multiply with
89
- # @return [Numeric]
90
- def *(other)
91
- `return self * other;`
92
- end
93
-
94
99
  # Raises `self` to the power of `other`.
95
100
  #
96
101
  # @param [Numeric] other number to raise to
@@ -99,66 +104,6 @@ class Numeric
99
104
  `return Math.pow(self, other);`
100
105
  end
101
106
 
102
- # Performs addition.
103
- #
104
- # @param [Numeric] other number to add
105
- # @return [Numeric]
106
- def +(other)
107
- `return self + other;`
108
- end
109
-
110
- # Performs subtraction
111
- #
112
- # @param [Numeric] other number to subtract
113
- # @return [Numeric]
114
- def -(other)
115
- `return self - other;`
116
- end
117
-
118
- # Performs division
119
- #
120
- # @param [Numeric] other number to divide by
121
- # @return [Numeric]
122
- def /(other)
123
- `return self / other;`
124
- end
125
-
126
- # Returns `true` if the value of `self` is less than that or `other`, `false`
127
- # otherwise.
128
- #
129
- # @param [Numeric] other number to compare
130
- # @return [true, false] result of comparison
131
- def <(other)
132
- `return self < other ? Qtrue : Qfalse;`
133
- end
134
-
135
- # Returns `true` if the value of `self` is less than or equal to `other`,
136
- # `false` otherwise.
137
- #
138
- # @param [Numeric] other number to comapre
139
- # @return [true, false] result of comparison
140
- def <=(other)
141
- `return self <= other ? Qtrue : Qfalse;`
142
- end
143
-
144
- # Returns `true` if the value of `self` is greater than `other`, `false`
145
- # otherwise.
146
- #
147
- # @param [Numeric] other number to compare with
148
- # @return [true, false] result of comparison
149
- def >(other)
150
- `return self > other ? Qtrue : Qfalse;`
151
- end
152
-
153
- # Returns `true` if `self` is greater than or equal to `other`, `false`
154
- # otherwise.
155
- #
156
- # @param [Numeric] other number to compare with
157
- # @return [true, false] result of comparison
158
- def >=(other)
159
- `return self >= other ? Qtrue : Qfalse;`
160
- end
161
-
162
107
  # Shift `self` left by `count` positions.
163
108
  #
164
109
  # @param [Numeric] count number to shift
@@ -187,14 +132,6 @@ class Numeric
187
132
  return 0;`
188
133
  end
189
134
 
190
- # Returns `true` if `self` equals `other` numerically, `false` otherwise.
191
- #
192
- # @param [Numeric] other number to compare
193
- # @return [true, false]
194
- def ==(other)
195
- `return self.valueOf() === other.valueOf() ? Qtrue : Qfalse;`
196
- end
197
-
198
135
  # Bitwise EXCLUSIVE OR.
199
136
  #
200
137
  # @param [Numeric] other number to XOR with
@@ -225,14 +162,14 @@ class Numeric
225
162
  #
226
163
  # @return [true, false]
227
164
  def even?
228
- `return (self % 2 == 0) ? Qtrue : Qfalse;`
165
+ `return (self % 2 == 0);`
229
166
  end
230
167
 
231
168
  # Returns `true` if self is odd, `false` otherwise.
232
169
  #
233
170
  # @return [true, false]
234
171
  def odd?
235
- `return (self % 2 == 0) ? Qfalse : Qtrue;`
172
+ `return (self % 2 == 0) ? false : true;`
236
173
  end
237
174
 
238
175
  # Returns the number equal to `self` + 1.
@@ -346,7 +283,7 @@ class Numeric
346
283
  #
347
284
  # @return [true, false]
348
285
  def zero?
349
- `return self == 0 ? Qtrue : Qfalse;`
286
+ `return self == 0;`
350
287
  end
351
288
 
352
289
  # Returns the receiver if it is not zero, `nil` otherwise
@@ -393,7 +330,7 @@ class Numeric
393
330
  #
394
331
  # @return [true, false]
395
332
  def integer?
396
- `return self % 1 == 0 ? Qtrue : Qfalse;`
333
+ `return self % 1 == 0;`
397
334
  end
398
335
 
399
336
  def inspect