Class: Opal::Nodes::ArityCheckNode

Inherits:
Base
  • Object
show all
Defined in:
opal/lib/opal/nodes/args/arity_check.rb

Direct Known Subclasses

IterArityCheckNode

Instance Attribute Summary

Attributes inherited from Base

#compiler, #type

Instance Method Summary collapse

Methods inherited from Base

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

Methods included from Helpers

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

Constructor Details

#initializeArityCheckNode

Returns a new instance of ArityCheckNode.



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'opal/lib/opal/nodes/args/arity_check.rb', line 12

def initialize(*)
  super

  arguments = Rewriters::Arguments.new(args_node.children)

  @args      = arguments.args
  @optargs   = arguments.optargs
  @restarg   = arguments.restarg
  @postargs  = arguments.postargs
  @kwargs    = arguments.kwargs
  @kwoptargs = arguments.kwoptargs
  @kwrestarg = arguments.kwrestarg
end

Instance Method Details

#all_argsObject



42
43
44
# File 'opal/lib/opal/nodes/args/arity_check.rb', line 42

def all_args
  @all_args ||= [*@args, *@optargs, @restarg, *@postargs, *kwargs].compact
end

#arityObject



74
75
76
77
78
79
80
# File 'opal/lib/opal/nodes/args/arity_check.rb', line 74

def arity
  if @restarg || @optargs.any? || has_only_optional_kwargs?
    negative_arity
  else
    positive_arity
  end
end

#arity_checksObject

Returns an array of JS conditions for raising and argument error caused by arity check



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
# File 'opal/lib/opal/nodes/args/arity_check.rb', line 48

def arity_checks
  return @arity_checks if defined?(@arity_checks)

  arity = all_args.size
  arity -= @optargs.size

  arity -= 1 if @restarg

  arity -= kwargs.size

  arity = -arity - 1 if !@optargs.empty? || !kwargs.empty? || @restarg

  @arity_checks = []

  if arity < 0 # splat or opt args
    min_arity = -(arity + 1)
    max_arity = all_args.size
    @arity_checks << "$arity < #{min_arity}" if min_arity > 0
    @arity_checks << "$arity > #{max_arity}" unless @restarg
  else
    @arity_checks << "$arity !== #{arity}"
  end

  @arity_checks
end

#compileObject



26
27
28
29
30
31
32
33
34
35
36
# File 'opal/lib/opal/nodes/args/arity_check.rb', line 26

def compile
  scope.arity = arity

  return unless compiler.arity_check?

  unless arity_checks.empty?
    meth = scope.mid.to_s.inspect
    line 'var $arity = arguments.length;'
    push " if (#{arity_checks.join(' || ')}) { Opal.ac($arity, #{arity}, this, #{meth}); }"
  end
end

#has_only_optional_kwargs?Boolean

Returns:

  • (Boolean)


107
108
109
# File 'opal/lib/opal/nodes/args/arity_check.rb', line 107

def has_only_optional_kwargs?
  kwargs.any? && kwargs.all? { |arg| %i[kwoptarg kwrestarg].include?(arg.type) }
end

#has_required_kwargs?Boolean

Returns:

  • (Boolean)


111
112
113
# File 'opal/lib/opal/nodes/args/arity_check.rb', line 111

def has_required_kwargs?
  kwargs.any? { |arg| arg.type == :kwarg }
end

#kwargsObject



38
39
40
# File 'opal/lib/opal/nodes/args/arity_check.rb', line 38

def kwargs
  [*@kwargs, *@kwoptargs, @kwrestarg].compact
end

#negative_arityObject



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'opal/lib/opal/nodes/args/arity_check.rb', line 82

def negative_arity
  required_plain_args = all_args.select do |arg|
    %i[arg mlhs].include?(arg.type)
  end

  result = required_plain_args.size

  if has_required_kwargs?
    result += 1
  end

  result = -result - 1

  result
end

#positive_arityObject



98
99
100
101
102
103
104
105
# File 'opal/lib/opal/nodes/args/arity_check.rb', line 98

def positive_arity
  result = all_args.size

  result -= kwargs.size
  result += 1 if kwargs.any?

  result
end