Class: Redom::Parser

Inherits:
Opal::Parser
  • Object
show all
Includes:
Utils
Defined in:
lib/redom/parser.rb

Instance Method Summary collapse

Methods included from Utils

#_dispatcher, #_logger, dispatcher=, logger=

Constructor Details

#initialize(conn) ⇒ Parser

Returns a new instance of Parser.



7
8
9
# File 'lib/redom/parser.rb', line 7

def initialize(conn)
  @conn = conn
end

Instance Method Details

#js_def(recvr, mid, args, stmts, line, end_line) ⇒ Object



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
# File 'lib/redom/parser.rb', line 59

def js_def(recvr, mid, args, stmts, line, end_line)
  jsid = mid_to_jsid(mid.to_s, false)

  if recvr
    @scope.defines_defs = true
    smethod = true if @scope.class_scope? && recvr.first == :self
    recv = process(recvr, :expr)
  else
    @scope.defines_defn = true
    recv = current_self
  end

  code = ''
  params = nil
  scope_name = nil
  uses_super = nil

  # opt args if last arg is sexp
  opt = args.pop if Array === args.last

  # block name &block
  if args.last.to_s.start_with? '&'
    block_name = args.pop.to_s[1..-1].to_sym
  end

  # splat args *splat
  if args.last.to_s.start_with? '*'
    if args.last == :*
      args.pop
    else
      splat = args[-1].to_s[1..-1].to_sym
      args[-1] = splat
      len = args.length - 2
    end
  end

  indent do
    in_scope(:def) do
      @scope.mid  = mid
      @scope.defs = true if recvr

      if block_name
        @scope.uses_block!
      end

      yielder = block_name || '__yield'
      @scope.block_name = yielder

      params = process args, :expr

      opt[1..-1].each do |o|
        next if o[2][2] == :undefined
        id = process s(:lvar, o[1]), :expr
        code += ("if (%s == null) {\n%s%s\n%s}" %
                  [id, @indent + INDENT, process(o, :expre), @indent])
      end if opt

      code += "#{splat} = __slice.call(arguments, #{len});" if splat
      code += "\n#@indent" + process(stmts, :stmt)

      # Returns the identity name if identified, nil otherwise
      scope_name = @scope.identity

      if @scope.uses_block?
        @scope.add_temp '__context'
        @scope.add_temp yielder

        blk = "\n%s%s = %s._p || nil, __context = %s._s, %s._p = null;\n%s" %
          [@indent, yielder, scope_name, yielder, scope_name, @indent]

        code = blk + code
      end

      uses_super = @scope.uses_super

      code = "#@indent#{@scope.to_vars}" + code
    end
  end

  defcode = "#{"#{scope_name} = " if scope_name}function(#{params}) {\n#{code}\n#@indent}"

  if recvr
    if smethod
      @scope.smethods << "$#{mid}"
      "#{ @scope.name }#{jsid} = #{defcode}"
    else
      "#{ recv }#{ jsid } = #{ defcode }"
    end
  elsif @scope.class_scope?
    @scope.methods << "$#{mid}"
    if uses_super
      @scope.add_temp uses_super
      uses_super = "#{uses_super} = #{@scope.proto}#{jsid};\n#@indent"
    end
    "#{uses_super}#{ @scope.proto }#{jsid} = #{defcode}"
  elsif @scope.type == :iter
    "def#{jsid} = #{defcode}"
  elsif @scope.type == :top
    "#{ current_self }#{ jsid } = #{ defcode }"
  else
    "def#{jsid} = #{defcode}"
  end
end

#mid_to_jsid(mid, djs_call = true) ⇒ String

Converts a ruby method name into its javascript equivalent for a method/function call. All ruby method names get prefixed with a ‘$’, and if the name is a valid javascript identifier, it will have a ‘.’ prefix (for dot-calling), otherwise it will be wrapped in brackets to use reference notation calling.

mid_to_jsid('foo')      # => ".$foo"
mid_to_jsid('class')    # => ".$class"
mid_to_jsid('==')       # => "['$==']"
mid_to_jsid('name=')    # => "['$name=']"

