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"
28const rb_cref_t *rb_vm_cref_in_context(
VALUE self,
VALUE cbase);
38 const rb_method_entry_t *
const me;
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];
93cfunc_proc_inline_env_p(
const rb_proc_t *proc)
95 return proc->block.as.captured.ep == ((
const cfunc_proc_t *)proc)->env + VM_ENV_DATA_SIZE - 1;
99proc_memsize(
const void *
ptr)
115 0, 0, RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED
119rb_proc_alloc(
VALUE klass)
133proc_clone(
VALUE self)
135 VALUE procval = rb_proc_dup(self);
136 return rb_obj_clone_setup(self, procval,
Qnil);
143 VALUE procval = rb_proc_dup(self);
144 return rb_obj_dup_setup(self, procval);
254 GetProcPtr(procval, proc);
256 return RBOOL(proc->is_lambda);
262binding_free(
void *
ptr)
264 RUBY_FREE_ENTER(
"binding");
266 RUBY_FREE_LEAVE(
"binding");
270binding_mark_and_move(
void *
ptr)
274 block_mark_and_move((
struct rb_block *)&bind->block);
275 rb_gc_mark_and_move((
VALUE *)&bind->pathobj);
279binding_memsize(
const void *
ptr)
287 binding_mark_and_move,
290 binding_mark_and_move,
292 0, 0, RUBY_TYPED_WB_PROTECTED | RUBY_TYPED_FREE_IMMEDIATELY
296rb_binding_alloc(
VALUE klass)
302 rb_yjit_collect_binding_alloc();
308binding_copy(
VALUE self)
312 GetBindingPtr(self, src);
313 GetBindingPtr(bindval, dst);
314 rb_vm_block_copy(bindval, &dst->block, &src->block);
316 dst->first_lineno = src->first_lineno;
322binding_dup(
VALUE self)
324 return rb_obj_dup_setup(self, binding_copy(self));
329binding_clone(
VALUE self)
331 return rb_obj_clone_setup(self, binding_copy(self),
Qnil);
337 rb_execution_context_t *ec = GET_EC();
338 return rb_vm_make_binding(ec, ec->cfp);
386rb_f_binding(
VALUE self)
408bind_eval(
int argc,
VALUE *argv,
VALUE bindval)
412 rb_scan_args(argc, argv,
"12", &args[0], &args[2], &args[3]);
414 return rb_f_eval(argc+1, args,
Qnil );
418get_local_variable_ptr(
const rb_env_t **envp,
ID lid,
bool search_outer)
422 if (!VM_ENV_FLAGS(env->ep, VM_FRAME_FLAG_CFRAME)) {
423 if (VM_ENV_FLAGS(env->ep, VM_ENV_FLAG_ISOLATED)) {
427 const rb_iseq_t *iseq = env->iseq;
429 VM_ASSERT(rb_obj_is_iseq((
VALUE)iseq));
431 const unsigned int local_table_size = ISEQ_BODY(iseq)->local_table_size;
432 for (
unsigned int i=0; i<local_table_size; i++) {
433 if (ISEQ_BODY(iseq)->local_table[i] == lid) {
434 if (ISEQ_BODY(iseq)->local_iseq == iseq &&
435 ISEQ_BODY(iseq)->param.flags.has_block &&
436 (
unsigned int)ISEQ_BODY(iseq)->param.block_start == i) {
437 const VALUE *ep = env->ep;
438 if (!VM_ENV_FLAGS(ep, VM_FRAME_FLAG_MODIFIED_BLOCK_PARAM)) {
439 RB_OBJ_WRITE(env, &env->env[i], rb_vm_bh_to_procval(GET_EC(), VM_ENV_BLOCK_HANDLER(ep)));
440 VM_ENV_FLAGS_SET(ep, VM_FRAME_FLAG_MODIFIED_BLOCK_PARAM);
445 unsigned int last_lvar = env->env_size+VM_ENV_INDEX_LAST_LVAR
447 return &env->env[last_lvar - (local_table_size - i)];
455 }
while (search_outer && (env = rb_vm_env_prev_env(env)) != NULL);
467check_local_id(
VALUE bindval,
volatile VALUE *pname)
474 rb_name_err_raise(
"wrong local variable name '%1$s' for %2$s",
479 if (!rb_is_local_name(name)) {
480 rb_name_err_raise(
"wrong local variable name '%1$s' for %2$s",
507bind_local_variables(
VALUE bindval)
512 GetBindingPtr(bindval, bind);
513 env = VM_ENV_ENVVAL_PTR(vm_block_ep(&bind->block));
514 return rb_vm_env_local_variables(env);
518rb_numparam_id_p(
ID id)
520 return (tNUMPARAM_1 << ID_SCOPE_SHIFT) <=
id &&
id < ((tNUMPARAM_1 + 9) << ID_SCOPE_SHIFT);
524rb_implicit_param_p(
ID id)
526 return id == idItImplicit || rb_numparam_id_p(
id);
547bind_local_variable_get(
VALUE bindval,
VALUE sym)
549 ID lid = check_local_id(bindval, &sym);
554 if (!lid)
goto undefined;
555 if (rb_numparam_id_p(lid)) {
556 rb_name_err_raise(
"numbered parameter '%1$s' is not a local variable",
560 GetBindingPtr(bindval, bind);
562 env = VM_ENV_ENVVAL_PTR(vm_block_ep(&bind->block));
563 if ((
ptr = get_local_variable_ptr(&env, lid, TRUE)) != NULL) {
569 rb_name_err_raise(
"local variable '%1$s' is not defined for %2$s",
602 ID lid = check_local_id(bindval, &sym);
607 if (!lid) lid = rb_intern_str(sym);
608 if (rb_numparam_id_p(lid)) {
609 rb_name_err_raise(
"numbered parameter '%1$s' is not a local variable",
613 GetBindingPtr(bindval, bind);
614 env = VM_ENV_ENVVAL_PTR(vm_block_ep(&bind->block));
615 if ((
ptr = get_local_variable_ptr(&env, lid, TRUE)) == NULL) {
617 ptr = rb_binding_add_dynavars(bindval, bind, 1, &lid);
618 env = VM_ENV_ENVVAL_PTR(vm_block_ep(&bind->block));
622 rb_yjit_collect_binding_set();
648bind_local_variable_defined_p(
VALUE bindval,
VALUE sym)
650 ID lid = check_local_id(bindval, &sym);
655 if (rb_numparam_id_p(lid)) {
656 rb_name_err_raise(
"numbered parameter '%1$s' is not a local variable",
660 GetBindingPtr(bindval, bind);
661 env = VM_ENV_ENVVAL_PTR(vm_block_ep(&bind->block));
662 return RBOOL(get_local_variable_ptr(&env, lid, TRUE));
686bind_implicit_parameters(
VALUE bindval)
691 GetBindingPtr(bindval, bind);
692 env = VM_ENV_ENVVAL_PTR(vm_block_ep(&bind->block));
694 if (get_local_variable_ptr(&env, idItImplicit, FALSE)) {
695 return rb_ary_new_from_args(1,
ID2SYM(idIt));
698 env = VM_ENV_ENVVAL_PTR(vm_block_ep(&bind->block));
699 return rb_vm_env_numbered_parameters(env);
723bind_implicit_parameter_get(
VALUE bindval,
VALUE sym)
725 ID lid = check_local_id(bindval, &sym);
730 if (lid == idIt) lid = idItImplicit;
732 if (!lid || !rb_implicit_param_p(lid)) {
733 rb_name_err_raise(
"'%1$s' is not an implicit parameter",
737 GetBindingPtr(bindval, bind);
739 env = VM_ENV_ENVVAL_PTR(vm_block_ep(&bind->block));
740 if ((
ptr = get_local_variable_ptr(&env, lid, FALSE)) != NULL) {
744 if (lid == idItImplicit) lid = idIt;
745 rb_name_err_raise(
"implicit parameter '%1$s' is not defined for %2$s", bindval,
ID2SYM(lid));
773bind_implicit_parameter_defined_p(
VALUE bindval,
VALUE sym)
775 ID lid = check_local_id(bindval, &sym);
779 if (lid == idIt) lid = idItImplicit;
781 if (!lid || !rb_implicit_param_p(lid)) {
782 rb_name_err_raise(
"'%1$s' is not an implicit parameter",
786 GetBindingPtr(bindval, bind);
787 env = VM_ENV_ENVVAL_PTR(vm_block_ep(&bind->block));
788 return RBOOL(get_local_variable_ptr(&env, lid, FALSE));
798bind_receiver(
VALUE bindval)
801 GetBindingPtr(bindval, bind);
802 return vm_block_self(&bind->block);
812bind_location(
VALUE bindval)
816 GetBindingPtr(bindval, bind);
817 loc[0] = pathobj_path(bind->pathobj);
818 loc[1] =
INT2FIX(bind->first_lineno);
831 proc = &sproc->basic;
832 vm_block_type_set(&proc->block, block_type_ifunc);
834 *(
VALUE **)&proc->block.as.captured.ep = ep = sproc->env + VM_ENV_DATA_SIZE-1;
835 ep[VM_ENV_DATA_INDEX_FLAGS] = VM_FRAME_MAGIC_IFUNC | VM_FRAME_FLAG_CFRAME | VM_ENV_FLAG_LOCAL | VM_ENV_FLAG_ESCAPED;
836 ep[VM_ENV_DATA_INDEX_ME_CREF] =
Qfalse;
837 ep[VM_ENV_DATA_INDEX_SPECVAL] = VM_BLOCK_HANDLER_NONE;
838 ep[VM_ENV_DATA_INDEX_ENV] =
Qundef;
841 RB_OBJ_WRITE(procval, &proc->block.as.captured.code.ifunc, ifunc);
842 proc->is_lambda = TRUE;
847rb_func_proc_dup(
VALUE src_obj)
849 RUBY_ASSERT(rb_typeddata_is_instance_of(src_obj, &proc_data_type));
852 GetProcPtr(src_obj, src_proc);
853 RUBY_ASSERT(vm_block_type(&src_proc->block) == block_type_ifunc);
858 memcpy(&proc->basic, src_proc,
sizeof(
rb_proc_t));
862 const VALUE *src_ep = src_proc->block.as.captured.ep;
863 if (cfunc_proc_inline_env_p(src_proc)) {
864 VALUE *ep = *(
VALUE **)&proc->basic.block.as.captured.ep = proc->env + VM_ENV_DATA_SIZE - 1;
865 ep[VM_ENV_DATA_INDEX_FLAGS] = src_ep[VM_ENV_DATA_INDEX_FLAGS];
866 ep[VM_ENV_DATA_INDEX_ME_CREF] = src_ep[VM_ENV_DATA_INDEX_ME_CREF];
867 ep[VM_ENV_DATA_INDEX_SPECVAL] = src_ep[VM_ENV_DATA_INDEX_SPECVAL];
868 RB_OBJ_WRITE(proc_obj, &ep[VM_ENV_DATA_INDEX_ENV], src_ep[VM_ENV_DATA_INDEX_ENV]);
871 rb_vm_block_ep_update(proc_obj, &proc->basic.block, src_ep);
880 VALUE procval = rb_proc_alloc(klass);
882 GetProcPtr(procval, proc);
884 vm_block_type_set(&proc->block, block_type_symbol);
885 proc->is_lambda = TRUE;
895 min_argc >= (
int)(1U << (
SIZEOF_VALUE * CHAR_BIT) / 2) ||
898 rb_raise(
rb_eRangeError,
"minimum argument number out of range: %d",
903 max_argc >= (
int)(1U << (
SIZEOF_VALUE * CHAR_BIT) / 2) ||
906 rb_raise(
rb_eRangeError,
"maximum argument number out of range: %d",
909 rb_execution_context_t *ec = GET_EC();
911 struct vm_ifunc *ifunc = IMEMO_NEW(
struct vm_ifunc, imemo_ifunc, (
VALUE)rb_vm_svar_lep(ec, ec->cfp));
914 ifunc->argc.min = min_argc;
915 ifunc->argc.max = max_argc;
923 struct vm_ifunc *ifunc = rb_vm_ifunc_new(func, (
void *)val, min_argc, max_argc);
927static const char proc_without_block[] =
"tried to create Proc object without a block";
930proc_new(
VALUE klass, int8_t is_lambda)
933 const rb_execution_context_t *ec = GET_EC();
934 rb_control_frame_t *cfp = ec->cfp;
937 if ((block_handler = rb_vm_frame_block_handler(cfp)) == VM_BLOCK_HANDLER_NONE) {
938 rb_raise(rb_eArgError, proc_without_block);
942 switch (vm_block_handler_type(block_handler)) {
943 case block_handler_type_proc:
944 procval = VM_BH_TO_PROC(block_handler);
946 if (RBASIC_CLASS(procval) == klass) {
950 VALUE newprocval = rb_proc_dup(procval);
951 RBASIC_SET_CLASS(newprocval, klass);
956 case block_handler_type_symbol:
958 sym_proc_new(klass, VM_BH_TO_SYMBOL(block_handler)) :
959 rb_sym_to_proc(VM_BH_TO_SYMBOL(block_handler));
962 case block_handler_type_ifunc:
963 case block_handler_type_iseq:
964 return rb_vm_make_proc_lambda(ec, VM_BH_TO_CAPT_BLOCK(block_handler), klass, is_lambda);
966 VM_UNREACHABLE(proc_new);
985rb_proc_s_new(
int argc,
VALUE *argv,
VALUE klass)
987 VALUE block = proc_new(klass, FALSE);
1019f_lambda_filter_non_literal(
void)
1021 rb_control_frame_t *cfp = GET_EC()->cfp;
1022 VALUE block_handler = rb_vm_frame_block_handler(cfp);
1024 if (block_handler == VM_BLOCK_HANDLER_NONE) {
1029 switch (vm_block_handler_type(block_handler)) {
1030 case block_handler_type_iseq:
1031 if (RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp)->ep == VM_BH_TO_ISEQ_BLOCK(block_handler)->ep) {
1035 case block_handler_type_symbol:
1037 case block_handler_type_proc:
1042 case block_handler_type_ifunc:
1046 rb_raise(rb_eArgError,
"the lambda method requires a literal block");
1060 f_lambda_filter_non_literal();
1115proc_call(
int argc,
VALUE *argv,
VALUE procval)
1121#if SIZEOF_LONG > SIZEOF_INT
1123check_argc(
long argc)
1125 if (argc > INT_MAX || argc < 0) {
1126 rb_raise(rb_eArgError,
"too many arguments (%lu)",
1127 (
unsigned long)argc);
1132#define check_argc(argc) (argc)
1146 VALUE *argv = RARRAY_PTR(args);
1147 GetProcPtr(self, proc);
1148 vret = rb_vm_invoke_proc(GET_EC(), proc, argc, argv,
1149 kw_splat, VM_BLOCK_HANDLER_NONE);
1162proc_to_block_handler(
VALUE procval)
1164 return NIL_P(procval) ? VM_BLOCK_HANDLER_NONE : procval;
1170 rb_execution_context_t *ec = GET_EC();
1173 GetProcPtr(self, proc);
1174 vret = rb_vm_invoke_proc(ec, proc, argc, argv, kw_splat, proc_to_block_handler(passed_procval));
1228proc_arity(
VALUE self)
1235rb_iseq_min_max_arity(
const rb_iseq_t *iseq,
int *max)
1237 *max = ISEQ_BODY(iseq)->param.flags.has_rest == FALSE ?
1238 ISEQ_BODY(iseq)->param.lead_num + ISEQ_BODY(iseq)->param.opt_num + ISEQ_BODY(iseq)->param.post_num +
1239 (ISEQ_BODY(iseq)->param.flags.has_kw == TRUE || ISEQ_BODY(iseq)->param.flags.has_kwrest == TRUE || ISEQ_BODY(iseq)->param.flags.forwardable == TRUE)
1241 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);
1245rb_vm_block_min_max_arity(
const struct rb_block *block,
int *max)
1248 switch (vm_block_type(block)) {
1249 case block_type_iseq:
1250 return rb_iseq_min_max_arity(rb_iseq_check(block->as.captured.code.iseq), max);
1251 case block_type_proc:
1252 block = vm_proc_block(block->as.proc);
1254 case block_type_ifunc:
1256 const struct vm_ifunc *ifunc = block->as.captured.code.ifunc;
1257 if (IS_METHOD_PROC_IFUNC(ifunc)) {
1259 return method_min_max_arity((
VALUE)ifunc->data, max);
1261 *max = ifunc->argc.max;
1262 return ifunc->argc.min;
1264 case block_type_symbol:
1279rb_proc_min_max_arity(
VALUE self,
int *max)
1282 GetProcPtr(self, proc);
1283 return rb_vm_block_min_max_arity(&proc->block, max);
1291 GetProcPtr(self, proc);
1292 min = rb_vm_block_min_max_arity(&proc->block, &max);
1299 switch (vm_block_handler_type(block_handler)) {
1300 case block_handler_type_iseq:
1301 block->type = block_type_iseq;
1302 block->as.captured = *VM_BH_TO_ISEQ_BLOCK(block_handler);
1304 case block_handler_type_ifunc:
1305 block->type = block_type_ifunc;
1306 block->as.captured = *VM_BH_TO_IFUNC_BLOCK(block_handler);
1308 case block_handler_type_symbol:
1309 block->type = block_type_symbol;
1310 block->as.symbol = VM_BH_TO_SYMBOL(block_handler);
1312 case block_handler_type_proc:
1313 block->type = block_type_proc;
1314 block->as.proc = VM_BH_TO_PROC(block_handler);
1319rb_block_pair_yield_optimizable(
void)
1322 const rb_execution_context_t *ec = GET_EC();
1323 rb_control_frame_t *cfp = ec->cfp;
1324 VALUE block_handler = rb_vm_frame_block_handler(cfp);
1327 if (block_handler == VM_BLOCK_HANDLER_NONE) {
1328 rb_raise(rb_eArgError,
"no block given");
1331 block_setup(&block, block_handler);
1332 min = rb_vm_block_min_max_arity(&block, &max);
1334 switch (vm_block_type(&block)) {
1335 case block_type_symbol:
1338 case block_type_proc:
1340 VALUE procval = block_handler;
1342 GetProcPtr(procval, proc);
1343 if (proc->is_lambda)
return 0;
1344 if (min != max)
return 0;
1348 case block_type_ifunc:
1350 const struct vm_ifunc *ifunc = block.as.captured.code.ifunc;
1351 if (ifunc->flags & IFUNC_YIELD_OPTIMIZABLE)
return 1;
1363 const rb_execution_context_t *ec = GET_EC();
1364 rb_control_frame_t *cfp = ec->cfp;
1365 VALUE block_handler = rb_vm_frame_block_handler(cfp);
1368 if (block_handler == VM_BLOCK_HANDLER_NONE) {
1369 rb_raise(rb_eArgError,
"no block given");
1372 block_setup(&block, block_handler);
1374 switch (vm_block_type(&block)) {
1375 case block_type_symbol:
1378 case block_type_proc:
1382 min = rb_vm_block_min_max_arity(&block, &max);
1388rb_block_min_max_arity(
int *max)
1390 const rb_execution_context_t *ec = GET_EC();
1391 rb_control_frame_t *cfp = ec->cfp;
1392 VALUE block_handler = rb_vm_frame_block_handler(cfp);
1395 if (block_handler == VM_BLOCK_HANDLER_NONE) {
1396 rb_raise(rb_eArgError,
"no block given");
1399 block_setup(&block, block_handler);
1400 return rb_vm_block_min_max_arity(&block, max);
1404rb_proc_get_iseq(
VALUE self,
int *is_proc)
1409 GetProcPtr(self, proc);
1410 block = &proc->block;
1411 if (is_proc) *is_proc = !proc->is_lambda;
1413 switch (vm_block_type(block)) {
1414 case block_type_iseq:
1415 return rb_iseq_check(block->as.captured.code.iseq);
1416 case block_type_proc:
1417 return rb_proc_get_iseq(block->as.proc, is_proc);
1418 case block_type_ifunc:
1420 const struct vm_ifunc *ifunc = block->as.captured.code.ifunc;
1421 if (IS_METHOD_PROC_IFUNC(ifunc)) {
1423 if (is_proc) *is_proc = 0;
1424 return rb_method_iseq((
VALUE)ifunc->data);
1430 case block_type_symbol:
1434 VM_UNREACHABLE(rb_proc_get_iseq);
1473 const rb_proc_t *self_proc, *other_proc;
1474 const struct rb_block *self_block, *other_block;
1480 GetProcPtr(self, self_proc);
1481 GetProcPtr(other, other_proc);
1483 if (self_proc->is_from_method != other_proc->is_from_method ||
1484 self_proc->is_lambda != other_proc->is_lambda) {
1488 self_block = &self_proc->block;
1489 other_block = &other_proc->block;
1491 if (vm_block_type(self_block) != vm_block_type(other_block)) {
1495 switch (vm_block_type(self_block)) {
1496 case block_type_iseq:
1497 if (self_block->as.captured.ep != \
1498 other_block->as.captured.ep ||
1499 self_block->as.captured.code.iseq != \
1500 other_block->as.captured.code.iseq) {
1504 case block_type_ifunc:
1505 if (self_block->as.captured.code.ifunc != \
1506 other_block->as.captured.code.ifunc) {
1510 if (self_block->as.captured.ep != other_block->as.captured.ep) {
1511 if (!cfunc_proc_inline_env_p(self_proc) ||
1512 !cfunc_proc_inline_env_p(other_proc) ||
1521 case block_type_proc:
1522 if (self_block->as.proc != other_block->as.proc) {
1526 case block_type_symbol:
1527 if (self_block->as.symbol != other_block->as.symbol) {
1537iseq_location(
const rb_iseq_t *iseq)
1541 if (!iseq)
return Qnil;
1542 rb_iseq_check(iseq);
1543 loc[0] = rb_iseq_path(iseq);
1544 loc[1] =
RB_INT2NUM(ISEQ_BODY(iseq)->location.first_lineno);
1550rb_iseq_location(
const rb_iseq_t *iseq)
1552 return iseq_location(iseq);
1571rb_proc_location(
VALUE self)
1573 return iseq_location(rb_proc_get_iseq(self, 0));
1577rb_unnamed_parameters(
int arity)
1580 int n = (arity < 0) ? ~arity : arity;
1614rb_proc_parameters(
int argc,
VALUE *argv,
VALUE self)
1616 static ID keyword_ids[1];
1620 const rb_iseq_t *iseq;
1622 iseq = rb_proc_get_iseq(self, &is_proc);
1624 if (!keyword_ids[0]) {
1625 CONST_ID(keyword_ids[0],
"lambda");
1632 if (!
NIL_P(lambda)) {
1633 is_proc = !
RTEST(lambda);
1640 return rb_iseq_parameters(iseq, is_proc);
1644rb_hash_proc(st_index_t hash,
VALUE prc)
1647 GetProcPtr(prc, proc);
1649 switch (vm_block_type(&proc->block)) {
1650 case block_type_iseq:
1651 hash = rb_st_hash_uint(hash, (st_index_t)proc->block.as.captured.code.iseq->body);
1653 case block_type_ifunc:
1654 hash = rb_st_hash_uint(hash, (st_index_t)proc->block.as.captured.code.ifunc->func);
1655 hash = rb_st_hash_uint(hash, (st_index_t)proc->block.as.captured.code.ifunc->data);
1657 case block_type_symbol:
1658 hash = rb_st_hash_uint(hash, rb_any_hash(proc->block.as.symbol));
1660 case block_type_proc:
1661 hash = rb_st_hash_uint(hash, rb_any_hash(proc->block.as.proc));
1664 rb_bug(
"rb_hash_proc: unknown block type %d", vm_block_type(&proc->block));
1670 if (vm_block_type(&proc->block) != block_type_ifunc) {
1671 hash =
rb_hash_uint(hash, (st_index_t)proc->block.as.captured.ep);
1694rb_sym_to_proc(
VALUE sym)
1696 enum {SYM_PROC_CACHE_SIZE = 67};
1698 if (rb_ractor_main_p()) {
1699 if (!sym_proc_cache) {
1705 long index = (
id % SYM_PROC_CACHE_SIZE);
1707 if (
RTEST(procval)) {
1709 GetProcPtr(procval, proc);
1711 if (proc->block.as.symbol == sym) {
1716 procval = sym_proc_new(
rb_cProc, sym);
1717 RARRAY_ASET(sym_proc_cache, index, procval);
1722 return sym_proc_new(
rb_cProc, sym);
1736proc_hash(
VALUE self)
1740 hash = rb_hash_proc(hash, self);
1746rb_block_to_s(
VALUE self,
const struct rb_block *block,
const char *additional_info)
1749 VALUE str = rb_sprintf(
"#<%"PRIsVALUE
":", cname);
1752 switch (vm_block_type(block)) {
1753 case block_type_proc:
1754 block = vm_proc_block(block->as.proc);
1756 case block_type_iseq:
1758 const rb_iseq_t *iseq = rb_iseq_check(block->as.captured.code.iseq);
1759 rb_str_catf(str,
"%p %"PRIsVALUE
":%d", (
void *)self,
1761 ISEQ_BODY(iseq)->location.first_lineno);
1764 case block_type_symbol:
1765 rb_str_catf(str,
"%p(&%+"PRIsVALUE
")", (
void *)self, block->as.symbol);
1767 case block_type_ifunc:
1768 rb_str_catf(str,
"%p", (
void *)block->as.captured.code.ifunc);
1786proc_to_s(
VALUE self)
1789 GetProcPtr(self, proc);
1790 return rb_block_to_s(self, &proc->block, proc->is_lambda ?
" (lambda)" : NULL);
1802proc_to_proc(
VALUE self)
1808bm_mark_and_move(
void *
ptr)
1811 rb_gc_mark_and_move((
VALUE *)&data->recv);
1812 rb_gc_mark_and_move((
VALUE *)&data->klass);
1813 rb_gc_mark_and_move((
VALUE *)&data->iclass);
1814 rb_gc_mark_and_move((
VALUE *)&data->owner);
1815 rb_gc_mark_and_move_ptr((rb_method_entry_t **)&data->me);
1826 0, 0, RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED | RUBY_TYPED_EMBEDDABLE | RUBY_TYPED_FROZEN_SHAREABLE_NO_REC
1839 ID rmiss = idRespond_to_missing;
1841 if (UNDEF_P(obj))
return 0;
1842 if (rb_method_basic_definition_p(klass, rmiss))
return 0;
1852 rb_method_entry_t *me;
1853 rb_method_definition_t *def;
1859 def =
ZALLOC(rb_method_definition_t);
1860 def->type = VM_METHOD_TYPE_MISSING;
1861 def->original_id = id;
1863 me = rb_method_entry_create(
id, klass, METHOD_VISI_UNDEF, def);
1875 if (!respond_to_missing_p(klass, obj, vid, scope))
return Qfalse;
1876 return mnew_missing(klass, obj,
SYM2ID(vid), mclass);
1880mnew_internal(
const rb_method_entry_t *me,
VALUE klass,
VALUE iclass,
1885 const rb_method_entry_t *original_me = me;
1886 rb_method_visibility_t visi = METHOD_VISI_UNDEF;
1889 if (UNDEFINED_METHOD_ENTRY_P(me)) {
1890 if (respond_to_missing_p(klass, obj,
ID2SYM(
id), scope)) {
1891 return mnew_missing(klass, obj,
id, mclass);
1893 if (!error)
return Qnil;
1894 rb_print_undef(klass,
id, METHOD_VISI_UNDEF);
1896 if (visi == METHOD_VISI_UNDEF) {
1897 visi = METHOD_ENTRY_VISI(me);
1899 if (scope && (visi != METHOD_VISI_PUBLIC)) {
1900 if (!error)
return Qnil;
1901 rb_print_inaccessible(klass,
id, visi);
1904 if (me->def->type == VM_METHOD_TYPE_ZSUPER) {
1905 if (me->defined_class) {
1907 id = me->def->original_id;
1908 me = (rb_method_entry_t *)rb_callable_method_entry_with_refinements(klass,
id, &iclass);
1912 id = me->def->original_id;
1913 me = rb_method_entry_without_refinements(klass,
id, &iclass);
1929 RB_OBJ_WRITE(method, &data->owner, original_me->owner);
1936mnew_from_me(
const rb_method_entry_t *me,
VALUE klass,
VALUE iclass,
1939 return mnew_internal(me, klass, iclass, obj,
id, mclass, scope, TRUE);
1945 const rb_method_entry_t *me;
1949 me = (rb_method_entry_t *)rb_callable_method_entry_with_refinements(klass,
id, &iclass);
1950 return mnew_from_me(me, klass, iclass, obj,
id, mclass, scope);
1954mnew_unbound(
VALUE klass,
ID id,
VALUE mclass,
int scope)
1956 const rb_method_entry_t *me;
1959 me = rb_method_entry_with_refinements(klass,
id, &iclass);
1960 return mnew_from_me(me, klass, iclass,
Qundef,
id, mclass, scope);
1964method_entry_defined_class(
const rb_method_entry_t *me)
1966 VALUE defined_class = me->defined_class;
1967 return defined_class ? defined_class : me->owner;
2012 VALUE klass1, klass2;
2020 m1 = (
struct METHOD *)RTYPEDDATA_GET_DATA(method);
2021 m2 = (
struct METHOD *)RTYPEDDATA_GET_DATA(other);
2023 klass1 = method_entry_defined_class(m1->me);
2024 klass2 = method_entry_defined_class(m2->me);
2025 if (RB_TYPE_P(klass1,
T_ICLASS)) klass1 = RBASIC_CLASS(klass1);
2026 if (RB_TYPE_P(klass2,
T_ICLASS)) klass2 = RBASIC_CLASS(klass2);
2028 if (!rb_method_entry_eq(m1->me, m2->me) ||
2030 m1->klass != m2->klass ||
2031 m1->recv != m2->recv) {
2052#define unbound_method_eq method_eq
2064method_hash(
VALUE method)
2071 hash = rb_hash_method_entry(hash, m->me);
2087method_unbind(
VALUE obj)
2090 struct METHOD *orig, *data;
2094 &method_data_type, data);
2099 RB_OBJ_WRITE(method, &data->me, rb_method_entry_clone(orig->me));
2114method_receiver(
VALUE obj)
2130method_name(
VALUE obj)
2135 return ID2SYM(data->me->called_id);
2152method_original_name(
VALUE obj)
2157 return ID2SYM(data->me->def->original_id);
2179method_owner(
VALUE obj)
2193method_box(
VALUE obj)
2196 const rb_box_t *box;
2199 box = data->me->def->box;
2200 if (!box)
return Qnil;
2201 if (box->box_object)
return box->box_object;
2202 rb_bug(
"Unexpected box on the method definition: %p", (
void*) box);
2209#define MSG(s) rb_fstring_lit("undefined method '%1$s' for"s" '%2$s'")
2213 if (RCLASS_SINGLETON_P(c)) {
2214 VALUE obj = RCLASS_ATTACHED_OBJECT(klass);
2231 rb_name_err_raise_str(s, c, str);
2243 VALUE m = mnew_missing_by_name(klass, obj, &vid, scope, mclass);
2245 rb_method_name_error(klass, vid);
2247 return mnew_callable(klass, obj,
id, mclass, scope);
2292 return obj_method(obj, vid, FALSE);
2305 return obj_method(obj, vid, TRUE);
2309rb_obj_singleton_method_lookup(
VALUE arg)
2316rb_obj_singleton_method_lookup_fail(
VALUE arg1,
VALUE arg2)
2353 NIL_P(klass = RCLASS_ORIGIN(sc)) ||
2354 !
NIL_P(rb_special_singleton_class(obj))) {
2363 VALUE args[2] = {obj, vid};
2364 VALUE ruby_method =
rb_rescue(rb_obj_singleton_method_lookup, (
VALUE)args, rb_obj_singleton_method_lookup_fail,
Qfalse);
2366 struct METHOD *method = (
struct METHOD *)RTYPEDDATA_GET_DATA(ruby_method);
2367 VALUE lookup_class = RBASIC_CLASS(obj);
2369 VALUE method_class = method->iclass;
2373 if (lookup_class == method_class) {
2377 }
while (lookup_class && lookup_class != stop_class);
2382 if (
id) vid =
ID2SYM(
id);
2383 rb_name_err_raise(
"undefined singleton method '%1$s' for '%2$s'",
2424 rb_method_name_error(mod, vid);
2437rb_mod_public_instance_method(
VALUE mod,
VALUE vid)
2441 rb_method_name_error(mod, vid);
2452 int is_method = FALSE;
2454 rb_check_arity(argc, 1, 2);
2471 "wrong argument type %s (expected Proc/Method/UnboundMethod)",
2478 struct METHOD *method = (
struct METHOD *)RTYPEDDATA_GET_DATA(body);
2479 if (method->me->owner != mod && !RB_TYPE_P(method->me->owner,
T_MODULE) &&
2481 if (RCLASS_SINGLETON_P(method->me->owner)) {
2483 "can't bind singleton method to a different class");
2487 "bind argument must be a subclass of % "PRIsVALUE,
2491 rb_method_entry_set(mod,
id, method->me, scope_visi->method_visi);
2492 if (scope_visi->module_func) {
2498 VALUE procval = rb_proc_dup(body);
2499 if (vm_proc_iseq(procval) != NULL) {
2501 GetProcPtr(procval, proc);
2502 proc->is_lambda = TRUE;
2503 proc->is_from_method = TRUE;
2505 rb_add_method(mod,
id, VM_METHOD_TYPE_BMETHOD, (
void *)procval, scope_visi->method_visi);
2506 if (scope_visi->module_func) {
2507 rb_add_method(
rb_singleton_class(mod),
id, VM_METHOD_TYPE_BMETHOD, (
void *)body, METHOD_VISI_PUBLIC);
2555rb_mod_define_method(
int argc,
VALUE *argv,
VALUE mod)
2557 const rb_cref_t *cref = rb_vm_cref_in_context(mod, mod);
2558 const rb_scope_visibility_t default_scope_visi = {METHOD_VISI_PUBLIC, FALSE};
2559 const rb_scope_visibility_t *scope_visi = &default_scope_visi;
2562 scope_visi = CREF_SCOPE_VISI(cref);
2565 return rb_mod_define_method_with_visibility(argc, argv, mod, scope_visi);
2600rb_obj_define_method(
int argc,
VALUE *argv,
VALUE obj)
2603 const rb_scope_visibility_t scope_visi = {METHOD_VISI_PUBLIC, FALSE};
2605 return rb_mod_define_method_with_visibility(argc, argv, klass, &scope_visi);
2616top_define_method(
int argc,
VALUE *argv,
VALUE obj)
2618 return rb_mod_define_method(argc, argv, rb_top_main_class(
"define_method"));
2639method_clone(
VALUE self)
2642 struct METHOD *orig, *data;
2646 rb_obj_clone_setup(self, clone,
Qnil);
2651 RB_OBJ_WRITE(clone, &data->me, rb_method_entry_clone(orig->me));
2657method_dup(
VALUE self)
2660 struct METHOD *orig, *data;
2664 rb_obj_dup_setup(self, clone);
2669 RB_OBJ_WRITE(clone, &data->me, rb_method_entry_clone(orig->me));
2698rb_method_call_pass_called_kw(
int argc,
const VALUE *argv,
VALUE method)
2717static const rb_callable_method_entry_t *
2718method_callable_method_entry(
const struct METHOD *data)
2720 if (data->me->defined_class == 0) rb_bug(
"method_callable_method_entry: not callable.");
2721 return (
const rb_callable_method_entry_t *)data->me;
2725call_method_data(rb_execution_context_t *ec,
const struct METHOD *data,
2726 int argc,
const VALUE *argv,
VALUE passed_procval,
int kw_splat)
2728 vm_passed_block_handler_set(ec, proc_to_block_handler(passed_procval));
2729 return rb_vm_call_kw(ec, data->recv, data->me->called_id, argc, argv,
2730 method_callable_method_entry(data), kw_splat);
2736 const struct METHOD *data;
2737 rb_execution_context_t *ec = GET_EC();
2740 if (UNDEF_P(data->recv)) {
2741 rb_raise(
rb_eTypeError,
"can't call unbound method; bind first");
2743 return call_method_data(ec, data, argc, argv, passed_procval, kw_splat);
2808convert_umethod_to_method_components(
const struct METHOD *data,
VALUE recv,
VALUE *methclass_out,
VALUE *klass_out,
VALUE *iclass_out,
const rb_method_entry_t **me_out,
const bool clone)
2810 VALUE methclass = data->owner;
2811 VALUE iclass = data->me->defined_class;
2814 if (RB_TYPE_P(methclass,
T_MODULE)) {
2815 VALUE refined_class = rb_refinement_module_get_refined_class(methclass);
2816 if (!
NIL_P(refined_class)) methclass = refined_class;
2819 if (RCLASS_SINGLETON_P(methclass)) {
2821 "singleton method called for a different object");
2824 rb_raise(
rb_eTypeError,
"bind argument must be an instance of % "PRIsVALUE,
2829 const rb_method_entry_t *me;
2831 me = rb_method_entry_clone(data->me);
2837 if (RB_TYPE_P(me->owner,
T_MODULE)) {
2841 me = rb_method_entry_clone(me);
2843 VALUE ic = rb_class_search_ancestor(klass, me->owner);
2849 klass = rb_include_class_new(methclass, klass);
2851 me = (
const rb_method_entry_t *) rb_method_entry_complement_defined_class(me, me->called_id, klass);
2854 *methclass_out = methclass;
2856 *iclass_out = iclass;
2898 VALUE methclass, klass, iclass;
2899 const rb_method_entry_t *me;
2900 const struct METHOD *data;
2902 convert_umethod_to_method_components(data, recv, &methclass, &klass, &iclass, &me,
true);
2924umethod_bind_call(
int argc,
VALUE *argv,
VALUE method)
2927 VALUE recv = argv[0];
2932 rb_execution_context_t *ec = GET_EC();
2934 const struct METHOD *data;
2937 const rb_callable_method_entry_t *cme = rb_callable_method_entry(
CLASS_OF(recv), data->me->called_id);
2938 if (data->me == (
const rb_method_entry_t *)cme) {
2939 vm_passed_block_handler_set(ec, proc_to_block_handler(passed_procval));
2943 VALUE methclass, klass, iclass;
2944 const rb_method_entry_t *me;
2945 convert_umethod_to_method_components(data, recv, &methclass, &klass, &iclass, &me,
false);
2946 struct METHOD bound = { recv, klass, 0, methclass, me };
2958method_def_min_max_arity(
const rb_method_definition_t *def,
int *max)
2961 if (!def)
return *max = 0;
2962 switch (def->type) {
2963 case VM_METHOD_TYPE_CFUNC:
2964 if (def->body.cfunc.argc < 0) {
2968 return *max = check_argc(def->body.cfunc.argc);
2969 case VM_METHOD_TYPE_ZSUPER:
2972 case VM_METHOD_TYPE_ATTRSET:
2974 case VM_METHOD_TYPE_IVAR:
2976 case VM_METHOD_TYPE_ALIAS:
2977 def = def->body.alias.original_me->def;
2979 case VM_METHOD_TYPE_BMETHOD:
2980 return rb_proc_min_max_arity(def->body.bmethod.proc, max);
2981 case VM_METHOD_TYPE_ISEQ:
2982 return rb_iseq_min_max_arity(rb_iseq_check(def->body.iseq.iseqptr), max);
2983 case VM_METHOD_TYPE_UNDEF:
2984 case VM_METHOD_TYPE_NOTIMPLEMENTED:
2986 case VM_METHOD_TYPE_MISSING:
2989 case VM_METHOD_TYPE_OPTIMIZED: {
2990 switch (def->body.optimized.type) {
2991 case OPTIMIZED_METHOD_TYPE_SEND:
2994 case OPTIMIZED_METHOD_TYPE_CALL:
2997 case OPTIMIZED_METHOD_TYPE_BLOCK_CALL:
3000 case OPTIMIZED_METHOD_TYPE_STRUCT_AREF:
3003 case OPTIMIZED_METHOD_TYPE_STRUCT_ASET:
3011 case VM_METHOD_TYPE_REFINED:
3015 rb_bug(
"method_def_min_max_arity: invalid method entry type (%d)", def->type);
3020method_def_arity(
const rb_method_definition_t *def)
3022 int max, min = method_def_min_max_arity(def, &max);
3023 return min == max ? min : -min-1;
3027rb_method_entry_arity(
const rb_method_entry_t *me)
3029 return method_def_arity(me->def);
3076method_arity_m(
VALUE method)
3078 int n = method_arity(method);
3083method_arity(
VALUE method)
3088 return rb_method_entry_arity(data->me);
3091static const rb_method_entry_t *
3092original_method_entry(
VALUE mod,
ID id)
3094 const rb_method_entry_t *me;
3096 while ((me = rb_method_entry(mod,
id)) != 0) {
3097 const rb_method_definition_t *def = me->def;
3098 if (def->type != VM_METHOD_TYPE_ZSUPER)
break;
3100 id = def->original_id;
3106method_min_max_arity(
VALUE method,
int *max)
3108 const struct METHOD *data;
3111 return method_def_min_max_arity(data->me->def, max);
3117 const rb_method_entry_t *me = original_method_entry(mod,
id);
3119 return rb_method_entry_arity(me);
3129rb_callable_receiver(
VALUE callable)
3132 VALUE binding = proc_binding(callable);
3133 return rb_funcall(binding, rb_intern(
"receiver"), 0);
3136 return method_receiver(callable);
3143const rb_method_definition_t *
3144rb_method_def(
VALUE method)
3146 const struct METHOD *data;
3149 return data->me->def;
3152static const rb_iseq_t *
3153method_def_iseq(
const rb_method_definition_t *def)
3155 switch (def->type) {
3156 case VM_METHOD_TYPE_ISEQ:
3157 return rb_iseq_check(def->body.iseq.iseqptr);
3158 case VM_METHOD_TYPE_BMETHOD:
3159 return rb_proc_get_iseq(def->body.bmethod.proc, 0);
3160 case VM_METHOD_TYPE_ALIAS:
3161 return method_def_iseq(def->body.alias.original_me->def);
3162 case VM_METHOD_TYPE_CFUNC:
3163 case VM_METHOD_TYPE_ATTRSET:
3164 case VM_METHOD_TYPE_IVAR:
3165 case VM_METHOD_TYPE_ZSUPER:
3166 case VM_METHOD_TYPE_UNDEF:
3167 case VM_METHOD_TYPE_NOTIMPLEMENTED:
3168 case VM_METHOD_TYPE_OPTIMIZED:
3169 case VM_METHOD_TYPE_MISSING:
3170 case VM_METHOD_TYPE_REFINED:
3177rb_method_iseq(
VALUE method)
3179 return method_def_iseq(rb_method_def(method));
3182static const rb_cref_t *
3183method_cref(
VALUE method)
3185 const rb_method_definition_t *def = rb_method_def(method);
3188 switch (def->type) {
3189 case VM_METHOD_TYPE_ISEQ:
3190 return def->body.iseq.cref;
3191 case VM_METHOD_TYPE_ALIAS:
3192 def = def->body.alias.original_me->def;
3200method_def_location(
const rb_method_definition_t *def)
3202 if (def->type == VM_METHOD_TYPE_ATTRSET || def->type == VM_METHOD_TYPE_IVAR) {
3203 if (!def->body.attr.location)
3207 return iseq_location(method_def_iseq(def));
3211rb_method_entry_location(
const rb_method_entry_t *me)
3213 if (!me)
return Qnil;
3214 return method_def_location(me->def);
3233rb_method_location(
VALUE method)
3235 return method_def_location(rb_method_def(method));
3238static const rb_method_definition_t *
3239vm_proc_method_def(
VALUE procval)
3245 GetProcPtr(procval, proc);
3246 block = &proc->block;
3248 if (vm_block_type(block) == block_type_ifunc &&
3249 IS_METHOD_PROC_IFUNC(ifunc = block->as.captured.code.ifunc)) {
3250 return rb_method_def((
VALUE)ifunc->data);
3258method_def_parameters(
const rb_method_definition_t *def)
3260 const rb_iseq_t *iseq;
3261 const rb_method_definition_t *bmethod_def;
3263 switch (def->type) {
3264 case VM_METHOD_TYPE_ISEQ:
3265 iseq = method_def_iseq(def);
3266 return rb_iseq_parameters(iseq, 0);
3267 case VM_METHOD_TYPE_BMETHOD:
3268 if ((iseq = method_def_iseq(def)) != NULL) {
3269 return rb_iseq_parameters(iseq, 0);
3271 else if ((bmethod_def = vm_proc_method_def(def->body.bmethod.proc)) != NULL) {
3272 return method_def_parameters(bmethod_def);
3276 case VM_METHOD_TYPE_ALIAS:
3277 return method_def_parameters(def->body.alias.original_me->def);
3279 case VM_METHOD_TYPE_OPTIMIZED:
3280 if (def->body.optimized.type == OPTIMIZED_METHOD_TYPE_STRUCT_ASET) {
3281 VALUE param = rb_ary_new_from_args(2,
ID2SYM(rb_intern(
"req")),
ID2SYM(rb_intern(
"_")));
3282 return rb_ary_new_from_args(1, param);
3286 case VM_METHOD_TYPE_CFUNC:
3287 case VM_METHOD_TYPE_ATTRSET:
3288 case VM_METHOD_TYPE_IVAR:
3289 case VM_METHOD_TYPE_ZSUPER:
3290 case VM_METHOD_TYPE_UNDEF:
3291 case VM_METHOD_TYPE_NOTIMPLEMENTED:
3292 case VM_METHOD_TYPE_MISSING:
3293 case VM_METHOD_TYPE_REFINED:
3297 return rb_unnamed_parameters(method_def_arity(def));
3321rb_method_parameters(
VALUE method)
3323 return method_def_parameters(rb_method_def(method));
3360method_inspect(
VALUE method)
3364 const char *sharp =
"#";
3366 VALUE defined_class;
3369 str = rb_sprintf(
"#<% "PRIsVALUE
": ",
rb_obj_class(method));
3371 mklass = data->iclass;
3372 if (!mklass) mklass = data->klass;
3379 mklass = RBASIC_CLASS(mklass);
3382 if (data->me->def->type == VM_METHOD_TYPE_ALIAS) {
3383 defined_class = data->me->def->body.alias.original_me->owner;
3386 defined_class = method_entry_defined_class(data->me);
3389 if (RB_TYPE_P(defined_class,
T_ICLASS)) {
3390 defined_class = RBASIC_CLASS(defined_class);
3393 if (UNDEF_P(data->recv)) {
3397 else if (RCLASS_SINGLETON_P(mklass)) {
3398 VALUE v = RCLASS_ATTACHED_OBJECT(mklass);
3400 if (UNDEF_P(data->recv)) {
3403 else if (data->recv == v) {
3416 mklass = data->klass;
3418 mklass = RBASIC_CLASS(mklass);
3420 if (RCLASS_SINGLETON_P(mklass)) {
3421 VALUE v = RCLASS_ATTACHED_OBJECT(mklass);
3425 }
while (RB_TYPE_P(mklass,
T_ICLASS));
3429 if (defined_class != mklass) {
3430 rb_str_catf(str,
"(% "PRIsVALUE
")", defined_class);
3435 if (data->me->called_id != data->me->def->original_id) {
3436 rb_str_catf(str,
"(%"PRIsVALUE
")",
3437 rb_id2str(data->me->def->original_id));
3439 if (data->me->def->type == VM_METHOD_TYPE_NOTIMPLEMENTED) {
3445 VALUE params = rb_method_parameters(method);
3446 VALUE pair, name, kind;
3452 const VALUE keyrest =
ID2SYM(rb_intern(
"keyrest"));
3469 for (
int i = 0; i <
RARRAY_LEN(params); i++) {
3477 if (kind == req || kind == opt) {
3480 else if (kind == rest || kind == keyrest) {
3483 else if (kind == block) {
3486 else if (kind == nokey) {
3495 rb_str_catf(str,
"%"PRIsVALUE, name);
3497 else if (kind == opt) {
3498 rb_str_catf(str,
"%"PRIsVALUE
"=...", name);
3500 else if (kind == keyreq) {
3501 rb_str_catf(str,
"%"PRIsVALUE
":", name);
3503 else if (kind == key) {
3504 rb_str_catf(str,
"%"PRIsVALUE
": ...", name);
3506 else if (kind == rest) {
3507 if (name ==
ID2SYM(
'*')) {
3511 rb_str_catf(str,
"*%"PRIsVALUE, name);
3514 else if (kind == keyrest) {
3515 if (name !=
ID2SYM(idPow)) {
3516 rb_str_catf(str,
"**%"PRIsVALUE, name);
3525 else if (kind == block) {
3526 if (name ==
ID2SYM(
'&')) {
3535 rb_str_catf(str,
"&%"PRIsVALUE, name);
3538 else if (kind == nokey) {
3550 VALUE loc = rb_method_location(method);
3552 rb_str_catf(str,
" %"PRIsVALUE
":%"PRIsVALUE,
3585method_to_proc(
VALUE method)
3599 procval =
rb_block_call(rb_mRubyVMFrozenCore, idLambda, 0, 0, bmcall, method);
3600 GetProcPtr(procval, proc);
3601 proc->is_from_method = 1;
3605extern VALUE rb_find_defined_class_by_owner(
VALUE current_class,
VALUE target_owner);
3616method_super_method(
VALUE method)
3618 const struct METHOD *data;
3619 VALUE super_class, iclass;
3621 const rb_method_entry_t *me;
3624 iclass = data->iclass;
3625 if (!iclass)
return Qnil;
3626 if (data->me->def->type == VM_METHOD_TYPE_ALIAS && data->me->defined_class) {
3627 super_class =
RCLASS_SUPER(rb_find_defined_class_by_owner(data->me->defined_class,
3628 data->me->def->body.alias.original_me->owner));
3629 mid = data->me->def->body.alias.original_me->def->original_id;
3633 mid = data->me->def->original_id;
3635 if (!super_class)
return Qnil;
3636 me = (rb_method_entry_t *)rb_callable_method_entry_with_refinements(super_class, mid, &iclass);
3637 if (!me)
return Qnil;
3638 return mnew_internal(me, me->owner, iclass, data->recv, mid,
rb_obj_class(method), FALSE, FALSE);
3648localjump_xvalue(
VALUE exc)
3662localjump_reason(
VALUE exc)
3667rb_cref_t *rb_vm_cref_new_toplevel(
void);
3670env_clone(
const rb_env_t *env,
const rb_cref_t *cref)
3676 VM_ASSERT(env->ep > env->env);
3677 VM_ASSERT(VM_ENV_ESCAPED_P(env->ep));
3680 cref = rb_vm_cref_new_toplevel();
3684 new_ep = &new_body[env->ep - env->env];
3685 new_env = vm_env_new(new_ep, new_body, env->env_size, env->iseq);
3692 new_ep[VM_ENV_DATA_INDEX_ENV] = (
VALUE)new_env;
3694 VM_ASSERT(VM_ENV_ESCAPED_P(new_ep));
3712proc_binding(
VALUE self)
3717 const rb_iseq_t *iseq = NULL;
3721 GetProcPtr(self, proc);
3722 block = &proc->block;
3724 if (proc->is_isolated) rb_raise(rb_eArgError,
"Can't create Binding from isolated Proc");
3727 switch (vm_block_type(block)) {
3728 case block_type_iseq:
3729 iseq = block->as.captured.code.iseq;
3730 binding_self = block->as.captured.self;
3731 env = VM_ENV_ENVVAL_PTR(block->as.captured.ep);
3733 case block_type_proc:
3734 GetProcPtr(block->as.proc, proc);
3735 block = &proc->block;
3737 case block_type_ifunc:
3739 const struct vm_ifunc *ifunc = block->as.captured.code.ifunc;
3740 if (IS_METHOD_PROC_IFUNC(ifunc)) {
3742 VALUE name = rb_fstring_lit(
"<empty_iseq>");
3744 binding_self = method_receiver(method);
3745 iseq = rb_method_iseq(method);
3746 env = VM_ENV_ENVVAL_PTR(block->as.captured.ep);
3747 env = env_clone(env, method_cref(method));
3749 empty = rb_iseq_new(
Qnil, name, name,
Qnil, 0, ISEQ_TYPE_TOP);
3755 case block_type_symbol:
3756 rb_raise(rb_eArgError,
"Can't create Binding from C level Proc");
3761 GetBindingPtr(bindval, bind);
3762 RB_OBJ_WRITE(bindval, &bind->block.as.captured.self, binding_self);
3763 RB_OBJ_WRITE(bindval, &bind->block.as.captured.code.iseq, env->iseq);
3764 rb_vm_block_ep_update(bindval, &bind->block, env->ep);
3768 rb_iseq_check(iseq);
3769 RB_OBJ_WRITE(bindval, &bind->pathobj, ISEQ_BODY(iseq)->location.pathobj);
3770 bind->first_lineno = ISEQ_BODY(iseq)->location.first_lineno;
3774 rb_iseq_pathobj_new(rb_fstring_lit(
"(binding)"),
Qnil));
3775 bind->first_lineno = 1;
3790 GetProcPtr(proc, procp);
3791 is_lambda = procp->is_lambda;
3795 GetProcPtr(proc, procp);
3796 procp->is_lambda = is_lambda;
3803 VALUE proc, passed, arity;
3812 if (!
NIL_P(blockarg)) {
3813 rb_warn(
"given block not used");
3815 arity = make_curry_proc(proc, passed, arity);
3878proc_curry(
int argc,
const VALUE *argv,
VALUE self)
3880 int sarity, max_arity, min_arity = rb_proc_min_max_arity(self, &max_arity);
3883 if (rb_check_arity(argc, 0, 1) == 0 ||
NIL_P(arity = argv[0])) {
3889 rb_check_arity(sarity, min_arity, max_arity);
3894 return make_curry_proc(self,
rb_ary_new(), arity);
3930rb_method_curry(
int argc,
const VALUE *argv,
VALUE self)
3932 VALUE proc = method_to_proc(self);
3933 return proc_curry(argc, argv, proc);
3951 return rb_funcallv(f, idCall, 1, &fargs);
3962 mesg = rb_fstring_lit(
"callable object is expected");
3986 return rb_proc_compose_to_left(self, to_callable(g));
3992 VALUE proc, args, procs[2];
3998 args = rb_ary_tmp_new_from_values(0, 2, procs);
4001 GetProcPtr(g, procp);
4002 is_lambda = procp->is_lambda;
4010 GetProcPtr(proc, procp);
4011 procp->is_lambda = is_lambda;
4045 return rb_proc_compose_to_right(self, to_callable(g));
4051 VALUE proc, args, procs[2];
4057 args = rb_ary_tmp_new_from_values(0, 2, procs);
4059 GetProcPtr(self, procp);
4060 is_lambda = procp->is_lambda;
4063 GetProcPtr(proc, procp);
4064 procp->is_lambda = is_lambda;
4085rb_method_compose_to_left(
VALUE self,
VALUE g)
4088 self = method_to_proc(self);
4089 return proc_compose_to_left(self, g);
4108rb_method_compose_to_right(
VALUE self,
VALUE g)
4111 self = method_to_proc(self);
4112 return proc_compose_to_right(self, g);
4148proc_ruby2_keywords(
VALUE procval)
4151 GetProcPtr(procval, proc);
4153 rb_check_frozen(procval);
4155 if (proc->is_from_method) {
4156 rb_warn(
"Skipping set of ruby2_keywords flag for proc (proc created from method)");
4160 switch (proc->block.type) {
4161 case block_type_iseq:
4162 if (ISEQ_BODY(proc->block.as.captured.code.iseq)->param.flags.has_rest &&
4163 !ISEQ_BODY(proc->block.as.captured.code.iseq)->param.flags.has_post &&
4164 !ISEQ_BODY(proc->block.as.captured.code.iseq)->param.flags.has_kw &&
4165 !ISEQ_BODY(proc->block.as.captured.code.iseq)->param.flags.has_kwrest) {
4166 ISEQ_BODY(proc->block.as.captured.code.iseq)->param.flags.ruby2_keywords = 1;
4169 rb_warn(
"Skipping set of ruby2_keywords flag for proc (proc accepts keywords or post arguments or proc does not accept argument splat)");
4173 rb_warn(
"Skipping set of ruby2_keywords flag for proc (proc not defined in Ruby)");
4595 rb_add_method_optimized(
rb_cProc, idCall, OPTIMIZED_METHOD_TYPE_CALL, 0, METHOD_VISI_PUBLIC);
4596 rb_add_method_optimized(
rb_cProc, rb_intern(
"[]"), OPTIMIZED_METHOD_TYPE_CALL, 0, METHOD_VISI_PUBLIC);
4597 rb_add_method_optimized(
rb_cProc, rb_intern(
"==="), OPTIMIZED_METHOD_TYPE_CALL, 0, METHOD_VISI_PUBLIC);
4598 rb_add_method_optimized(
rb_cProc, rb_intern(
"yield"), OPTIMIZED_METHOD_TYPE_CALL, 0, METHOD_VISI_PUBLIC);
4632 rb_vm_register_special_exception(ruby_error_sysstack,
rb_eSysStackError,
"stack level too deep");
4701 "define_method", top_define_method, -1);
4742 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 NUM2INT
Old name of RB_NUM2INT.
#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.
int rb_typeddata_is_kind_of(VALUE obj, const rb_data_type_t *data_type)
Checks if the given object is of given kind.
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_rescue(VALUE(*b_proc)(VALUE), VALUE data1, VALUE(*r_proc)(VALUE, VALUE), VALUE data2)
Identical to rb_rescue2(), except it does not take a list of exception classes.
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_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().
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.
char * ptr
Pointer to the underlying memory region, of at least capa bytes.
#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.
#define RARRAY_LEN
Just another name of rb_array_len.
#define RARRAY_AREF(a, i)
#define RARRAY_CONST_PTR
Just another name of rb_array_const_ptr.
#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 TypedData_Get_Struct(obj, type, data_type, sval)
Obtains a C struct from inside of a wrapper Ruby object.
struct rb_data_type_struct rb_data_type_t
This is the struct that holds necessary info for a struct.
#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.
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.