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

Inherits:
Base
  • Object
show all
Defined in:
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, #dynamic!, #error, #on_top, #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
# File 'lib/opal/rewriters/inline_args.rb', line 87

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

  @inline = []
  @initialization = []

  @type = type

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

def initialization
  @initialization
end

#inlineObject (readonly)

Returns the value of attribute inline.



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

def inline
  @inline
end

Instance Method Details

#args_to_keepObject



202
203
204
# File 'lib/opal/rewriters/inline_args.rb', line 202

def args_to_keep
  @args.postargs.length
end

#extract_argsObject



118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/opal/rewriters/inline_args.rb', line 118

def extract_args
  @args.args.each do |arg|
    if @type == :iter
      # block args are not required,
      # so we need to tell compiler that required args
      # must be initialized with nil-s
      @initialization << arg.updated(:initialize_iter_arg)
    else
      # required inline def argument like 'def m(req)'
      # no initialization is required
    end
    @inline << arg
  end
end

#extract_blockargObject



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

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

#extract_kwargsObject



152
153
154
155
156
# File 'lib/opal/rewriters/inline_args.rb', line 152

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

#extract_kwoptargsObject



158
159
160
161
162
# File 'lib/opal/rewriters/inline_args.rb', line 158

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

#extract_kwrestargObject



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

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

#extract_optargsObject



178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/opal/rewriters/inline_args.rb', line 178

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



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

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



194
195
196
197
198
199
200
# File 'lib/opal/rewriters/inline_args.rb', line 194

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



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

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

#prepare_kwargsObject



139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/opal/rewriters/inline_args.rb', line 139

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



133
134
135
136
137
# File 'lib/opal/rewriters/inline_args.rb', line 133

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