Skip to content

Commit

Permalink
Merge pull request #2422 from jeremyevans/rb_keyword_given_p
Browse files Browse the repository at this point in the history
Add rb_keyword_given_p to the C-API
  • Loading branch information
jeremyevans committed Sep 3, 2019
1 parent f702cd6 commit 39c3252
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 3 deletions.
8 changes: 8 additions & 0 deletions eval.c
Expand Up @@ -899,6 +899,14 @@ rb_block_given_p(void)
}
}

int rb_vm_cframe_keyword_p(const rb_control_frame_t *cfp);

int
rb_keyword_given_p(void)
{
return rb_vm_cframe_keyword_p(GET_EC()->cfp);
}

VALUE rb_eThreadError;

/*! Declares that the current method needs a block.
Expand Down
1 change: 1 addition & 0 deletions include/ruby/ruby.h
Expand Up @@ -1960,6 +1960,7 @@ VALUE rb_yield_values(int n, ...);
VALUE rb_yield_values2(int n, const VALUE *argv);
VALUE rb_yield_splat(VALUE);
VALUE rb_yield_block(VALUE, VALUE, int, const VALUE *, VALUE); /* rb_block_call_func */
int rb_keyword_given_p(void);
int rb_block_given_p(void);
void rb_need_block(void);
VALUE rb_iterate(VALUE(*)(VALUE),VALUE,rb_block_call_func_t,VALUE);
Expand Down
6 changes: 6 additions & 0 deletions vm.c
Expand Up @@ -96,6 +96,12 @@ VM_CF_BLOCK_HANDLER(const rb_control_frame_t * const cfp)
return VM_ENV_BLOCK_HANDLER(ep);
}

int
rb_vm_cframe_keyword_p(const rb_control_frame_t *cfp)
{
return VM_FRAME_CFRAME_KW_P(cfp);
}

VALUE
rb_vm_frame_block_handler(const rb_control_frame_t *cfp)
{
Expand Down
11 changes: 9 additions & 2 deletions vm_core.h
Expand Up @@ -1166,11 +1166,11 @@ typedef rb_control_frame_t *

enum {
/* Frame/Environment flag bits:
* MMMM MMMM MMMM MMMM ____ __FF FFFF EEEX (LSB)
* MMMM MMMM MMMM MMMM ____ _FFF FFFF EEEX (LSB)
*
* X : tag for GC marking (It seems as Fixnum)
* EEE : 3 bits Env flags
* FF..: 6 bits Frame flags
* FF..: 7 bits Frame flags
* MM..: 15 bits frame magic (to check frame corruption)
*/

Expand All @@ -1194,6 +1194,7 @@ enum {
VM_FRAME_FLAG_CFRAME = 0x0080,
VM_FRAME_FLAG_LAMBDA = 0x0100,
VM_FRAME_FLAG_MODIFIED_BLOCK_PARAM = 0x0200,
VM_FRAME_FLAG_CFRAME_KW = 0x0400,

/* env flag */
VM_ENV_FLAG_LOCAL = 0x0002,
Expand Down Expand Up @@ -1248,6 +1249,12 @@ VM_FRAME_LAMBDA_P(const rb_control_frame_t *cfp)
return VM_ENV_FLAGS(cfp->ep, VM_FRAME_FLAG_LAMBDA) != 0;
}

static inline int
VM_FRAME_CFRAME_KW_P(const rb_control_frame_t *cfp)
{
return VM_ENV_FLAGS(cfp->ep, VM_FRAME_FLAG_CFRAME_KW) != 0;
}

static inline int
VM_FRAME_FINISHED_P(const rb_control_frame_t *cfp)
{
Expand Down
6 changes: 5 additions & 1 deletion vm_insnhelper.c
Expand Up @@ -2185,6 +2185,7 @@ vm_call_cfunc_with_frame(rb_execution_context_t *ec, rb_control_frame_t *reg_cfp

VALUE recv = calling->recv;
VALUE block_handler = calling->block_handler;
VALUE frame_type = VM_FRAME_MAGIC_CFUNC | VM_FRAME_FLAG_CFRAME | VM_ENV_FLAG_LOCAL;
int argc = calling->argc;
int orig_argc = argc;

Expand All @@ -2193,11 +2194,14 @@ vm_call_cfunc_with_frame(rb_execution_context_t *ec, rb_control_frame_t *reg_cfp
argc--;
}
}
if (UNLIKELY(IS_ARGS_KW_OR_KW_SPLAT(ci))) {
frame_type |= VM_FRAME_FLAG_CFRAME_KW;
}

RUBY_DTRACE_CMETHOD_ENTRY_HOOK(ec, me->owner, me->def->original_id);
EXEC_EVENT_HOOK(ec, RUBY_EVENT_C_CALL, recv, me->def->original_id, ci->mid, me->owner, Qundef);

vm_push_frame(ec, NULL, VM_FRAME_MAGIC_CFUNC | VM_FRAME_FLAG_CFRAME | VM_ENV_FLAG_LOCAL, recv,
vm_push_frame(ec, NULL, frame_type, recv,
block_handler, (VALUE)me,
0, ec->cfp->sp, 0, 0);

Expand Down
1 change: 1 addition & 0 deletions vm_insnhelper.h
Expand Up @@ -241,6 +241,7 @@ THROW_DATA_CONSUMED_SET(struct vm_throw_data *obj)
#define IS_ARGS_SPLAT(ci) ((ci)->flag & VM_CALL_ARGS_SPLAT)
#define IS_ARGS_KEYWORD(ci) ((ci)->flag & VM_CALL_KWARG)
#define IS_ARGS_KW_SPLAT(ci) ((ci)->flag & VM_CALL_KW_SPLAT)
#define IS_ARGS_KW_OR_KW_SPLAT(ci) ((ci)->flag & (VM_CALL_KWARG | VM_CALL_KW_SPLAT))

/* If this returns true, an optimized function returned by `vm_call_iseq_setup_func`
can be used as a fastpath. */
Expand Down

0 comments on commit 39c3252

Please sign in to comment.