Parameters:

  • mid (String)

    ruby method id

Returns:

  • (String)


43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/redom/parser.rb', line 43

def mid_to_jsid(mid, djs_call = true)
  if /^([a-zA-Z0-9$_]+)=$/ =~ mid.to_s
    ".$djsAssign('#{$1}')"
  elsif '[]' == mid.to_s
    ".$djsCall('')"
  elsif /\=|\+|\-|\*|\/|\!|\?|\<|\>|\&|\||\^|\%|\~|\[/ =~ mid.to_s
    "['$#{mid}']"
  else
    if djs_call
      ".$djsCall('$#{mid}')"
    else
      ".$#{mid}"
    end
  end
end

#process_call(sexp, level) ⇒ Object

s(:call, recv, :mid, s(:arglist)) s(:call, nil, :mid, s(:arglist))



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
# File 'lib/redom/parser.rb', line 170

def process_call(sexp, level)
  recv, meth, arglist, iter = sexp
  mid = mid_to_jsid meth.to_s

  case meth
  when :attr_reader, :attr_writer, :attr_accessor
    return handle_attr_optimize(meth, arglist[1..-1])
  when :block_given?
    return js_block_given(sexp, level)
  when :alias_native
    return handle_alias_native(sexp) if @scope.class_scope?
  when :require
    path = arglist[1]

    if path and path[0] == :str
      @requires << path[1]
    end

    return "//= require #{path[1]}"
  when :respond_to?
    return handle_respond_to(sexp, level)
  end

  splat = arglist[1..-1].any? { |a| a.first == :splat }

  if Array === arglist.last and arglist.last.first == :block_pass
    block   = process s(:js_tmp, process(arglist.pop, :expr)), :expr
  elsif iter
    block   = iter
  end

  recv ||= s(:self)

  if block
    tmprecv = @scope.new_temp
  elsif splat and recv != [:self] and recv[0] != :lvar
    tmprecv = @scope.new_temp
  end

  args      = ""

  recv_code = process recv, :recv

  args = process arglist, :expr

  result = if block
    if meth == :sync || meth == :async
      dispatch = "(%s = %s, %s%s._p = %s, %s%s" %
        [tmprecv, recv_code, tmprecv, "#{mid}()", block, tmprecv, mid]
    else
      dispatch = "(%s = %s, %s%s._p = %s, %s%s" %
        [tmprecv, recv_code, tmprecv, mid_to_jsid(meth.to_s, false), block, tmprecv, mid]
    end
    if splat
      "%s.apply(null, %s))" % [dispatch, args]
    else
      "%s(%s))" % [dispatch, args]
    end
  else
    # m_missing = " || __mm(#{meth.to_s.inspect})"
    # dispatch = "((#{tmprecv} = #{recv_code}).$m#{mid}#{ m_missing })"
    # splat ? "#{dispatch}.apply(null, #{args})" : "#{dispatch}(#{args})"
    dispatch = tmprecv ? "(#{tmprecv} = #{recv_code})#{mid}" : "#{recv_code}#{mid}"
    splat ? "#{dispatch}.apply(#{tmprecv || recv_code}, #{args})" : "#{dispatch}(#{args})"
  end

  result
end

#process_const(sexp, level) ⇒ Object

s(:const, :const)



164
165
166
# File 'lib/redom/parser.rb', line 164

def process_const(sexp, level)
  "__scope.$djsCall('#{sexp.shift}')()"
end

#process_lit(sexp, level) ⇒ Object

s(:lit, 1) s(:lit, :foo)



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/redom/parser.rb', line 13

def process_lit(sexp, level)
  val = sexp.shift
  case val
  when Numeric
    level == :recv ? "(#{val.inspect})" : val.inspect
  when Symbol
    "Opal.$method(#{@conn._cid.inspect}, #{val.to_s.inspect})"
  when Regexp
    val == // ? /^/.inspect : val.inspect
  when Range
    @helpers[:range] = true
    "__range(#{val.begin}, #{val.end}, #{val.exclude_end?})"
  else
    raise "Bad lit: #{val.inspect}"
  end
end