ruby2js 1.9.3 → 1.10.0
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.
- data/README.md +4 -0
- data/lib/ruby2js/converter.rb +2 -1
- data/lib/ruby2js/converter/block.rb +18 -3
- data/lib/ruby2js/converter/class.rb +14 -0
- data/lib/ruby2js/converter/const.rb +3 -0
- data/lib/ruby2js/converter/kwbegin.rb +47 -7
- data/lib/ruby2js/converter/send.rb +3 -0
- data/lib/ruby2js/filter/functions.rb +31 -0
- data/lib/ruby2js/version.rb +2 -2
- data/ruby2js.gemspec +3 -3
- metadata +49 -49
data/README.md
CHANGED
@@ -156,6 +156,7 @@ the script.
|
|
156
156
|
* `.last(n)` becomes `.slice(*.length-1, *.length)`
|
157
157
|
* `.max` becomes `Math.max.apply(Math)`
|
158
158
|
* `.min` becomes `Math.min.apply(Math)`
|
159
|
+
* `.nil?` becomes `== null`
|
159
160
|
* `.ord` becomes `charCodeAt(0)`
|
160
161
|
* `puts` becomes `console.log`
|
161
162
|
* `.replace` becomes `.length = 0; ...push.apply(*)`
|
@@ -178,6 +179,9 @@ the script.
|
|
178
179
|
* for the following methods, if the block consists entirely of a simple
|
179
180
|
expression (or ends with one), a `return` is added prior to the
|
180
181
|
expression: `sub`, `gsub`, `any?`, `all?`, `map`.
|
182
|
+
* New classes subclassed off of `Exception` will become subclassed off
|
183
|
+
of `Error` instead; and default constructors will be provided
|
184
|
+
* `loop do...end` will be replaced with `while (true) {...}`
|
181
185
|
|
182
186
|
* [underscore](https://github.com/rubys/ruby2js/blob/master/lib/ruby2js/filter/underscore.rb)
|
183
187
|
|
data/lib/ruby2js/converter.rb
CHANGED
@@ -4,7 +4,7 @@ module Ruby2JS
|
|
4
4
|
class Converter
|
5
5
|
LOGICAL = :and, :not, :or
|
6
6
|
OPERATORS = [:[], :[]=], [:not, :!], [:*, :/, :%], [:+, :-], [:>>, :<<],
|
7
|
-
[:<=, :<, :>, :>=], [:==, :!=, :===, :"!=="], [:and, :or]
|
7
|
+
[:&], [:^, :|], [:<=, :<, :>, :>=], [:==, :!=, :===, :"!=="], [:and, :or]
|
8
8
|
|
9
9
|
INVERT_OP = {
|
10
10
|
:< => :>=,
|
@@ -24,6 +24,7 @@ module Ruby2JS
|
|
24
24
|
@nl = ''
|
25
25
|
@ws = ' '
|
26
26
|
@varstack = []
|
27
|
+
@rbstack = []
|
27
28
|
@width = 80
|
28
29
|
@next_token = :return
|
29
30
|
|
@@ -8,9 +8,24 @@ module Ruby2JS
|
|
8
8
|
# (lvar :a))
|
9
9
|
|
10
10
|
handle :block do |call, args, block|
|
11
|
-
|
12
|
-
|
13
|
-
|
11
|
+
|
12
|
+
if
|
13
|
+
@state == :statement and args.children.length == 1 and
|
14
|
+
call.children.first and call.children.first.type == :begin and
|
15
|
+
[:irange, :erange].include? call.children.first.children.first.type
|
16
|
+
then
|
17
|
+
var = args.children.first
|
18
|
+
expression = call.children.first.children.first
|
19
|
+
comp = (expression.type == :irange ? '<=' : '<')
|
20
|
+
"for (var #{parse var} = #{ parse expression.children.first }; " +
|
21
|
+
"#{ parse var } #{ comp } #{ parse expression.children.last }; " +
|
22
|
+
"#{ parse var } += #{ parse call.children[2] }) " +
|
23
|
+
"{#@nl#{ scope block }#@nl}"
|
24
|
+
else
|
25
|
+
block ||= s(:begin)
|
26
|
+
function = s(:def, nil, args, block)
|
27
|
+
parse s(:send, *call.children, function)
|
28
|
+
end
|
14
29
|
end
|
15
30
|
end
|
16
31
|
end
|
@@ -22,6 +22,7 @@ module Ruby2JS
|
|
22
22
|
end
|
23
23
|
|
24
24
|
body.compact!
|
25
|
+
visible = {}
|
25
26
|
body.map! do |m|
|
26
27
|
if m.type == :def
|
27
28
|
if m.children.first == :initialize
|
@@ -35,6 +36,8 @@ module Ruby2JS
|
|
35
36
|
{enumerable: s(:true), configurable: s(:true),
|
36
37
|
set: s(:block, s(:send, nil, :proc), *m.children[1..-1])})
|
37
38
|
else
|
39
|
+
visible[m.children[0]] = s(:self)
|
40
|
+
|
38
41
|
if not m.is_method?
|
39
42
|
# property getter
|
40
43
|
s(:prop, s(:attr, name, :prototype), m.children.first =>
|
@@ -122,7 +125,12 @@ module Ruby2JS
|
|
122
125
|
*m.children[1..-1])
|
123
126
|
elsif m.type == :casgn and m.children[0] == nil
|
124
127
|
# class constant
|
128
|
+
visible[m.children[1]] = name
|
125
129
|
s(:send, name, "#{m.children[1]}=", *m.children[2..-1])
|
130
|
+
elsif m.type == :alias
|
131
|
+
s(:send, s(:attr, name, :prototype),
|
132
|
+
"#{m.children[0].children.first}=",
|
133
|
+
s(:attr, s(:attr, name, :prototype), m.children[1].children.first))
|
126
134
|
else
|
127
135
|
raise NotImplementedError, "class #{ m.type }"
|
128
136
|
end
|
@@ -178,13 +186,19 @@ module Ruby2JS
|
|
178
186
|
# save class name
|
179
187
|
class_name, @class_name = @class_name, name
|
180
188
|
class_parent, @class_parent = @class_parent, inheritance
|
189
|
+
|
181
190
|
# inhibit ivar substitution within a class definition. See ivars.rb
|
182
191
|
ivars, self.ivars = self.ivars, nil
|
192
|
+
|
193
|
+
# add locally visible interfaces to rbstack. See send.rb, const.rb
|
194
|
+
@rbstack.push visible
|
195
|
+
|
183
196
|
parse s(:begin, *body.compact)
|
184
197
|
ensure
|
185
198
|
self.ivars = ivars
|
186
199
|
@class_name = class_name
|
187
200
|
@class_parent = class_parent
|
201
|
+
@rbstack.pop
|
188
202
|
end
|
189
203
|
end
|
190
204
|
|
@@ -18,24 +18,64 @@ module Ruby2JS
|
|
18
18
|
end
|
19
19
|
|
20
20
|
if block and block.type == :rescue
|
21
|
-
body,
|
21
|
+
body, *recovers, otherwise = block.children
|
22
22
|
raise NotImplementedError, "block else" if otherwise
|
23
|
-
|
24
|
-
|
23
|
+
|
24
|
+
if recovers.any? {|recover| not recover.children[1]}
|
25
|
+
raise NotImplementedError, "recover without exception variable"
|
26
|
+
end
|
27
|
+
|
28
|
+
var = recovers.first.children[1]
|
29
|
+
|
30
|
+
if recovers.any? {|recover| recover.children[1] != var}
|
31
|
+
raise NotImplementedError,
|
32
|
+
"multiple recovers with different exception variables"
|
33
|
+
end
|
25
34
|
else
|
26
35
|
body = block
|
27
36
|
end
|
28
37
|
|
29
38
|
output = "try {#@nl#{ parse body, :statement }#@nl}"
|
30
39
|
|
31
|
-
if
|
32
|
-
|
33
|
-
|
40
|
+
if recovers
|
41
|
+
if recovers.length == 1 and not recovers.first.children.first
|
42
|
+
# single catch with no exception named
|
43
|
+
output += " catch (#{ parse var }) " +
|
44
|
+
"{#@nl#{ parse recovers.first.children.last, :statement }#@nl}"
|
45
|
+
else
|
46
|
+
output += " catch (#{ parse var }) {#@nl"
|
47
|
+
|
48
|
+
first = true
|
49
|
+
recovers.each do |recover|
|
50
|
+
exceptions, var, recovery = recover.children
|
51
|
+
|
52
|
+
if exceptions
|
53
|
+
tests = exceptions.children.map do |exception|
|
54
|
+
"#{ parse var} instanceof #{ parse exception }"
|
55
|
+
end
|
56
|
+
|
57
|
+
output += "} else " if not first
|
58
|
+
first = false
|
59
|
+
|
60
|
+
output += "if (#{ tests.join(' || ') }) {#@nl"
|
61
|
+
else
|
62
|
+
output += "} else {#@nl"
|
63
|
+
end
|
64
|
+
|
65
|
+
output += "#{ parse recovery, :statement }#@nl"
|
66
|
+
end
|
67
|
+
|
68
|
+
if recovers.last.children.first
|
69
|
+
output += "} else {#{@nl}throw #{ parse var }#@nl"
|
70
|
+
end
|
71
|
+
|
72
|
+
output += "}#@nl}"
|
73
|
+
end
|
34
74
|
end
|
35
75
|
|
36
76
|
output += " finally {#@nl#{ parse finally, :statement }#@nl}" if finally
|
37
77
|
|
38
|
-
if
|
78
|
+
if recovers or finally
|
39
79
|
output
|
40
80
|
else
|
41
81
|
parse s(:begin, *children)
|
@@ -42,6 +42,9 @@ module Ruby2JS
|
|
42
42
|
target = args.first
|
43
43
|
end
|
44
44
|
|
45
|
+
# resolve anonymous receivers against rbstack
|
46
|
+
receiver ||= @rbstack.map {|rb| rb[method]}.compact.last
|
47
|
+
|
45
48
|
if receiver
|
46
49
|
group_receiver = receiver.type == :send &&
|
47
50
|
op_index < operator_index( receiver.children[1] ) if receiver
|
@@ -87,6 +87,9 @@ module Ruby2JS
|
|
87
87
|
elsif method == :empty? and args.length == 0
|
88
88
|
process s(:send, s(:attr, target, :length), :==, s(:int, 0))
|
89
89
|
|
90
|
+
elsif method == :nil? and args.length == 0
|
91
|
+
process s(:send, target, :==, s(:nil))
|
92
|
+
|
90
93
|
elsif [:start_with?, :end_with?].include? method and args.length == 1
|
91
94
|
if args.first.type == :str
|
92
95
|
length = s(:int, args.first.children.first.length)
|
@@ -251,6 +254,34 @@ module Ruby2JS
|
|
251
254
|
s(:block, s(:send, target, method, *call.children[2..-1]),
|
252
255
|
*node.children[1..-1]))))
|
253
256
|
|
257
|
+
elsif node.children[0..1] == [s(:send, nil, :loop), s(:args)]
|
258
|
+
# input: loop {statements}
|
259
|
+
# output: while(true) {statements}
|
260
|
+
s(:while, s(:true), node.children[2])
|
261
|
+
|
262
|
+
else
|
263
|
+
super
|
264
|
+
end
|
265
|
+
end
|
266
|
+
|
267
|
+
def on_class(node)
|
268
|
+
name, inheritance, *body = node.children
|
269
|
+
body.compact!
|
270
|
+
|
271
|
+
if inheritance == s(:const, nil, :Exception)
|
272
|
+
unless
|
273
|
+
body.any? {|statement| statement.type == :def and
|
274
|
+
statement.children.first == :initialize}
|
275
|
+
then
|
276
|
+
body.unshift s(:def, :initialize, s(:args, s(:arg, :message)),
|
277
|
+
s(:begin, s(:send, s(:self), :message=, s(:lvar, :message)),
|
278
|
+
s(:send, s(:self), :name=, s(:sym, name.children[1])),
|
279
|
+
s(:send, s(:self), :stack=, s(:send, s(:send, nil, :Error,
|
280
|
+
s(:lvar, :message)), :stack))))
|
281
|
+
end
|
282
|
+
|
283
|
+
body = [s(:begin, *body)] if body.length > 1
|
284
|
+
s(:class, name, s(:const, nil, :Error), *body)
|
254
285
|
else
|
255
286
|
super
|
256
287
|
end
|
data/lib/ruby2js/version.rb
CHANGED
data/ruby2js.gemspec
CHANGED
@@ -2,14 +2,14 @@
|
|
2
2
|
|
3
3
|
Gem::Specification.new do |s|
|
4
4
|
s.name = "ruby2js"
|
5
|
-
s.version = "1.
|
5
|
+
s.version = "1.10.0"
|
6
6
|
|
7
7
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
8
8
|
s.authors = ["Sam Ruby"]
|
9
|
-
s.date = "2014-10-
|
9
|
+
s.date = "2014-10-25"
|
10
10
|
s.description = " The base package maps Ruby syntax to JavaScript semantics.\n Filters may be provided to add Ruby-specific or framework specific\n behavior.\n"
|
11
11
|
s.email = "rubys@intertwingly.net"
|
12
|
-
s.files = ["ruby2js.gemspec", "README.md", "lib/ruby2js", "lib/ruby2js
|
12
|
+
s.files = ["ruby2js.gemspec", "README.md", "lib/ruby2js.rb", "lib/ruby2js", "lib/ruby2js/converter.rb", "lib/ruby2js/version.rb", "lib/ruby2js/cgi.rb", "lib/ruby2js/rails.rb", "lib/ruby2js/converter", "lib/ruby2js/converter/prototype.rb", "lib/ruby2js/converter/begin.rb", "lib/ruby2js/converter/undef.rb", "lib/ruby2js/converter/kwbegin.rb", "lib/ruby2js/converter/const.rb", "lib/ruby2js/converter/self.rb", "lib/ruby2js/converter/class.rb", "lib/ruby2js/converter/untilpost.rb", "lib/ruby2js/converter/while.rb", "lib/ruby2js/converter/blockpass.rb", "lib/ruby2js/converter/array.rb", "lib/ruby2js/converter/for.rb", "lib/ruby2js/converter/hash.rb", "lib/ruby2js/converter/block.rb", "lib/ruby2js/converter/def.rb", "lib/ruby2js/converter/nil.rb", "lib/ruby2js/converter/if.rb", "lib/ruby2js/converter/break.rb", "lib/ruby2js/converter/send.rb", "lib/ruby2js/converter/module.rb", "lib/ruby2js/converter/defs.rb", "lib/ruby2js/converter/ivar.rb", "lib/ruby2js/converter/var.rb", "lib/ruby2js/converter/case.rb", "lib/ruby2js/converter/literal.rb", "lib/ruby2js/converter/super.rb", "lib/ruby2js/converter/opasgn.rb", "lib/ruby2js/converter/return.rb", "lib/ruby2js/converter/arg.rb", "lib/ruby2js/converter/regexp.rb", "lib/ruby2js/converter/args.rb", "lib/ruby2js/converter/until.rb", "lib/ruby2js/converter/dstr.rb", "lib/ruby2js/converter/vasgn.rb", "lib/ruby2js/converter/whilepost.rb", "lib/ruby2js/converter/xstr.rb", "lib/ruby2js/converter/defined.rb", "lib/ruby2js/converter/next.rb", "lib/ruby2js/converter/ivasgn.rb", "lib/ruby2js/converter/cvasgn.rb", "lib/ruby2js/converter/logical.rb", "lib/ruby2js/converter/sym.rb", "lib/ruby2js/converter/boolean.rb", "lib/ruby2js/converter/masgn.rb", "lib/ruby2js/converter/cvar.rb", "lib/ruby2js/converter/casgn.rb", "lib/ruby2js/sinatra.rb", "lib/ruby2js/filter", "lib/ruby2js/filter/angular-resource.rb", "lib/ruby2js/filter/functions.rb", "lib/ruby2js/filter/angularrb.rb", "lib/ruby2js/filter/return.rb", "lib/ruby2js/filter/underscore.rb", "lib/ruby2js/filter/strict.rb", "lib/ruby2js/filter/minitest-jasmine.rb", "lib/ruby2js/filter/angular-route.rb", "lib/ruby2js/filter/jquery.rb"]
|
13
13
|
s.homepage = "http://github.com/rubys/ruby2js"
|
14
14
|
s.licenses = ["MIT"]
|
15
15
|
s.require_paths = ["lib"]
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ruby2js
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.10.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2014-10-
|
12
|
+
date: 2014-10-25 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: parser
|
@@ -36,67 +36,67 @@ extra_rdoc_files: []
|
|
36
36
|
files:
|
37
37
|
- ruby2js.gemspec
|
38
38
|
- README.md
|
39
|
+
- lib/ruby2js.rb
|
39
40
|
- lib/ruby2js/converter.rb
|
40
41
|
- lib/ruby2js/version.rb
|
41
|
-
- lib/ruby2js/filter/jquery.rb
|
42
|
-
- lib/ruby2js/filter/return.rb
|
43
|
-
- lib/ruby2js/filter/underscore.rb
|
44
|
-
- lib/ruby2js/filter/angularrb.rb
|
45
|
-
- lib/ruby2js/filter/minitest-jasmine.rb
|
46
|
-
- lib/ruby2js/filter/strict.rb
|
47
|
-
- lib/ruby2js/filter/functions.rb
|
48
|
-
- lib/ruby2js/filter/angular-resource.rb
|
49
|
-
- lib/ruby2js/filter/angular-route.rb
|
50
|
-
- lib/ruby2js/sinatra.rb
|
51
|
-
- lib/ruby2js/rails.rb
|
52
42
|
- lib/ruby2js/cgi.rb
|
53
|
-
- lib/ruby2js/
|
54
|
-
- lib/ruby2js/converter/arg.rb
|
55
|
-
- lib/ruby2js/converter/dstr.rb
|
56
|
-
- lib/ruby2js/converter/xstr.rb
|
57
|
-
- lib/ruby2js/converter/sym.rb
|
58
|
-
- lib/ruby2js/converter/return.rb
|
59
|
-
- lib/ruby2js/converter/break.rb
|
60
|
-
- lib/ruby2js/converter/for.rb
|
61
|
-
- lib/ruby2js/converter/defined.rb
|
62
|
-
- lib/ruby2js/converter/cvasgn.rb
|
63
|
-
- lib/ruby2js/converter/whilepost.rb
|
64
|
-
- lib/ruby2js/converter/logical.rb
|
65
|
-
- lib/ruby2js/converter/args.rb
|
66
|
-
- lib/ruby2js/converter/def.rb
|
43
|
+
- lib/ruby2js/rails.rb
|
67
44
|
- lib/ruby2js/converter/prototype.rb
|
68
|
-
- lib/ruby2js/converter/
|
69
|
-
- lib/ruby2js/converter/
|
70
|
-
- lib/ruby2js/converter/module.rb
|
45
|
+
- lib/ruby2js/converter/begin.rb
|
46
|
+
- lib/ruby2js/converter/undef.rb
|
71
47
|
- lib/ruby2js/converter/kwbegin.rb
|
72
|
-
- lib/ruby2js/converter/send.rb
|
73
|
-
- lib/ruby2js/converter/boolean.rb
|
74
|
-
- lib/ruby2js/converter/masgn.rb
|
75
|
-
- lib/ruby2js/converter/hash.rb
|
76
|
-
- lib/ruby2js/converter/cvar.rb
|
77
|
-
- lib/ruby2js/converter/ivasgn.rb
|
78
|
-
- lib/ruby2js/converter/case.rb
|
79
48
|
- lib/ruby2js/converter/const.rb
|
80
|
-
- lib/ruby2js/converter/
|
49
|
+
- lib/ruby2js/converter/self.rb
|
50
|
+
- lib/ruby2js/converter/class.rb
|
81
51
|
- lib/ruby2js/converter/untilpost.rb
|
82
|
-
- lib/ruby2js/converter/begin.rb
|
83
|
-
- lib/ruby2js/converter/ivar.rb
|
84
52
|
- lib/ruby2js/converter/while.rb
|
85
|
-
- lib/ruby2js/converter/next.rb
|
86
|
-
- lib/ruby2js/converter/nil.rb
|
87
53
|
- lib/ruby2js/converter/blockpass.rb
|
88
|
-
- lib/ruby2js/converter/super.rb
|
89
|
-
- lib/ruby2js/converter/defs.rb
|
90
54
|
- lib/ruby2js/converter/array.rb
|
55
|
+
- lib/ruby2js/converter/for.rb
|
56
|
+
- lib/ruby2js/converter/hash.rb
|
91
57
|
- lib/ruby2js/converter/block.rb
|
58
|
+
- lib/ruby2js/converter/def.rb
|
59
|
+
- lib/ruby2js/converter/nil.rb
|
92
60
|
- lib/ruby2js/converter/if.rb
|
93
|
-
- lib/ruby2js/converter/
|
61
|
+
- lib/ruby2js/converter/break.rb
|
62
|
+
- lib/ruby2js/converter/send.rb
|
63
|
+
- lib/ruby2js/converter/module.rb
|
64
|
+
- lib/ruby2js/converter/defs.rb
|
65
|
+
- lib/ruby2js/converter/ivar.rb
|
66
|
+
- lib/ruby2js/converter/var.rb
|
67
|
+
- lib/ruby2js/converter/case.rb
|
68
|
+
- lib/ruby2js/converter/literal.rb
|
69
|
+
- lib/ruby2js/converter/super.rb
|
70
|
+
- lib/ruby2js/converter/opasgn.rb
|
71
|
+
- lib/ruby2js/converter/return.rb
|
72
|
+
- lib/ruby2js/converter/arg.rb
|
94
73
|
- lib/ruby2js/converter/regexp.rb
|
74
|
+
- lib/ruby2js/converter/args.rb
|
75
|
+
- lib/ruby2js/converter/until.rb
|
76
|
+
- lib/ruby2js/converter/dstr.rb
|
77
|
+
- lib/ruby2js/converter/vasgn.rb
|
78
|
+
- lib/ruby2js/converter/whilepost.rb
|
79
|
+
- lib/ruby2js/converter/xstr.rb
|
80
|
+
- lib/ruby2js/converter/defined.rb
|
81
|
+
- lib/ruby2js/converter/next.rb
|
82
|
+
- lib/ruby2js/converter/ivasgn.rb
|
83
|
+
- lib/ruby2js/converter/cvasgn.rb
|
84
|
+
- lib/ruby2js/converter/logical.rb
|
85
|
+
- lib/ruby2js/converter/sym.rb
|
86
|
+
- lib/ruby2js/converter/boolean.rb
|
87
|
+
- lib/ruby2js/converter/masgn.rb
|
88
|
+
- lib/ruby2js/converter/cvar.rb
|
95
89
|
- lib/ruby2js/converter/casgn.rb
|
96
|
-
- lib/ruby2js/
|
97
|
-
- lib/ruby2js/
|
98
|
-
- lib/ruby2js/
|
99
|
-
- lib/ruby2js.rb
|
90
|
+
- lib/ruby2js/sinatra.rb
|
91
|
+
- lib/ruby2js/filter/angular-resource.rb
|
92
|
+
- lib/ruby2js/filter/functions.rb
|
93
|
+
- lib/ruby2js/filter/angularrb.rb
|
94
|
+
- lib/ruby2js/filter/return.rb
|
95
|
+
- lib/ruby2js/filter/underscore.rb
|
96
|
+
- lib/ruby2js/filter/strict.rb
|
97
|
+
- lib/ruby2js/filter/minitest-jasmine.rb
|
98
|
+
- lib/ruby2js/filter/angular-route.rb
|
99
|
+
- lib/ruby2js/filter/jquery.rb
|
100
100
|
homepage: http://github.com/rubys/ruby2js
|
101
101
|
licenses:
|
102
102
|
- MIT
|