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"
22#include "internal/vm.h"
26#include "ractor_core.h"
29const rb_cref_t *rb_vm_cref_in_context(
VALUE self,
VALUE cbase);
39 const rb_method_entry_t *
const me;
47static VALUE rb_cSourceRange;
50static int method_arity(
VALUE);
51static int method_min_max_arity(
VALUE,
int *max);
64source_range_memsize(
const void *ptr)
69RUBY_REFERENCES(source_range_refs) = {
78 RUBY_REFS_LIST_PTR(source_range_refs),
86rb_source_range_new(
VALUE path,
VALUE absolute_path,
const rb_code_location_t *location)
93 data->start_line = location->beg_pos.lineno;
94 data->start_column = location->beg_pos.column;
95 data->end_line = location->end_pos.lineno;
96 data->end_column = location->end_pos.column;
102source_range_new(
const rb_iseq_t *iseq)
109 VALUE path = rb_iseq_path(iseq);
110 VALUE absolute_path = rb_iseq_realpath(iseq);
115 return rb_source_range_new(path, absolute_path, &ISEQ_BODY(iseq)->location.code_location);
119source_range_data_get(
VALUE self)
134source_range_path(
VALUE self)
136 return source_range_data_get(self)->path;
147source_range_absolute_path(
VALUE self)
149 return source_range_data_get(self)->absolute_path;
159source_range_start_line(
VALUE self)
161 return INT2NUM(source_range_data_get(self)->start_line);
176source_range_start_column(
VALUE self)
178 return INT2NUM(source_range_data_get(self)->start_column);
197source_range_end_line(
VALUE self)
199 return INT2NUM(source_range_data_get(self)->end_line);
218source_range_end_column(
VALUE self)
220 return INT2NUM(source_range_data_get(self)->end_column);
231source_range_inspect(
VALUE self)
235 VALUE path =
NIL_P(data->absolute_path) ? data->path : data->absolute_path;
237 VM_ASSERT(!
NIL_P(path));
240 rb_str_catf(str,
":(%d,%d)-(%d,%d)>",
241 data->start_line, data->start_column,
242 data->end_line, data->end_column);
249#define IS_METHOD_PROC_IFUNC(ifunc) ((ifunc)->func == bmcall)
252block_mark_and_move(
struct rb_block *block)
254 switch (block->type) {
255 case block_type_iseq:
256 case block_type_ifunc:
259 rb_gc_mark_and_move(&captured->self);
260 rb_gc_mark_and_move(&captured->code.val);
262 rb_gc_mark_and_move((
VALUE *)&captured->ep[VM_ENV_DATA_INDEX_ENV]);
266 case block_type_symbol:
267 rb_gc_mark_and_move(&block->as.symbol);
269 case block_type_proc:
270 rb_gc_mark_and_move(&block->as.proc);
275static ID id_refinements_recipe;
278proc_mark_and_move(
void *ptr)
281 block_mark_and_move((
struct rb_block *)&proc->block);
284enum refinement_recipe_index {
285 REFINEMENT_RECIPE_BASE_CREF,
286 REFINEMENT_RECIPE_CREF,
287 REFINEMENT_RECIPE_SRC_ISEQ,
288 REFINEMENT_RECIPE_MODS
294 if (r1 == r2)
return true;
297 if (
RARRAY_AREF(r1, REFINEMENT_RECIPE_BASE_CREF) !=
298 RARRAY_AREF(r2, REFINEMENT_RECIPE_BASE_CREF))
return false;
300 RARRAY_AREF(r2, REFINEMENT_RECIPE_SRC_ISEQ))
return false;
301 for (
rb_len_t i = REFINEMENT_RECIPE_MODS; i <
len; i++) {
308rb_proc_refinements_recipe(
VALUE procval)
311 GetProcPtr(procval, proc);
312 if (!proc->header.is_refined)
return Qnil;
313 return rb_ivar_get(procval, id_refinements_recipe);
317rb_proc_set_refinements_recipe(
VALUE procval,
VALUE recipe)
320 GetProcPtr(procval, proc);
321 rb_ivar_set(procval, id_refinements_recipe, recipe);
322 proc->header.is_refined = 1;
327 VALUE env[VM_ENV_DATA_SIZE + 1];
331cfunc_proc_inline_env_p(
const rb_proc_t *proc)
333 return proc->block.as.captured.ep == ((
const cfunc_proc_t *)proc)->env + VM_ENV_DATA_SIZE - 1;
337proc_memsize(
const void *ptr)
340 switch (proc->block.type) {
341 case block_type_iseq:
342 case block_type_ifunc:
343 if (proc->block.as.captured.ep == ((
const cfunc_proc_t *)ptr)->env+1)
346 case block_type_symbol:
348 case block_type_proc:
351 VM_UNREACHABLE(proc_memsize);
363 0, 0, RUBY_TYPED_THREAD_SAFE_FREE | RUBY_TYPED_WB_PROTECTED
366#define proc_data_type ruby_proc_data_type
369rb_proc_alloc(
VALUE klass,
enum rb_block_type block_type)
372 switch (block_type) {
373 case block_type_symbol:
376 case block_type_proc:
379 case block_type_iseq:
380 case block_type_ifunc:
384 VM_UNREACHABLE(rb_proc_alloc);
388 return rb_data_typed_object_zalloc(klass, size, &proc_data_type);
394 return RBOOL(rb_typeddata_is_kind_of(proc, &proc_data_type));
399proc_clone(
VALUE self)
401 VALUE procval = rb_proc_dup_0(self);
402 return rb_obj_clone_setup(self, procval,
Qnil);
409 VALUE procval = rb_proc_dup_0(self);
410 return rb_obj_dup_setup(self, procval);
413rb_cref_t *rb_vm_get_cref(
const VALUE *ep);
414VALUE rb_proc_dup_with_iseq_and_recipe(
VALUE self,
const rb_iseq_t *iseq,
VALUE recipe);
431enum refinement_memo_index {
432 REFINEMENT_MEMO_RECIPE,
433 REFINEMENT_MEMO_COPIED_ISEQ
436static VALUE refinement_memo_map;
439refinement_memo_get(
const rb_iseq_t *src_iseq)
443 if (refinement_memo_map) {
444 memo = rb_hash_lookup(refinement_memo_map, (
VALUE)src_iseq);
451refinement_memo_set(
const rb_iseq_t *src_iseq,
VALUE recipe,
const rb_iseq_t *copied_iseq)
453 VM_ASSERT(ISEQ_BODY(src_iseq)->
type == ISEQ_TYPE_BLOCK);
465 if (!refinement_memo_map) {
470 if (!refinement_memo_map) {
473 rb_vm_register_global_object(new_map);
474 refinement_memo_map = new_map;
476 rb_hash_aset(refinement_memo_map, (
VALUE)src_iseq, memo);
481refinement_recipe_modc(
VALUE recipe)
483 return NIL_P(recipe) ? 0 :
RARRAY_LEN(recipe) - REFINEMENT_RECIPE_MODS;
487refinement_recipe_match(
VALUE recipe,
const rb_cref_t *base_cref,
VALUE src_recipe,
488 long argc,
const VALUE *mods)
490 rb_len_t inherited = refinement_recipe_modc(src_recipe);
491 if (
RARRAY_AREF(recipe, REFINEMENT_RECIPE_BASE_CREF) != (
VALUE)base_cref)
return false;
492 if (refinement_recipe_modc(recipe) != inherited + argc)
return false;
493 for (
rb_len_t i = 0; i < inherited; i++) {
494 if (
RARRAY_AREF(recipe, REFINEMENT_RECIPE_MODS + i) !=
495 RARRAY_AREF(src_recipe, REFINEMENT_RECIPE_MODS + i))
return false;
497 for (
long i = 0; i < argc; i++) {
498 if (
RARRAY_AREF(recipe, REFINEMENT_RECIPE_MODS + inherited + i) != mods[i])
return false;
504refinement_recipe_new(
const rb_cref_t *base_cref,
const rb_cref_t *cref,
505 const rb_iseq_t *src_iseq,
VALUE src_recipe,
506 long argc,
const VALUE *mods)
508 rb_len_t inherited = refinement_recipe_modc(src_recipe);
513 for (
rb_len_t i = 0; i < inherited; i++) {
516 for (
long i = 0; i < argc; i++) {
525refinement_memo_lookup(
const rb_iseq_t *src_iseq,
const rb_cref_t *base_cref,
VALUE src_recipe,
526 long argc,
const VALUE *mods)
528 VM_ASSERT(ISEQ_BODY(src_iseq)->
type == ISEQ_TYPE_BLOCK);
529 VALUE memo = refinement_memo_get(src_iseq);
532 if (!refinement_recipe_match(recipe, base_cref, src_recipe, argc, mods))
return Qnil;
536static const rb_iseq_t *
537refinement_iseq_copy(
VALUE recipe)
539 const rb_iseq_t *src_iseq =
540 (
const rb_iseq_t *)
RARRAY_AREF(recipe, REFINEMENT_RECIPE_SRC_ISEQ);
541 VALUE memo = refinement_memo_get(src_iseq);
543 if (refinement_recipe_eq(
RARRAY_AREF(memo, REFINEMENT_MEMO_RECIPE), recipe)) {
544 const rb_iseq_t *copied_iseq =
545 (
const rb_iseq_t *)
RARRAY_AREF(memo, REFINEMENT_MEMO_COPIED_ISEQ);
546 if (ISEQ_BODY(copied_iseq)->param.flags.ruby2_keywords ==
547 ISEQ_BODY(src_iseq)->param.flags.ruby2_keywords) {
552 "Proc#refined re-copies the block because the ruby2_keywords flag changed after the copy was memoized"
558 "Proc#refined called with different modules for the same block disables memoization"
564 const rb_iseq_t *copied_iseq = rb_iseq_dup_with_independent_caches(src_iseq);
565 refinement_memo_set(src_iseq, recipe, copied_iseq);
569NOINLINE(
static void refinement_iseq_install(
VALUE procval,
rb_proc_t *proc));
574 const rb_iseq_t *copied_iseq = refinement_iseq_copy(recipe);
577 if (!
FL_TEST_RAW((
VALUE)proc->block.as.captured.code.iseq, ISEQ_REFINED_COPY)) {
586 if (UNLIKELY(!
FL_TEST_RAW((
VALUE)proc->block.as.captured.code.iseq, ISEQ_REFINED_COPY))) {
587 refinement_iseq_install(procval, proc);
592rb_proc_refinements_cref_for_call(
VALUE procval)
595 GetProcPtr(procval, proc);
596 if (!proc->header.is_refined)
return NULL;
598 refinement_iseq_ensure(procval, proc);
600 return (
const rb_cref_t *)
RARRAY_AREF(recipe, REFINEMENT_RECIPE_CREF);
657proc_refined(
int argc,
VALUE *argv,
VALUE self)
660 GetProcPtr(self, src);
666 if (vm_block_type(&src->block) != block_type_iseq || src->header.is_from_method) {
667 rb_raise(rb_eArgError,
"can't apply refinements to a Proc without a Ruby block");
670 for (
int i = 0; i < argc; i++) {
674 const rb_iseq_t *src_iseq = src->block.as.captured.code.iseq;
675 VALUE src_recipe = rb_proc_refinements_recipe(self);
676 const rb_cref_t *src_cref, *base_cref;
677 if (
NIL_P(src_recipe)) {
678 src_cref = base_cref = rb_vm_get_cref(src->block.as.captured.ep);
683 src_cref = (
const rb_cref_t *)
RARRAY_AREF(src_recipe, REFINEMENT_RECIPE_CREF);
684 base_cref = (
const rb_cref_t *)
RARRAY_AREF(src_recipe, REFINEMENT_RECIPE_BASE_CREF);
694 "Proc#refined on a Proc whose block was already copied by Proc#refined is not memoized"
699 refinement_memo_lookup(src_iseq, base_cref, src_recipe, argc, argv);
701 rb_cref_t *cref = rb_vm_cref_dup(src_cref);
704 for (
int i = 0; i < argc; i++) {
705 rb_using_module_recursive(cref, argv[i]);
710 VALUE refs = CREF_REFINEMENTS(cref);
715 CREF_OMOD_SHARED_SET(cref);
716 CREF_REFINED_PROC_SET(cref);
717 recipe = refinement_recipe_new(base_cref, cref, src_iseq, src_recipe, argc, argv);
720 const rb_iseq_t *new_iseq = copied_src ?
721 rb_iseq_dup_with_independent_caches(src_iseq) : src_iseq;
723 return rb_proc_dup_with_iseq_and_recipe(self, new_iseq, recipe);
833 GetProcPtr(procval, proc);
835 return RBOOL(proc->header.is_lambda);
841binding_free(
void *ptr)
843 RUBY_FREE_ENTER(
"binding");
845 RUBY_FREE_LEAVE(
"binding");
849binding_mark_and_move(
void *ptr)
853 block_mark_and_move((
struct rb_block *)&bind->block);
854 rb_gc_mark_and_move((
VALUE *)&bind->pathobj);
858binding_memsize(
const void *ptr)
866 binding_mark_and_move,
869 binding_mark_and_move,
871 0, 0, RUBY_TYPED_WB_PROTECTED | RUBY_TYPED_THREAD_SAFE_FREE
875rb_binding_alloc(
VALUE klass)
881 rb_yjit_collect_binding_alloc();
887binding_copy(
VALUE self)
891 GetBindingPtr(self, src);
892 GetBindingPtr(bindval, dst);
893 rb_vm_block_copy(bindval, &dst->block, &src->block);
895 dst->first_lineno = src->first_lineno;
901binding_dup(
VALUE self)
903 return rb_obj_dup_setup(self, binding_copy(self));
908binding_clone(
VALUE self)
910 return rb_obj_clone_setup(self, binding_copy(self),
Qnil);
916 rb_execution_context_t *ec = GET_EC();
917 return rb_vm_make_binding(ec, ec->cfp);
965rb_f_binding(
VALUE self)
999bind_eval(
int argc,
VALUE *argv,
VALUE bindval)
1003 rb_scan_args(argc, argv,
"12", &args[0], &args[2], &args[3]);
1005 return rb_f_eval(argc+1, args,
Qnil );
1009get_local_variable_ptr(
const rb_env_t **envp,
ID lid,
bool search_outer)
1013 if (!VM_ENV_FLAGS(env->ep, VM_FRAME_FLAG_CFRAME)) {
1014 if (VM_ENV_FLAGS(env->ep, VM_ENV_FLAG_ISOLATED)) {
1018 const rb_iseq_t *iseq = env->iseq;
1020 VM_ASSERT(rb_obj_is_iseq((
VALUE)iseq));
1022 const unsigned int local_table_size = ISEQ_BODY(iseq)->local_table_size;
1023 for (
unsigned int i=0; i<local_table_size; i++) {
1024 if (ISEQ_BODY(iseq)->local_table[i] == lid) {
1025 if (ISEQ_BODY(iseq)->local_iseq == iseq &&
1026 ISEQ_BODY(iseq)->param.flags.has_block &&
1027 (
unsigned int)ISEQ_BODY(iseq)->param.block_start == i) {
1028 const VALUE *ep = env->ep;
1029 if (!VM_ENV_FLAGS(ep, VM_FRAME_FLAG_MODIFIED_BLOCK_PARAM)) {
1030 RB_OBJ_WRITE(env, &env->env[i], rb_vm_bh_to_procval(GET_EC(), VM_ENV_BLOCK_HANDLER(ep)));
1031 VM_ENV_FLAGS_SET(ep, VM_FRAME_FLAG_MODIFIED_BLOCK_PARAM);
1036 unsigned int last_lvar = env->env_size+VM_ENV_INDEX_LAST_LVAR
1038 return &env->env[last_lvar - (local_table_size - i)];
1046 }
while (search_outer && (env = rb_vm_env_prev_env(env)) != NULL);
1058check_local_id(
VALUE bindval,
volatile VALUE *pname)
1061 VALUE name = *pname;
1065 rb_name_err_raise(
"wrong local variable name '%1$s' for %2$s",
1070 if (!rb_is_local_name(name)) {
1071 rb_name_err_raise(
"wrong local variable name '%1$s' for %2$s",
1098bind_local_variables(
VALUE bindval)
1103 GetBindingPtr(bindval, bind);
1104 env = VM_ENV_ENVVAL_PTR(vm_block_ep(&bind->block));
1105 return rb_vm_env_local_variables(env);
1109rb_numparam_id_p(
ID id)
1111 return (tNUMPARAM_1 << ID_SCOPE_SHIFT) <=
id &&
id < ((tNUMPARAM_1 + 9) << ID_SCOPE_SHIFT);
1115rb_implicit_param_p(
ID id)
1117 return id == idItImplicit || rb_numparam_id_p(
id);
1138bind_local_variable_get(
VALUE bindval,
VALUE sym)
1140 ID lid = check_local_id(bindval, &sym);
1145 if (!lid)
goto undefined;
1146 if (rb_numparam_id_p(lid)) {
1147 rb_name_err_raise(
"numbered parameter '%1$s' is not a local variable",
1151 GetBindingPtr(bindval, bind);
1153 env = VM_ENV_ENVVAL_PTR(vm_block_ep(&bind->block));
1154 if ((ptr = get_local_variable_ptr(&env, lid, TRUE)) != NULL) {
1160 rb_name_err_raise(
"local variable '%1$s' is not defined for %2$s",
1193 ID lid = check_local_id(bindval, &sym);
1198 if (!lid) lid = rb_intern_str(sym);
1199 if (rb_numparam_id_p(lid)) {
1200 rb_name_err_raise(
"numbered parameter '%1$s' is not a local variable",
1204 GetBindingPtr(bindval, bind);
1205 env = VM_ENV_ENVVAL_PTR(vm_block_ep(&bind->block));
1206 if ((ptr = get_local_variable_ptr(&env, lid, TRUE)) == NULL) {
1208 ptr = rb_binding_add_dynavars(bindval, bind, 1, &lid);
1209 env = VM_ENV_ENVVAL_PTR(vm_block_ep(&bind->block));
1213 rb_yjit_collect_binding_set();
1239bind_local_variable_defined_p(
VALUE bindval,
VALUE sym)
1241 ID lid = check_local_id(bindval, &sym);
1246 if (rb_numparam_id_p(lid)) {
1247 rb_name_err_raise(
"numbered parameter '%1$s' is not a local variable",
1251 GetBindingPtr(bindval, bind);
1252 env = VM_ENV_ENVVAL_PTR(vm_block_ep(&bind->block));
1253 return RBOOL(get_local_variable_ptr(&env, lid, TRUE));
1277bind_implicit_parameters(
VALUE bindval)
1282 GetBindingPtr(bindval, bind);
1283 env = VM_ENV_ENVVAL_PTR(vm_block_ep(&bind->block));
1285 if (get_local_variable_ptr(&env, idItImplicit, FALSE)) {
1286 return rb_ary_new_from_args(1,
ID2SYM(idIt));
1289 env = VM_ENV_ENVVAL_PTR(vm_block_ep(&bind->block));
1290 return rb_vm_env_numbered_parameters(env);
1314bind_implicit_parameter_get(
VALUE bindval,
VALUE sym)
1316 ID lid = check_local_id(bindval, &sym);
1321 if (lid == idIt) lid = idItImplicit;
1323 if (!lid || !rb_implicit_param_p(lid)) {
1324 rb_name_err_raise(
"'%1$s' is not an implicit parameter",
1328 GetBindingPtr(bindval, bind);
1330 env = VM_ENV_ENVVAL_PTR(vm_block_ep(&bind->block));
1331 if ((ptr = get_local_variable_ptr(&env, lid, FALSE)) != NULL) {
1335 if (lid == idItImplicit) lid = idIt;
1336 rb_name_err_raise(
"implicit parameter '%1$s' is not defined for %2$s", bindval,
ID2SYM(lid));
1364bind_implicit_parameter_defined_p(
VALUE bindval,
VALUE sym)
1366 ID lid = check_local_id(bindval, &sym);
1370 if (lid == idIt) lid = idItImplicit;
1372 if (!lid || !rb_implicit_param_p(lid)) {
1373 rb_name_err_raise(
"'%1$s' is not an implicit parameter",
1377 GetBindingPtr(bindval, bind);
1378 env = VM_ENV_ENVVAL_PTR(vm_block_ep(&bind->block));
1379 return RBOOL(get_local_variable_ptr(&env, lid, FALSE));
1389bind_receiver(
VALUE bindval)
1392 GetBindingPtr(bindval, bind);
1393 return vm_block_self(&bind->block);
1403bind_location(
VALUE bindval)
1407 GetBindingPtr(bindval, bind);
1408 loc[0] = pathobj_path(bind->pathobj);
1409 loc[1] =
INT2NUM(bind->first_lineno);
1422 proc = &sproc->basic;
1423 vm_block_type_set(&proc->block, block_type_ifunc);
1425 *(
VALUE **)&proc->block.as.captured.ep = ep = sproc->env + VM_ENV_DATA_SIZE-1;
1426 ep[VM_ENV_DATA_INDEX_FLAGS] = VM_FRAME_MAGIC_IFUNC | VM_FRAME_FLAG_CFRAME | VM_ENV_FLAG_LOCAL | VM_ENV_FLAG_ESCAPED;
1427 ep[VM_ENV_DATA_INDEX_ME_CREF] =
Qfalse;
1428 ep[VM_ENV_DATA_INDEX_SPECVAL] = VM_BLOCK_HANDLER_NONE;
1429 ep[VM_ENV_DATA_INDEX_ENV] =
Qundef;
1432 RB_OBJ_WRITE(procval, &proc->block.as.captured.code.ifunc, ifunc);
1433 proc->header.is_lambda = TRUE;
1438rb_func_proc_dup(
VALUE src_obj)
1440 RUBY_ASSERT(rb_typeddata_is_instance_of(src_obj, &proc_data_type));
1443 GetProcPtr(src_obj, src_proc);
1444 RUBY_ASSERT(vm_block_type(&src_proc->block) == block_type_ifunc);
1449 memcpy(&proc->basic, src_proc,
sizeof(
rb_proc_t));
1453 const VALUE *src_ep = src_proc->block.as.captured.ep;
1454 if (cfunc_proc_inline_env_p(src_proc)) {
1455 VALUE *ep = *(
VALUE **)&proc->basic.block.as.captured.ep = proc->env + VM_ENV_DATA_SIZE - 1;
1456 ep[VM_ENV_DATA_INDEX_FLAGS] = src_ep[VM_ENV_DATA_INDEX_FLAGS];
1457 ep[VM_ENV_DATA_INDEX_ME_CREF] = src_ep[VM_ENV_DATA_INDEX_ME_CREF];
1458 ep[VM_ENV_DATA_INDEX_SPECVAL] = src_ep[VM_ENV_DATA_INDEX_SPECVAL];
1459 RB_OBJ_WRITE(proc_obj, &ep[VM_ENV_DATA_INDEX_ENV], src_ep[VM_ENV_DATA_INDEX_ENV]);
1462 rb_vm_block_ep_update(proc_obj, &proc->basic.block, src_ep);
1471 VALUE procval = rb_proc_alloc(klass, block_type_symbol);
1473 GetProcPtr(procval, proc);
1475 vm_block_type_set(&proc->block, block_type_symbol);
1476 proc->header.is_lambda = TRUE;
1486 min_argc >= (
int)(1U << (
SIZEOF_VALUE * CHAR_BIT) / 2) ||
1489 rb_raise(
rb_eRangeError,
"minimum argument number out of range: %d",
1494 max_argc >= (
int)(1U << (
SIZEOF_VALUE * CHAR_BIT) / 2) ||
1497 rb_raise(
rb_eRangeError,
"maximum argument number out of range: %d",
1500 rb_execution_context_t *ec = GET_EC();
1502 struct vm_ifunc *ifunc = IMEMO_NEW(
struct vm_ifunc, imemo_ifunc, (
VALUE)rb_vm_svar_lep(ec, ec->cfp));
1504 rb_gc_register_pinning_obj((
VALUE)ifunc);
1508 ifunc->argc.min = min_argc;
1509 ifunc->argc.max = max_argc;
1517 struct vm_ifunc *ifunc = rb_vm_ifunc_new(func, (
void *)val, min_argc, max_argc);
1521static const char proc_without_block[] =
"tried to create Proc object without a block";
1524proc_new(
VALUE klass, int8_t is_lambda)
1527 const rb_execution_context_t *ec = GET_EC();
1528 rb_control_frame_t *cfp = ec->cfp;
1529 VALUE block_handler;
1531 if ((block_handler = rb_vm_frame_block_handler(cfp)) == VM_BLOCK_HANDLER_NONE) {
1532 rb_raise(rb_eArgError, proc_without_block);
1536 switch (vm_block_handler_type(block_handler)) {
1537 case block_handler_type_proc:
1538 procval = VM_BH_TO_PROC(block_handler);
1544 VALUE newprocval = rb_proc_dup(procval);
1545 RBASIC_SET_CLASS(newprocval, klass);
1550 case block_handler_type_symbol:
1552 sym_proc_new(klass, VM_BH_TO_SYMBOL(block_handler)) :
1553 rb_sym_to_proc(VM_BH_TO_SYMBOL(block_handler));
1556 case block_handler_type_ifunc:
1557 case block_handler_type_iseq:
1558 return rb_vm_make_proc_lambda(ec, VM_BH_TO_CAPT_BLOCK(block_handler), klass, is_lambda);
1560 VM_UNREACHABLE(proc_new);
1579rb_proc_s_new(
int argc,
VALUE *argv,
VALUE klass)
1581 VALUE block = proc_new(klass, FALSE);
1613f_lambda_filter_non_literal(
void)
1615 rb_control_frame_t *cfp = GET_EC()->cfp;
1616 VALUE block_handler = rb_vm_frame_block_handler(cfp);
1618 if (block_handler == VM_BLOCK_HANDLER_NONE) {
1623 switch (vm_block_handler_type(block_handler)) {
1624 case block_handler_type_iseq:
1625 if (RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp)->ep == VM_BH_TO_ISEQ_BLOCK(block_handler)->ep) {
1629 case block_handler_type_symbol:
1631 case block_handler_type_proc:
1636 case block_handler_type_ifunc:
1640 rb_raise(rb_eArgError,
"the lambda method requires a literal block");
1654 f_lambda_filter_non_literal();
1709proc_call(
int argc,
VALUE *argv,
VALUE procval)
1715#if SIZEOF_RB_LEN_T > SIZEOF_INT
1719 if (argc > INT_MAX || argc < 0) {
1720 rb_raise(rb_eArgError,
"too many arguments (%"PRIuLEN
")",
1726#define check_argc(argc) (argc)
1741 GetProcPtr(self, proc);
1742 vret = rb_vm_invoke_proc(GET_EC(), proc, argc, argv,
1743 kw_splat, VM_BLOCK_HANDLER_NONE,
1744 rb_proc_refinements_cref_for_call(self));
1757proc_to_block_handler(
VALUE procval)
1759 return NIL_P(procval) ? VM_BLOCK_HANDLER_NONE : procval;
1765 rb_execution_context_t *ec = GET_EC();
1768 GetProcPtr(self, proc);
1769 vret = rb_vm_invoke_proc(ec, proc, argc, argv, kw_splat, proc_to_block_handler(passed_procval),
1770 rb_proc_refinements_cref_for_call(self));
1824proc_arity(
VALUE self)
1831rb_iseq_min_max_arity(
const rb_iseq_t *iseq,
int *max)
1833 *max = ISEQ_BODY(iseq)->param.flags.has_rest == FALSE ?
1834 ISEQ_BODY(iseq)->param.lead_num + ISEQ_BODY(iseq)->param.opt_num + ISEQ_BODY(iseq)->param.post_num +
1835 (ISEQ_BODY(iseq)->param.flags.has_kw == TRUE || ISEQ_BODY(iseq)->param.flags.has_kwrest == TRUE || ISEQ_BODY(iseq)->param.flags.forwardable == TRUE)
1837 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);
1841rb_vm_block_min_max_arity(
const struct rb_block *block,
int *max)
1844 switch (vm_block_type(block)) {
1845 case block_type_iseq:
1846 return rb_iseq_min_max_arity(rb_iseq_check(block->as.captured.code.iseq), max);
1847 case block_type_proc:
1848 block = vm_proc_block(block->as.proc);
1850 case block_type_ifunc:
1852 const struct vm_ifunc *ifunc = block->as.captured.code.ifunc;
1853 if (IS_METHOD_PROC_IFUNC(ifunc)) {
1855 return method_min_max_arity((
VALUE)ifunc->data, max);
1857 *max = ifunc->argc.max;
1858 return ifunc->argc.min;
1860 case block_type_symbol:
1875rb_proc_min_max_arity(
VALUE self,
int *max)
1878 GetProcPtr(self, proc);
1879 return rb_vm_block_min_max_arity(&proc->block, max);
1887 GetProcPtr(self, proc);
1888 min = rb_vm_block_min_max_arity(&proc->block, &max);
1895 switch (vm_block_handler_type(block_handler)) {
1896 case block_handler_type_iseq:
1897 block->type = block_type_iseq;
1898 block->as.captured = *VM_BH_TO_ISEQ_BLOCK(block_handler);
1900 case block_handler_type_ifunc:
1901 block->type = block_type_ifunc;
1902 block->as.captured = *VM_BH_TO_IFUNC_BLOCK(block_handler);
1904 case block_handler_type_symbol:
1905 block->type = block_type_symbol;
1906 block->as.symbol = VM_BH_TO_SYMBOL(block_handler);
1908 case block_handler_type_proc:
1909 block->type = block_type_proc;
1910 block->as.proc = VM_BH_TO_PROC(block_handler);
1915rb_block_pair_yield_optimizable(
void)
1918 const rb_execution_context_t *ec = GET_EC();
1919 rb_control_frame_t *cfp = ec->cfp;
1920 VALUE block_handler = rb_vm_frame_block_handler(cfp);
1923 if (block_handler == VM_BLOCK_HANDLER_NONE) {
1924 rb_raise(rb_eArgError,
"no block given");
1927 block_setup(&block, block_handler);
1928 min = rb_vm_block_min_max_arity(&block, &max);
1930 switch (vm_block_type(&block)) {
1931 case block_type_symbol:
1934 case block_type_proc:
1936 VALUE procval = block_handler;
1938 GetProcPtr(procval, proc);
1939 if (proc->header.is_lambda)
return 0;
1940 if (min != max)
return 0;
1944 case block_type_ifunc:
1946 const struct vm_ifunc *ifunc = block.as.captured.code.ifunc;
1947 if (ifunc->flags & IFUNC_YIELD_OPTIMIZABLE)
return 1;
1959 const rb_execution_context_t *ec = GET_EC();
1960 rb_control_frame_t *cfp = ec->cfp;
1961 VALUE block_handler = rb_vm_frame_block_handler(cfp);
1964 if (block_handler == VM_BLOCK_HANDLER_NONE) {
1965 rb_raise(rb_eArgError,
"no block given");
1968 block_setup(&block, block_handler);
1970 switch (vm_block_type(&block)) {
1971 case block_type_symbol:
1974 case block_type_proc:
1978 min = rb_vm_block_min_max_arity(&block, &max);
1984rb_block_min_max_arity(
int *max)
1986 const rb_execution_context_t *ec = GET_EC();
1987 rb_control_frame_t *cfp = ec->cfp;
1988 VALUE block_handler = rb_vm_frame_block_handler(cfp);
1991 if (block_handler == VM_BLOCK_HANDLER_NONE) {
1992 rb_raise(rb_eArgError,
"no block given");
1995 block_setup(&block, block_handler);
1996 return rb_vm_block_min_max_arity(&block, max);
2000rb_proc_get_iseq(
VALUE self,
int *is_proc)
2005 GetProcPtr(self, proc);
2006 block = &proc->block;
2007 if (is_proc) *is_proc = !proc->header.is_lambda;
2009 switch (vm_block_type(block)) {
2010 case block_type_iseq:
2011 return rb_iseq_check(block->as.captured.code.iseq);
2012 case block_type_proc:
2013 return rb_proc_get_iseq(block->as.proc, is_proc);
2014 case block_type_ifunc:
2016 const struct vm_ifunc *ifunc = block->as.captured.code.ifunc;
2017 if (IS_METHOD_PROC_IFUNC(ifunc)) {
2019 if (is_proc) *is_proc = 0;
2020 return rb_method_iseq((
VALUE)ifunc->data);
2026 case block_type_symbol:
2030 VM_UNREACHABLE(rb_proc_get_iseq);
2069 const rb_proc_t *self_proc, *other_proc;
2070 const struct rb_block *self_block, *other_block;
2076 GetProcPtr(self, self_proc);
2077 GetProcPtr(other, other_proc);
2079 if (self_proc->header.is_from_method != other_proc->header.is_from_method ||
2080 self_proc->header.is_lambda != other_proc->header.is_lambda ||
2081 self_proc->header.is_refined != other_proc->header.is_refined) {
2085 self_block = &self_proc->block;
2086 other_block = &other_proc->block;
2088 if (vm_block_type(self_block) != vm_block_type(other_block)) {
2092 switch (vm_block_type(self_block)) {
2093 case block_type_iseq:
2094 if (self_block->as.captured.ep != \
2095 other_block->as.captured.ep) {
2100 if (self_proc->header.is_refined) {
2101 if (!refinement_recipe_eq(rb_proc_refinements_recipe(self),
2102 rb_proc_refinements_recipe(other))) {
2106 else if (self_block->as.captured.code.iseq != \
2107 other_block->as.captured.code.iseq) {
2111 case block_type_ifunc:
2112 if (self_block->as.captured.code.ifunc != \
2113 other_block->as.captured.code.ifunc) {
2117 if (self_block->as.captured.ep != other_block->as.captured.ep) {
2118 if (!cfunc_proc_inline_env_p(self_proc) ||
2119 !cfunc_proc_inline_env_p(other_proc) ||
2128 case block_type_proc:
2129 if (self_block->as.proc != other_block->as.proc) {
2133 case block_type_symbol:
2134 if (self_block->as.symbol != other_block->as.symbol) {
2144iseq_location(
const rb_iseq_t *iseq)
2148 if (!iseq)
return Qnil;
2149 rb_iseq_check(iseq);
2150 loc[0] = rb_iseq_path(iseq);
2151 loc[1] =
RB_INT2NUM(ISEQ_BODY(iseq)->location.first_lineno);
2157rb_iseq_location(
const rb_iseq_t *iseq)
2159 return iseq_location(iseq);
2171rb_proc_location(
VALUE self)
2173 return iseq_location(rb_proc_get_iseq(self, 0));
2190rb_proc_source_range(
VALUE self)
2192 return source_range_new(rb_proc_get_iseq(self, 0));
2196rb_unnamed_parameters(
int arity)
2199 int n = (arity < 0) ? ~arity : arity;
2233rb_proc_parameters(
int argc,
VALUE *argv,
VALUE self)
2235 static ID keyword_ids[1];
2239 const rb_iseq_t *iseq;
2241 iseq = rb_proc_get_iseq(self, &is_proc);
2243 if (!keyword_ids[0]) {
2244 CONST_ID(keyword_ids[0],
"lambda");
2251 if (!
NIL_P(lambda)) {
2252 is_proc = !
RTEST(lambda);
2259 return rb_iseq_parameters(iseq, is_proc);
2263iseq_location_hash(st_index_t hash,
const rb_iseq_t *iseq)
2267 const rb_iseq_location_t *loc = &body->location;
2268 hash = rb_st_hash_uint(hash, (st_index_t)loc->code_location.beg_pos.lineno);
2269 hash = rb_st_hash_uint(hash, (st_index_t)loc->code_location.beg_pos.column);
2270 hash = rb_st_hash_uint(hash, (st_index_t)loc->code_location.end_pos.lineno);
2271 hash = rb_st_hash_uint(hash, (st_index_t)loc->code_location.end_pos.column);
2277rb_hash_proc(st_index_t hash,
VALUE prc)
2280 GetProcPtr(prc, proc);
2282 switch (vm_block_type(&proc->block)) {
2283 case block_type_iseq:
2284 if (proc->header.is_refined) {
2287 VALUE recipe = rb_proc_refinements_recipe(prc);
2289 hash = rb_st_hash_uint(hash, (st_index_t)
RARRAY_AREF(recipe, REFINEMENT_RECIPE_BASE_CREF));
2290 hash = iseq_location_hash(hash, (
const rb_iseq_t *)
RARRAY_AREF(recipe, REFINEMENT_RECIPE_SRC_ISEQ));
2291 for (
rb_len_t i = REFINEMENT_RECIPE_MODS; i <
len; i++) {
2292 hash = rb_st_hash_uint(hash, (st_index_t)
RARRAY_AREF(recipe, i));
2296 hash = iseq_location_hash(hash, proc->block.as.captured.code.iseq);
2299 case block_type_ifunc:
2300 hash = rb_st_hash_uint(hash, (st_index_t)proc->block.as.captured.code.ifunc->func);
2301 hash = rb_st_hash_uint(hash, (st_index_t)proc->block.as.captured.code.ifunc->data);
2303 case block_type_symbol:
2304 hash = rb_st_hash_uint(hash, rb_any_hash(proc->block.as.symbol));
2306 case block_type_proc:
2307 hash = rb_st_hash_uint(hash, rb_any_hash(proc->block.as.proc));
2310 rb_bug(
"rb_hash_proc: unknown block type %d", vm_block_type(&proc->block));
2317 if (vm_block_type(&proc->block) == block_type_iseq) {
2318 hash =
rb_hash_uint(hash, (st_index_t)proc->block.as.captured.ep);
2341rb_sym_to_proc(
VALUE sym)
2343 enum {SYM_PROC_CACHE_SIZE = 67};
2345 if (rb_ractor_main_p()) {
2346 if (!sym_proc_cache) {
2352 rb_len_t index = (
id % SYM_PROC_CACHE_SIZE);
2354 if (
RTEST(procval)) {
2356 GetProcPtr(procval, proc);
2358 if (proc->block.as.symbol == sym) {
2363 procval = sym_proc_new(
rb_cProc, sym);
2369 return sym_proc_new(
rb_cProc, sym);
2383proc_hash(
VALUE self)
2387 hash = rb_hash_proc(hash, self);
2393rb_block_to_s(
VALUE self,
const struct rb_block *block,
const char *additional_info)
2396 VALUE str = rb_sprintf(
"#<%"PRIsVALUE
":", cname);
2399 switch (vm_block_type(block)) {
2400 case block_type_proc:
2401 block = vm_proc_block(block->as.proc);
2403 case block_type_iseq:
2405 const rb_iseq_t *iseq = rb_iseq_check(block->as.captured.code.iseq);
2406 rb_str_catf(str,
"%p %"PRIsVALUE
":%d", (
void *)self,
2408 ISEQ_BODY(iseq)->location.first_lineno);
2411 case block_type_symbol:
2412 rb_str_catf(str,
"%p(&%+"PRIsVALUE
")", (
void *)self, block->as.symbol);
2414 case block_type_ifunc:
2415 rb_str_catf(str,
"%p", (
void *)block->as.captured.code.ifunc);
2433proc_to_s(
VALUE self)
2436 GetProcPtr(self, proc);
2437 return rb_block_to_s(self, &proc->block, proc->header.is_lambda ?
" (lambda)" : NULL);
2449proc_to_proc(
VALUE self)
2455bm_mark_and_move(
void *ptr)
2457 struct METHOD *data = ptr;
2458 rb_gc_mark_and_move((
VALUE *)&data->recv);
2459 rb_gc_mark_and_move((
VALUE *)&data->klass);
2460 rb_gc_mark_and_move((
VALUE *)&data->iclass);
2461 rb_gc_mark_and_move((
VALUE *)&data->owner);
2462 rb_gc_mark_and_move_ptr((rb_method_entry_t **)&data->me);
2473 0, 0, RUBY_TYPED_THREAD_SAFE_FREE | RUBY_TYPED_WB_PROTECTED | RUBY_TYPED_EMBEDDABLE | RUBY_TYPED_FROZEN_SHAREABLE_NO_REC
2479 return RBOOL(rb_typeddata_is_kind_of(m, &method_data_type));
2486 ID rmiss = idRespond_to_missing;
2488 if (UNDEF_P(obj))
return 0;
2499 rb_method_entry_t *me;
2500 rb_method_definition_t *def;
2506 def =
ZALLOC(rb_method_definition_t);
2507 def->type = VM_METHOD_TYPE_MISSING;
2508 def->original_id = id;
2510 me = rb_method_entry_create(
id, klass, METHOD_VISI_UNDEF, def);
2522 if (!respond_to_missing_p(klass, obj, vid, scope))
return Qfalse;
2523 return mnew_missing(klass, obj,
SYM2ID(vid), mclass);
2529refinement_module_p(
VALUE mod)
2534VALUE rb_vm_module_refinement_iclass(
VALUE refinement_iclass,
VALUE defined_class);
2535VALUE rb_vm_refinement_iclass_for_cme(
VALUE refinement,
const rb_callable_method_entry_t *cme);
2538find_refined_target_ancestor(
VALUE start,
VALUE refined_target)
2541 for (klass = start; klass; klass =
RCLASS_SUPER(klass)) {
2553 for (klass = start; klass; klass =
RCLASS_SUPER(klass)) {
2554 if (klass == ancestor)
return klass;
2562 VALUE refined_target = rb_refinement_module_get_refined_class(owner);
2570 while (RICLASS_FOR_REFINEMENT_P(refinement_iclass)) {
2574 RBASIC(refinement_iclass)->klass == refined_target &&
2575 find_ancestor(klass, refinement_iclass)) {
2579 VALUE refined_ancestor = find_refined_target_ancestor(klass, refined_target);
2580 if (!refined_ancestor) {
2581 refined_ancestor = rb_include_class_new(refined_target, klass);
2583 iclass = rb_vm_module_refinement_iclass(iclass, refined_ancestor);
2590mnew_internal(
const rb_method_entry_t *me,
VALUE klass,
VALUE iclass,
2595 const rb_method_entry_t *original_me = me;
2596 rb_method_visibility_t visi = METHOD_VISI_UNDEF;
2599 if (UNDEFINED_METHOD_ENTRY_P(me)) {
2600 if (respond_to_missing_p(klass, obj,
ID2SYM(
id), scope)) {
2601 return mnew_missing(klass, obj,
id, mclass);
2603 if (!error)
return Qnil;
2604 rb_print_undef(klass,
id, METHOD_VISI_UNDEF);
2606 if (visi == METHOD_VISI_UNDEF) {
2607 visi = METHOD_ENTRY_VISI(me);
2609 if (scope && (visi != METHOD_VISI_PUBLIC)) {
2610 if (!error)
return Qnil;
2611 rb_print_inaccessible(klass,
id, visi);
2614 if (me->def->type == VM_METHOD_TYPE_ZSUPER ||
2615 (me->def->type == VM_METHOD_TYPE_CFUNC && me->def->body.cfunc.func == (rb_cfunc_t)rb_zsuper_to_super)) {
2616 if (me->def->type == VM_METHOD_TYPE_ZSUPER && me->defined_class) {
2618 id = me->def->original_id;
2619 me = (rb_method_entry_t *)rb_callable_method_entry_with_refinements(klass,
id, &iclass);
2623 id = me->def->original_id;
2624 me = rb_method_entry_without_refinements(klass,
id, &iclass);
2640 RB_OBJ_WRITE(method, &data->owner, original_me->owner);
2647mnew_from_me(
const rb_method_entry_t *me,
VALUE klass,
VALUE iclass,
2650 if (me && refinement_module_p(me->owner) && !RICLASS_FOR_REFINEMENT_P(iclass)) {
2654 return mnew_internal(me, klass, iclass, obj,
id, mclass, scope, TRUE);
2660 const rb_method_entry_t *me;
2664 me = (rb_method_entry_t *)rb_callable_method_entry_with_refinements(klass,
id, &iclass);
2665 return mnew_from_me(me, klass, iclass, obj,
id, mclass, scope);
2669mnew_unbound(
VALUE klass,
ID id,
VALUE mclass,
int scope)
2671 const rb_method_entry_t *me;
2674 me = rb_method_entry_with_refinements(klass,
id, &iclass);
2675 return mnew_from_me(me, klass, iclass,
Qundef,
id, mclass, scope);
2679method_entry_defined_class(
const rb_method_entry_t *me)
2681 VALUE defined_class = me->defined_class;
2682 return defined_class ? defined_class : me->owner;
2726 VALUE klass1, klass2;
2734 m1 = (
struct METHOD *)RTYPEDDATA_GET_DATA(method);
2735 m2 = (
struct METHOD *)RTYPEDDATA_GET_DATA(other);
2737 klass1 = method_entry_defined_class(m1->me);
2738 klass2 = method_entry_defined_class(m2->me);
2742 if (!rb_method_entry_eq(m1->me, m2->me) ||
2744 m1->klass != m2->klass ||
2745 m1->recv != m2->recv) {
2766#define unbound_method_eq method_eq
2778method_hash(
VALUE method)
2785 hash = rb_hash_method_entry(hash, m->me);
2801method_unbind(
VALUE obj)
2804 struct METHOD *orig, *data;
2808 &method_data_type, data);
2813 RB_OBJ_WRITE(method, &data->me, rb_method_entry_clone(orig->me));
2828method_receiver(
VALUE obj)
2844method_name(
VALUE obj)
2849 return ID2SYM(data->me->called_id);
2866method_original_name(
VALUE obj)
2871 return ID2SYM(data->me->def->original_id);
2893method_owner(
VALUE obj)
2907method_box(
VALUE obj)
2910 const rb_box_t *box;
2913 box = data->me->def->box;
2914 if (!box)
return Qnil;
2915 if (box->box_object)
return box->box_object;
2916 rb_bug(
"Unexpected box on the method definition: %p", (
void*) box);
2923#define MSG(s) rb_fstring_lit("undefined method '%1$s' for"s" '%2$s'")
2927 if (RCLASS_SINGLETON_P(c)) {
2928 VALUE obj = RCLASS_ATTACHED_OBJECT(klass);
2945 rb_name_err_raise_str(s, c, str);
2957 VALUE m = mnew_missing_by_name(klass, obj, &vid, scope, mclass);
2959 rb_method_name_error(klass, vid);
2961 return mnew_callable(klass, obj,
id, mclass, scope);
3006 return obj_method(obj, vid, FALSE);
3019 return obj_method(obj, vid, TRUE);
3023rb_obj_singleton_method_lookup(
VALUE arg)
3030rb_obj_singleton_method_lookup_fail(
VALUE arg1,
VALUE arg2)
3067 NIL_P(klass = RCLASS_ORIGIN(sc)) ||
3068 !
NIL_P(rb_special_singleton_class(obj))) {
3077 VALUE args[2] = {obj, vid};
3078 VALUE ruby_method =
rb_rescue(rb_obj_singleton_method_lookup, (
VALUE)args, rb_obj_singleton_method_lookup_fail,
Qfalse);
3080 struct METHOD *method = (
struct METHOD *)RTYPEDDATA_GET_DATA(ruby_method);
3083 VALUE method_class = method->iclass;
3087 if (lookup_class == method_class) {
3091 }
while (lookup_class && lookup_class != stop_class);
3096 if (
id) vid =
ID2SYM(
id);
3097 rb_name_err_raise(
"undefined singleton method '%1$s' for '%2$s'",
3138 rb_method_name_error(mod, vid);
3151rb_mod_public_instance_method(
VALUE mod,
VALUE vid)
3155 rb_method_name_error(mod, vid);
3166 int is_method = FALSE;
3185 "wrong argument type %s (expected Proc/Method/UnboundMethod)",
3192 struct METHOD *method = (
struct METHOD *)RTYPEDDATA_GET_DATA(body);
3195 if (RCLASS_SINGLETON_P(method->me->owner)) {
3197 "can't bind singleton method to a different class");
3201 "bind argument must be a subclass of % "PRIsVALUE,
3205 rb_method_entry_set(mod,
id, method->me, scope_visi->method_visi);
3206 if (scope_visi->module_func) {
3213 GetProcPtr(body, body_proc);
3216 if (body_proc->header.is_refined) {
3217 rb_raise(rb_eArgError,
3218 "can't define a method from a Proc with refinements");
3220 VALUE procval = rb_proc_dup(body);
3221 if (vm_proc_iseq(procval) != NULL) {
3223 GetProcPtr(procval, proc);
3224 proc->header.is_lambda = TRUE;
3225 proc->header.is_from_method = TRUE;
3227 rb_add_method(mod,
id, VM_METHOD_TYPE_BMETHOD, (
void *)procval, scope_visi->method_visi);
3228 if (scope_visi->module_func) {
3229 rb_add_method(
rb_singleton_class(mod),
id, VM_METHOD_TYPE_BMETHOD, (
void *)body, METHOD_VISI_PUBLIC);
3277rb_mod_define_method(
int argc,
VALUE *argv,
VALUE mod)
3279 const rb_cref_t *cref = rb_vm_cref_in_context(mod, mod);
3280 const rb_scope_visibility_t default_scope_visi = {METHOD_VISI_PUBLIC, FALSE};
3281 const rb_scope_visibility_t *scope_visi = &default_scope_visi;
3284 scope_visi = CREF_SCOPE_VISI(cref);
3287 return rb_mod_define_method_with_visibility(argc, argv, mod, scope_visi);
3322rb_obj_define_method(
int argc,
VALUE *argv,
VALUE obj)
3325 const rb_scope_visibility_t scope_visi = {METHOD_VISI_PUBLIC, FALSE};
3327 return rb_mod_define_method_with_visibility(argc, argv, klass, &scope_visi);
3338top_define_method(
int argc,
VALUE *argv,
VALUE obj)
3340 return rb_mod_define_method(argc, argv, rb_top_main_class(
"define_method"));
3361method_clone(
VALUE self)
3364 struct METHOD *orig, *data;
3368 rb_obj_clone_setup(self, clone,
Qnil);
3373 RB_OBJ_WRITE(clone, &data->me, rb_method_entry_clone(orig->me));
3379method_dup(
VALUE self)
3382 struct METHOD *orig, *data;
3386 rb_obj_dup_setup(self, clone);
3391 RB_OBJ_WRITE(clone, &data->me, rb_method_entry_clone(orig->me));
3420rb_method_call_pass_called_kw(
int argc,
const VALUE *argv,
VALUE method)
3439static const rb_callable_method_entry_t *
3440method_callable_method_entry(
const struct METHOD *data)
3442 if (data->me->defined_class == 0) rb_bug(
"method_callable_method_entry: not callable.");
3443 return (
const rb_callable_method_entry_t *)data->me;
3447call_method_data(rb_execution_context_t *ec,
const struct METHOD *data,
3448 int argc,
const VALUE *argv,
VALUE passed_procval,
int kw_splat)
3450 vm_passed_block_handler_set(ec, proc_to_block_handler(passed_procval));
3451 return rb_vm_call_kw(ec, data->recv, data->me->called_id, argc, argv,
3452 method_callable_method_entry(data), kw_splat);
3458 const struct METHOD *data;
3459 rb_execution_context_t *ec = GET_EC();
3462 if (UNDEF_P(data->recv)) {
3463 rb_raise(
rb_eTypeError,
"can't call unbound method; bind first");
3465 return call_method_data(ec, data, argc, argv, passed_procval, kw_splat);
3530convert_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)
3532 VALUE methclass = data->owner;
3533 VALUE iclass = data->iclass;
3537 VALUE refined_class = rb_refinement_module_get_refined_class(methclass);
3538 if (!
NIL_P(refined_class)) methclass = refined_class;
3541 if (RCLASS_SINGLETON_P(methclass)) {
3543 "singleton method called for a different object");
3546 rb_raise(
rb_eTypeError,
"bind argument must be an instance of % "PRIsVALUE,
3551 const rb_method_entry_t *me;
3553 me = rb_method_entry_clone(data->me);
3563 me = rb_method_entry_clone(me);
3565 if (refinement_module_p(me->owner) && RICLASS_FOR_REFINEMENT_P(iclass)) {
3566 iclass = find_refinement_iclass(me->owner, klass, iclass);
3571 if (!ic) ic = rb_class_search_ancestor(klass, me->owner);
3577 klass = rb_include_class_new(methclass, klass);
3581 me = (
const rb_method_entry_t *) rb_method_entry_complement_defined_class(me, me->called_id, klass);
3584 *methclass_out = methclass;
3586 *iclass_out = iclass;
3628 VALUE methclass, klass, iclass;
3629 const rb_method_entry_t *me;
3630 const struct METHOD *data;
3632 convert_umethod_to_method_components(data, recv, &methclass, &klass, &iclass, &me,
true);
3654umethod_bind_call(
int argc,
VALUE *argv,
VALUE method)
3657 VALUE recv = argv[0];
3662 rb_execution_context_t *ec = GET_EC();
3664 const struct METHOD *data;
3667 const rb_callable_method_entry_t *cme = rb_callable_method_entry(
CLASS_OF(recv), data->me->called_id);
3668 if (data->me == (
const rb_method_entry_t *)cme) {
3669 vm_passed_block_handler_set(ec, proc_to_block_handler(passed_procval));
3673 VALUE methclass, klass, iclass;
3674 const rb_method_entry_t *me;
3675 convert_umethod_to_method_components(data, recv, &methclass, &klass, &iclass, &me,
false);
3676 struct METHOD bound = { recv, klass, 0, methclass, me };
3688method_def_min_max_arity(
const rb_method_definition_t *def,
int *max)
3691 if (!def)
return *max = 0;
3692 switch (def->type) {
3693 case VM_METHOD_TYPE_CFUNC:
3694 if (def->body.cfunc.argc < 0) {
3698 return *max = check_argc(def->body.cfunc.argc);
3699 case VM_METHOD_TYPE_ZSUPER:
3702 case VM_METHOD_TYPE_ATTRSET:
3704 case VM_METHOD_TYPE_IVAR:
3706 case VM_METHOD_TYPE_ALIAS:
3707 def = rb_method_entry_def_acquire(rb_method_alias_original_me(def));
3709 case VM_METHOD_TYPE_BMETHOD:
3710 return rb_proc_min_max_arity(def->body.bmethod.proc, max);
3711 case VM_METHOD_TYPE_ISEQ:
3712 return rb_iseq_min_max_arity(rb_iseq_check(def->body.iseq.
iseqptr), max);
3713 case VM_METHOD_TYPE_UNDEF:
3714 case VM_METHOD_TYPE_NOTIMPLEMENTED:
3716 case VM_METHOD_TYPE_MISSING:
3719 case VM_METHOD_TYPE_OPTIMIZED: {
3720 switch (def->body.optimized.type) {
3721 case OPTIMIZED_METHOD_TYPE_SEND:
3724 case OPTIMIZED_METHOD_TYPE_CALL:
3727 case OPTIMIZED_METHOD_TYPE_BLOCK_CALL:
3730 case OPTIMIZED_METHOD_TYPE_STRUCT_AREF:
3733 case OPTIMIZED_METHOD_TYPE_STRUCT_ASET:
3741 case VM_METHOD_TYPE_REFINED:
3745 rb_bug(
"method_def_min_max_arity: invalid method entry type (%d)", def->type);
3750method_def_arity(
const rb_method_definition_t *def)
3752 int max, min = method_def_min_max_arity(def, &max);
3753 return min == max ? min : -min-1;
3757rb_method_entry_arity(
const rb_method_entry_t *me)
3759 return method_def_arity(rb_method_entry_def_acquire(me));
3806method_arity_m(
VALUE method)
3808 int n = method_arity(method);
3813method_arity(
VALUE method)
3818 return rb_method_entry_arity(data->me);
3821static const rb_method_entry_t *
3822original_method_entry(
VALUE mod,
ID id)
3824 const rb_method_entry_t *me;
3826 while ((me = rb_method_entry(mod,
id)) != 0) {
3827 const rb_method_definition_t *def = me->def;
3829 if (def->type != VM_METHOD_TYPE_ZSUPER &&
3830 (def->type != VM_METHOD_TYPE_CFUNC ||
3831 def->body.cfunc.func != (rb_cfunc_t)rb_zsuper_to_super))
break;
3834 id = def->original_id;
3840method_min_max_arity(
VALUE method,
int *max)
3842 const struct METHOD *data;
3845 return method_def_min_max_arity(rb_method_entry_def_acquire(data->me), max);
3851 const rb_method_entry_t *me = original_method_entry(mod,
id);
3853 return rb_method_entry_arity(me);
3863rb_callable_receiver(
VALUE callable)
3866 VALUE binding = proc_binding(callable);
3867 return rb_funcall(binding, rb_intern(
"receiver"), 0);
3870 return method_receiver(callable);
3877const rb_method_definition_t *
3878rb_method_def(
VALUE method)
3880 const struct METHOD *data;
3883 return rb_method_entry_def_acquire(data->me);
3886static const rb_iseq_t *
3887method_def_iseq(
const rb_method_definition_t *def)
3889 switch (def->type) {
3890 case VM_METHOD_TYPE_ISEQ:
3891 return rb_iseq_check(def->body.iseq.
iseqptr);
3892 case VM_METHOD_TYPE_BMETHOD:
3893 return rb_proc_get_iseq(def->body.bmethod.proc, 0);
3894 case VM_METHOD_TYPE_ALIAS:
3895 return method_def_iseq(rb_method_entry_def_acquire(rb_method_alias_original_me(def)));
3896 case VM_METHOD_TYPE_CFUNC:
3897 case VM_METHOD_TYPE_ATTRSET:
3898 case VM_METHOD_TYPE_IVAR:
3899 case VM_METHOD_TYPE_ZSUPER:
3900 case VM_METHOD_TYPE_UNDEF:
3901 case VM_METHOD_TYPE_NOTIMPLEMENTED:
3902 case VM_METHOD_TYPE_OPTIMIZED:
3903 case VM_METHOD_TYPE_MISSING:
3904 case VM_METHOD_TYPE_REFINED:
3911rb_method_iseq(
VALUE method)
3913 return method_def_iseq(rb_method_def(method));
3916static const rb_cref_t *
3917method_cref(
VALUE method)
3919 const rb_method_definition_t *def = rb_method_def(method);
3922 switch (def->type) {
3923 case VM_METHOD_TYPE_ISEQ:
3924 return def->body.iseq.
cref;
3925 case VM_METHOD_TYPE_ALIAS:
3926 def = rb_method_entry_def_acquire(rb_method_alias_original_me(def));
3934method_def_location(
const rb_method_definition_t *def)
3936 if (def->type == VM_METHOD_TYPE_ATTRSET || def->type == VM_METHOD_TYPE_IVAR) {
3937 if (!def->body.attr.location)
3941 return iseq_location(method_def_iseq(def));
3945rb_method_entry_location(
const rb_method_entry_t *me)
3947 if (!me)
return Qnil;
3948 return method_def_location(rb_method_entry_def_acquire(me));
3968rb_method_location(
VALUE method)
3970 return method_def_location(rb_method_def(method));
3974method_def_source_range(
const rb_method_definition_t *def)
3976 return source_range_new(method_def_iseq(def));
3993rb_method_source_range(
VALUE method)
3995 return method_def_source_range(rb_method_def(method));
3998static const rb_method_definition_t *
3999vm_proc_method_def(
VALUE procval)
4005 GetProcPtr(procval, proc);
4006 block = &proc->block;
4008 if (vm_block_type(block) == block_type_ifunc &&
4009 IS_METHOD_PROC_IFUNC(ifunc = block->as.captured.code.ifunc)) {
4010 return rb_method_def((
VALUE)ifunc->data);
4018method_def_parameters(
const rb_method_definition_t *def)
4020 const rb_iseq_t *iseq;
4021 const rb_method_definition_t *bmethod_def;
4023 switch (def->type) {
4024 case VM_METHOD_TYPE_ISEQ:
4025 iseq = method_def_iseq(def);
4026 return rb_iseq_parameters(iseq, 0);
4027 case VM_METHOD_TYPE_BMETHOD:
4028 if ((iseq = method_def_iseq(def)) != NULL) {
4029 return rb_iseq_parameters(iseq, 0);
4031 else if ((bmethod_def = vm_proc_method_def(def->body.bmethod.proc)) != NULL) {
4032 return method_def_parameters(bmethod_def);
4036 case VM_METHOD_TYPE_ALIAS:
4037 return method_def_parameters(rb_method_entry_def_acquire(rb_method_alias_original_me(def)));
4039 case VM_METHOD_TYPE_OPTIMIZED:
4040 if (def->body.optimized.type == OPTIMIZED_METHOD_TYPE_STRUCT_ASET) {
4041 VALUE param = rb_ary_new_from_args(2,
ID2SYM(rb_intern(
"req")),
ID2SYM(rb_intern(
"_")));
4042 return rb_ary_new_from_args(1, param);
4046 case VM_METHOD_TYPE_CFUNC:
4047 case VM_METHOD_TYPE_ATTRSET:
4048 case VM_METHOD_TYPE_IVAR:
4049 case VM_METHOD_TYPE_ZSUPER:
4050 case VM_METHOD_TYPE_UNDEF:
4051 case VM_METHOD_TYPE_NOTIMPLEMENTED:
4052 case VM_METHOD_TYPE_MISSING:
4053 case VM_METHOD_TYPE_REFINED:
4057 return rb_unnamed_parameters(method_def_arity(def));
4081rb_method_parameters(
VALUE method)
4083 return method_def_parameters(rb_method_def(method));
4087append_param_name(
VALUE str,
VALUE name,
const char *unnamed)
4132method_inspect(
VALUE method)
4136 const char *sharp =
"#";
4138 VALUE defined_class;
4141 str = rb_sprintf(
"#<% "PRIsVALUE
": ",
rb_obj_class(method));
4143 mklass = data->iclass;
4144 if (!mklass) mklass = data->klass;
4154 const rb_method_definition_t *def = rb_method_entry_def_acquire(data->me);
4155 if (def->type == VM_METHOD_TYPE_ALIAS) {
4156 defined_class = rb_method_alias_original_me(def)->owner;
4159 defined_class = method_entry_defined_class(data->me);
4166 if (UNDEF_P(data->recv)) {
4170 else if (RCLASS_SINGLETON_P(mklass)) {
4171 VALUE v = RCLASS_ATTACHED_OBJECT(mklass);
4173 if (UNDEF_P(data->recv)) {
4176 else if (data->recv == v) {
4189 mklass = data->klass;
4193 if (RCLASS_SINGLETON_P(mklass)) {
4194 VALUE v = RCLASS_ATTACHED_OBJECT(mklass);
4202 if (defined_class != mklass) {
4203 rb_str_catf(str,
"(% "PRIsVALUE
")", defined_class);
4208 if (data->me->called_id != def->original_id) {
4209 rb_str_catf(str,
"(%"PRIsVALUE
")",
4210 rb_id2str(def->original_id));
4212 if (def->type == VM_METHOD_TYPE_NOTIMPLEMENTED) {
4218 VALUE params = rb_method_parameters(method);
4219 VALUE pair, name, kind;
4225 const VALUE keyrest =
ID2SYM(rb_intern(
"keyrest"));
4228 const VALUE noblock =
ID2SYM(rb_intern(
"noblock"));
4243 for (
int i = 0; i <
RARRAY_LEN(params); i++) {
4254 append_param_name(str, name,
"_");
4256 else if (kind == opt) {
4259 else if (kind == keyreq) {
4262 else if (kind == key) {
4265 else if (kind == rest) {
4267 if (name !=
ID2SYM(
'*')) {
4268 append_param_name(str, name, NULL);
4271 else if (kind == keyrest) {
4272 if (name !=
ID2SYM(idPow)) {
4282 else if (kind == block) {
4283 if (name ==
ID2SYM(
'&')) {
4295 else if (kind == nokey) {
4298 else if (kind == noblock) {
4310 VALUE loc = rb_method_location(method);
4312 rb_str_catf(str,
" %"PRIsVALUE
":%"PRIsVALUE,
4345method_to_proc(
VALUE method)
4359 procval =
rb_block_call(rb_mRubyVMFrozenCore, idLambda, 0, 0, bmcall, method);
4360 GetProcPtr(procval, proc);
4361 proc->header.is_from_method = 1;
4365extern VALUE rb_find_defined_class_by_owner(
VALUE current_class,
VALUE target_owner);
4366extern int rb_method_definition_eq(
const rb_method_definition_t *d1,
const rb_method_definition_t *d2);
4367rb_cref_t * rb_vm_get_cref(
const VALUE *ep);
4378method_super_method(
VALUE method)
4380 const struct METHOD *data;
4381 VALUE super_class, iclass;
4383 const rb_method_entry_t *me;
4386 iclass = data->iclass;
4387 if (!iclass)
return Qnil;
4388 bool is_refinement_method = refinement_module_p(data->me->owner);
4389 const rb_method_definition_t *def = rb_method_entry_def_acquire(data->me);
4390 if (def->type == VM_METHOD_TYPE_ALIAS && data->me->defined_class) {
4391 const rb_method_entry_t *orig_me = rb_method_alias_original_me(def);
4392 super_class =
RCLASS_SUPER(rb_find_defined_class_by_owner(data->me->defined_class,
4394 mid = rb_method_entry_def_acquire(orig_me)->original_id;
4396 else if (is_refinement_method) {
4397 if (RICLASS_FOR_REFINEMENT_P(iclass)) {
4400 iclass = super_class;
4401 }
while (RICLASS_FOR_REFINEMENT_P(super_class));
4404 super_class = find_refined_target_ancestor(iclass, rb_refinement_module_get_refined_class(data->me->owner));
4409 mid = def->original_id;
4412 VALUE klass = iclass;
4413 if (RICLASS_FOR_REFINEMENT_P(klass)) {
4415 klass =
RBASIC(klass)->klass;
4417 else if (data->me->defined_class &&
RB_TYPE_P(data->me->defined_class,
T_CLASS) &&
4418 data->me->owner != data->me->defined_class) {
4420 klass = data->me->defined_class;
4423 mid = def->original_id;
4425 if (!super_class)
return Qnil;
4429 const rb_method_definition_t *skip_def = is_refinement_method ? def : NULL;
4434 const rb_cref_t *cref = NULL;
4435 switch (def->type) {
4436 case VM_METHOD_TYPE_ISEQ:
4437 cref = def->body.iseq.
cref;
4439 case VM_METHOD_TYPE_BMETHOD: {
4441 GetProcPtr(def->body.bmethod.proc, proc);
4442 const struct rb_block *block = &proc->block;
4443 if (vm_block_type(block) == block_type_iseq)
4444 cref = rb_vm_get_cref(block->as.captured.ep);
4450 VALUE klass = super_class;
4453 const rb_callable_method_entry_t *cme = rb_callable_method_entry(klass, mid);
4455 if (cme->def->type != VM_METHOD_TYPE_REFINED) {
4456 me = (rb_method_entry_t *)cme;
4457 iclass = cme->defined_class;
4463 for (c = cref; c; c = CREF_NEXT(c)) {
4464 VALUE refs = CREF_REFINEMENTS(c);
4465 if (
NIL_P(refs))
continue;
4466 VALUE r = rb_hash_lookup(refs, cme->owner);
4467 if (
NIL_P(r))
continue;
4468 r = rb_vm_refinement_iclass_for_cme(r, cme);
4469 const rb_callable_method_entry_t *ref_cme = rb_callable_method_entry(r, mid);
4470 if (!ref_cme)
break;
4471 if (ref_cme->def->type == VM_METHOD_TYPE_REFINED)
continue;
4472 if (skip_def && rb_method_definition_eq(ref_cme->def, skip_def))
continue;
4473 me = (rb_method_entry_t *)ref_cme;
4474 iclass = ref_cme->defined_class;
4480 const rb_method_entry_t *orig_me = cme->def->body.refined.orig_me;
4482 me = (rb_method_entry_t *)orig_me;
4483 iclass = cme->defined_class;
4488 if (!me)
return Qnil;
4490 return mnew_internal(me, mklass, iclass, data->recv, mid,
rb_obj_class(method), FALSE, FALSE);
4500localjump_xvalue(
VALUE exc)
4514localjump_reason(
VALUE exc)
4519rb_cref_t *rb_vm_cref_new_toplevel(
void);
4522env_clone(
const rb_env_t *env,
const rb_cref_t *cref)
4528 VM_ASSERT(env->ep > env->env);
4529 VM_ASSERT(VM_ENV_ESCAPED_P(env->ep));
4532 cref = rb_vm_cref_new_toplevel();
4536 new_ep = &new_body[env->ep - env->env];
4537 new_env = vm_env_new(new_ep, new_body, env->env_size, env->iseq);
4544 new_ep[VM_ENV_DATA_INDEX_ENV] = (
VALUE)new_env;
4546 VM_ASSERT(VM_ENV_ESCAPED_P(new_ep));
4564proc_binding(
VALUE self)
4569 const rb_iseq_t *iseq = NULL;
4573 GetProcPtr(self, proc);
4574 block = &proc->block;
4576 if (proc->header.is_isolated) rb_raise(rb_eArgError,
"Can't create Binding from isolated Proc");
4579 switch (vm_block_type(block)) {
4580 case block_type_iseq:
4581 iseq = block->as.captured.code.iseq;
4582 binding_self = block->as.captured.self;
4583 env = VM_ENV_ENVVAL_PTR(block->as.captured.ep);
4585 case block_type_proc:
4586 GetProcPtr(block->as.proc, proc);
4587 block = &proc->block;
4589 case block_type_ifunc:
4591 const struct vm_ifunc *ifunc = block->as.captured.code.ifunc;
4592 if (IS_METHOD_PROC_IFUNC(ifunc)) {
4594 VALUE name = rb_fstring_lit(
"<empty_iseq>");
4596 binding_self = method_receiver(method);
4597 iseq = rb_method_iseq(method);
4598 env = VM_ENV_ENVVAL_PTR(block->as.captured.ep);
4599 env = env_clone(env, method_cref(method));
4601 empty = rb_iseq_new(
Qnil, name, name,
Qnil, 0, ISEQ_TYPE_TOP);
4607 case block_type_symbol:
4608 rb_raise(rb_eArgError,
"Can't create Binding from C level Proc");
4613 GetBindingPtr(bindval, bind);
4614 RB_OBJ_WRITE(bindval, &bind->block.as.captured.self, binding_self);
4615 RB_OBJ_WRITE(bindval, &bind->block.as.captured.code.iseq, env->iseq);
4616 rb_vm_block_ep_update(bindval, &bind->block, env->ep);
4620 rb_iseq_check(iseq);
4621 RB_OBJ_WRITE(bindval, &bind->pathobj, ISEQ_BODY(iseq)->location.pathobj);
4622 bind->no_realpath = ISEQ_BODY(iseq)->no_realpath;
4623 bind->first_lineno = ISEQ_BODY(iseq)->location.first_lineno;
4628 rb_iseq_pathobj_new(rb_fstring_lit(
"(binding)"),
Qnil, &no_realpath));
4629 bind->no_realpath = no_realpath;
4630 bind->first_lineno = 1;
4645 GetProcPtr(proc, procp);
4646 is_lambda = procp->header.is_lambda;
4650 GetProcPtr(proc, procp);
4651 procp->header.is_lambda = is_lambda;
4658 VALUE proc, passed, arity;
4667 if (!
NIL_P(blockarg)) {
4668 rb_warn(
"given block not used");
4670 arity = make_curry_proc(proc, passed, arity);
4733proc_curry(
int argc,
const VALUE *argv,
VALUE self)
4735 int sarity, max_arity, min_arity = rb_proc_min_max_arity(self, &max_arity);
4749 return make_curry_proc(self,
rb_ary_new(), arity);
4785rb_method_curry(
int argc,
const VALUE *argv,
VALUE self)
4787 VALUE proc = method_to_proc(self);
4788 return proc_curry(argc, argv, proc);
4817 mesg = rb_fstring_lit(
"callable object is expected");
4841 return rb_proc_compose_to_left(self, to_callable(g));
4847 VALUE proc, args, procs[2];
4853 args = rb_ary_tmp_new_from_values(0, 2, procs);
4856 GetProcPtr(g, procp);
4857 is_lambda = procp->header.is_lambda;
4865 GetProcPtr(proc, procp);
4866 procp->header.is_lambda = is_lambda;
4900 return rb_proc_compose_to_right(self, to_callable(g));
4906 VALUE proc, args, procs[2];
4912 args = rb_ary_tmp_new_from_values(0, 2, procs);
4914 GetProcPtr(self, procp);
4915 is_lambda = procp->header.is_lambda;
4918 GetProcPtr(proc, procp);
4919 procp->header.is_lambda = is_lambda;
4940rb_method_compose_to_left(
VALUE self,
VALUE g)
4943 self = method_to_proc(self);
4944 return proc_compose_to_left(self, g);
4963rb_method_compose_to_right(
VALUE self,
VALUE g)
4966 self = method_to_proc(self);
4967 return proc_compose_to_right(self, g);
4995proc_ruby2_keywords(
VALUE procval)
4998 GetProcPtr(procval, proc);
5000 rb_check_frozen(procval);
5002 if (proc->header.is_from_method) {
5003 rb_warn(
"Skipping set of ruby2_keywords flag for proc (proc created from method)");
5007 switch (proc->block.type) {
5008 case block_type_iseq:
5009 if (ISEQ_BODY(proc->block.as.captured.code.iseq)->param.flags.has_rest &&
5010 !ISEQ_BODY(proc->block.as.captured.code.iseq)->param.flags.has_post &&
5011 !ISEQ_BODY(proc->block.as.captured.code.iseq)->param.flags.has_kw &&
5012 !ISEQ_BODY(proc->block.as.captured.code.iseq)->param.flags.has_kwrest) {
5013 if (proc->header.is_refined) {
5017 const rb_iseq_t *copy =
5018 rb_iseq_dup_with_independent_caches(proc->block.as.captured.code.iseq);
5019 ISEQ_BODY(copy)->param.flags.ruby2_keywords = 1;
5025 ISEQ_BODY(proc->block.as.captured.code.iseq)->param.flags.ruby2_keywords = 1;
5029 rb_warn(
"Skipping set of ruby2_keywords flag for proc (proc accepts keywords or post arguments or proc does not accept argument splat)");
5033 rb_warn(
"Skipping set of ruby2_keywords flag for proc (proc not defined in Ruby)");
5473 id_refinements_recipe = rb_make_internal_id();
5475 VALUE mRuby = rb_define_module(
"Ruby");
5478 rb_cSourceRange = rb_define_class_under(mRuby,
"SourceRange",
rb_cObject);
5482 rb_define_method(rb_cSourceRange,
"absolute_path", source_range_absolute_path, 0);
5483 rb_define_method(rb_cSourceRange,
"start_line", source_range_start_line, 0);
5484 rb_define_method(rb_cSourceRange,
"start_column", source_range_start_column, 0);
5486 rb_define_method(rb_cSourceRange,
"end_column", source_range_end_column, 0);
5494 rb_add_method_optimized(
rb_cProc, idCall, OPTIMIZED_METHOD_TYPE_CALL, 0, METHOD_VISI_PUBLIC);
5495 rb_add_method_optimized(
rb_cProc, rb_intern(
"[]"), OPTIMIZED_METHOD_TYPE_CALL, 0, METHOD_VISI_PUBLIC);
5496 rb_add_method_optimized(
rb_cProc, rb_intern(
"==="), OPTIMIZED_METHOD_TYPE_CALL, 0, METHOD_VISI_PUBLIC);
5497 rb_add_method_optimized(
rb_cProc, rb_intern(
"yield"), OPTIMIZED_METHOD_TYPE_CALL, 0, METHOD_VISI_PUBLIC);
5532 rb_vm_register_special_exception(ruby_error_sysstack,
rb_eSysStackError,
"stack level too deep");
5603 "define_method", top_define_method, -1);
5644 rb_gc_register_address(&sym_proc_cache);
#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_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_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 FL_TEST_RAW
Old name of RB_FL_TEST_RAW.
#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 INT2NUM
Old name of RB_INT2NUM.
#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_category_warn(rb_warning_category_t category, const char *fmt,...)
Identical to rb_category_warning(), except it reports unless $VERBOSE is nil.
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.
@ RB_WARN_CATEGORY_PERFORMANCE
Warning is for performance issues (not enabled by -w).
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_obj_hide(VALUE obj)
Make the object invisible from Ruby code.
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.
#define RUBY_ASSERT(...)
Asserts that the given expression is truthy if and only if RUBY_DEBUG is truthy.
VALUE rb_funcall(VALUE recv, ID mid, int n,...)
Calls a method.
VALUE rb_funcallv(VALUE recv, ID mid, int argc, const VALUE *argv)
Identical to rb_funcall(), except it takes the method arguments as a C array.
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.
void rb_ary_store(VALUE ary, rb_len_t key, VALUE val)
Destructively stores the passed value to the passed array's passed index.
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(rb_len_t 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.
#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.
void rb_str_set_len(VALUE str, rb_len_t len)
Overwrites the length of the string.
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.
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.
#define rb_str_new_cstr(str)
Identical to rb_str_new, 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.
VALUE rb_ivar_set(VALUE obj, ID name, VALUE val)
Identical to rb_iv_set(), except it accepts the name as an ID instead of a C string.
VALUE rb_ivar_get(VALUE obj, ID name)
Identical to rb_iv_get(), except it accepts the name as an ID instead of a C string.
void rb_undef_alloc_func(VALUE klass)
Deletes the allocator function of a class.
int rb_method_basic_definition_p(VALUE klass, ID mid)
Well... Let us hesitate from describing what a "basic definition" is.
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.
VALUE rb_sym2str(VALUE symbol)
Obtain a frozen string representation of a symbol (not including the leading colon).
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.
int len
Length of the buffer.
#define RB_OBJ_SET_SHAREABLE(obj)
Wrapper of rb_obj_set_shareable().
#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 type(ANYARGS)
ANYARGS-ed function type.
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 VALUE * RARRAY_PTR(VALUE ary)
Wild use of a C pointer.
#define RARRAY_AREF(a, i)
References an object in an array.
static void RARRAY_ASET(VALUE ary, rb_len_t i, VALUE v)
Assigns an object in an array.
#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 RBASIC(obj)
Convenient casting macro.
#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.
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.
rb_cref_t * cref
class reference, should be marked
const rb_iseq_t * iseqptr
iseq pointer, should be separated from iseqval
IFUNC (Internal FUNCtion).
long rb_len_t
A signed integer type for lengths and indices of objects such as String and Array.
unsigned long rb_ulen_t
An unsigned integer type that has the same width with rb_len_t.
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 void Check_Type(VALUE v, enum ruby_value_type t)
Identical to RB_TYPE_P(), except it raises exceptions on predication failure.
static bool RB_TYPE_P(VALUE obj, enum ruby_value_type t)
Queries if the given object is of given type.