Module: Ruby2JS::Filter::Node

Extended by:
SEXP
Includes:
SEXP
Defined in:
lib/ruby2js/filter/node.rb

Constant Summary collapse

IMPORT_CHILD_PROCESS =
s(:import, ['child_process'],
s(:attr, nil, :child_process))
IMPORT_FS =
s(:import, ['fs'], s(:attr, nil, :fs))
SETUP_ARGV =
s(:lvasgn, :ARGV, s(:send, s(:attr, 
s(:attr, nil, :process), :argv), :slice, s(:int, 2)))

Instance Method Summary collapse

Methods included from SEXP

S, s

Instance Method Details

#on___FILE__(node) ⇒ Object



326
327
328
# File 'lib/ruby2js/filter/node.rb', line 326

def on___FILE__(node)
  s(:attr, nil, :__filename)
end

#on_block(node) ⇒ Object



257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
# File 'lib/ruby2js/filter/node.rb', line 257

def on_block(node)
  call = node.children.first
  target, method, *args = call.children

  if \
    method == :chdir and args.length == 1 and
    target.children.last == :Dir and
    target.type == :const and target.children.first == nil
  then
    s(:begin,
      s(:gvasgn, :$oldwd, s(:send, s(:attr, nil, :process), :cwd)),
      s(:kwbegin, s(:ensure, 
        s(:begin, process(call), process(node.children.last)),
        s(:send, s(:attr, nil, :process), :chdir, s(:gvar, :$oldwd)))))
  else
    super
  end
end

#on_const(node) ⇒ Object



276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
# File 'lib/ruby2js/filter/node.rb', line 276

def on_const(node)
  if node.children == [nil, :ARGV]
    prepend_list << SETUP_ARGV
    super
  elsif node.children == [nil, :ENV]
    S(:attr, s(:attr, nil, :process), :env)
  elsif node.children == [nil, :STDIN]
    S(:attr, s(:attr, nil, :process), :stdin)
  elsif node.children == [nil, :STDOUT]
    S(:attr, s(:attr, nil, :process), :stdout)
  elsif node.children == [nil, :STDERR]
    S(:attr, s(:attr, nil, :process), :stderr)
  else
    super
  end
end

#on_gvar(node) ⇒ Object



293
294
295
296
297
298
299
300
301
302
303
# File 'lib/ruby2js/filter/node.rb', line 293

def on_gvar(node)
  if node.children == [:$stdin]
    S(:attr, s(:attr, nil, :process), :stdin)
  elsif node.children == [:$stdout]
    S(:attr, s(:attr, nil, :process), :stdout)
  elsif node.children == [:$stderr]
    S(:attr, s(:attr, nil, :process), :stderr)
  else
    super
  end
end

