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))
IMPORT_OS =
s(:import, ['os'], s(:attr, nil, :os))
IMPORT_PATH =
s(:import, ['path'], s(:attr, nil, :path))
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



370
371
372
# File 'lib/ruby2js/filter/node.rb', line 370

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

#on_block(node) ⇒ Object



291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
# File 'lib/ruby2js/filter/node.rb', line 291

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



310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
# File 'lib/ruby2js/filter/node.rb', line 310

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)
  elsif node.children.first == s(:const, nil, :File)
    if node.children.last == :SEPARATOR
      prepend_list << IMPORT_PATH
      S(:attr, s(:attr, nil, :path), :sep)
    elsif node.children.last == :PATH_SEPARATOR
      prepend_list << IMPORT_PATH
      S(:attr, s(:attr, nil, :path), :delimiter)
    else
      super
    end
  else
    super
  end
end

#on_gvar(node) ⇒ Object



337
338
339
340
341
342
343
344
345
346
347
# File 'lib/ruby2js/filter/node.rb', line 337

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



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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
# File 'lib/ruby2js/filter/node.rb', line 24

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))
      })

    elsif target.children.last == :File
      if method == :absolute_path
        prepend_list << IMPORT_PATH
        S(:send, s(:attr, nil, :path), :resolve,
          *process_all(args.reverse))
      elsif method == :absolute_path?
        prepend_list << IMPORT_PATH
        S(:send, s(:attr, nil, :path), :isAbsolute, *process_all(args))
      elsif method == :basename
        prepend_list << IMPORT_PATH
        S(:send, s(:attr, nil, :path), :basename, *process_all(args))
      elsif method == :dirname
        prepend_list << IMPORT_PATH
        S(:send, s(:attr, nil, :path), :dirname, *process_all(args))
      elsif method == :extname
        prepend_list << IMPORT_PATH
        S(:send, s(:attr, nil, :path), :extname, *process_all(args))
      elsif method == :join
        prepend_list << IMPORT_PATH
        S(:send, s(:attr, nil, :path), :join, *process_all(args))
      else
        super
      end
    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))
    elsif method == :home and args.length == 0
      prepend_list << IMPORT_OS
      S(:send!, s(:attr, nil, :os), :homedir)
    elsif method == :tmpdir and args.length == 0
      prepend_list << IMPORT_OS
      S(:send!, s(:attr, nil, :os), :tmpdir)

    else
      super
    end

  else
    super
  end
end

#on_xstr(node) ⇒ Object



349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
# File 'lib/ruby2js/filter/node.rb', line 349

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