Module: Ruby2JS::Filter::Functions

Includes:
SEXP
Defined in:
lib/ruby2js/filter/functions.rb

Constant Summary collapse

VAR_TO_ASSIGN =
{
  lvar: :lvasgn,
  ivar: :ivasgn,
  cvar: :cvasgn,
  gvar: :gvasgn
}

Instance Method Summary collapse

Methods included from SEXP

#s

Instance Method Details

#on_block(node) ⇒ Object



211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
# File 'lib/ruby2js/filter/functions.rb', line 211

def on_block(node)
  call = node.children.first
  if [:setInterval, :setTimeout].include? call.children[1]
    return super unless call.children.first == nil
    block = process s(:block, s(:send, nil, :proc), *node.children[1..-1])
    on_send call.updated nil, [*call.children[0..1], block,
      *call.children[2..-1]]

  elsif [:sub, :gsub, :sub!, :gsub!].include? call.children[1]
    return super if call.children.first == nil
    block = s(:block, s(:send, nil, :proc), node.children[1],
      s(:autoreturn, *node.children[2..-1]))
    process call.updated(nil, [*call.children, block])

  elsif call.children[1] == :select and call.children.length == 2
    call = call.updated nil, [call.children.first, :filter]
    node.updated nil, [process(call), process(node.children[1]),
      s(:autoreturn, *process_all(node.children[2..-1]))]

  elsif call.children[1] == :any? and call.children.length == 2
    call = call.updated nil, [call.children.first, :some]
    node.updated nil, [process(call), process(node.children[1]),
      s(:autoreturn, *process_all(node.children[2..-1]))]

  elsif call.children[1] == :all? and call.children.length == 2
    call = call.updated nil, [call.children.first, :every]
    node.updated nil, [process(call), process(node.children[1]),
      s(:autoreturn, *process_all(node.children[2..-1]))]

  elsif call.children[1] == :map and call.children.length == 2
    node.updated nil, [process(call), process(node.children[1]),
      s(:autoreturn, *process_all(node.children[2..-1]))]

  elsif [:map!, :select!].include? call.children[1]
    # input: a.map! {expression}
    # output: a.splice(0, a.length, *a.map {expression})
    method = (call.children[1] == :map! ? :map : :select)
    target = call.children.first
    process s(:send, target, :splice, s(:splat, s(:send, s(:array, 
      s(:int, 0), s(:attr, target, :length)), :concat,
      s(:block, s(:send, target, method, *call.children[2..-1]),
      *node.children[1..-1]))))

  else
    super
  end
end

#on_send(node) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/ruby2js/filter/functions.rb', line 15

