12#include "eval_intern.h"
14#include "internal/class.h"
15#include "internal/error.h"
16#include "internal/eval.h"
17#include "internal/gc.h"
18#include "internal/hash.h"
19#include "internal/object.h"
20#include "internal/proc.h"
21#include "internal/symbol.h"
25#include "ractor_core.h"
48static int method_arity(
VALUE);
49static int method_min_max_arity(
VALUE,
int *max);
54#define IS_METHOD_PROC_IFUNC(ifunc) ((ifunc)->func == bmcall)
57block_mark_and_move(
struct rb_block *block)
59 switch (block->type) {
61 case block_type_ifunc:
64 rb_gc_mark_and_move(&captured->self);
65 rb_gc_mark_and_move(&captured->code.val);
67 rb_gc_mark_and_move((
VALUE *)&captured->ep[VM_ENV_DATA_INDEX_ENV]);
71 case block_type_symbol:
72 rb_gc_mark_and_move(&block->as.symbol);
75 rb_gc_mark_and_move(&block->as.proc);
81proc_mark_and_move(
void *ptr)
84 block_mark_and_move((
struct rb_block *)&proc->block);
89 VALUE env[VM_ENV_DATA_SIZE + 1];
93proc_memsize(
const void *ptr)
96 if (proc->block.as.captured.ep == ((
const cfunc_proc_t *)ptr)->env+1)
112#define proc_data_type ruby_proc_data_type
115rb_proc_alloc(
VALUE klass)
124 return RBOOL(rb_typeddata_is_kind_of(proc, &proc_data_type));
129proc_clone(
VALUE self)
131 VALUE procval = rb_proc_dup(self);
132 return rb_obj_clone_setup(self, procval,
Qnil);
139 VALUE procval = rb_proc_dup(self);
140 return rb_obj_dup_setup(self, procval);
250 GetProcPtr(procval, proc);
252 return RBOOL(proc->is_lambda);
258binding_free(
void *ptr)
260 RUBY_FREE_ENTER(
"binding");
262 RUBY_FREE_LEAVE(
"binding");
266binding_mark_and_move(
void *ptr)
270 block_mark_and_move((
struct rb_block *)&bind->block);
271 rb_gc_mark_and_move((
VALUE *)&bind->pathobj);
275binding_memsize(
const void *ptr)
283 binding_mark_and_move,
286 binding_mark_and_move,
292rb_binding_alloc(
VALUE klass)
298 rb_yjit_collect_binding_alloc();
304binding_copy(
VALUE self)
308 GetBindingPtr(self, src);
309 GetBindingPtr(bindval, dst);
310 rb_vm_block_copy(bindval, &dst->block, &src->block);
312 dst->first_lineno = src->first_lineno;
318binding_dup(
VALUE self)
320 return rb_obj_dup_setup(self, binding_copy(self));
325binding_clone(
VALUE self)
327 return rb_obj_clone_setup(self, binding_copy(self),
Qnil);
334 return rb_vm_make_binding(ec, ec->cfp);
382rb_f_binding(
VALUE self)
404bind_eval(
int argc,
VALUE *argv,
VALUE bindval)
408 rb_scan_args(argc, argv,
"12", &args[0], &args[2], &args[3]);
410 return rb_f_eval(argc+1, args,
Qnil );
414get_local_variable_ptr(
const rb_env_t **envp,
ID lid,
bool search_outer)
418 if (!VM_ENV_FLAGS(env->ep, VM_FRAME_FLAG_CFRAME)) {
419 if (VM_ENV_FLAGS(env->ep, VM_ENV_FLAG_ISOLATED)) {
425 VM_ASSERT(rb_obj_is_iseq((
VALUE)iseq));
427 const unsigned int local_table_size = ISEQ_BODY(iseq)->local_table_size;
428 for (
unsigned int i=0; i<local_table_size; i++) {
429 if (ISEQ_BODY(iseq)->local_table[i] == lid) {
430 if (ISEQ_BODY(iseq)->local_iseq == iseq &&
431 ISEQ_BODY(iseq)->param.flags.has_block &&
432 (
unsigned int)ISEQ_BODY(iseq)->param.block_start == i) {
433 const VALUE *ep = env->ep;
434 if (!VM_ENV_FLAGS(ep, VM_FRAME_FLAG_MODIFIED_BLOCK_PARAM)) {
435 RB_OBJ_WRITE(env, &env->env[i], rb_vm_bh_to_procval(GET_EC(), VM_ENV_BLOCK_HANDLER(ep)));
436 VM_ENV_FLAGS_SET(ep, VM_FRAME_FLAG_MODIFIED_BLOCK_PARAM);
441 unsigned int last_lvar = env->env_size+VM_ENV_INDEX_LAST_LVAR
443 return &env->env[last_lvar - (local_table_size - i)];
451 }
while (search_outer && (env = rb_vm_env_prev_env(env)) != NULL);
463check_local_id(
VALUE bindval,
volatile VALUE *pname)
470 rb_name_err_raise(
"wrong local variable name '%1$s' for %2$s",
475 if (!rb_is_local_name(name)) {
476 rb_name_err_raise(
"wrong local variable name '%1$s' for %2$s",
503bind_local_variables(
VALUE bindval)
508 GetBindingPtr(bindval, bind);
509 env = VM_ENV_ENVVAL_PTR(vm_block_ep(&bind->block));
510 return rb_vm_env_local_variables(env);
514rb_numparam_id_p(
ID id)
516 return (tNUMPARAM_1 << ID_SCOPE_SHIFT) <=
id &&
id < ((tNUMPARAM_1 + 9) << ID_SCOPE_SHIFT);
520rb_implicit_param_p(
ID id)
522 return id == idItImplicit || rb_numparam_id_p(
id);
543bind_local_variable_get(
VALUE bindval,
VALUE sym)
545 ID lid = check_local_id(bindval, &sym);
550 if (!lid)
goto undefined;
551 if (rb_numparam_id_p(lid)) {
552 rb_name_err_raise(
"numbered parameter '%1$s' is not a local variable",
556 GetBindingPtr(bindval, bind);
558 env = VM_ENV_ENVVAL_PTR(vm_block_ep(&bind->block));
559 if ((ptr = get_local_variable_ptr(&env, lid, TRUE)) != NULL) {
565 rb_name_err_raise(
"local variable '%1$s' is not defined for %2$s",
598 ID lid = check_local_id(bindval, &sym);
603 if (!lid) lid = rb_intern_str(sym);
604 if (rb_numparam_id_p(lid)) {
605 rb_name_err_raise(
"numbered parameter '%1$s' is not a local variable",
609 GetBindingPtr(bindval, bind);
610 env = VM_ENV_ENVVAL_PTR(vm_block_ep(&bind->block));
611 if ((ptr = get_local_variable_ptr(&env, lid, TRUE)) == NULL) {
613 ptr = rb_binding_add_dynavars(bindval, bind, 1, &lid);
614 env = VM_ENV_ENVVAL_PTR(vm_block_ep(&bind->block));
618 rb_yjit_collect_binding_set();
644bind_local_variable_defined_p(
VALUE bindval,
VALUE sym)
646 ID lid = check_local_id(bindval, &sym);
651 if (rb_numparam_id_p(lid)) {
652 rb_name_err_raise(
"numbered parameter '%1$s' is not a local variable",
656 GetBindingPtr(bindval, bind);
657 env = VM_ENV_ENVVAL_PTR(vm_block_ep(&bind->block));
658 return RBOOL(get_local_variable_ptr(&env, lid, TRUE));
682bind_implicit_parameters(
VALUE bindval)
687 GetBindingPtr(bindval, bind);
688 env = VM_ENV_ENVVAL_PTR(vm_block_ep(&bind->block));
690 if (get_local_variable_ptr(&env, idItImplicit, FALSE)) {
691 return rb_ary_new_from_args(1,
ID2SYM(idIt));
694 env = VM_ENV_ENVVAL_PTR(vm_block_ep(&bind->block));
695 return rb_vm_env_numbered_parameters(env);
719bind_implicit_parameter_get(
VALUE bindval,
VALUE sym)
721 ID lid = check_local_id(bindval, &sym);
726 if (lid == idIt) lid = idItImplicit;
728 if (!lid || !rb_implicit_param_p(lid)) {
729 rb_name_err_raise(
"'%1$s' is not an implicit parameter",
733 GetBindingPtr(bindval, bind);
735 env = VM_ENV_ENVVAL_PTR(vm_block_ep(&bind->block));
736 if ((ptr = get_local_variable_ptr(&env, lid, FALSE)) != NULL) {
740 if (lid == idItImplicit) lid = idIt;
741 rb_name_err_raise(
"implicit parameter '%1$s' is not defined for %2$s", bindval,
ID2SYM(lid));
769bind_implicit_parameter_defined_p(
VALUE bindval,
VALUE sym)
771 ID lid = check_local_id(bindval, &sym);
775 if (lid == idIt) lid = idItImplicit;
777 if (!lid || !rb_implicit_param_p(lid)) {
778 rb_name_err_raise(
"'%1$s' is not an implicit parameter",
782 GetBindingPtr(bindval, bind);
783 env = VM_ENV_ENVVAL_PTR(vm_block_ep(&bind->block));
784 return RBOOL(get_local_variable_ptr(&env, lid, FALSE));
794bind_receiver(
VALUE bindval)
797 GetBindingPtr(bindval, bind);
798 return vm_block_self(&bind->block);
808bind_location(
VALUE bindval)
812 GetBindingPtr(bindval, bind);
813 loc[0] = pathobj_path(bind->pathobj);
814 loc[1] =
INT2FIX(bind->first_lineno);
827 proc = &sproc->basic;
828 vm_block_type_set(&proc->block, block_type_ifunc);
830 *(
VALUE **)&proc->block.as.captured.ep = ep = sproc->env + VM_ENV_DATA_SIZE-1;
831 ep[VM_ENV_DATA_INDEX_FLAGS] = VM_FRAME_MAGIC_IFUNC | VM_FRAME_FLAG_CFRAME | VM_ENV_FLAG_LOCAL | VM_ENV_FLAG_ESCAPED;
832 ep[VM_ENV_DATA_INDEX_ME_CREF] =
Qfalse;
833 ep[VM_ENV_DATA_INDEX_SPECVAL] = VM_BLOCK_HANDLER_NONE;
834 ep[VM_ENV_DATA_INDEX_ENV] =
Qundef;
837 RB_OBJ_WRITE(procval, &proc->block.as.captured.code.ifunc, ifunc);
838 proc->is_lambda = TRUE;
843rb_func_proc_dup(
VALUE src_obj)
845 RUBY_ASSERT(rb_typeddata_is_instance_of(src_obj, &proc_data_type));
848 GetProcPtr(src_obj, src_proc);
849 RUBY_ASSERT(vm_block_type(&src_proc->block) == block_type_ifunc);
854 memcpy(&proc->basic, src_proc,
sizeof(
rb_proc_t));
858 const VALUE *src_ep = src_proc->block.as.captured.ep;
859 VALUE *ep = *(
VALUE **)&proc->basic.block.as.captured.ep = proc->env + VM_ENV_DATA_SIZE - 1;
860 ep[VM_ENV_DATA_INDEX_FLAGS] = src_ep[VM_ENV_DATA_INDEX_FLAGS];
861 ep[VM_ENV_DATA_INDEX_ME_CREF] = src_ep[VM_ENV_DATA_INDEX_ME_CREF];
862 ep[VM_ENV_DATA_INDEX_SPECVAL] = src_ep[VM_ENV_DATA_INDEX_SPECVAL];
863 RB_OBJ_WRITE(proc_obj, &ep[VM_ENV_DATA_INDEX_ENV], src_ep[VM_ENV_DATA_INDEX_ENV]);
871 VALUE procval = rb_proc_alloc(klass);
873 GetProcPtr(procval, proc);
875 vm_block_type_set(&proc->block, block_type_symbol);
876 proc->is_lambda = TRUE;
886 min_argc >= (
int)(1U << (
SIZEOF_VALUE * CHAR_BIT) / 2) ||
889 rb_raise(
rb_eRangeError,
"minimum argument number out of range: %d",
894 max_argc >= (
int)(1U << (
SIZEOF_VALUE * CHAR_BIT) / 2) ||
897 rb_raise(
rb_eRangeError,
"maximum argument number out of range: %d",
902 struct vm_ifunc *ifunc = IMEMO_NEW(
struct vm_ifunc, imemo_ifunc, (
VALUE)rb_vm_svar_lep(ec, ec->cfp));
904 rb_gc_register_pinning_obj((
VALUE)ifunc);
908 ifunc->argc.min = min_argc;
909 ifunc->argc.max = max_argc;
917 struct vm_ifunc *ifunc = rb_vm_ifunc_new(func, (
void *)val, min_argc, max_argc);
921static const char proc_without_block[] =
"tried to create Proc object without a block";
924proc_new(
VALUE klass, int8_t is_lambda)
931 if ((block_handler = rb_vm_frame_block_handler(cfp)) == VM_BLOCK_HANDLER_NONE) {
932 rb_raise(rb_eArgError, proc_without_block);
936 switch (vm_block_handler_type(block_handler)) {
937 case block_handler_type_proc:
938 procval = VM_BH_TO_PROC(block_handler);
944 VALUE newprocval = rb_proc_dup(procval);
945 RBASIC_SET_CLASS(newprocval, klass);
950 case block_handler_type_symbol:
952 sym_proc_new(klass, VM_BH_TO_SYMBOL(block_handler)) :
953 rb_sym_to_proc(VM_BH_TO_SYMBOL(block_handler));
956 case block_handler_type_ifunc:
957 case block_handler_type_iseq:
958 return rb_vm_make_proc_lambda(ec, VM_BH_TO_CAPT_BLOCK(block_handler), klass, is_lambda);
960 VM_UNREACHABLE(proc_new);
979rb_proc_s_new(
int argc,
VALUE *argv,
VALUE klass)
981 VALUE block = proc_new(klass, FALSE);
1013f_lambda_filter_non_literal(
void)
1016 VALUE block_handler = rb_vm_frame_block_handler(cfp);
1018 if (block_handler == VM_BLOCK_HANDLER_NONE) {
1023 switch (vm_block_handler_type(block_handler)) {
1024 case block_handler_type_iseq:
1025 if (RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp)->ep == VM_BH_TO_ISEQ_BLOCK(block_handler)->ep) {
1029 case block_handler_type_symbol:
1031 case block_handler_type_proc:
1036 case block_handler_type_ifunc:
1040 rb_raise(rb_eArgError,
"the lambda method requires a literal block");
1054 f_lambda_filter_non_literal();
1109proc_call(
int argc,
VALUE *argv,
VALUE procval)
1115#if SIZEOF_LONG > SIZEOF_INT
1117check_argc(
long argc)
1119 if (argc > INT_MAX || argc < 0) {
1120 rb_raise(rb_eArgError,
"too many arguments (%lu)",
1121 (
unsigned long)argc);
1126#define check_argc(argc) (argc)
1141 GetProcPtr(self, proc);
1142 vret = rb_vm_invoke_proc(GET_EC(), proc, argc, argv,
1143 kw_splat, VM_BLOCK_HANDLER_NONE);
1156proc_to_block_handler(
VALUE procval)
1158 return NIL_P(procval) ? VM_BLOCK_HANDLER_NONE : procval;
1167 GetProcPtr(self, proc);
1168 vret = rb_vm_invoke_proc(ec, proc, argc, argv, kw_splat, proc_to_block_handler(passed_procval));
1222proc_arity(
VALUE self)
1229rb_iseq_min_max_arity(
const rb_iseq_t *iseq,
int *max)
1231 *max = ISEQ_BODY(iseq)->param.flags.has_rest == FALSE ?
1232 ISEQ_BODY(iseq)->param.lead_num + ISEQ_BODY(iseq)->param.opt_num + ISEQ_BODY(iseq)->param.post_num +
1233 (ISEQ_BODY(iseq)->param.flags.has_kw == TRUE || ISEQ_BODY(iseq)->param.flags.has_kwrest == TRUE || ISEQ_BODY(iseq)->param.flags.forwardable == TRUE)
1235 return ISEQ_BODY(iseq)->param.lead_num + ISEQ_BODY(iseq)->param.post_num + (ISEQ_BODY(iseq)->param.flags.has_kw && ISEQ_BODY(iseq)->param.keyword->required_num > 0);
1239rb_vm_block_min_max_arity(
const struct rb_block *block,
int *max)
1242 switch (vm_block_type(block)) {
1243 case block_type_iseq:
1244 return rb_iseq_min_max_arity(rb_iseq_check(block->as.captured.code.iseq), max);
1245 case block_type_proc:
1246 block = vm_proc_block(block->as.proc);
1248 case block_type_ifunc:
1250 const struct vm_ifunc *ifunc = block->as.captured.code.ifunc;
1251 if (IS_METHOD_PROC_IFUNC(ifunc)) {
1253 return method_min_max_arity((
VALUE)ifunc->data, max);
1255 *max = ifunc->argc.max;
1256 return ifunc->argc.min;
1258 case block_type_symbol:
1273rb_proc_min_max_arity(
VALUE self,
int *max)
1276 GetProcPtr(self, proc);
1277 return rb_vm_block_min_max_arity(&proc->block, max);
1285 GetProcPtr(self, proc);
1286 min = rb_vm_block_min_max_arity(&proc->block, &max);
1293 switch (vm_block_handler_type(block_handler)) {
1294 case block_handler_type_iseq:
1295 block->type = block_type_iseq;
1296 block->as.captured = *VM_BH_TO_ISEQ_BLOCK(block_handler);
1298 case block_handler_type_ifunc:
1299 block->type = block_type_ifunc;
1300 block->as.captured = *VM_BH_TO_IFUNC_BLOCK(block_handler);
1302 case block_handler_type_symbol:
1303 block->type = block_type_symbol;
1304 block->as.symbol = VM_BH_TO_SYMBOL(block_handler);
1306 case block_handler_type_proc:
1307 block->type = block_type_proc;
1308 block->as.proc = VM_BH_TO_PROC(block_handler);
1313rb_block_pair_yield_optimizable(
void)
1318 VALUE block_handler = rb_vm_frame_block_handler(cfp);
1321 if (block_handler == VM_BLOCK_HANDLER_NONE) {
1322 rb_raise(rb_eArgError,
"no block given");
1325 block_setup(&block, block_handler);
1326 min = rb_vm_block_min_max_arity(&block, &max);
1328 switch (vm_block_type(&block)) {
1329 case block_type_symbol:
1332 case block_type_proc:
1334 VALUE procval = block_handler;
1336 GetProcPtr(procval, proc);
1337 if (proc->is_lambda)
return 0;
1338 if (min != max)
return 0;
1342 case block_type_ifunc:
1344 const struct vm_ifunc *ifunc = block.as.captured.code.ifunc;
1345 if (ifunc->flags & IFUNC_YIELD_OPTIMIZABLE)
return 1;
1359 VALUE block_handler = rb_vm_frame_block_handler(cfp);
1362 if (block_handler == VM_BLOCK_HANDLER_NONE) {
1363 rb_raise(rb_eArgError,
"no block given");
1366 block_setup(&block, block_handler);
1368 switch (vm_block_type(&block)) {
1369 case block_type_symbol:
1372 case block_type_proc:
1376 min = rb_vm_block_min_max_arity(&block, &max);
1382rb_block_min_max_arity(
int *max)
1386 VALUE block_handler = rb_vm_frame_block_handler(cfp);
1389 if (block_handler == VM_BLOCK_HANDLER_NONE) {
1390 rb_raise(rb_eArgError,
"no block given");
1393 block_setup(&block, block_handler);
1394 return rb_vm_block_min_max_arity(&block, max);
1398rb_proc_get_iseq(
VALUE self,
int *is_proc)
1403 GetProcPtr(self, proc);
1404 block = &proc->block;
1405 if (is_proc) *is_proc = !proc->is_lambda;
1407 switch (vm_block_type(block)) {
1408 case block_type_iseq:
1409 return rb_iseq_check(block->as.captured.code.iseq);
1410 case block_type_proc:
1411 return rb_proc_get_iseq(block->as.proc, is_proc);
1412 case block_type_ifunc:
1414 const struct vm_ifunc *ifunc = block->as.captured.code.ifunc;
1415 if (IS_METHOD_PROC_IFUNC(ifunc)) {
1417 if (is_proc) *is_proc = 0;
1418 return rb_method_iseq((
VALUE)ifunc->data);
1424 case block_type_symbol:
1428 VM_UNREACHABLE(rb_proc_get_iseq);
1461 const rb_proc_t *self_proc, *other_proc;
1462 const struct rb_block *self_block, *other_block;
1468 GetProcPtr(self, self_proc);
1469 GetProcPtr(other, other_proc);
1471 if (self_proc->is_from_method != other_proc->is_from_method ||
1472 self_proc->is_lambda != other_proc->is_lambda) {
1476 self_block = &self_proc->block;
1477 other_block = &other_proc->block;
1479 if (vm_block_type(self_block) != vm_block_type(other_block)) {
1483 switch (vm_block_type(self_block)) {
1484 case block_type_iseq:
1485 if (self_block->as.captured.ep != \
1486 other_block->as.captured.ep ||
1487 self_block->as.captured.code.iseq != \
1488 other_block->as.captured.code.iseq) {
1492 case block_type_ifunc:
1493 if (self_block->as.captured.code.ifunc != \
1494 other_block->as.captured.code.ifunc) {
1505 case block_type_proc:
1506 if (self_block->as.proc != other_block->as.proc) {
1510 case block_type_symbol:
1511 if (self_block->as.symbol != other_block->as.symbol) {
1525 if (!iseq)
return Qnil;
1526 rb_iseq_check(iseq);
1527 loc[0] = rb_iseq_path(iseq);
1528 loc[1] =
RB_INT2NUM(ISEQ_BODY(iseq)->location.first_lineno);
1536 return iseq_location(iseq);
1548rb_proc_location(
VALUE self)
1550 return iseq_location(rb_proc_get_iseq(self, 0));
1554rb_unnamed_parameters(
int arity)
1557 int n = (arity < 0) ? ~arity : arity;
1591rb_proc_parameters(
int argc,
VALUE *argv,
VALUE self)
1593 static ID keyword_ids[1];
1599 iseq = rb_proc_get_iseq(self, &is_proc);
1601 if (!keyword_ids[0]) {
1602 CONST_ID(keyword_ids[0],
"lambda");
1609 if (!
NIL_P(lambda)) {
1610 is_proc = !
RTEST(lambda);
1617 return rb_iseq_parameters(iseq, is_proc);
1621rb_hash_proc(st_index_t hash,
VALUE prc)
1624 GetProcPtr(prc, proc);
1626 switch (vm_block_type(&proc->block)) {
1627 case block_type_iseq:
1628 hash = rb_st_hash_uint(hash, (st_index_t)proc->block.as.captured.code.iseq->body);
1630 case block_type_ifunc:
1631 hash = rb_st_hash_uint(hash, (st_index_t)proc->block.as.captured.code.ifunc->func);
1632 hash = rb_st_hash_uint(hash, (st_index_t)proc->block.as.captured.code.ifunc->data);
1634 case block_type_symbol:
1635 hash = rb_st_hash_uint(hash, rb_any_hash(proc->block.as.symbol));
1637 case block_type_proc:
1638 hash = rb_st_hash_uint(hash, rb_any_hash(proc->block.as.proc));
1641 rb_bug(
"rb_hash_proc: unknown block type %d", vm_block_type(&proc->block));
1647 if (vm_block_type(&proc->block) != block_type_ifunc) {
1648 hash =
rb_hash_uint(hash, (st_index_t)proc->block.as.captured.ep);
1671rb_sym_to_proc(
VALUE sym)
1673 enum {SYM_PROC_CACHE_SIZE = 67};
1675 if (rb_ractor_main_p()) {
1676 if (!sym_proc_cache) {
1682 long index = (
id % SYM_PROC_CACHE_SIZE);
1684 if (
RTEST(procval)) {
1686 GetProcPtr(procval, proc);
1688 if (proc->block.as.symbol == sym) {
1693 procval = sym_proc_new(
rb_cProc, sym);
1699 return sym_proc_new(
rb_cProc, sym);
1713proc_hash(
VALUE self)
1717 hash = rb_hash_proc(hash, self);
1723rb_block_to_s(
VALUE self,
const struct rb_block *block,
const char *additional_info)
1726 VALUE str = rb_sprintf(
"#<%"PRIsVALUE
":", cname);
1729 switch (vm_block_type(block)) {
1730 case block_type_proc:
1731 block = vm_proc_block(block->as.proc);
1733 case block_type_iseq:
1735 const rb_iseq_t *iseq = rb_iseq_check(block->as.captured.code.iseq);
1736 rb_str_catf(str,
"%p %"PRIsVALUE
":%d", (
void *)self,
1738 ISEQ_BODY(iseq)->location.first_lineno);
1741 case block_type_symbol:
1742 rb_str_catf(str,
"%p(&%+"PRIsVALUE
")", (
void *)self, block->as.symbol);
1744 case block_type_ifunc:
1745 rb_str_catf(str,
"%p", (
void *)block->as.captured.code.ifunc);
1763proc_to_s(
VALUE self)
1766 GetProcPtr(self, proc);
1767 return rb_block_to_s(self, &proc->block, proc->is_lambda ?
" (lambda)" : NULL);
1779proc_to_proc(
VALUE self)
1785bm_mark_and_move(
void *ptr)
1787 struct METHOD *data = ptr;
1788 rb_gc_mark_and_move((
VALUE *)&data->recv);
1789 rb_gc_mark_and_move((
VALUE *)&data->klass);
1790 rb_gc_mark_and_move((
VALUE *)&data->iclass);
1791 rb_gc_mark_and_move((
VALUE *)&data->owner);
1809 return RBOOL(rb_typeddata_is_kind_of(m, &method_data_type));
1816 ID rmiss = idRespond_to_missing;
1818 if (UNDEF_P(obj))
return 0;
1819 if (rb_method_basic_definition_p(klass, rmiss))
return 0;
1837 def->type = VM_METHOD_TYPE_MISSING;
1838 def->original_id = id;
1840 me = rb_method_entry_create(
id, klass, METHOD_VISI_UNDEF, def);
1852 if (!respond_to_missing_p(klass, obj, vid, scope))
return Qfalse;
1853 return mnew_missing(klass, obj,
SYM2ID(vid), mclass);
1863 rb_method_visibility_t visi = METHOD_VISI_UNDEF;
1866 if (UNDEFINED_METHOD_ENTRY_P(me)) {
1867 if (respond_to_missing_p(klass, obj,
ID2SYM(
id), scope)) {
1868 return mnew_missing(klass, obj,
id, mclass);
1870 if (!error)
return Qnil;
1871 rb_print_undef(klass,
id, METHOD_VISI_UNDEF);
1873 if (visi == METHOD_VISI_UNDEF) {
1874 visi = METHOD_ENTRY_VISI(me);
1876 if (scope && (visi != METHOD_VISI_PUBLIC)) {
1877 if (!error)
return Qnil;
1878 rb_print_inaccessible(klass,
id, visi);
1881 if (me->def->type == VM_METHOD_TYPE_ZSUPER) {
1882 if (me->defined_class) {
1884 id = me->def->original_id;
1885 me = (
rb_method_entry_t *)rb_callable_method_entry_with_refinements(klass,
id, &iclass);
1889 id = me->def->original_id;
1890 me = rb_method_entry_without_refinements(klass,
id, &iclass);
1906 RB_OBJ_WRITE(method, &data->owner, original_me->owner);
1916 return mnew_internal(me, klass, iclass, obj,
id, mclass, scope, TRUE);
1926 me = (
rb_method_entry_t *)rb_callable_method_entry_with_refinements(klass,
id, &iclass);
1927 return mnew_from_me(me, klass, iclass, obj,
id, mclass, scope);
1931mnew_unbound(
VALUE klass,
ID id,
VALUE mclass,
int scope)
1936 me = rb_method_entry_with_refinements(klass,
id, &iclass);
1937 return mnew_from_me(me, klass, iclass,
Qundef,
id, mclass, scope);
1943 VALUE defined_class = me->defined_class;
1944 return defined_class ? defined_class : me->owner;
1988 VALUE klass1, klass2;
1996 m1 = (
struct METHOD *)RTYPEDDATA_GET_DATA(method);
1997 m2 = (
struct METHOD *)RTYPEDDATA_GET_DATA(other);
1999 klass1 = method_entry_defined_class(m1->me);
2000 klass2 = method_entry_defined_class(m2->me);
2004 if (!rb_method_entry_eq(m1->me, m2->me) ||
2006 m1->klass != m2->klass ||
2007 m1->recv != m2->recv) {
2028#define unbound_method_eq method_eq
2040method_hash(
VALUE method)
2047 hash = rb_hash_method_entry(hash, m->me);
2063method_unbind(
VALUE obj)
2066 struct METHOD *orig, *data;
2070 &method_data_type, data);
2075 RB_OBJ_WRITE(method, &data->me, rb_method_entry_clone(orig->me));
2090method_receiver(
VALUE obj)
2106method_name(
VALUE obj)
2111 return ID2SYM(data->me->called_id);
2128method_original_name(
VALUE obj)
2133 return ID2SYM(data->me->def->original_id);
2155method_owner(
VALUE obj)
2169method_box(
VALUE obj)
2175 box = data->me->def->box;
2176 if (!box)
return Qnil;
2177 if (box->box_object)
return box->box_object;
2178 rb_bug(
"Unexpected box on the method definition: %p", (
void*) box);
2185#define MSG(s) rb_fstring_lit("undefined method '%1$s' for"s" '%2$s'")
2189 if (RCLASS_SINGLETON_P(c)) {
2190 VALUE obj = RCLASS_ATTACHED_OBJECT(klass);
2207 rb_name_err_raise_str(s, c, str);
2219 VALUE m = mnew_missing_by_name(klass, obj, &vid, scope, mclass);
2221 rb_method_name_error(klass, vid);
2223 return mnew_callable(klass, obj,
id, mclass, scope);
2268 return obj_method(obj, vid, FALSE);
2281 return obj_method(obj, vid, TRUE);
2285rb_obj_singleton_method_lookup(
VALUE arg)
2292rb_obj_singleton_method_lookup_fail(
VALUE arg1,
VALUE arg2)
2329 NIL_P(klass = RCLASS_ORIGIN(sc)) ||
2330 !
NIL_P(rb_special_singleton_class(obj))) {
2339 VALUE args[2] = {obj, vid};
2340 VALUE ruby_method =
rb_rescue(rb_obj_singleton_method_lookup, (
VALUE)args, rb_obj_singleton_method_lookup_fail,
Qfalse);
2342 struct METHOD *method = (
struct METHOD *)RTYPEDDATA_GET_DATA(ruby_method);
2345 VALUE method_class = method->iclass;
2349 if (lookup_class == method_class) {
2353 }
while (lookup_class && lookup_class != stop_class);
2359 rb_name_err_raise(
"undefined singleton method '%1$s' for '%2$s'",
2400 rb_method_name_error(mod, vid);
2413rb_mod_public_instance_method(
VALUE mod,
VALUE vid)
2417 rb_method_name_error(mod, vid);
2428 int is_method = FALSE;
2447 "wrong argument type %s (expected Proc/Method/UnboundMethod)",
2454 struct METHOD *method = (
struct METHOD *)RTYPEDDATA_GET_DATA(body);
2457 if (RCLASS_SINGLETON_P(method->me->owner)) {
2459 "can't bind singleton method to a different class");
2463 "bind argument must be a subclass of % "PRIsVALUE,
2467 rb_method_entry_set(mod,
id, method->me, scope_visi->method_visi);
2468 if (scope_visi->module_func) {
2474 VALUE procval = rb_proc_dup(body);
2475 if (vm_proc_iseq(procval) != NULL) {
2477 GetProcPtr(procval, proc);
2478 proc->is_lambda = TRUE;
2479 proc->is_from_method = TRUE;
2481 rb_add_method(mod,
id, VM_METHOD_TYPE_BMETHOD, (
void *)procval, scope_visi->method_visi);
2482 if (scope_visi->module_func) {
2483 rb_add_method(
rb_singleton_class(mod),
id, VM_METHOD_TYPE_BMETHOD, (
void *)body, METHOD_VISI_PUBLIC);
2531rb_mod_define_method(
int argc,
VALUE *argv,
VALUE mod)
2533 const rb_cref_t *cref = rb_vm_cref_in_context(mod, mod);
2538 scope_visi = CREF_SCOPE_VISI(cref);
2541 return rb_mod_define_method_with_visibility(argc, argv, mod, scope_visi);
2576rb_obj_define_method(
int argc,
VALUE *argv,
VALUE obj)
2581 return rb_mod_define_method_with_visibility(argc, argv, klass, &scope_visi);
2592top_define_method(
int argc,
VALUE *argv,
VALUE obj)
2594 return rb_mod_define_method(argc, argv, rb_top_main_class(
"define_method"));
2615method_clone(
VALUE self)
2618 struct METHOD *orig, *data;
2622 rb_obj_clone_setup(self, clone,
Qnil);
2627 RB_OBJ_WRITE(clone, &data->me, rb_method_entry_clone(orig->me));
2633method_dup(
VALUE self)
2636 struct METHOD *orig, *data;
2640 rb_obj_dup_setup(self, clone);
2645 RB_OBJ_WRITE(clone, &data->me, rb_method_entry_clone(orig->me));
2674rb_method_call_pass_called_kw(
int argc,
const VALUE *argv,
VALUE method)
2694method_callable_method_entry(
const struct METHOD *data)
2696 if (data->me->defined_class == 0) rb_bug(
"method_callable_method_entry: not callable.");
2702 int argc,
const VALUE *argv,
VALUE passed_procval,
int kw_splat)
2704 vm_passed_block_handler_set(ec, proc_to_block_handler(passed_procval));
2705 return rb_vm_call_kw(ec, data->recv, data->me->called_id, argc, argv,
2706 method_callable_method_entry(data), kw_splat);
2712 const struct METHOD *data;
2716 if (UNDEF_P(data->recv)) {
2717 rb_raise(
rb_eTypeError,
"can't call unbound method; bind first");
2719 return call_method_data(ec, data, argc, argv, passed_procval, kw_splat);
2786 VALUE methclass = data->owner;
2787 VALUE iclass = data->me->defined_class;
2791 VALUE refined_class = rb_refinement_module_get_refined_class(methclass);
2792 if (!
NIL_P(refined_class)) methclass = refined_class;
2795 if (RCLASS_SINGLETON_P(methclass)) {
2797 "singleton method called for a different object");
2800 rb_raise(
rb_eTypeError,
"bind argument must be an instance of % "PRIsVALUE,
2807 me = rb_method_entry_clone(data->me);
2817 me = rb_method_entry_clone(me);
2819 VALUE ic = rb_class_search_ancestor(klass, me->owner);
2825 klass = rb_include_class_new(methclass, klass);
2827 me = (
const rb_method_entry_t *) rb_method_entry_complement_defined_class(me, me->called_id, klass);
2830 *methclass_out = methclass;
2832 *iclass_out = iclass;
2874 VALUE methclass, klass, iclass;
2876 const struct METHOD *data;
2878 convert_umethod_to_method_components(data, recv, &methclass, &klass, &iclass, &me,
true);
2900umethod_bind_call(
int argc,
VALUE *argv,
VALUE method)
2903 VALUE recv = argv[0];
2910 const struct METHOD *data;
2915 vm_passed_block_handler_set(ec, proc_to_block_handler(passed_procval));
2919 VALUE methclass, klass, iclass;
2921 convert_umethod_to_method_components(data, recv, &methclass, &klass, &iclass, &me,
false);
2922 struct METHOD bound = { recv, klass, 0, methclass, me };
2937 if (!def)
return *max = 0;
2938 switch (def->type) {
2939 case VM_METHOD_TYPE_CFUNC:
2940 if (def->body.cfunc.argc < 0) {
2944 return *max = check_argc(def->body.cfunc.argc);
2945 case VM_METHOD_TYPE_ZSUPER:
2948 case VM_METHOD_TYPE_ATTRSET:
2950 case VM_METHOD_TYPE_IVAR:
2952 case VM_METHOD_TYPE_ALIAS:
2953 def = def->body.alias.original_me->def;
2955 case VM_METHOD_TYPE_BMETHOD:
2956 return rb_proc_min_max_arity(def->body.bmethod.proc, max);
2957 case VM_METHOD_TYPE_ISEQ:
2958 return rb_iseq_min_max_arity(rb_iseq_check(def->body.iseq.
iseqptr), max);
2959 case VM_METHOD_TYPE_UNDEF:
2960 case VM_METHOD_TYPE_NOTIMPLEMENTED:
2962 case VM_METHOD_TYPE_MISSING:
2965 case VM_METHOD_TYPE_OPTIMIZED: {
2966 switch (def->body.optimized.type) {
2967 case OPTIMIZED_METHOD_TYPE_SEND:
2970 case OPTIMIZED_METHOD_TYPE_CALL:
2973 case OPTIMIZED_METHOD_TYPE_BLOCK_CALL:
2976 case OPTIMIZED_METHOD_TYPE_STRUCT_AREF:
2979 case OPTIMIZED_METHOD_TYPE_STRUCT_ASET:
2987 case VM_METHOD_TYPE_REFINED:
2991 rb_bug(
"method_def_min_max_arity: invalid method entry type (%d)", def->type);
2998 int max, min = method_def_min_max_arity(def, &max);
2999 return min == max ? min : -min-1;
3005 return method_def_arity(me->def);
3052method_arity_m(
VALUE method)
3054 int n = method_arity(method);
3059method_arity(
VALUE method)
3064 return rb_method_entry_arity(data->me);
3068original_method_entry(
VALUE mod,
ID id)
3072 while ((me = rb_method_entry(mod,
id)) != 0) {
3074 if (def->type != VM_METHOD_TYPE_ZSUPER)
break;
3076 id = def->original_id;
3082method_min_max_arity(
VALUE method,
int *max)
3084 const struct METHOD *data;
3087 return method_def_min_max_arity(data->me->def, max);
3095 return rb_method_entry_arity(me);
3105rb_callable_receiver(
VALUE callable)
3108 VALUE binding = proc_binding(callable);
3109 return rb_funcall(binding, rb_intern(
"receiver"), 0);
3112 return method_receiver(callable);
3120rb_method_def(
VALUE method)
3122 const struct METHOD *data;
3125 return data->me->def;
3131 switch (def->type) {
3132 case VM_METHOD_TYPE_ISEQ:
3133 return rb_iseq_check(def->body.iseq.
iseqptr);
3134 case VM_METHOD_TYPE_BMETHOD:
3135 return rb_proc_get_iseq(def->body.bmethod.proc, 0);
3136 case VM_METHOD_TYPE_ALIAS:
3137 return method_def_iseq(def->body.alias.original_me->def);
3138 case VM_METHOD_TYPE_CFUNC:
3139 case VM_METHOD_TYPE_ATTRSET:
3140 case VM_METHOD_TYPE_IVAR:
3141 case VM_METHOD_TYPE_ZSUPER:
3142 case VM_METHOD_TYPE_UNDEF:
3143 case VM_METHOD_TYPE_NOTIMPLEMENTED:
3144 case VM_METHOD_TYPE_OPTIMIZED:
3145 case VM_METHOD_TYPE_MISSING:
3146 case VM_METHOD_TYPE_REFINED:
3153rb_method_iseq(
VALUE method)
3155 return method_def_iseq(rb_method_def(method));
3159method_cref(
VALUE method)
3164 switch (def->type) {
3165 case VM_METHOD_TYPE_ISEQ:
3166 return def->body.iseq.
cref;
3167 case VM_METHOD_TYPE_ALIAS:
3168 def = def->body.alias.original_me->def;
3178 if (def->type == VM_METHOD_TYPE_ATTRSET || def->type == VM_METHOD_TYPE_IVAR) {
3179 if (!def->body.attr.location)
3183 return iseq_location(method_def_iseq(def));
3189 if (!me)
return Qnil;
3190 return method_def_location(me->def);
3202rb_method_location(
VALUE method)
3204 return method_def_location(rb_method_def(method));
3208vm_proc_method_def(
VALUE procval)
3214 GetProcPtr(procval, proc);
3215 block = &proc->block;
3217 if (vm_block_type(block) == block_type_ifunc &&
3218 IS_METHOD_PROC_IFUNC(ifunc = block->as.captured.code.ifunc)) {
3219 return rb_method_def((
VALUE)ifunc->data);
3232 switch (def->type) {
3233 case VM_METHOD_TYPE_ISEQ:
3234 iseq = method_def_iseq(def);
3235 return rb_iseq_parameters(iseq, 0);
3236 case VM_METHOD_TYPE_BMETHOD:
3237 if ((iseq = method_def_iseq(def)) != NULL) {
3238 return rb_iseq_parameters(iseq, 0);
3240 else if ((bmethod_def = vm_proc_method_def(def->body.bmethod.proc)) != NULL) {
3241 return method_def_parameters(bmethod_def);
3245 case VM_METHOD_TYPE_ALIAS:
3246 return method_def_parameters(def->body.alias.original_me->def);
3248 case VM_METHOD_TYPE_OPTIMIZED:
3249 if (def->body.optimized.type == OPTIMIZED_METHOD_TYPE_STRUCT_ASET) {
3250 VALUE param = rb_ary_new_from_args(2,
ID2SYM(rb_intern(
"req")),
ID2SYM(rb_intern(
"_")));
3251 return rb_ary_new_from_args(1, param);
3255 case VM_METHOD_TYPE_CFUNC:
3256 case VM_METHOD_TYPE_ATTRSET:
3257 case VM_METHOD_TYPE_IVAR:
3258 case VM_METHOD_TYPE_ZSUPER:
3259 case VM_METHOD_TYPE_UNDEF:
3260 case VM_METHOD_TYPE_NOTIMPLEMENTED:
3261 case VM_METHOD_TYPE_MISSING:
3262 case VM_METHOD_TYPE_REFINED:
3266 return rb_unnamed_parameters(method_def_arity(def));
3290rb_method_parameters(
VALUE method)
3292 return method_def_parameters(rb_method_def(method));
3329method_inspect(
VALUE method)
3333 const char *sharp =
"#";
3335 VALUE defined_class;
3338 str = rb_sprintf(
"#<% "PRIsVALUE
": ",
rb_obj_class(method));
3340 mklass = data->iclass;
3341 if (!mklass) mklass = data->klass;
3351 if (data->me->def->type == VM_METHOD_TYPE_ALIAS) {
3352 defined_class = data->me->def->body.alias.original_me->owner;
3355 defined_class = method_entry_defined_class(data->me);
3362 if (UNDEF_P(data->recv)) {
3366 else if (RCLASS_SINGLETON_P(mklass)) {
3367 VALUE v = RCLASS_ATTACHED_OBJECT(mklass);
3369 if (UNDEF_P(data->recv)) {
3372 else if (data->recv == v) {
3385 mklass = data->klass;
3386 if (RCLASS_SINGLETON_P(mklass)) {
3387 VALUE v = RCLASS_ATTACHED_OBJECT(mklass);
3395 if (defined_class != mklass) {
3396 rb_str_catf(str,
"(% "PRIsVALUE
")", defined_class);
3401 if (data->me->called_id != data->me->def->original_id) {
3402 rb_str_catf(str,
"(%"PRIsVALUE
")",
3403 rb_id2str(data->me->def->original_id));
3405 if (data->me->def->type == VM_METHOD_TYPE_NOTIMPLEMENTED) {
3411 VALUE params = rb_method_parameters(method);
3412 VALUE pair, name, kind;
3418 const VALUE keyrest =
ID2SYM(rb_intern(
"keyrest"));
3421 const VALUE noblock =
ID2SYM(rb_intern(
"noblock"));
3436 for (
int i = 0; i <
RARRAY_LEN(params); i++) {
3444 if (kind == req || kind == opt) {
3447 else if (kind == rest || kind == keyrest) {
3450 else if (kind == block) {
3453 else if (kind == nokey) {
3456 else if (kind == noblock) {
3465 rb_str_catf(str,
"%"PRIsVALUE, name);
3467 else if (kind == opt) {
3468 rb_str_catf(str,
"%"PRIsVALUE
"=...", name);
3470 else if (kind == keyreq) {
3471 rb_str_catf(str,
"%"PRIsVALUE
":", name);
3473 else if (kind == key) {
3474 rb_str_catf(str,
"%"PRIsVALUE
": ...", name);
3476 else if (kind == rest) {
3477 if (name ==
ID2SYM(
'*')) {
3481 rb_str_catf(str,
"*%"PRIsVALUE, name);
3484 else if (kind == keyrest) {
3485 if (name !=
ID2SYM(idPow)) {
3486 rb_str_catf(str,
"**%"PRIsVALUE, name);
3495 else if (kind == block) {
3496 if (name ==
ID2SYM(
'&')) {
3505 rb_str_catf(str,
"&%"PRIsVALUE, name);
3508 else if (kind == nokey) {
3511 else if (kind == noblock) {
3523 VALUE loc = rb_method_location(method);
3525 rb_str_catf(str,
" %"PRIsVALUE
":%"PRIsVALUE,
3558method_to_proc(
VALUE method)
3572 procval =
rb_block_call(rb_mRubyVMFrozenCore, idLambda, 0, 0, bmcall, method);
3573 GetProcPtr(procval, proc);
3574 proc->is_from_method = 1;
3578extern VALUE rb_find_defined_class_by_owner(
VALUE current_class,
VALUE target_owner);
3589method_super_method(
VALUE method)
3591 const struct METHOD *data;
3592 VALUE super_class, iclass;
3597 iclass = data->iclass;
3598 if (!iclass)
return Qnil;
3599 if (data->me->def->type == VM_METHOD_TYPE_ALIAS && data->me->defined_class) {
3600 super_class =
RCLASS_SUPER(rb_find_defined_class_by_owner(data->me->defined_class,
3601 data->me->def->body.alias.original_me->owner));
3602 mid = data->me->def->body.alias.original_me->def->original_id;
3606 mid = data->me->def->original_id;
3608 if (!super_class)
return Qnil;
3609 me = (
rb_method_entry_t *)rb_callable_method_entry_with_refinements(super_class, mid, &iclass);
3610 if (!me)
return Qnil;
3611 return mnew_internal(me, me->owner, iclass, data->recv, mid,
rb_obj_class(method), FALSE, FALSE);
3621localjump_xvalue(
VALUE exc)
3635localjump_reason(
VALUE exc)
3640rb_cref_t *rb_vm_cref_new_toplevel(
void);
3649 VM_ASSERT(env->ep > env->env);
3650 VM_ASSERT(VM_ENV_ESCAPED_P(env->ep));
3653 cref = rb_vm_cref_new_toplevel();
3657 new_ep = &new_body[env->ep - env->env];
3658 new_env = vm_env_new(new_ep, new_body, env->env_size, env->iseq);
3665 new_ep[VM_ENV_DATA_INDEX_ENV] = (
VALUE)new_env;
3667 VM_ASSERT(VM_ENV_ESCAPED_P(new_ep));
3685proc_binding(
VALUE self)
3694 GetProcPtr(self, proc);
3695 block = &proc->block;
3697 if (proc->is_isolated) rb_raise(rb_eArgError,
"Can't create Binding from isolated Proc");
3700 switch (vm_block_type(block)) {
3701 case block_type_iseq:
3702 iseq = block->as.captured.code.iseq;
3703 binding_self = block->as.captured.self;
3704 env = VM_ENV_ENVVAL_PTR(block->as.captured.ep);
3706 case block_type_proc:
3707 GetProcPtr(block->as.proc, proc);
3708 block = &proc->block;
3710 case block_type_ifunc:
3712 const struct vm_ifunc *ifunc = block->as.captured.code.ifunc;
3713 if (IS_METHOD_PROC_IFUNC(ifunc)) {
3715 VALUE name = rb_fstring_lit(
"<empty_iseq>");
3717 binding_self = method_receiver(method);
3718 iseq = rb_method_iseq(method);
3719 env = VM_ENV_ENVVAL_PTR(block->as.captured.ep);
3720 env = env_clone(env, method_cref(method));
3722 empty = rb_iseq_new(
Qnil, name, name,
Qnil, 0, ISEQ_TYPE_TOP);
3728 case block_type_symbol:
3729 rb_raise(rb_eArgError,
"Can't create Binding from C level Proc");
3734 GetBindingPtr(bindval, bind);
3735 RB_OBJ_WRITE(bindval, &bind->block.as.captured.self, binding_self);
3736 RB_OBJ_WRITE(bindval, &bind->block.as.captured.code.iseq, env->iseq);
3737 rb_vm_block_ep_update(bindval, &bind->block, env->ep);
3741 rb_iseq_check(iseq);
3742 RB_OBJ_WRITE(bindval, &bind->pathobj, ISEQ_BODY(iseq)->location.pathobj);
3743 bind->first_lineno = ISEQ_BODY(iseq)->location.first_lineno;
3747 rb_iseq_pathobj_new(rb_fstring_lit(
"(binding)"),
Qnil));
3748 bind->first_lineno = 1;
3763 GetProcPtr(proc, procp);
3764 is_lambda = procp->is_lambda;
3768 GetProcPtr(proc, procp);
3769 procp->is_lambda = is_lambda;
3776 VALUE proc, passed, arity;
3785 if (!
NIL_P(blockarg)) {
3786 rb_warn(
"given block not used");
3788 arity = make_curry_proc(proc, passed, arity);
3843proc_curry(
int argc,
const VALUE *argv,
VALUE self)
3845 int sarity, max_arity, min_arity = rb_proc_min_max_arity(self, &max_arity);
3858 return make_curry_proc(self,
rb_ary_new(), arity);
3894rb_method_curry(
int argc,
const VALUE *argv,
VALUE self)
3896 VALUE proc = method_to_proc(self);
3897 return proc_curry(argc, argv, proc);
3915 return rb_funcallv(f, idCall, 1, &fargs);
3926 mesg = rb_fstring_lit(
"callable object is expected");
3950 return rb_proc_compose_to_left(self, to_callable(g));
3956 VALUE proc, args, procs[2];
3962 args = rb_ary_tmp_new_from_values(0, 2, procs);
3965 GetProcPtr(g, procp);
3966 is_lambda = procp->is_lambda;
3974 GetProcPtr(proc, procp);
3975 procp->is_lambda = is_lambda;
4009 return rb_proc_compose_to_right(self, to_callable(g));
4015 VALUE proc, args, procs[2];
4021 args = rb_ary_tmp_new_from_values(0, 2, procs);
4023 GetProcPtr(self, procp);
4024 is_lambda = procp->is_lambda;
4027 GetProcPtr(proc, procp);
4028 procp->is_lambda = is_lambda;
4049rb_method_compose_to_left(
VALUE self,
VALUE g)
4052 self = method_to_proc(self);
4053 return proc_compose_to_left(self, g);
4072rb_method_compose_to_right(
VALUE self,
VALUE g)
4075 self = method_to_proc(self);
4076 return proc_compose_to_right(self, g);
4112proc_ruby2_keywords(
VALUE procval)
4115 GetProcPtr(procval, proc);
4117 rb_check_frozen(procval);
4119 if (proc->is_from_method) {
4120 rb_warn(
"Skipping set of ruby2_keywords flag for proc (proc created from method)");
4124 switch (proc->block.type) {
4125 case block_type_iseq:
4126 if (ISEQ_BODY(proc->block.as.captured.code.iseq)->param.flags.has_rest &&
4127 !ISEQ_BODY(proc->block.as.captured.code.iseq)->param.flags.has_post &&
4128 !ISEQ_BODY(proc->block.as.captured.code.iseq)->param.flags.has_kw &&
4129 !ISEQ_BODY(proc->block.as.captured.code.iseq)->param.flags.has_kwrest) {
4130 ISEQ_BODY(proc->block.as.captured.code.iseq)->param.flags.ruby2_keywords = 1;
4133 rb_warn(
"Skipping set of ruby2_keywords flag for proc (proc accepts keywords or post arguments or proc does not accept argument splat)");
4137 rb_warn(
"Skipping set of ruby2_keywords flag for proc (proc not defined in Ruby)");
4559 rb_add_method_optimized(
rb_cProc, idCall, OPTIMIZED_METHOD_TYPE_CALL, 0, METHOD_VISI_PUBLIC);
4560 rb_add_method_optimized(
rb_cProc, rb_intern(
"[]"), OPTIMIZED_METHOD_TYPE_CALL, 0, METHOD_VISI_PUBLIC);
4561 rb_add_method_optimized(
rb_cProc, rb_intern(
"==="), OPTIMIZED_METHOD_TYPE_CALL, 0, METHOD_VISI_PUBLIC);
4562 rb_add_method_optimized(
rb_cProc, rb_intern(
"yield"), OPTIMIZED_METHOD_TYPE_CALL, 0, METHOD_VISI_PUBLIC);
4596 rb_vm_register_special_exception(ruby_error_sysstack,
rb_eSysStackError,
"stack level too deep");
4665 "define_method", top_define_method, -1);
4706 rb_gc_register_address(&sym_proc_cache);
#define RUBY_ASSERT(...)
Asserts that the given expression is truthy if and only if RUBY_DEBUG is truthy.
#define rb_define_method(klass, mid, func, arity)
Defines klass#mid.
#define rb_define_singleton_method(klass, mid, func, arity)
Defines klass.mid.
#define rb_define_private_method(klass, mid, func, arity)
Defines klass#mid and makes it private.
#define rb_define_global_function(mid, func, arity)
Defines rb_mKernel #mid.
VALUE rb_define_class(const char *name, VALUE super)
Defines a top-level class.
VALUE rb_singleton_class(VALUE obj)
Finds or creates the singleton class of the passed object.
VALUE rb_singleton_class_get(VALUE obj)
Returns the singleton class of obj, or nil if obj is not a singleton object.
void rb_define_alias(VALUE klass, const char *name1, const char *name2)
Defines an alias of a method.
void rb_undef_method(VALUE klass, const char *name)
Defines an undef of a method.
int rb_scan_args(int argc, const VALUE *argv, const char *fmt,...)
Retrieves argument from argc and argv to given VALUE references according to the format string.
int rb_block_given_p(void)
Determines if the current method is given a block.
int rb_get_kwargs(VALUE keyword_hash, const ID *table, int required, int optional, VALUE *values)
Keyword argument deconstructor.
#define rb_str_new2
Old name of rb_str_new_cstr.
#define rb_str_buf_cat2
Old name of rb_usascii_str_new_cstr.
#define Qundef
Old name of RUBY_Qundef.
#define INT2FIX
Old name of RB_INT2FIX.
#define ID2SYM
Old name of RB_ID2SYM.
#define OBJ_FREEZE
Old name of RB_OBJ_FREEZE.
#define UNREACHABLE_RETURN
Old name of RBIMPL_UNREACHABLE_RETURN.
#define SYM2ID
Old name of RB_SYM2ID.
#define ZALLOC
Old name of RB_ZALLOC.
#define CLASS_OF
Old name of rb_class_of.
#define rb_ary_new4
Old name of rb_ary_new_from_values.
#define FIX2INT
Old name of RB_FIX2INT.
#define T_MODULE
Old name of RUBY_T_MODULE.
#define ASSUME
Old name of RBIMPL_ASSUME.
#define T_ICLASS
Old name of RUBY_T_ICLASS.
#define ALLOC_N
Old name of RB_ALLOC_N.
#define rb_ary_new3
Old name of rb_ary_new_from_args.
#define Qtrue
Old name of RUBY_Qtrue.
#define ST2FIX
Old name of RB_ST2FIX.
#define Qnil
Old name of RUBY_Qnil.
#define Qfalse
Old name of RUBY_Qfalse.
#define NIL_P
Old name of RB_NIL_P.
#define T_CLASS
Old name of RUBY_T_CLASS.
#define BUILTIN_TYPE
Old name of RB_BUILTIN_TYPE.
#define Check_TypedStruct(v, t)
Old name of rb_check_typeddata.
#define CONST_ID
Old name of RUBY_CONST_ID.
#define rb_ary_new2
Old name of rb_ary_new_capa.
VALUE rb_eLocalJumpError
LocalJumpError exception.
void rb_exc_raise(VALUE mesg)
Raises an exception in the current thread.
VALUE rb_eStandardError
StandardError exception.
VALUE rb_eRangeError
RangeError exception.
VALUE rb_eTypeError
TypeError exception.
void rb_warn(const char *fmt,...)
Identical to rb_warning(), except it reports unless $VERBOSE is nil.
VALUE rb_exc_new_str(VALUE etype, VALUE str)
Identical to rb_exc_new_cstr(), except it takes a Ruby's string instead of C's.
VALUE rb_eException
Mother of all exceptions.
VALUE rb_eSysStackError
SystemStackError exception.
VALUE rb_class_superclass(VALUE klass)
Queries the parent of the given class.
VALUE rb_cUnboundMethod
UnboundMethod class.
VALUE rb_mKernel
Kernel module.
VALUE rb_cObject
Object class.
VALUE rb_cBinding
Binding class.
VALUE rb_obj_class(VALUE obj)
Queries the class of an object.
VALUE rb_inspect(VALUE obj)
Generates a human-readable textual representation of the given object.
VALUE rb_cModule
Module class.
VALUE rb_class_inherited_p(VALUE scion, VALUE ascendant)
Determines if the given two modules are relatives.
VALUE rb_obj_is_kind_of(VALUE obj, VALUE klass)
Queries if the given object is an instance (of possibly descendants) of the given class.
VALUE rb_cProc
Proc class.
VALUE rb_cMethod
Method class.
#define RB_OBJ_WRITTEN(old, oldv, young)
Identical to RB_OBJ_WRITE(), except it doesn't write any values, but only a WB declaration.
#define RB_OBJ_WRITE(old, slot, young)
Declaration of a "back" pointer.
VALUE rb_funcall(VALUE recv, ID mid, int n,...)
Calls a method.
VALUE rb_funcall_with_block_kw(VALUE recv, ID mid, int argc, const VALUE *argv, VALUE procval, int kw_splat)
Identical to rb_funcallv_with_block(), except you can specify how to handle the last element of the g...
VALUE rb_ary_dup(VALUE ary)
Duplicates an array.
VALUE rb_ary_plus(VALUE lhs, VALUE rhs)
Creates a new array, concatenating the former to the latter.
VALUE rb_ary_new(void)
Allocates a new, empty array.
VALUE rb_ary_hidden_new(long capa)
Allocates a hidden (no class) empty array.
VALUE rb_ary_push(VALUE ary, VALUE elem)
Special case of rb_ary_cat() that it adds only one element.
VALUE rb_ary_freeze(VALUE obj)
Freeze an array, preventing further modifications.
void rb_ary_store(VALUE ary, long key, VALUE val)
Destructively stores the passed value to the passed array's passed index.
#define UNLIMITED_ARGUMENTS
This macro is used in conjunction with rb_check_arity().
static int rb_check_arity(int argc, int min, int max)
Ensures that the passed integer is in the passed range.
int rb_is_local_id(ID id)
Classifies the given ID, then sees if it is a local variable.
VALUE rb_method_call_with_block(int argc, const VALUE *argv, VALUE recv, VALUE proc)
Identical to rb_proc_call(), except you can additionally pass a proc as a block.
int rb_obj_method_arity(VALUE obj, ID mid)
Identical to rb_mod_method_arity(), except it searches for singleton methods rather than instance met...
VALUE rb_proc_call(VALUE recv, VALUE args)
Evaluates the passed proc with the passed arguments.
VALUE rb_proc_call_with_block_kw(VALUE recv, int argc, const VALUE *argv, VALUE proc, int kw_splat)
Identical to rb_proc_call_with_block(), except you can specify how to handle the last element of the ...
VALUE rb_method_call_kw(int argc, const VALUE *argv, VALUE recv, int kw_splat)
Identical to rb_method_call(), except you can specify how to handle the last element of the given arr...
VALUE rb_obj_method(VALUE recv, VALUE mid)
Creates a method object.
VALUE rb_proc_lambda_p(VALUE recv)
Queries if the given object is a lambda.
VALUE rb_block_proc(void)
Constructs a Proc object from implicitly passed components.
VALUE rb_proc_call_with_block(VALUE recv, int argc, const VALUE *argv, VALUE proc)
Identical to rb_proc_call(), except you can additionally pass another proc object,...
int rb_mod_method_arity(VALUE mod, ID mid)
Queries the number of mandatory arguments of the method defined in the given module.
VALUE rb_method_call_with_block_kw(int argc, const VALUE *argv, VALUE recv, VALUE proc, int kw_splat)
Identical to rb_method_call_with_block(), except you can specify how to handle the last element of th...
VALUE rb_obj_is_method(VALUE recv)
Queries if the given object is a method.
VALUE rb_block_lambda(void)
Identical to rb_proc_new(), except it returns a lambda.
VALUE rb_proc_call_kw(VALUE recv, VALUE args, int kw_splat)
Identical to rb_proc_call(), except you can specify how to handle the last element of the given array...
VALUE rb_binding_new(void)
Snapshots the current execution context and turn it into an instance of rb_cBinding.
int rb_proc_arity(VALUE recv)
Queries the number of mandatory arguments of the given Proc.
VALUE rb_method_call(int argc, const VALUE *argv, VALUE recv)
Evaluates the passed method with the passed arguments.
VALUE rb_obj_is_proc(VALUE recv)
Queries if the given object is a proc.
#define rb_hash_uint(h, i)
Just another name of st_hash_uint.
#define rb_hash_end(h)
Just another name of st_hash_end.
VALUE rb_str_append(VALUE dst, VALUE src)
Identical to rb_str_buf_append(), except it converts the right hand side before concatenating.
VALUE rb_str_buf_append(VALUE dst, VALUE src)
Identical to rb_str_cat_cstr(), except it takes Ruby's string instead of C's.
void rb_str_set_len(VALUE str, long len)
Overwrites the length of the string.
st_index_t rb_hash_start(st_index_t i)
Starts a series of hashing.
#define rb_str_cat_cstr(buf, str)
Identical to rb_str_cat(), except it assumes the passed pointer is a pointer to a C string.
VALUE rb_str_intern(VALUE str)
Identical to rb_to_symbol(), except it assumes the receiver being an instance of RString.
void rb_undef_alloc_func(VALUE klass)
Deletes the allocator function of a class.
int rb_obj_respond_to(VALUE obj, ID mid, int private_p)
Identical to rb_respond_to(), except it additionally takes the visibility parameter.
ID rb_check_id(volatile VALUE *namep)
Detects if the given name is already interned or not.
ID rb_to_id(VALUE str)
Identical to rb_intern_str(), except it tries to convert the parameter object to an instance of rb_cS...
VALUE rb_iv_get(VALUE obj, const char *name)
Obtains an instance variable.
#define RB_INT2NUM
Just another name of rb_int2num_inline.
#define RB_BLOCK_CALL_FUNC_ARGLIST(yielded_arg, callback_arg)
Shim for block function parameters.
rb_block_call_func * rb_block_call_func_t
Shorthand type that represents an iterator-written-in-C function pointer.
VALUE rb_block_call_func(RB_BLOCK_CALL_FUNC_ARGLIST(yielded_arg, callback_arg))
This is the type of a function that the interpreter expect for C-backended blocks.
#define MEMCPY(p1, p2, type, n)
Handy macro to call memcpy.
#define RB_GC_GUARD(v)
Prevents premature destruction of local objects.
VALUE rb_block_call(VALUE q, ID w, int e, const VALUE *r, type *t, VALUE y)
Call a method with a block.
VALUE rb_proc_new(type *q, VALUE w)
Creates a rb_cProc instance.
VALUE rb_rescue(type *q, VALUE w, type *e, VALUE r)
An equivalent of rescue clause.
#define RARRAY_LEN
Just another name of rb_array_len.
static void RARRAY_ASET(VALUE ary, long i, VALUE v)
Assigns an object in an array.
static VALUE * RARRAY_PTR(VALUE ary)
Wild use of a C pointer.
#define RARRAY_AREF(a, i)
#define RARRAY_CONST_PTR
Just another name of rb_array_const_ptr.
static VALUE RBASIC_CLASS(VALUE obj)
Queries the class of an object.
#define RCLASS_SUPER
Just another name of rb_class_get_superclass.
#define RUBY_TYPED_DEFAULT_FREE
This is a value you can set to rb_data_type_struct::dfree.
#define RUBY_TYPED_FREE_IMMEDIATELY
Macros to see if each corresponding flag is defined.
#define TypedData_Get_Struct(obj, type, data_type, sval)
Obtains a C struct from inside of a wrapper Ruby object.
#define TypedData_Make_Struct(klass, type, data_type, sval)
Identical to TypedData_Wrap_Struct, except it allocates a new data region internally instead of takin...
const char * rb_obj_classname(VALUE obj)
Queries the name of the class of the passed object.
#define RB_PASS_CALLED_KEYWORDS
Pass keywords if current method is called with keywords, useful for argument delegation.
#define RB_NO_KEYWORDS
Do not pass keywords.
#define RTEST
This is an old name of RB_TEST.
#define _(args)
This was a transition path from K&R to ANSI.
Internal header for Ruby Box.
This is the struct that holds necessary info for a struct.
rb_cref_t * cref
class reference, should be marked
const rb_iseq_t * iseqptr
iseq pointer, should be separated from iseqval
IFUNC (Internal FUNCtion)
uintptr_t ID
Type that represents a Ruby identifier such as a variable name.
#define SIZEOF_VALUE
Identical to sizeof(VALUE), except it is a macro that can also be used inside of preprocessor directi...
uintptr_t VALUE
Type that represents a Ruby object.
static bool RB_TYPE_P(VALUE obj, enum ruby_value_type t)
Queries if the given object is of given type.