Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Uninline non-typical argument handling #2419

Merged
merged 3 commits into from
Oct 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 2 additions & 6 deletions lib/opal/nodes/args/ensure_kwargs_are_kwargs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,9 @@ class EnsureKwargsAreKwargs < Base
handle :ensure_kwargs_are_kwargs

def compile
helper :hash2
helper :ensure_kwargs

line 'if ($kwargs == null) {'
line ' $kwargs = $hash2([], {});'
line '} else if (!$kwargs.$$is_hash) {'
line " throw Opal.ArgumentError.$new('expected kwargs');"
line '}'
push '$kwargs = $ensure_kwargs($kwargs)'
end
end
end
Expand Down
7 changes: 3 additions & 4 deletions lib/opal/nodes/args/extract_kwarg.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,9 @@ def compile

add_temp lvar_name

line "if (!Opal.hasOwnProperty.call($kwargs.$$smap, '#{key_name}')) {"
line " throw Opal.ArgumentError.$new('missing keyword: #{key_name}');"
line '}'
line "#{lvar_name} = $kwargs.$$smap[#{key_name.to_s.inspect}];"
helper :get_kwarg

push "#{lvar_name} = $get_kwarg($kwargs, #{key_name.to_s.inspect})"
end
end
end
Expand Down
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
23 changes: 19 additions & 4 deletions opal/corelib/runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -1815,10 +1815,7 @@
var kwargs = parameters[parameters.length - 1];
if (kwargs != null && Opal.respond_to(kwargs, '$to_hash', true)) {
$splice.call(parameters, parameters.length - 1);
return kwargs.$to_hash();
}
else {
return Opal.hash2([], {});
return kwargs;
}
};

Expand Down Expand Up @@ -2846,6 +2843,24 @@
}
}

// Primitives for handling parameters
Opal.ensure_kwargs = function(kwargs) {
if (kwargs == null) {
return Opal.hash2([], {});
} else if (kwargs.$$is_hash) {
return kwargs;
} else {
throw Opal.ArgumentError.$new('expected kwargs');
}
}

Opal.get_kwarg = function(kwargs, key) {
if (!$has_own.call(kwargs.$$smap, key)) {
throw Opal.ArgumentError.$new('missing keyword: '+key);
}
return kwargs.$$smap[key];
}

// Initialization
// --------------
Opal.BasicObject = BasicObject = Opal.allocate_class('BasicObject', null);
Expand Down