Class: Opal::Nodes::NodeWithArgs

Inherits:
ScopeNode show all
Defined in:
lib/opal/nodes/node_with_args.rb,
lib/opal/nodes/node_with_args/shortcuts.rb

Direct Known Subclasses

DefNode, IterNode

Defined Under Namespace

Classes: Shortcut

Instance Attribute Summary collapse

Attributes inherited from ScopeNode

#await_encountered, #block_name, #catch_return, #defs, #gvars, #has_break, #has_retry, #identity, #ivars, #locals, #methods, #mid, #name, #parent, #rescue_else_sexp, #scope_name

Attributes inherited from Base

#compiler, #sexp, #type

Attributes included from Closure::NodeSupport

#closure

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ScopeNode

#accepts_using?, #add_arg, #add_proto_ivar, #add_scope_gvar, #add_scope_ivar, #add_scope_local, #add_scope_temp, #class?, #class_scope?, #collect_refinements_temps, #current_rescue, #def?, #def_in_class?, #defines_lambda, #find_parent_def, #gen_retry_id, #has_local?, #has_rescue_else?, #has_temp?, #identify!, #in_ensure, #in_ensure?, #in_resbody, #in_resbody?, #in_rescue, #in_scope, #in_while?, #is_lambda!, #iter?, #lambda?, #lambda_definition?, #module?, #nesting, #new_refinements_temp, #new_temp, #next_temp, #pop_while, #prepare_block, #prepend_scope_temp, #push_while, #queue_temp, #refinements_temp, #relative_access, #sclass?, #scope_locals, #self, #super_chain, #to_vars, #top?, #uses_block!, #uses_block?

Methods inherited from Base

#add_gvar, #add_ivar, #add_local, #add_temp, #children, children, #class_variable_owner, #class_variable_owner_nesting_level, #comments, #compile, #compile_to_fragments, #error, #expr, #expr?, #expr_or_empty, #expr_or_nil, #fragment, handle, handlers, #has_rescue_else?, #helper, #in_ensure, #in_ensure?, #in_resbody, #in_resbody?, #in_rescue, #in_while?, #process, #push, #recv, #recv?, #s, #scope, #source_location, #stmt, #stmt?, #top_scope, truthy_optimize?, #unshift, #while_loop, #with_temp, #wrap

Methods included from Closure::NodeSupport

#closure_is?, #compile_catcher, #generate_thrower, #generate_thrower_without_catcher, #in_closure, #pop_closure, #push_closure, #select_closure, #thrower

Methods included from Helpers

#current_indent, #empty_line, #indent, #js_truthy, #js_truthy_optimize, #line, #mid_to_jsid, #property, #valid_name?

Constructor Details

#initializeNodeWithArgs

Returns a new instance of NodeWithArgs.



14
15
16
17
18
19
20
# File 'lib/opal/nodes/node_with_args.rb', line 14

def initialize(*)
  super

  @original_args = @sexp.meta[:original_args]
  @used_kwargs = []
  @arity = 0
end

Instance Attribute Details

#arityObject

Returns the value of attribute arity.



11
12
13
# File 'lib/opal/nodes/node_with_args.rb', line 11

def arity
  @arity
end

#original_argsObject (readonly)

Returns the value of attribute original_args.



12
13
14
# File 'lib/opal/nodes/node_with_args.rb', line 12

def original_args
  @original_args
end

#used_kwargsObject (readonly)

Returns the value of attribute used_kwargs.



10
11
12
# File 'lib/opal/nodes/node_with_args.rb', line 10

def used_kwargs
  @used_kwargs
end

Class Method Details

.define_shortcut(name, **kwargs, &block) ⇒ Object



20
21
22
23
# File 'lib/opal/nodes/node_with_args/shortcuts.rb', line 20

def self.define_shortcut(name, **kwargs, &block)
  kwargs[:for] ||= :def
  @shortcuts << Shortcut.new(name, kwargs[:for], kwargs[:when], block)
end

.shortcuts_for(node_type) ⇒ Object



25
26
27
28
29
30
# File 'lib/opal/nodes/node_with_args/shortcuts.rb', line 25

def self.shortcuts_for(node_type)
  @shortcuts_for[node_type] ||=
    @shortcuts.select do |shortcut|
      [node_type, :*].include? shortcut.for
    end
end

Instance Method Details

#arity_check_nodeObject



22
23
24
# File 'lib/opal/nodes/node_with_args.rb', line 22

def arity_check_node
  s(:arity_check, original_args)
end

#compile_arity_checkObject

Returns code used in debug mode to check arity of method call



27
28
29
# File 'lib/opal/nodes/node_with_args.rb', line 27

def compile_arity_check
  push process(arity_check_node)
end

#compile_block_argObject



31
32
33
34
35
# File 'lib/opal/nodes/node_with_args.rb', line 31

def compile_block_arg
  if scope.uses_block?
    scope.prepare_block
  end
end

#compile_body_or_shortcutObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/opal/nodes/node_with_args/shortcuts.rb', line 32

def compile_body_or_shortcut
  # The shortcuts don't check arity. If we want to check arity,
  # we can't use them.
  return compile_body if compiler.arity_check?

  node_type = is_a?(DefNode) ? :def : :iter

  NodeWithArgs.shortcuts_for(node_type).each do |shortcut|
    if shortcut.match?(self)
      if ENV['OPAL_DEBUG_SHORTCUTS']
        node_desc = node_type == :def ? "def #{mid}" : "iter"
        warn "* shortcut #{shortcut.name} used for #{node_desc}"
      end

      return shortcut.compile(self)
    end
  end

  compile_body
end

#parameters_codeObject



37
38
39
# File 'lib/opal/nodes/node_with_args.rb', line 37

def parameters_code
  Args::Parameters.new(original_args).to_code
end

#simple_value?(node = stmts) ⇒ Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/opal/nodes/node_with_args/shortcuts.rb', line 61

def simple_value?(node = stmts)
  %i[true false nil int float str sym].include?(node.type)
end