#on_send(node) ⇒ Object



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
210
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
# File 'lib/ruby2js/filter/node.rb', line 20

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

  if target == nil
    if method == :__dir__ and args.length == 0
      S(:attr, nil, :__dirname)

    elsif method == :exit and args.length <= 1
      s(:send, s(:attr, nil, :process), :exit, *process_all(args));

    elsif method == :system
      prepend_list << IMPORT_CHILD_PROCESS

      if args.length == 1
        s(:send, s(:attr, nil, :child_process), :execSync,
        process(args.first),
        s(:hash, s(:pair, s(:sym, :stdio), s(:str, 'inherit'))))
      else
        s(:send, s(:attr, nil, :child_process), :execFileSync,
        process(args.first), s(:array, *process_all(args[1..-1])),
        s(:hash, s(:pair, s(:sym, :stdio), s(:str, 'inherit'))))
      end

    elsif \
      method == :require and args.length == 1 and 
      args.first.type == :str and 
      %w(fileutils tmpdir).include? args.first.children.first
    then
      s(:begin)

    else
      super
    end

  elsif \
    [:File, :IO].include? target.children.last and
    target.type == :const and target.children.first == nil
  then
    if method == :read and args.length == 1
      prepend_list << IMPORT_FS
      s(:send, s(:attr, nil, :fs), :readFileSync, *process_all(args),
        s(:str, 'utf8'))

    elsif method == :write and args.length == 2
      prepend_list << IMPORT_FS
      S(:send, s(:attr, nil, :fs), :writeFileSync, *process_all(args))

    elsif target.children.last == :IO
      super

    elsif [:exist?, :exists?].include? method and args.length == 1
      prepend_list << IMPORT_FS
      S(:send, s(:attr, nil, :fs), :existsSync, process(args.first))

    elsif method == :readlink and args.length == 1
      prepend_list << IMPORT_FS
      S(:send, s(:attr, nil, :fs), :readlinkSync, process(args.first))

    elsif method == :realpath and args.length == 1
      prepend_list << IMPORT_FS
      S(:send, s(:attr, nil, :fs), :realpathSync, process(args.first))

    elsif method == :rename and args.length == 2
      prepend_list << IMPORT_FS
      S(:send, s(:attr, nil, :fs), :renameSync, *process_all(args))

    elsif \
      [:chmod, :lchmod].include? method and 
      args.length > 1 and args.first.type == :int
    then
      prepend_list << IMPORT_FS

      S(:begin, *args[1..-1].map{|file|
        S(:send, s(:attr, nil, :fs), method.to_s + 'Sync', process(file),
          s(:octal, *args.first.children))
      })

    elsif \
      [:chown, :lchown].include? method and args.length > 2 and 
      args[0].type == :int and args[1].type == :int
    then
      prepend_list << IMPORT_FS

      S(:begin, *args[2..-1].map{|file|
        s(:send, s(:attr, nil, :fs), method.to_s + 'Sync', process(file),
          *process_all(args[0..1]))
      })

    elsif [:ln, :link].include? method and args.length == 2
      prepend_list << IMPORT_FS
      s(:send, s(:attr, nil, :fs), :linkSync, *process_all(args))
      
    elsif method == :symlink and args.length == 2
      prepend_list << IMPORT_FS
      s(:send, s(:attr, nil, :fs), :symlinkSync, *process_all(args))
      
    elsif method == :truncate and args.length == 2
      prepend_list << IMPORT_FS
      s(:send, s(:attr, nil, :fs), :truncateSync, *process_all(args))
      
    elsif [:stat, :lstat].include? method and args.length == 1
      prepend_list << IMPORT_FS
      s(:send, s(:attr, nil, :fs), method.to_s + 'Sync',
        process(args.first))

    elsif method == :unlink and args.length == 1
      prepend_list << IMPORT_FS
      S(:begin, *args.map{|file|
        S(:send, s(:attr, nil, :fs), :unlinkSync, process(file))
      })

    else
      super
    end

  elsif \
    target.children.last == :FileUtils and
    target.type == :const and target.children.first == nil
  then

    list = proc do |arg|
      if arg.type == :array
        arg.children
      else
        [arg]
      end
    end
      
    if [:cp, :copy].include? method and args.length == 2
      prepend_list << IMPORT_FS
      s(:send, s(:attr, nil, :fs), :copyFileSync, *process_all(args))
      
    elsif [:mv, :move].include? method and args.length == 2
      prepend_list << IMPORT_FS
      S(:send, s(:attr, nil, :fs), :renameSync, *process_all(args))

    elsif method == :mkdir and args.length == 1
      prepend_list << IMPORT_FS
      S(:begin, *list[args.last].map {|file|
        s(:send, s(:attr, nil, :fs), :mkdirSync, process(file))
      })
      
    elsif method == :cd and args.length == 1
      S(:send, s(:attr, nil, :process), :chdir, *process_all(args))

    elsif method == :pwd and args.length == 0
      s(:send, s(:attr, nil, :process), :cwd)

    elsif method == :rmdir and args.length == 1
      prepend_list << IMPORT_FS
      S(:begin, *list[args.last].map {|file|
        s(:send, s(:attr, nil, :fs), :rmdirSync, process(file))
      })

    elsif method == :ln and args.length == 2
      prepend_list << IMPORT_FS
      s(:send, s(:attr, nil, :fs), :linkSync, *process_all(args))
      
    elsif method == :ln_s and args.length == 2
      prepend_list << IMPORT_FS
      s(:send, s(:attr, nil, :fs), :symlinkSync, *process_all(args))
      
    elsif method == :rm and args.length == 1
      prepend_list << IMPORT_FS
      S(:begin, *list[args.last].map {|file|
        s(:send, s(:attr, nil, :fs), :unlinkSync, process(file))
      })

    elsif \
      method == :chmod and args.length == 2 and args.first.type == :int
    then
      prepend_list << IMPORT_FS

      S(:begin, *list[args.last].map {|file|
        S(:send, s(:attr, nil, :fs), method.to_s + 'Sync', process(file),
          s(:octal, *args.first.children))
      })

    elsif \
      method == :chown and args.length == 3 and 
      args[0].type == :int and args[1].type == :int
    then
      prepend_list << IMPORT_FS

      S(:begin, *list[args.last].map {|file|
        s(:send, s(:attr, nil, :fs), method.to_s + 'Sync', process(file),
          *process_all(args[0..1]))})

    elsif method == :touch
      prepend_list << IMPORT_FS

      S(:begin, *list[args.first].map {|file|
        S(:send, s(:attr, nil, :fs), :closeSync,
          s(:send, s(:attr, nil, :fs), :openSync, file,
          s(:str, "w")))})

    else
      super
    end

  elsif \
    target.type == :const and target.children.first == nil and
    target.children.last == :Dir
  then
    if method == :chdir and args.length == 1
      S(:send, s(:attr, nil, :process), :chdir, *process_all(args))
    elsif method == :pwd and args.length == 0
      s(:send, s(:attr, nil, :process), :cwd)
    elsif method == :entries
      prepend_list << IMPORT_FS
      s(:send, s(:attr, nil, :fs), :readdirSync, *process_all(args))
    elsif method == :mkdir and args.length == 1
      prepend_list << IMPORT_FS
      s(:send, s(:attr, nil, :fs), :mkdirSync, process(args.first))
    elsif method == :rmdir and args.length == 1
      prepend_list << IMPORT_FS
      s(:send, s(:attr, nil, :fs), :rmdirSync, process(args.first))
    elsif method == :mktmpdir and args.length <=1
      prepend_list << IMPORT_FS
      if args.length == 0
        prefix = s(:str, 'd')
      elsif args.first.type == :array
        prefix = args.first.children.first
      else
        prefix = args.first
      end

      s(:send, s(:attr, nil, :fs), :mkdtempSync, process(prefix))
    else
      super
    end

  else
    super
  end
end

#on_xstr(node) ⇒ Object



305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
# File 'lib/ruby2js/filter/node.rb', line 305

def on_xstr(node)
  prepend_list << IMPORT_CHILD_PROCESS

  children = node.children.dup
  command = children.shift
  while children.length > 0
    child = children.shift
    if \
      child.type == :begin and child.children.length == 1 and
      child.children.first.type == :send and
      child.children.first.children.first == nil
    then
      child = child.children.first
    end
    command = s(:send, command, :+, child)
  end

  s(:send, s(:attr, nil, :child_process), :execSync, command,
    s(:hash, s(:pair, s(:sym, :encoding), s(:str, 'utf8'))))
end