Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[js] Support passing null as the named arguments when there are none.
  • Loading branch information
pmurias committed Feb 16, 2016
1 parent 2dd1099 commit 1a8567e
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 7 deletions.
13 changes: 11 additions & 2 deletions src/vm/js/Compiler.nqp
Expand Up @@ -231,8 +231,13 @@ class QAST::CompilerJS does DWIMYNameMangling does SerializeOnce {

# We want to always have at leat 1 thing to pass as the named argument
if @named || @named_groups == 0 {
@named_groups.push(Chunk.new($T_OBJ,'{' ~ nqp::join(',',@named_exprs) ~ '}', @named));
if @named_exprs == 0 {
@named_groups.push(Chunk.new($T_OBJ, 'null', []));
} else {
@named_groups.push(Chunk.new($T_OBJ,'{' ~ nqp::join(',',@named_exprs) ~ '}', @named));
}
}

if $cont {
@groups[0].unshift($cont);
}
Expand Down Expand Up @@ -318,8 +323,12 @@ class QAST::CompilerJS does DWIMYNameMangling does SerializeOnce {

my $default := self.as_js($_.default, :want($T_OBJ), :$cps);
@setup.push($default);
$value := "(_NAMED.hasOwnProperty($quoted) ? $value : {$default.expr})";
$value := "((_NAMED !== null && _NAMED.hasOwnProperty($quoted)) ? $value : {$default.expr})";
}
else {
$value := "(_NAMED !== null ? $value : null)";
}

# TODO required named arguments and defaultless optional ones

if self.is_dynamic_var($_) {
Expand Down
6 changes: 3 additions & 3 deletions src/vm/js/nqp-runtime/bootstrap.js
Expand Up @@ -114,16 +114,16 @@ add_knowhow_how_method('new_type', function(ctx, _NAMED) {
var HOW = this._STable.REPR.allocate(this._STable);

/* See if we have a representation name; if not default to P6opaque. */
var repr_name = _NAMED['repr'] || 'P6opaque';
var repr_name = (_NAMED && _NAMED.repr) ? _NAMED.repr : 'P6opaque';

/* Create a new type object of the desired REPR. (Note that we can't
* default to KnowHOWREPR here, since it doesn't know how to actually
* store attributes, it's just for bootstrapping knowhow's. */
var type_object = (new reprs[repr_name]).type_object_for(HOW);

/* See if we were given a name; put it into the meta-object if so. */
if (_NAMED['name']) {
HOW.__name = _NAMED['name'];
if (_NAMED && _NAMED.name) {
HOW.__name = _NAMED.name;
} else {
HOW.__name = null;
}
Expand Down
4 changes: 2 additions & 2 deletions src/vm/js/nqp-runtime/core.js
Expand Up @@ -245,11 +245,11 @@ op.captureposarg = function(capture, i) {
};

op.capturehasnameds = function(capture) {
return Object.keys(capture.named).length == 0 ? 0 : 1;
return (!capture.named || Object.keys(capture.named).length == 0) ? 0 : 1;
};

op.captureexistsnamed = function(capture, arg) {
return capture.named.hasOwnProperty(arg) ? 1 : 0;
return (capture.named && capture.named.hasOwnProperty(arg)) ? 1 : 0;
};

op.capturenamedshash = function(capture) {
Expand Down

0 comments on commit 1a8567e

Please sign in to comment.