Skip to content

Commit

Permalink
Helperize and slightly improve output of arg nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
hmdne committed Oct 26, 2022
1 parent 6f65f91 commit 64deb42
Show file tree
Hide file tree
Showing 10 changed files with 19 additions and 12 deletions.
4 changes: 3 additions & 1 deletion lib/opal/nodes/args/extract_kwargs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ class ExtractKwargs < Base
def compile
add_temp '$kwargs'

line '$kwargs = Opal.extract_kwargs($post_args)'
helper :extract_kwargs

push '$kwargs = $extract_kwargs($post_args)'
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/opal/nodes/args/extract_kwoptarg.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def compile

return if default_value.children[1] == :undefined

line "if (#{lvar_name} == null) #{lvar_name} = ", expr(default_value)
push "if (#{lvar_name} == null) #{lvar_name} = ", expr(default_value)
end
end
end
Expand Down
5 changes: 4 additions & 1 deletion lib/opal/nodes/args/extract_kwrestarg.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ def compile
name = self.name || '$kw_rest_arg'

add_temp name
line "#{name} = Opal.kwrestargs($kwargs, #{used_kwargs});"

helper :kwrestargs

push "#{name} = $kwrestargs($kwargs, #{used_kwargs})"
end

def used_kwargs
Expand Down
2 changes: 1 addition & 1 deletion lib/opal/nodes/args/extract_optarg.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class ExtractOptargNode < Base
def compile
return if default_value.children[1] == :undefined

line "if (#{name} == null) #{name} = ", expr(default_value), ";"
push "if (#{name} == null) #{name} = ", expr(default_value)
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/opal/nodes/args/extract_post_arg.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def compile

line "#{name} = $post_args.shift();"

line "if (#{name} == null) #{name} = nil;"
push "if (#{name} == null) #{name} = nil"
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/opal/nodes/args/extract_post_optarg.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def compile

return if default_value.children[1] == :undefined

line "if (#{name} == null) #{name} = ", expr(default_value), ";"
push "if (#{name} == null) #{name} = ", expr(default_value)
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/opal/nodes/args/extract_restarg.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ def compile

if args_to_keep == 0
# no post-args, we are free to grab everything
line "#{name} = $post_args;"
push "#{name} = $post_args"
else
line "#{name} = $post_args.splice(0, $post_args.length - #{args_to_keep});"
push "#{name} = $post_args.splice(0, $post_args.length - #{args_to_keep})"
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/opal/nodes/args/initialize_iterarg.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class InitializeIterarg < Base
children :name

def compile
line "if (#{name} == null) #{name} = nil;"
push "if (#{name} == null) #{name} = nil"
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/opal/nodes/args/initialize_shadowarg.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class InitializeShadowarg < Base
def compile
scope.locals << name
scope.add_arg(name)
line "#{name} = nil;"
push "#{name} = nil"
end
end
end
Expand Down
6 changes: 4 additions & 2 deletions lib/opal/nodes/args/prepare_post_args.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@ class PreparePostArgs < Base
def compile
add_temp '$post_args'

helper :slice

if offset == 0
line "$post_args = Opal.slice.call(arguments)"
push "$post_args = $slice.call(arguments)"
else
line "$post_args = Opal.slice.call(arguments, #{offset})"
push "$post_args = $slice.call(arguments, #{offset})"
end
end
end
Expand Down

0 comments on commit 64deb42

Please sign in to comment.