Class: Opal::Rewriters::InlineArgs::Initializer

Inherits:
Base
  • Object
show all
Defined in:
opal/lib/opal/rewriters/inline_args.rb

Constant Summary collapse

STEPS =
%i[
  extract_blockarg
  initialize_shadowargs
  extract_args

  prepare_post_args
  prepare_kwargs

  extract_optargs
  extract_restarg
  extract_post_args

  extract_kwargs
  extract_kwoptargs
  extract_kwrestarg
].freeze

Constants inherited from Base

Base::DUMMY_LOCATION

Instance Attribute Summary collapse

Attributes inherited from Base

#current_node

Instance Method Summary collapse

Methods inherited from Base

#append_to_body, #begin_with_stmts, #error, #prepend_to_body, #process, #s, s, #stmts_of

Constructor Details

#initialize(args, type:) ⇒ Initializer

Returns a new instance of Initializer.



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'opal/lib/opal/rewriters/inline_args.rb', line 87

def initialize(args, type:)
  @args = Arguments.new(args.children)

  @inline = []
  @initialization = []

  @type = type
  @underscore_found = false

  STEPS.each do |step|
    send(step)
  end

  if @initialization.any?
    @initialization = s(:begin, *@initialization)
  else
    @initialization = nil
  end
end

Instance Attribute Details

#initializationObject (readonly)

Returns the value of attribute initialization.



68
69
70
# File 'opal/lib/opal/rewriters/inline_args.rb', line 68

def initialization
  @initialization
end

#inlineObject (readonly)

Returns the value of attribute inline.



68
69
70
# File 'opal/lib/opal/rewriters/inline_args.rb', line 68

def inline
  @inline
end

Instance Method Details

#args_to_keepObject



214
215
216
# File 'opal/lib/opal/rewriters/inline_args.rb', line 214

def args_to_keep
  @args.postargs.length
end

#extract_argsObject



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
# File 'opal/lib/opal/rewriters/inline_args.rb', line 119

def extract_args
  @args.args.each do |arg|
    if @type == :iter
      # block args are not required,
      # so we neeed to tell compiler that required args
      # must be initialized with nil-s
      @initialization << arg.updated(:initialize_iter_arg)

      if arg.children[0] == :_
        if @underscore_found
          # for proc { |_, _| _ }.call(1, 2) result must be 1
          # here we convert all underscore args starting from the 2nd
          # to a "fake" arg
          arg = s(:fake_arg)
        end

        @underscore_found = true
      end
    else
      # required inline def argument like 'def m(req)'
      # no initialization is required
    end
    @inline << arg
  end
end

#extract_blockargObject



107
108
109
110
111
# File 'opal/lib/opal/rewriters/inline_args.rb', line 107

def extract_blockarg
  if (arg = @args.blockarg)
    @initialization << arg.updated(:extract_blockarg)
  end
end

#extract_kwargsObject



164
165
166
167
168
# File 'opal/lib/opal/rewriters/inline_args.rb', line 164

def extract_kwargs
  @args.kwargs.each do |arg|
    @initialization << arg.updated(:extract_kwarg)
  end
end

#extract_kwoptargsObject



170
171
172
173
174
# File 'opal/lib/opal/rewriters/inline_args.rb', line 170

def extract_kwoptargs
  @args.kwoptargs.each do |arg|
    @initialization << arg.updated(:extract_kwoptarg)
  end
end

#extract_kwrestargObject



176
177
178
179
180
# File 'opal/lib/opal/rewriters/inline_args.rb', line 176

def extract_kwrestarg
  if (arg = @args.kwrestarg)
    @initialization << arg.updated(:extract_kwrestarg)
  end
end

#extract_optargsObject



190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'opal/lib/opal/rewriters/inline_args.rb', line 190

def extract_optargs
  has_post_args = @args.has_post_args?
  @args.optargs.each do |arg|
    if has_post_args
      # optional post argument like 'def m(opt = 1, a)'
      arg_name, default_value = *arg
      @initialization << arg.updated(:extract_post_optarg, [arg_name, default_value, args_to_keep])
      @inline << s(:fake_arg)
    else
      # optional inline argument like 'def m(a, opt = 1)'
      @inline << arg.updated(:arg)
      @initialization << arg.updated(:extract_optarg)
    end
  end
end

#extract_post_argsObject



182
183
184
185
186
187
188
# File 'opal/lib/opal/rewriters/inline_args.rb', line 182

def extract_post_args
  # post arguments must be extracted with an offset
  @args.postargs.each do |arg|
    @initialization << arg.updated(:extract_post_arg)
    @inline << s(:fake_arg)
  end
end

#extract_restargObject



206
207
208
209
210
211
212
# File 'opal/lib/opal/rewriters/inline_args.rb', line 206

def extract_restarg
  if (arg = @args.restarg)
    arg_name = arg.children[0]
    @initialization << arg.updated(:extract_restarg, [arg_name, args_to_keep])
    @inline << s(:fake_arg)
  end
end

#initialize_shadowargsObject



113
114
115
116
117
# File 'opal/lib/opal/rewriters/inline_args.rb', line 113

def initialize_shadowargs
  @args.shadowargs.each do |arg|
    @initialization << arg.updated(:initialize_shadowarg)
  end
end

#prepare_kwargsObject



151
152
153
154
155
156
157
158
159
160
161
162
# File 'opal/lib/opal/rewriters/inline_args.rb', line 151

def prepare_kwargs
  return unless @args.has_any_kwargs?

  if @args.can_inline_kwargs?
    @inline << s(:arg, :'$kwargs')
  else
    @initialization << s(:extract_kwargs)
    @inline << s(:fake_arg)
  end

  @initialization << s(:ensure_kwargs_are_kwargs)
end

#prepare_post_argsObject



145
146
147
148
149
# File 'opal/lib/opal/rewriters/inline_args.rb', line 145

def prepare_post_args
  if @args.has_post_args?
    @initialization << s(:prepare_post_args, @args.args.length)
  end
end