def on_send(node)
  target, method, *args = node.children

  if [:max, :min].include? node.children[1] and args.length == 0
    return super unless node.is_method?
    process s(:send, s(:attr, s(:const, nil, :Math), node.children[1]),
      :apply, s(:const, nil, :Math), target)

  elsif method == :keys and args.length == 0 and node.is_method?
    process s(:send, s(:const, nil, :Object), :keys, target)

  elsif method == :delete and args.length == 1
    process s(:undef, s(:send, target, :[], args.first))

  elsif method == :to_s
    process s(:send, target, :toString, *args)

  elsif method == :to_a
    process s(:send, target, :toArray, *args)

  elsif method == :to_i
    process node.updated :send, [nil, :parseInt, target, *args]

  elsif method == :to_f
    process node.updated :send, [nil, :parseFloat, target, *args]

  elsif method == :sub and args.length == 2
    process node.updated nil, [target, :replace, *args]

  elsif [:sub!, :gsub!].include? method
    method = :"#{method.to_s[0..-2]}"
    if VAR_TO_ASSIGN.keys.include? target.type
      process s(VAR_TO_ASSIGN[target.type], target.children[0], 
        s(:send, target, method, *node.children[2..-1]))
    elsif target.type == :send
      if target.children[0] == nil
        process s(:lvasgn, target.children[1], s(:send,
          s(:lvar, target.children[1]), method, *node.children[2..-1]))
      else
        process s(:send, target.children[0], :"#{target.children[1]}=", 
          s(:send, target, method, *node.children[2..-1]))
      end
    else
      super
    end

  elsif method == :gsub and args.length == 2
    before, after = args
    if before.type == :regexp
      before = s(:regexp, *before.children[0...-1],
        s(:regopt, :g, *before.children.last))
    elsif before.type == :str
      before = s(:regexp, s(:str, Regexp.escape(before.children.first)),
        s(:regopt, :g))
    end
    process node.updated nil, [target, :replace, before, after]

  elsif method == :ord and args.length == 0
    if target.type == :str
      process s(:int, target.children.last.ord)
    else
      process node.updated nil, [target, :charCodeAt, s(:int, 0)]
    end

  elsif method == :chr and args.length == 0
    if target.type == :int
      process s(:str, target.children.last.chr)
    else
      process node.updated nil, [s(:const, nil, :String), :fromCharCode,
        target]
    end

  elsif method == :empty? and args.length == 0
    process s(:send, s(:attr, target, :length), :==, s(:int, 0))

  elsif [:start_with?, :end_with?].include? method and args.length == 1
    if args.first.type == :str
      length = s(:int, args.first.children.first.length)
    else
      length = s(:attr, *args, :length)
    end

    if method == :start_with?
      process s(:send, s(:send, target, :substring, s(:int, 0), 
        length), :==, *args)
    else
      process s(:send, s(:send, target, :slice, 
        s(:send, length, :-@)), :==, *args)
    end

  elsif method == :clear and args.length == 0 and node.is_method?
    process s(:send, target, :length=, s(:int, 0))

  elsif method == :replace and args.length == 1
    process s(:begin, s(:send, target, :length=, s(:int, 0)),
       s(:send, target, :push, s(:splat, node.children[2])))

  elsif method == :include? and args.length == 1
    process s(:send, s(:send, target, :indexOf, args.first), :!=,
      s(:int, -1))

  elsif method == :each
    process node.updated nil, [target, :forEach, *args]

  elsif method == :downcase and args.length == 0
    process node.updated nil, [target, :toLowerCase]

  elsif method == :upcase and args.length == 0
    process node.updated nil, [target, :toUpperCase]

  elsif node.children[0..1] == [nil, :puts]
    process s(:send, s(:attr, nil, :console), :log, *args)

  elsif method == :first
    if node.children.length == 2
      process node.updated nil, [target, :[], s(:int, 0)]
    elsif node.children.length == 3
      process on_send node.updated nil, [target, :[], s(:erange,
        s(:int, 0), node.children[2])]
    else
      super
    end

  elsif method == :last
    if node.children.length == 2
      process on_send node.updated nil, [target, :[], s(:int, -1)]
    elsif node.children.length == 3
      process node.updated nil, [target, :slice,
        s(:send, s(:attr, target, :length), :-, node.children[2]),
        s(:attr, target, :length)]
    else
      super
    end


  elsif method == :[]
    index = args.first

    # resolve negative literal indexes
    i = proc do |index|
      if index.type == :int and index.children.first < 0
        process s(:send, s(:attr, target, :length), :-, 
          s(:int, -index.children.first))
      else
        index
      end
    end

    if index.type == :regexp
      process s(:send, s(:send, target, :match, index), :[], 
        args[1] || s(:int, 0))

    elsif node.children.length != 3
      super

    elsif index.type == :int and index.children.first < 0
      process node.updated nil, [target, :[], i.(index)]

    elsif index.type == :erange
      start, finish = index.children
      process node.updated nil, [target, :slice, i.(start), i.(finish)]

    elsif index.type == :irange
      start, finish = index.children
      start = i.(start)
      if finish.type == :int
        if finish.children.first == -1
          finish = s(:attr, target, :length)
        else
          finish = i.(s(:int, finish.children.first+1))
        end
      else
        finish = s(:send, finish, :+, s(:int, 1))
      end
      process node.updated nil, [target, :slice, start, finish]

    else
      super
    end

  elsif method == :reverse! and node.is_method? 
    # input: a.reverse!
    # output: a.splice(0, a.length, *a.reverse)
    target = node.children.first
    process s(:send, target, :splice, s(:int, 0), 
      s(:attr, target, :length), s(:splat, s(:send, target, 
      :"#{node.children[1].to_s[0..-2]}", *node.children[2..-1])))

  elsif method == :each_with_index
    process node.updated nil, [target, :forEach, *args]

  else
    super
  end
end