[フレーム]

Class: Opal::Nodes::ArityCheckNode

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

Direct Known Subclasses

IterArityCheckNode

Instance Attribute Summary

Attributes inherited from Base

#compiler , #sexp , #type

Attributes included from Closure::NodeSupport

#closure

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_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

#initializeArityCheckNode

Returns a new instance of ArityCheckNode.

12
13
14
15
16
17
18
19
20
21
22
23
24
25
# 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
 @kwnilarg = arguments.kwnilarg
end

Instance Method Details

#all_argsObject

44
45
46
# File 'opal/lib/opal/nodes/args/arity_check.rb', line 44
def all_args
 @all_args ||= [*@args, *@optargs, @restarg, *@postargs, *kwargs].compact
end

#arityObject

76
77
78
79
80
81
82
# File 'opal/lib/opal/nodes/args/arity_check.rb', line 76
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

50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'opal/lib/opal/nodes/args/arity_check.rb', line 50
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

27
28
29
30
31
32
33
34
35
36
37
38
# File 'opal/lib/opal/nodes/args/arity_check.rb', line 27
def compile
 scope.arity = arity
 return unless compiler.arity_check?
 unless arity_checks.empty?
 helper :ac
 meth = scope.mid.to_s.inspect
 line 'var $arity = arguments.length;'
 push " if (#{arity_checks.join(' || ')}) { $ac($arity, #{arity}, this, #{meth}); }"
 end
end

#has_only_optional_kwargs?Boolean

Returns:

  • (Boolean)
109
110
111
# File 'opal/lib/opal/nodes/args/arity_check.rb', line 109
def has_only_optional_kwargs?
 kwargs.any? && kwargs.all? { |arg| %i[kwoptargkwrestarg].include?(arg.type) }
end

#has_required_kwargs?Boolean

Returns:

  • (Boolean)
113
114
115
# File 'opal/lib/opal/nodes/args/arity_check.rb', line 113
def has_required_kwargs?
 kwargs.any? { |arg| arg.type == :kwarg }
end

#kwargsObject

40
41
42
# File 'opal/lib/opal/nodes/args/arity_check.rb', line 40
def kwargs
 [*@kwargs, *@kwoptargs, @kwrestarg].compact
end

#negative_arityObject

84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'opal/lib/opal/nodes/args/arity_check.rb', line 84
def negative_arity
 required_plain_args = all_args.select do |arg|
 %i[argmlhs].include?(arg.type)
 end
 result = required_plain_args.size
 if has_required_kwargs?
 result += 1
 end
 result = -result - 1
 result
end

#positive_arityObject

100
101
102
103
104
105
106
107
# File 'opal/lib/opal/nodes/args/arity_check.rb', line 100
def positive_arity
 result = all_args.size
 result -= kwargs.size
 result += 1 if kwargs.any?
 result
end

AltStyle によって変換されたページ (->オリジナル) /