Skip to content

Commit

Permalink
add safe-navigation (aka lonely) operator &.
Browse files Browse the repository at this point in the history
  • Loading branch information
matz committed Mar 22, 2016
1 parent 7d06125 commit 21a3ae0
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 67 deletions.
24 changes: 20 additions & 4 deletions mrbgems/mruby-compiler/core/codegen.c
Original file line number Diff line number Diff line change
Expand Up @@ -826,13 +826,23 @@ gen_values(codegen_scope *s, node *t, int val)
#define CALL_MAXARGS 127

static void
gen_call(codegen_scope *s, node *tree, mrb_sym name, int sp, int val)
gen_call(codegen_scope *s, node *tree, mrb_sym name, int sp, int val, int safe)
{
mrb_sym sym = name ? name : sym(tree->cdr->car);
int idx;
int idx, skip;
int n = 0, noop = 0, sendv = 0, blk = 0;

codegen(s, tree->car, VAL); /* receiver */
if (safe) {
int recv = cursp()-1;
genop(s, MKOP_A(OP_LOADNIL, cursp()));
push();
genop(s, MKOP_AB(OP_MOVE, cursp(), recv));
pop();
idx = new_msym(s, mrb_intern_lit(s->mrb, "=="));
genop(s, MKOP_ABC(OP_EQ, cursp(), idx, 1));
skip = genop(s, MKOP_AsBx(OP_JMPIF, cursp(), 0));
}
idx = new_msym(s, sym);
tree = tree->cdr->cdr->car;
if (tree) {
Expand Down Expand Up @@ -905,6 +915,9 @@ gen_call(codegen_scope *s, node *tree, mrb_sym name, int sp, int val)
}
}
}
if (safe) {
dispatch(s, skip);
}
if (val) {
push();
}
Expand Down Expand Up @@ -968,7 +981,7 @@ gen_assignment(codegen_scope *s, node *tree, int sp, int val)

case NODE_CALL:
push();
gen_call(s, tree, attrsym(s, sym(tree->cdr->car)), sp, NOVAL);
gen_call(s, tree, attrsym(s, sym(tree->cdr->car)), sp, NOVAL, 0);
pop();
if (val) {
genop_peep(s, MKOP_AB(OP_MOVE, cursp(), sp), val);
Expand Down Expand Up @@ -1511,7 +1524,10 @@ codegen(codegen_scope *s, node *tree, int val)

case NODE_FCALL:
case NODE_CALL:
gen_call(s, tree, 0, 0, val);
gen_call(s, tree, 0, 0, val, 0);
break;
case NODE_SCALL:
gen_call(s, tree, 0, 0, val, 1);
break;

case NODE_DOT2:
Expand Down
1 change: 1 addition & 0 deletions mrbgems/mruby-compiler/core/node.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ enum node_type {
NODE_CVDECL,
NODE_OP_ASGN,
NODE_CALL,
NODE_SCALL,
NODE_FCALL,
NODE_VCALL,
NODE_SUPER,
Expand Down
Loading

1 comment on commit 21a3ae0

@zzak
Copy link
Contributor

@zzak zzak commented on 21a3ae0 Mar 23, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/cc #3140

Please sign in to comment.