2007-02-02 10:21:41 -05:00
|
|
|
/**********************************************************************
|
|
|
|
|
2007-08-30 01:06:52 -04:00
|
|
|
proc.c - Proc, Binding, Env
|
2007-02-02 10:21:41 -05:00
|
|
|
|
|
|
|
$Author$
|
|
|
|
created at: Wed Jan 17 12:13:14 2007
|
|
|
|
|
|
|
|
Copyright (C) 2004-2007 Koichi Sasada
|
|
|
|
|
|
|
|
**********************************************************************/
|
|
|
|
|
|
|
|
#include "eval_intern.h"
|
2011-05-18 09:41:54 -04:00
|
|
|
#include "internal.h"
|
2007-02-02 10:21:41 -05:00
|
|
|
#include "gc.h"
|
* internal.h: declare internal functions here.
* node.h: declare NODE dependent internal functions here.
* iseq.h: declare rb_iseq_t dependent internal functions here.
* vm_core.h: declare rb_thread_t dependent internal functions here.
* bignum.c, class.c, compile.c, complex.c, cont.c, dir.c, encoding.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c, io.c,
iseq.c, load.c, marshal.c, math.c, numeric.c, object.c, parse.y,
proc.c, process.c, range.c, rational.c, re.c, ruby.c, string.c,
thread.c, time.c, transcode.c, variable.c, vm.c,
tool/compile_prelude.rb: don't declare internal functions declared
in above headers. include above headers if required.
Note that rb_thread_mark() was declared as
void rb_thread_mark(rb_thread_t *th) in cont.c but defined as
void rb_thread_mark(void *ptr) in vm.c. Now it is declared as
the later in internal.h.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@32156 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2011-06-17 18:43:38 -04:00
|
|
|
#include "iseq.h"
|
2007-02-02 10:21:41 -05:00
|
|
|
|
2015-06-19 01:53:41 -04:00
|
|
|
/* Proc.new with no block will raise an exception in the future
|
|
|
|
* versions */
|
|
|
|
#define PROC_NEW_REQUIRES_BLOCK 0
|
|
|
|
|
2016-11-20 05:41:09 -05:00
|
|
|
#if !defined(__GNUC__) || __GNUC__ < 5 || defined(__MINGW32__)
|
2016-05-05 03:11:34 -04:00
|
|
|
# define NO_CLOBBERED(v) (*(volatile VALUE *)&(v))
|
|
|
|
#else
|
|
|
|
# define NO_CLOBBERED(v) (v)
|
|
|
|
#endif
|
|
|
|
|
2015-03-08 17:22:43 -04:00
|
|
|
const rb_cref_t *rb_vm_cref_in_context(VALUE self, VALUE cbase);
|
2013-12-24 02:28:11 -05:00
|
|
|
|
2007-02-02 10:21:41 -05:00
|
|
|
struct METHOD {
|
* method.h: introduce rb_callable_method_entry_t to remove
rb_control_frame_t::klass.
[Bug #11278], [Bug #11279]
rb_method_entry_t data belong to modules/classes.
rb_method_entry_t::owner points defined module or class.
module M
def foo; end
end
In this case, owner is M.
rb_callable_method_entry_t data belong to only classes.
For modules, MRI creates corresponding T_ICLASS internally.
rb_callable_method_entry_t can also belong to T_ICLASS.
rb_callable_method_entry_t::defined_class points T_CLASS or
T_ICLASS.
rb_method_entry_t data for classes (not for modules) are also
rb_callable_method_entry_t data because it is completely same data.
In this case, rb_method_entry_t::owner == rb_method_entry_t::defined_class.
For example, there are classes C and D, and incldues M,
class C; include M; end
class D; include M; end
then, two T_ICLASS objects for C's super class and D's super class
will be created.
When C.new.foo is called, then M#foo is searcheed and
rb_callable_method_t data is used by VM to invoke M#foo.
rb_method_entry_t data is only one for M#foo.
However, rb_callable_method_entry_t data are two (and can be more).
It is proportional to the number of including (and prepending)
classes (the number of T_ICLASS which point to the module).
Now, created rb_callable_method_entry_t are collected when
the original module M was modified. We can think it is a cache.
We need to select what kind of method entry data is needed.
To operate definition, then you need to use rb_method_entry_t.
You can access them by the following functions.
* rb_method_entry(VALUE klass, ID id);
* rb_method_entry_with_refinements(VALUE klass, ID id);
* rb_method_entry_without_refinements(VALUE klass, ID id);
* rb_resolve_refined_method(VALUE refinements, const rb_method_entry_t *me);
To invoke methods, then you need to use rb_callable_method_entry_t
which you can get by the following APIs corresponding to the
above listed functions.
* rb_callable_method_entry(VALUE klass, ID id);
* rb_callable_method_entry_with_refinements(VALUE klass, ID id);
* rb_callable_method_entry_without_refinements(VALUE klass, ID id);
* rb_resolve_refined_method_callable(VALUE refinements, const rb_callable_method_entry_t *me);
VM pushes rb_callable_method_entry_t, so that rb_vm_frame_method_entry()
returns rb_callable_method_entry_t.
You can check a super class of current method by
rb_callable_method_entry_t::defined_class.
* method.h: renamed from rb_method_entry_t::klass to
rb_method_entry_t::owner.
* internal.h: add rb_classext_struct::callable_m_tbl to cache
rb_callable_method_entry_t data.
We need to consider abotu this field again because it is only
active for T_ICLASS.
* class.c (method_entry_i): ditto.
* class.c (rb_define_attr): rb_method_entry() does not takes
defiend_class_ptr.
* gc.c (mark_method_entry): mark RCLASS_CALLABLE_M_TBL() for T_ICLASS.
* cont.c (fiber_init): rb_control_frame_t::klass is removed.
* proc.c: fix `struct METHOD' data structure because
rb_callable_method_t has all information.
* vm_core.h: remove several fields.
* rb_control_frame_t::klass.
* rb_block_t::klass.
And catch up changes.
* eval.c: catch up changes.
* gc.c: ditto.
* insns.def: ditto.
* vm.c: ditto.
* vm_args.c: ditto.
* vm_backtrace.c: ditto.
* vm_dump.c: ditto.
* vm_eval.c: ditto.
* vm_insnhelper.c: ditto.
* vm_method.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@51126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2015-07-03 07:24:50 -04:00
|
|
|
const VALUE recv;
|
|
|
|
const VALUE klass;
|
|
|
|
const rb_method_entry_t * const me;
|
|
|
|
/* for bound methods, `me' should be rb_callable_method_entry_t * */
|
2007-02-02 10:21:41 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
VALUE rb_cUnboundMethod;
|
|
|
|
VALUE rb_cMethod;
|
|
|
|
VALUE rb_cBinding;
|
|
|
|
VALUE rb_cProc;
|
|
|
|
|
2013-06-17 08:38:52 -04:00
|
|
|
static VALUE bmcall(VALUE, VALUE, int, VALUE *, VALUE);
|
2007-02-02 10:21:41 -05:00
|
|
|
static int method_arity(VALUE);
|
2013-02-01 17:46:07 -05:00
|
|
|
static int method_min_max_arity(VALUE, int *max);
|
2015-05-21 04:45:57 -04:00
|
|
|
|
2013-05-02 03:54:17 -04:00
|
|
|
#define attached id__attached__
|
2007-02-02 10:21:41 -05:00
|
|
|
|
|
|
|
/* Proc */
|
|
|
|
|
2015-10-15 11:54:48 -04:00
|
|
|
#define IS_METHOD_PROC_IFUNC(ifunc) ((ifunc)->func == bmcall)
|
2016-07-28 07:02:30 -04:00
|
|
|
|
|
|
|
static VALUE proc_to_s_(VALUE self, const rb_proc_t *proc);
|
|
|
|
|
|
|
|
static void
|
|
|
|
block_mark(const struct rb_block *block)
|
|
|
|
{
|
|
|
|
switch (vm_block_type(block)) {
|
|
|
|
case block_type_iseq:
|
|
|
|
case block_type_ifunc:
|
|
|
|
{
|
|
|
|
const struct rb_captured_block *captured = &block->as.captured;
|
|
|
|
RUBY_MARK_UNLESS_NULL(captured->self);
|
|
|
|
RUBY_MARK_UNLESS_NULL((VALUE)captured->code.val);
|
|
|
|
if (captured->ep && captured->ep[VM_ENV_DATA_INDEX_ENV] != Qundef /* cfunc_proc_t */) {
|
|
|
|
RUBY_MARK_UNLESS_NULL(VM_ENV_ENVVAL(captured->ep));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case block_type_symbol:
|
|
|
|
RUBY_MARK_UNLESS_NULL(block->as.symbol);
|
|
|
|
break;
|
|
|
|
case block_type_proc:
|
|
|
|
RUBY_MARK_UNLESS_NULL(block->as.proc);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2009-09-21 04:12:12 -04:00
|
|
|
|
2007-02-02 10:21:41 -05:00
|
|
|
static void
|
|
|
|
proc_mark(void *ptr)
|
|
|
|
{
|
2014-09-12 17:34:12 -04:00
|
|
|
rb_proc_t *proc = ptr;
|
2016-07-28 07:02:30 -04:00
|
|
|
block_mark(&proc->block);
|
2007-06-24 22:44:20 -04:00
|
|
|
RUBY_MARK_LEAVE("proc");
|
2007-02-02 10:21:41 -05:00
|
|
|
}
|
|
|
|
|
2015-10-15 10:56:05 -04:00
|
|
|
typedef struct {
|
|
|
|
rb_proc_t basic;
|
2016-07-28 07:02:30 -04:00
|
|
|
VALUE env[VM_ENV_DATA_SIZE + 1]; /* ..., envval */
|
2015-11-10 04:24:41 -05:00
|
|
|
} cfunc_proc_t;
|
2015-10-15 10:56:05 -04:00
|
|
|
|
2009-07-08 04:13:41 -04:00
|
|
|
static size_t
|
2009-09-08 22:11:35 -04:00
|
|
|
proc_memsize(const void *ptr)
|
2009-07-08 04:13:41 -04:00
|
|
|
{
|
2015-10-15 10:56:05 -04:00
|
|
|
const rb_proc_t *proc = ptr;
|
2016-07-28 07:02:30 -04:00
|
|
|
if (proc->block.as.captured.ep == ((const cfunc_proc_t *)ptr)->env+1)
|
2015-11-10 04:24:41 -05:00
|
|
|
return sizeof(cfunc_proc_t);
|
2014-09-12 17:34:12 -04:00
|
|
|
return sizeof(rb_proc_t);
|
2009-07-08 04:13:41 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static const rb_data_type_t proc_data_type = {
|
|
|
|
"proc",
|
2010-07-18 03:31:54 -04:00
|
|
|
{
|
|
|
|
proc_mark,
|
2014-09-12 17:34:12 -04:00
|
|
|
RUBY_TYPED_DEFAULT_FREE,
|
2010-07-18 03:31:54 -04:00
|
|
|
proc_memsize,
|
|
|
|
},
|
2016-07-28 07:02:30 -04:00
|
|
|
0, 0, RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED
|
2009-07-08 04:13:41 -04:00
|
|
|
};
|
|
|
|
|
* blockinlining.c: remove "yarv" prefix.
* array.c, numeric.c: ditto.
* insnhelper.ci, insns.def, vm_evalbody.ci: ditto.
* yarvcore.c: removed.
* yarvcore.h: renamed to core.h.
* cont.c, debug.c, error.c, process.c, signal.c : ditto.
* ext/probeprofiler/probeprofiler.c: ditto.
* id.c, id.h: added.
* inits.c: ditto.
* compile.c: rename internal functions.
* compile.h: fix debug flag.
* eval.c, object.c, vm.c: remove ruby_top_self.
use rb_vm_top_self() instead.
* eval_intern.h, eval_load: ditto.
* gc.c: rename yarv_machine_stack_mark() to
rb_gc_mark_machine_stack().
* insnhelper.h: remove unused macros.
* iseq.c: add iseq_compile() to create iseq object
from source string.
* proc.c: rename a internal function.
* template/insns.inc.tmpl: remove YARV prefix.
* thread.c:
* vm.c (rb_iseq_eval): added.
* vm.c: move some functions from yarvcore.c.
* vm_dump.c: fix to remove compiler warning.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@12741 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-07-12 00:25:46 -04:00
|
|
|
VALUE
|
2015-05-16 08:21:25 -04:00
|
|
|
rb_proc_alloc(VALUE klass)
|
2007-02-02 10:21:41 -05:00
|
|
|
{
|
2015-05-16 08:21:25 -04:00
|
|
|
rb_proc_t *proc;
|
|
|
|
return TypedData_Make_Struct(klass, rb_proc_t, &proc_data_type, proc);
|
2007-02-02 10:21:41 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
* blockinlining.c, compile.c, compile.h, error.c, eval.c,
eval_intern.h, eval_jump.h, eval_load.c, eval_method.h,
eval_safe.h, gc.c, insnhelper.h, insns.def, iseq.c, proc.c,
process.c, signal.c, thread.c, thread_pthread.ci, thread_win32.ci,
vm.c, vm.h, vm_dump.c, vm_evalbody.ci, vm_macro.def,
yarv.h, yarvcore.h, yarvcore.c: change type and macro names:
* yarv_*_t -> rb_*_t
* yarv_*_struct -> rb_*_struct
* yarv_tag -> rb_vm_tag
* YARV_* -> RUBY_VM_*
* proc.c, vm.c: move functions about env object creation
from proc.c to vm.c.
* proc.c, yarvcore.c: fix rb_cVM initialization place.
* inits.c: change Init_ISeq() order (after Init_VM).
* ruby.h, proc.c: change declaration place of rb_cEnv
from proc.c to ruby.c.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@11651 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-02-06 14:00:03 -05:00
|
|
|
rb_obj_is_proc(VALUE proc)
|
2007-02-02 10:21:41 -05:00
|
|
|
{
|
2009-07-08 04:13:41 -04:00
|
|
|
if (rb_typeddata_is_kind_of(proc, &proc_data_type)) {
|
2007-02-02 10:21:41 -05:00
|
|
|
return Qtrue;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return Qfalse;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-28 07:02:30 -04:00
|
|
|
VALUE rb_proc_create(VALUE klass, const struct rb_block *block,
|
|
|
|
int8_t safe_level, int8_t is_from_method, int8_t is_lambda);
|
|
|
|
|
2009-09-21 07:06:32 -04:00
|
|
|
/* :nodoc: */
|
2007-02-02 10:21:41 -05:00
|
|
|
static VALUE
|
|
|
|
proc_dup(VALUE self)
|
|
|
|
{
|
2014-09-12 16:57:45 -04:00
|
|
|
VALUE procval;
|
|
|
|
rb_proc_t *src;
|
|
|
|
|
2007-02-02 10:21:41 -05:00
|
|
|
GetProcPtr(self, src);
|
2016-07-28 07:02:30 -04:00
|
|
|
procval = rb_proc_create(rb_cProc, &src->block,
|
|
|
|
src->safe_level, src->is_from_method, src->is_lambda);
|
2014-09-12 16:57:45 -04:00
|
|
|
RB_GC_GUARD(self); /* for: body = proc_dup(body) */
|
2007-02-02 10:21:41 -05:00
|
|
|
return procval;
|
|
|
|
}
|
|
|
|
|
2009-09-21 07:06:32 -04:00
|
|
|
/* :nodoc: */
|
2007-02-02 10:21:41 -05:00
|
|
|
static VALUE
|
|
|
|
proc_clone(VALUE self)
|
|
|
|
{
|
|
|
|
VALUE procval = proc_dup(self);
|
|
|
|
CLONESETUP(procval, self);
|
|
|
|
return procval;
|
|
|
|
}
|
|
|
|
|
2007-12-23 10:36:00 -05:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-17 17:07:33 -04:00
|
|
|
* prc.lambda? -> true or false
|
2007-12-23 10:36:00 -05:00
|
|
|
*
|
2011-05-27 09:55:43 -04:00
|
|
|
* Returns +true+ for a Proc object for which argument handling is rigid.
|
|
|
|
* Such procs are typically generated by +lambda+.
|
2007-12-23 10:36:00 -05:00
|
|
|
*
|
2011-05-27 09:55:43 -04:00
|
|
|
* A Proc object generated by +proc+ ignores extra arguments.
|
2007-12-25 22:49:10 -05:00
|
|
|
*
|
2010-05-17 17:07:33 -04:00
|
|
|
* proc {|a,b| [a,b] }.call(1,2,3) #=> [1,2]
|
2007-12-25 22:49:10 -05:00
|
|
|
*
|
2011-05-27 09:55:43 -04:00
|
|
|
* It provides +nil+ for missing arguments.
|
2007-12-25 22:49:10 -05:00
|
|
|
*
|
2010-05-17 17:07:33 -04:00
|
|
|
* proc {|a,b| [a,b] }.call(1) #=> [1,nil]
|
2007-12-25 22:49:10 -05:00
|
|
|
*
|
2011-05-27 09:55:43 -04:00
|
|
|
* It expands a single array argument.
|
2007-12-25 22:49:10 -05:00
|
|
|
*
|
2010-05-17 17:07:33 -04:00
|
|
|
* proc {|a,b| [a,b] }.call([1,2]) #=> [1,2]
|
2007-12-25 22:49:10 -05:00
|
|
|
*
|
2011-05-27 09:55:43 -04:00
|
|
|
* A Proc object generated by +lambda+ doesn't have such tricks.
|
2007-12-25 22:49:10 -05:00
|
|
|
*
|
2010-05-17 17:07:33 -04:00
|
|
|
* lambda {|a,b| [a,b] }.call(1,2,3) #=> ArgumentError
|
|
|
|
* lambda {|a,b| [a,b] }.call(1) #=> ArgumentError
|
|
|
|
* lambda {|a,b| [a,b] }.call([1,2]) #=> ArgumentError
|
2007-12-25 22:49:10 -05:00
|
|
|
*
|
|
|
|
* Proc#lambda? is a predicate for the tricks.
|
2011-05-27 09:55:43 -04:00
|
|
|
* It returns +true+ if no tricks apply.
|
2007-12-23 10:36:00 -05:00
|
|
|
*
|
2010-05-17 17:07:33 -04:00
|
|
|
* lambda {}.lambda? #=> true
|
|
|
|
* proc {}.lambda? #=> false
|
2007-12-25 22:49:10 -05:00
|
|
|
*
|
2011-05-27 09:55:43 -04:00
|
|
|
* Proc.new is the same as +proc+.
|
2007-12-24 01:56:06 -05:00
|
|
|
*
|
2010-05-17 17:07:33 -04:00
|
|
|
* Proc.new {}.lambda? #=> false
|
2007-12-23 10:36:00 -05:00
|
|
|
*
|
2011-05-27 09:55:43 -04:00
|
|
|
* +lambda+, +proc+ and Proc.new preserve the tricks of
|
|
|
|
* a Proc object given by <code>&</code> argument.
|
2007-12-25 22:49:10 -05:00
|
|
|
*
|
2010-05-17 17:07:33 -04:00
|
|
|
* lambda(&lambda {}).lambda? #=> true
|
|
|
|
* proc(&lambda {}).lambda? #=> true
|
|
|
|
* Proc.new(&lambda {}).lambda? #=> true
|
2007-12-25 22:49:10 -05:00
|
|
|
*
|
2010-05-17 17:07:33 -04:00
|
|
|
* lambda(&proc {}).lambda? #=> false
|
|
|
|
* proc(&proc {}).lambda? #=> false
|
|
|
|
* Proc.new(&proc {}).lambda? #=> false
|
2007-12-25 22:49:10 -05:00
|
|
|
*
|
2011-05-27 09:55:43 -04:00
|
|
|
* A Proc object generated by <code>&</code> argument has the tricks
|
2007-12-23 10:36:00 -05:00
|
|
|
*
|
|
|
|
* def n(&b) b.lambda? end
|
2010-05-17 17:07:33 -04:00
|
|
|
* n {} #=> false
|
2007-12-25 22:49:10 -05:00
|
|
|
*
|
2011-05-27 09:55:43 -04:00
|
|
|
* The <code>&</code> argument preserves the tricks if a Proc object
|
|
|
|
* is given by <code>&</code> argument.
|
2007-12-25 22:49:10 -05:00
|
|
|
*
|
2010-05-17 17:07:33 -04:00
|
|
|
* n(&lambda {}) #=> true
|
|
|
|
* n(&proc {}) #=> false
|
|
|
|
* n(&Proc.new {}) #=> false
|
2007-12-25 22:49:10 -05:00
|
|
|
*
|
|
|
|
* A Proc object converted from a method has no tricks.
|
|
|
|
*
|
|
|
|
* def m() end
|
2010-05-17 17:07:33 -04:00
|
|
|
* method(:m).to_proc.lambda? #=> true
|
2007-12-25 22:49:10 -05:00
|
|
|
*
|
2010-05-17 17:07:33 -04:00
|
|
|
* n(&method(:m)) #=> true
|
|
|
|
* n(&method(:m).to_proc) #=> true
|
2007-12-23 10:36:00 -05:00
|
|
|
*
|
2011-05-27 09:55:43 -04:00
|
|
|
* +define_method+ is treated the same as method definition.
|
2007-12-25 22:49:10 -05:00
|
|
|
* The defined method has no tricks.
|
|
|
|
*
|
2007-12-24 13:36:30 -05:00
|
|
|
* class C
|
|
|
|
* define_method(:d) {}
|
|
|
|
* end
|
2011-05-27 09:55:43 -04:00
|
|
|
* C.new.d(1,2) #=> ArgumentError
|
2010-05-17 17:07:33 -04:00
|
|
|
* C.new.method(:d).to_proc.lambda? #=> true
|
2007-12-24 13:36:30 -05:00
|
|
|
*
|
2011-05-27 09:55:43 -04:00
|
|
|
* +define_method+ always defines a method without the tricks,
|
2007-12-25 22:49:10 -05:00
|
|
|
* even if a non-lambda Proc object is given.
|
2011-05-27 09:55:43 -04:00
|
|
|
* This is the only exception for which the tricks are not preserved.
|
2007-12-25 22:49:10 -05:00
|
|
|
*
|
|
|
|
* class C
|
|
|
|
* define_method(:e, &proc {})
|
|
|
|
* end
|
2010-05-17 17:07:33 -04:00
|
|
|
* C.new.e(1,2) #=> ArgumentError
|
|
|
|
* C.new.method(:e).to_proc.lambda? #=> true
|
2007-12-25 22:49:10 -05:00
|
|
|
*
|
2016-11-08 14:37:59 -05:00
|
|
|
* This exception ensures that methods never have tricks
|
2011-05-27 09:55:43 -04:00
|
|
|
* and makes it easy to have wrappers to define methods that behave as usual.
|
2007-12-25 22:49:10 -05:00
|
|
|
*
|
2011-05-27 09:55:43 -04:00
|
|
|
* class C
|
|
|
|
* def self.def2(name, &body)
|
2007-12-25 22:49:10 -05:00
|
|
|
* define_method(name, &body)
|
|
|
|
* end
|
2011-05-27 09:55:43 -04:00
|
|
|
*
|
2007-12-25 22:49:10 -05:00
|
|
|
* def2(:f) {}
|
|
|
|
* end
|
2010-05-17 17:07:33 -04:00
|
|
|
* C.new.f(1,2) #=> ArgumentError
|
2007-12-25 22:49:10 -05:00
|
|
|
*
|
2011-05-27 09:55:43 -04:00
|
|
|
* The wrapper <i>def2</i> defines a method which has no tricks.
|
2007-12-25 22:49:10 -05:00
|
|
|
*
|
2007-12-23 10:36:00 -05:00
|
|
|
*/
|
|
|
|
|
2009-10-24 12:53:11 -04:00
|
|
|
VALUE
|
|
|
|
rb_proc_lambda_p(VALUE procval)
|
2007-06-05 13:26:00 -04:00
|
|
|
{
|
|
|
|
rb_proc_t *proc;
|
|
|
|
GetProcPtr(procval, proc);
|
|
|
|
|
|
|
|
return proc->is_lambda ? Qtrue : Qfalse;
|
|
|
|
}
|
|
|
|
|
2007-02-02 10:21:41 -05:00
|
|
|
/* Binding */
|
|
|
|
|
|
|
|
static void
|
|
|
|
binding_free(void *ptr)
|
|
|
|
{
|
* blockinlining.c, compile.c, compile.h, error.c, eval.c,
eval_intern.h, eval_jump.h, eval_load.c, eval_method.h,
eval_safe.h, gc.c, insnhelper.h, insns.def, iseq.c, proc.c,
process.c, signal.c, thread.c, thread_pthread.ci, thread_win32.ci,
vm.c, vm.h, vm_dump.c, vm_evalbody.ci, vm_macro.def,
yarv.h, yarvcore.h, yarvcore.c: change type and macro names:
* yarv_*_t -> rb_*_t
* yarv_*_struct -> rb_*_struct
* yarv_tag -> rb_vm_tag
* YARV_* -> RUBY_VM_*
* proc.c, vm.c: move functions about env object creation
from proc.c to vm.c.
* proc.c, yarvcore.c: fix rb_cVM initialization place.
* inits.c: change Init_ISeq() order (after Init_VM).
* ruby.h, proc.c: change declaration place of rb_cEnv
from proc.c to ruby.c.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@11651 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-02-06 14:00:03 -05:00
|
|
|
rb_binding_t *bind;
|
2007-06-24 22:44:20 -04:00
|
|
|
RUBY_FREE_ENTER("binding");
|
2007-02-02 10:21:41 -05:00
|
|
|
if (ptr) {
|
|
|
|
bind = ptr;
|
* bignum.c (big_rshift), compile.c (validate_label,
iseq_build_from_ary_exception), cont.c (cont_capture), dir.c
(dir_open_dir), gc.c (objspace_each_objects), io.c (pipe_open)
(rb_io_advise), parse.y (parser_compile_string)
(rb_parser_compile_file), proc.c (binding_free), process.c
(rb_proc_exec_n, rb_seteuid_core, proc_setegid, rb_setegid_core)
(p_uid_exchange, p_gid_exchange), regparse.c (strdup_with_null),
signal.c (sig_dfl), vm.c (rb_iseq_eval, rb_iseq_eval_main),
vm_insnhelper.c (vm_expandarray): suppress
unused-but-set-variable warnings.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@33951 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2011-12-05 04:57:00 -05:00
|
|
|
ruby_xfree(bind);
|
2007-02-02 10:21:41 -05:00
|
|
|
}
|
2007-06-24 22:44:20 -04:00
|
|
|
RUBY_FREE_LEAVE("binding");
|
2007-02-02 10:21:41 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
binding_mark(void *ptr)
|
|
|
|
{
|
2015-07-15 04:29:22 -04:00
|
|
|
rb_binding_t *bind = ptr;
|
|
|
|
|
2007-06-24 22:44:20 -04:00
|
|
|
RUBY_MARK_ENTER("binding");
|
2015-07-15 04:29:22 -04:00
|
|
|
|
2016-07-28 07:02:30 -04:00
|
|
|
block_mark(&bind->block);
|
|
|
|
|
2015-07-15 04:29:22 -04:00
|
|
|
RUBY_MARK_UNLESS_NULL(bind->path);
|
|
|
|
|
2007-06-24 22:44:20 -04:00
|
|
|
RUBY_MARK_LEAVE("binding");
|
2007-02-02 10:21:41 -05:00
|
|
|
}
|
|
|
|
|
2009-07-08 05:28:09 -04:00
|
|
|
static size_t
|
2009-09-08 22:11:35 -04:00
|
|
|
binding_memsize(const void *ptr)
|
2009-07-08 05:28:09 -04:00
|
|
|
{
|
2015-12-08 19:38:32 -05:00
|
|
|
return sizeof(rb_binding_t);
|
2009-07-08 05:28:09 -04:00
|
|
|
}
|
|
|
|
|
2013-07-22 03:32:52 -04:00
|
|
|
const rb_data_type_t ruby_binding_data_type = {
|
2009-07-08 05:28:09 -04:00
|
|
|
"binding",
|
2010-07-18 03:31:54 -04:00
|
|
|
{
|
|
|
|
binding_mark,
|
|
|
|
binding_free,
|
|
|
|
binding_memsize,
|
|
|
|
},
|
2014-12-01 01:38:04 -05:00
|
|
|
0, 0, RUBY_TYPED_FREE_IMMEDIATELY
|
2009-07-08 05:28:09 -04:00
|
|
|
};
|
|
|
|
|
2014-10-18 07:46:31 -04:00
|
|
|
VALUE
|
|
|
|
rb_binding_alloc(VALUE klass)
|
2007-02-02 10:21:41 -05:00
|
|
|
{
|
|
|
|
VALUE obj;
|
* blockinlining.c, compile.c, compile.h, error.c, eval.c,
eval_intern.h, eval_jump.h, eval_load.c, eval_method.h,
eval_safe.h, gc.c, insnhelper.h, insns.def, iseq.c, proc.c,
process.c, signal.c, thread.c, thread_pthread.ci, thread_win32.ci,
vm.c, vm.h, vm_dump.c, vm_evalbody.ci, vm_macro.def,
yarv.h, yarvcore.h, yarvcore.c: change type and macro names:
* yarv_*_t -> rb_*_t
* yarv_*_struct -> rb_*_struct
* yarv_tag -> rb_vm_tag
* YARV_* -> RUBY_VM_*
* proc.c, vm.c: move functions about env object creation
from proc.c to vm.c.
* proc.c, yarvcore.c: fix rb_cVM initialization place.
* inits.c: change Init_ISeq() order (after Init_VM).
* ruby.h, proc.c: change declaration place of rb_cEnv
from proc.c to ruby.c.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@11651 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-02-06 14:00:03 -05:00
|
|
|
rb_binding_t *bind;
|
2013-07-22 03:32:52 -04:00
|
|
|
obj = TypedData_Make_Struct(klass, rb_binding_t, &ruby_binding_data_type, bind);
|
2007-02-02 10:21:41 -05:00
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
2009-09-21 07:06:32 -04:00
|
|
|
/* :nodoc: */
|
2007-02-02 10:21:41 -05:00
|
|
|
static VALUE
|
|
|
|
binding_dup(VALUE self)
|
|
|
|
{
|
2014-10-18 07:46:31 -04:00
|
|
|
VALUE bindval = rb_binding_alloc(rb_cBinding);
|
* blockinlining.c, compile.c, compile.h, error.c, eval.c,
eval_intern.h, eval_jump.h, eval_load.c, eval_method.h,
eval_safe.h, gc.c, insnhelper.h, insns.def, iseq.c, proc.c,
process.c, signal.c, thread.c, thread_pthread.ci, thread_win32.ci,
vm.c, vm.h, vm_dump.c, vm_evalbody.ci, vm_macro.def,
yarv.h, yarvcore.h, yarvcore.c: change type and macro names:
* yarv_*_t -> rb_*_t
* yarv_*_struct -> rb_*_struct
* yarv_tag -> rb_vm_tag
* YARV_* -> RUBY_VM_*
* proc.c, vm.c: move functions about env object creation
from proc.c to vm.c.
* proc.c, yarvcore.c: fix rb_cVM initialization place.
* inits.c: change Init_ISeq() order (after Init_VM).
* ruby.h, proc.c: change declaration place of rb_cEnv
from proc.c to ruby.c.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@11651 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-02-06 14:00:03 -05:00
|
|
|
rb_binding_t *src, *dst;
|
2007-02-02 10:21:41 -05:00
|
|
|
GetBindingPtr(self, src);
|
|
|
|
GetBindingPtr(bindval, dst);
|
2016-07-28 07:02:30 -04:00
|
|
|
dst->block = src->block;
|
2012-06-03 22:49:37 -04:00
|
|
|
dst->path = src->path;
|
|
|
|
dst->first_lineno = src->first_lineno;
|
2007-02-02 10:21:41 -05:00
|
|
|
return bindval;
|
|
|
|
}
|
|
|
|
|
2009-09-21 07:06:32 -04:00
|
|
|
/* :nodoc: */
|
2007-02-02 10:21:41 -05:00
|
|
|
static VALUE
|
|
|
|
binding_clone(VALUE self)
|
|
|
|
{
|
|
|
|
VALUE bindval = binding_dup(self);
|
|
|
|
CLONESETUP(bindval, self);
|
|
|
|
return bindval;
|
|
|
|
}
|
|
|
|
|
2012-08-22 01:12:31 -04:00
|
|
|
VALUE
|
|
|
|
rb_binding_new(void)
|
|
|
|
{
|
|
|
|
rb_thread_t *th = GET_THREAD();
|
2014-10-18 07:46:41 -04:00
|
|
|
return rb_vm_make_binding(th, th->cfp);
|
2012-08-22 01:12:31 -04:00
|
|
|
}
|
|
|
|
|
2007-02-02 10:21:41 -05:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* binding -> a_binding
|
2009-02-22 09:23:33 -05:00
|
|
|
*
|
2007-02-02 10:21:41 -05:00
|
|
|
* Returns a +Binding+ object, describing the variable and
|
|
|
|
* method bindings at the point of call. This object can be used when
|
|
|
|
* calling +eval+ to execute the evaluated command in this
|
2011-05-27 09:55:43 -04:00
|
|
|
* environment. See also the description of class +Binding+.
|
2009-02-22 09:23:33 -05:00
|
|
|
*
|
2011-03-07 03:44:45 -05:00
|
|
|
* def get_binding(param)
|
2007-02-02 10:21:41 -05:00
|
|
|
* return binding
|
|
|
|
* end
|
2011-03-07 03:44:45 -05:00
|
|
|
* b = get_binding("hello")
|
2007-02-02 10:21:41 -05:00
|
|
|
* eval("param", b) #=> "hello"
|
|
|
|
*/
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
rb_f_binding(VALUE self)
|
|
|
|
{
|
|
|
|
return rb_binding_new();
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-17 17:07:33 -04:00
|
|
|
* binding.eval(string [, filename [,lineno]]) -> obj
|
2007-02-02 10:21:41 -05:00
|
|
|
*
|
|
|
|
* Evaluates the Ruby expression(s) in <em>string</em>, in the
|
|
|
|
* <em>binding</em>'s context. If the optional <em>filename</em> and
|
|
|
|
* <em>lineno</em> parameters are present, they will be used when
|
|
|
|
* reporting syntax errors.
|
|
|
|
*
|
2011-03-07 03:44:45 -05:00
|
|
|
* def get_binding(param)
|
2007-02-02 10:21:41 -05:00
|
|
|
* return binding
|
|
|
|
* end
|
2011-03-07 03:44:45 -05:00
|
|
|
* b = get_binding("hello")
|
2007-02-02 10:21:41 -05:00
|
|
|
* b.eval("param") #=> "hello"
|
|
|
|
*/
|
|
|
|
|
|
|
|
static VALUE
|
2007-02-05 13:50:35 -05:00
|
|
|
bind_eval(int argc, VALUE *argv, VALUE bindval)
|
2007-02-02 10:21:41 -05:00
|
|
|
{
|
2007-02-05 13:50:35 -05:00
|
|
|
VALUE args[4];
|
|
|
|
|
|
|
|
rb_scan_args(argc, argv, "12", &args[0], &args[2], &args[3]);
|
|
|
|
args[1] = bindval;
|
|
|
|
return rb_f_eval(argc+1, args, Qnil /* self will be searched in eval */);
|
2007-02-02 10:21:41 -05:00
|
|
|
}
|
|
|
|
|
2016-07-28 07:02:30 -04:00
|
|
|
static const VALUE *
|
2016-07-28 15:13:26 -04:00
|
|
|
get_local_variable_ptr(const rb_env_t *env, ID lid)
|
2013-08-09 05:51:00 -04:00
|
|
|
{
|
|
|
|
do {
|
2016-08-02 21:50:50 -04:00
|
|
|
if (!VM_ENV_FLAGS(env->ep, VM_FRAME_FLAG_CFRAME)) {
|
|
|
|
const rb_iseq_t *iseq = env->iseq;
|
|
|
|
unsigned int i;
|
2013-08-09 05:51:00 -04:00
|
|
|
|
2016-08-02 21:50:50 -04:00
|
|
|
VM_ASSERT(rb_obj_is_iseq((VALUE)iseq));
|
2013-08-09 05:51:00 -04:00
|
|
|
|
2015-07-21 18:52:59 -04:00
|
|
|
for (i=0; i<iseq->body->local_table_size; i++) {
|
|
|
|
if (iseq->body->local_table[i] == lid) {
|
2015-05-21 07:52:21 -04:00
|
|
|
return &env->env[i];
|
|
|
|
}
|
2013-08-09 05:51:00 -04:00
|
|
|
}
|
|
|
|
}
|
2015-05-21 04:45:57 -04:00
|
|
|
else {
|
|
|
|
return NULL;
|
|
|
|
}
|
2016-07-28 15:13:26 -04:00
|
|
|
} while ((env = rb_vm_env_prev_env(env)) != NULL);
|
2013-08-09 05:51:00 -04:00
|
|
|
|
2015-05-21 04:45:57 -04:00
|
|
|
return NULL;
|
2013-08-09 05:51:00 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* check local variable name.
|
|
|
|
* returns ID if it's an already interned symbol, or 0 with setting
|
|
|
|
* local name in String to *namep.
|
|
|
|
*/
|
|
|
|
static ID
|
|
|
|
check_local_id(VALUE bindval, volatile VALUE *pname)
|
|
|
|
{
|
|
|
|
ID lid = rb_check_id(pname);
|
2015-10-28 02:24:12 -04:00
|
|
|
VALUE name = *pname;
|
2013-08-09 05:51:00 -04:00
|
|
|
|
|
|
|
if (lid) {
|
2013-08-09 09:24:37 -04:00
|
|
|
if (!rb_is_local_id(lid)) {
|
2015-10-28 02:24:12 -04:00
|
|
|
rb_name_err_raise("wrong local variable name `%1$s' for %2$s",
|
|
|
|
bindval, ID2SYM(lid));
|
2013-08-09 05:51:00 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2015-10-28 02:24:12 -04:00
|
|
|
if (!rb_is_local_name(name)) {
|
|
|
|
rb_name_err_raise("wrong local variable name `%1$s' for %2$s",
|
|
|
|
bindval, name);
|
|
|
|
}
|
2013-08-09 05:51:00 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return lid;
|
|
|
|
}
|
|
|
|
|
2013-12-24 11:03:12 -05:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* binding.local_variables -> Array
|
|
|
|
*
|
|
|
|
* Returns the +symbol+ names of the binding's local variables
|
|
|
|
*
|
|
|
|
* def foo
|
|
|
|
* a = 1
|
|
|
|
* 2.times do |n|
|
|
|
|
* binding.local_variables #=> [:a, :n]
|
|
|
|
* end
|
|
|
|
* end
|
|
|
|
*
|
|
|
|
* This method is short version of the following code.
|
|
|
|
*
|
|
|
|
* binding.eval("local_variables")
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
static VALUE
|
|
|
|
bind_local_variables(VALUE bindval)
|
|
|
|
{
|
|
|
|
const rb_binding_t *bind;
|
2015-07-14 13:36:36 -04:00
|
|
|
const rb_env_t *env;
|
2013-12-24 11:03:12 -05:00
|
|
|
|
|
|
|
GetBindingPtr(bindval, bind);
|
2016-07-28 15:13:26 -04:00
|
|
|
env = VM_ENV_ENVVAL_PTR(vm_block_ep(&bind->block));
|
2015-07-14 13:36:36 -04:00
|
|
|
return rb_vm_env_local_variables(env);
|
2013-12-24 11:03:12 -05:00
|
|
|
}
|
|
|
|
|
2013-08-09 05:51:00 -04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* binding.local_variable_get(symbol) -> obj
|
|
|
|
*
|
|
|
|
* Returns a +value+ of local variable +symbol+.
|
|
|
|
*
|
2013-08-09 20:19:44 -04:00
|
|
|
* def foo
|
|
|
|
* a = 1
|
|
|
|
* binding.local_variable_get(:a) #=> 1
|
|
|
|
* binding.local_variable_get(:b) #=> NameError
|
|
|
|
* end
|
2013-08-09 05:51:00 -04:00
|
|
|
*
|
|
|
|
* This method is short version of the following code.
|
|
|
|
*
|
2013-08-09 20:19:44 -04:00
|
|
|
* binding.eval("#{symbol}")
|
2013-08-09 05:51:00 -04:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
static VALUE
|
|
|
|
bind_local_variable_get(VALUE bindval, VALUE sym)
|
|
|
|
{
|
|
|
|
ID lid = check_local_id(bindval, &sym);
|
|
|
|
const rb_binding_t *bind;
|
|
|
|
const VALUE *ptr;
|
|
|
|
|
|
|
|
if (!lid) goto undefined;
|
|
|
|
|
|
|
|
GetBindingPtr(bindval, bind);
|
|
|
|
|
2016-07-28 15:13:26 -04:00
|
|
|
if ((ptr = get_local_variable_ptr(VM_ENV_ENVVAL_PTR(vm_block_ep(&bind->block)), lid)) == NULL) {
|
2015-10-28 02:24:12 -04:00
|
|
|
sym = ID2SYM(lid);
|
2013-08-09 05:51:00 -04:00
|
|
|
undefined:
|
2015-10-28 02:24:12 -04:00
|
|
|
rb_name_err_raise("local variable `%1$s' not defined for %2$s",
|
|
|
|
bindval, sym);
|
2013-08-09 05:51:00 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return *ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* binding.local_variable_set(symbol, obj) -> obj
|
|
|
|
*
|
|
|
|
* Set local variable named +symbol+ as +obj+.
|
|
|
|
*
|
2013-08-09 20:19:44 -04:00
|
|
|
* def foo
|
|
|
|
* a = 1
|
2015-05-07 09:28:03 -04:00
|
|
|
* bind = binding
|
|
|
|
* bind.local_variable_set(:a, 2) # set existing local variable `a'
|
|
|
|
* bind.local_variable_set(:b, 3) # create new local variable `b'
|
|
|
|
* # `b' exists only in binding.
|
|
|
|
* p bind.local_variable_get(:a) #=> 2
|
|
|
|
* p bind.local_variable_get(:b) #=> 3
|
2013-08-09 20:19:44 -04:00
|
|
|
* p a #=> 2
|
|
|
|
* p b #=> NameError
|
|
|
|
* end
|
2013-08-09 05:51:00 -04:00
|
|
|
*
|
|
|
|
* This method is a similar behavior of the following code
|
|
|
|
*
|
|
|
|
* binding.eval("#{symbol} = #{obj}")
|
|
|
|
*
|
|
|
|
* if obj can be dumped in Ruby code.
|
|
|
|
*/
|
|
|
|
static VALUE
|
|
|
|
bind_local_variable_set(VALUE bindval, VALUE sym, VALUE val)
|
|
|
|
{
|
|
|
|
ID lid = check_local_id(bindval, &sym);
|
|
|
|
rb_binding_t *bind;
|
2016-07-28 07:02:30 -04:00
|
|
|
const VALUE *ptr;
|
2016-07-28 15:13:26 -04:00
|
|
|
const rb_env_t *env;
|
2013-08-09 05:51:00 -04:00
|
|
|
|
|
|
|
if (!lid) lid = rb_intern_str(sym);
|
|
|
|
|
|
|
|
GetBindingPtr(bindval, bind);
|
2016-07-28 15:13:26 -04:00
|
|
|
env = VM_ENV_ENVVAL_PTR(vm_block_ep(&bind->block));
|
|
|
|
if ((ptr = get_local_variable_ptr(env, lid)) == NULL) {
|
2013-08-09 05:51:00 -04:00
|
|
|
/* not found. create new env */
|
|
|
|
ptr = rb_binding_add_dynavars(bind, 1, &lid);
|
2016-07-28 15:13:26 -04:00
|
|
|
env = VM_ENV_ENVVAL_PTR(vm_block_ep(&bind->block));
|
2013-08-09 05:51:00 -04:00
|
|
|
}
|
|
|
|
|
2016-07-28 15:13:26 -04:00
|
|
|
RB_OBJ_WRITE(env, ptr, val);
|
2013-08-09 05:51:00 -04:00
|
|
|
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* binding.local_variable_defined?(symbol) -> obj
|
|
|
|
*
|
|
|
|
* Returns a +true+ if a local variable +symbol+ exists.
|
|
|
|
*
|
2013-08-09 20:19:44 -04:00
|
|
|
* def foo
|
|
|
|
* a = 1
|
|
|
|
* binding.local_variable_defined?(:a) #=> true
|
|
|
|
* binding.local_variable_defined?(:b) #=> false
|
|
|
|
* end
|
2013-08-09 05:51:00 -04:00
|
|
|
*
|
|
|
|
* This method is short version of the following code.
|
|
|
|
*
|
2013-08-09 20:19:44 -04:00
|
|
|
* binding.eval("defined?(#{symbol}) == 'local-variable'")
|
2013-08-09 05:51:00 -04:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
static VALUE
|
|
|
|
bind_local_variable_defined_p(VALUE bindval, VALUE sym)
|
|
|
|
{
|
|
|
|
ID lid = check_local_id(bindval, &sym);
|
|
|
|
const rb_binding_t *bind;
|
|
|
|
|
|
|
|
if (!lid) return Qfalse;
|
|
|
|
|
|
|
|
GetBindingPtr(bindval, bind);
|
2016-07-28 15:13:26 -04:00
|
|
|
return get_local_variable_ptr(VM_ENV_ENVVAL_PTR(vm_block_ep(&bind->block)), lid) ? Qtrue : Qfalse;
|
2013-08-09 05:51:00 -04:00
|
|
|
}
|
|
|
|
|
2014-07-01 13:24:02 -04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* binding.receiver -> object
|
|
|
|
*
|
|
|
|
* Returns the bound receiver of the binding object.
|
|
|
|
*/
|
|
|
|
static VALUE
|
|
|
|
bind_receiver(VALUE bindval)
|
|
|
|
{
|
|
|
|
const rb_binding_t *bind;
|
|
|
|
GetBindingPtr(bindval, bind);
|
2016-07-28 07:02:30 -04:00
|
|
|
return vm_block_self(&bind->block);
|
2014-07-01 13:24:02 -04:00
|
|
|
}
|
|
|
|
|
2015-10-15 23:21:10 -04:00
|
|
|
static VALUE
|
2015-11-10 04:24:41 -05:00
|
|
|
cfunc_proc_new(VALUE klass, VALUE ifunc, int8_t is_lambda)
|
2015-10-15 23:21:10 -04:00
|
|
|
{
|
|
|
|
rb_proc_t *proc;
|
2015-11-10 04:24:41 -05:00
|
|
|
cfunc_proc_t *sproc;
|
|
|
|
VALUE procval = TypedData_Make_Struct(klass, cfunc_proc_t, &proc_data_type, sproc);
|
2016-07-28 07:02:30 -04:00
|
|
|
VALUE *ep;
|
|
|
|
|
2015-11-10 04:24:41 -05:00
|
|
|
proc = &sproc->basic;
|
2016-07-28 07:02:30 -04:00
|
|
|
vm_block_type_set(&proc->block, block_type_ifunc);
|
|
|
|
|
|
|
|
*(VALUE **)&proc->block.as.captured.ep = ep = sproc->env + VM_ENV_DATA_SIZE-1;
|
2016-08-02 20:16:34 -04:00
|
|
|
ep[VM_ENV_DATA_INDEX_FLAGS] = VM_FRAME_MAGIC_IFUNC | VM_FRAME_FLAG_CFRAME | VM_ENV_FLAG_LOCAL | VM_ENV_FLAG_ESCAPED;
|
2016-07-28 07:02:30 -04:00
|
|
|
ep[VM_ENV_DATA_INDEX_ME_CREF] = Qfalse;
|
|
|
|
ep[VM_ENV_DATA_INDEX_SPECVAL] = VM_BLOCK_HANDLER_NONE;
|
|
|
|
ep[VM_ENV_DATA_INDEX_ENV] = Qundef; /* envval */
|
|
|
|
|
|
|
|
/* self? */
|
|
|
|
RB_OBJ_WRITE(procval, &proc->block.as.captured.code.ifunc, ifunc);
|
2015-11-10 04:24:41 -05:00
|
|
|
proc->is_lambda = is_lambda;
|
2015-10-15 23:21:10 -04:00
|
|
|
return procval;
|
|
|
|
}
|
|
|
|
|
2015-11-10 04:24:41 -05:00
|
|
|
static VALUE
|
|
|
|
sym_proc_new(VALUE klass, VALUE sym)
|
|
|
|
{
|
2016-07-28 07:02:30 -04:00
|
|
|
VALUE procval = rb_proc_alloc(klass);
|
|
|
|
rb_proc_t *proc;
|
|
|
|
GetProcPtr(procval, proc);
|
|
|
|
|
|
|
|
vm_block_type_set(&proc->block, block_type_symbol);
|
|
|
|
RB_OBJ_WRITE(procval, &proc->block.as.symbol, sym);
|
|
|
|
return procval;
|
2015-11-10 04:24:41 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
|
|
|
rb_func_proc_new(rb_block_call_func_t func, VALUE val)
|
|
|
|
{
|
|
|
|
return cfunc_proc_new(rb_cProc, (VALUE)IFUNC_NEW(func, val, 0), 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
|
|
|
rb_func_lambda_new(rb_block_call_func_t func, VALUE val)
|
|
|
|
{
|
|
|
|
return cfunc_proc_new(rb_cProc, (VALUE)IFUNC_NEW(func, val, 0), 1);
|
|
|
|
}
|
|
|
|
|
2015-06-19 00:54:23 -04:00
|
|
|
static const char proc_without_block[] = "tried to create Proc object without a block";
|
|
|
|
|
2007-02-02 10:21:41 -05:00
|
|
|
static VALUE
|
2014-11-13 22:38:42 -05:00
|
|
|
proc_new(VALUE klass, int8_t is_lambda)
|
2007-02-02 10:21:41 -05:00
|
|
|
{
|
2016-07-28 07:02:30 -04:00
|
|
|
VALUE procval;
|
* blockinlining.c, error.c, eval.c, eval_error.h, eval_intern.h,
eval_jump.h, eval_load.c, eval_safe.h, gc.c, proc.c, signal.c,
thread.c, thread_pthread.ci, thread_win32.ci, vm.c, vm.h,
vm_dump.c, vm_evalbody.ci, yarvcore.c, yarvcore.h:
fix typo (rb_thead_t -> rb_thread_t).
* eval_intern.h: remove unused definitions.
* common.mk: fix around vm_opts.h path
and remove harmful argument passed to insns2vm.rb.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@11658 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-02-08 01:37:46 -05:00
|
|
|
rb_thread_t *th = GET_THREAD();
|
* blockinlining.c, compile.c, compile.h, error.c, eval.c,
eval_intern.h, eval_jump.h, eval_load.c, eval_method.h,
eval_safe.h, gc.c, insnhelper.h, insns.def, iseq.c, proc.c,
process.c, signal.c, thread.c, thread_pthread.ci, thread_win32.ci,
vm.c, vm.h, vm_dump.c, vm_evalbody.ci, vm_macro.def,
yarv.h, yarvcore.h, yarvcore.c: change type and macro names:
* yarv_*_t -> rb_*_t
* yarv_*_struct -> rb_*_struct
* yarv_tag -> rb_vm_tag
* YARV_* -> RUBY_VM_*
* proc.c, vm.c: move functions about env object creation
from proc.c to vm.c.
* proc.c, yarvcore.c: fix rb_cVM initialization place.
* inits.c: change Init_ISeq() order (after Init_VM).
* ruby.h, proc.c: change declaration place of rb_cEnv
from proc.c to ruby.c.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@11651 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-02-06 14:00:03 -05:00
|
|
|
rb_control_frame_t *cfp = th->cfp;
|
2016-07-28 07:02:30 -04:00
|
|
|
VALUE block_handler;
|
2007-02-02 10:21:41 -05:00
|
|
|
|
2016-07-28 07:02:30 -04:00
|
|
|
if ((block_handler = rb_vm_frame_block_handler(cfp)) == VM_BLOCK_HANDLER_NONE) {
|
2015-06-19 01:53:41 -04:00
|
|
|
#if !PROC_NEW_REQUIRES_BLOCK
|
* blockinlining.c, compile.c, compile.h, error.c, eval.c,
eval_intern.h, eval_jump.h, eval_load.c, eval_method.h,
eval_safe.h, gc.c, insnhelper.h, insns.def, iseq.c, proc.c,
process.c, signal.c, thread.c, thread_pthread.ci, thread_win32.ci,
vm.c, vm.h, vm_dump.c, vm_evalbody.ci, vm_macro.def,
yarv.h, yarvcore.h, yarvcore.c: change type and macro names:
* yarv_*_t -> rb_*_t
* yarv_*_struct -> rb_*_struct
* yarv_tag -> rb_vm_tag
* YARV_* -> RUBY_VM_*
* proc.c, vm.c: move functions about env object creation
from proc.c to vm.c.
* proc.c, yarvcore.c: fix rb_cVM initialization place.
* inits.c: change Init_ISeq() order (after Init_VM).
* ruby.h, proc.c: change declaration place of rb_cEnv
from proc.c to ruby.c.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@11651 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-02-06 14:00:03 -05:00
|
|
|
cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp);
|
2007-05-01 00:35:58 -04:00
|
|
|
|
2016-07-28 07:02:30 -04:00
|
|
|
if ((block_handler = rb_vm_frame_block_handler(cfp)) != VM_BLOCK_HANDLER_NONE) {
|
|
|
|
const VALUE *lep = rb_vm_ep_local_ep(cfp->ep);
|
|
|
|
|
|
|
|
if (VM_ENV_ESCAPED_P(lep)) {
|
|
|
|
procval = VM_ENV_PROCVAL(lep);
|
|
|
|
goto return_existing_proc;
|
|
|
|
}
|
|
|
|
|
2007-02-02 10:21:41 -05:00
|
|
|
if (is_lambda) {
|
2015-06-19 00:54:23 -04:00
|
|
|
rb_warn(proc_without_block);
|
2007-02-02 10:21:41 -05:00
|
|
|
}
|
|
|
|
}
|
2015-06-19 01:53:41 -04:00
|
|
|
#else
|
|
|
|
if (0)
|
|
|
|
#endif
|
2007-02-02 10:21:41 -05:00
|
|
|
else {
|
2015-06-19 00:54:23 -04:00
|
|
|
rb_raise(rb_eArgError, proc_without_block);
|
2007-02-02 10:21:41 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-28 07:02:30 -04:00
|
|
|
/* block is in cf */
|
|
|
|
switch (vm_block_handler_type(block_handler)) {
|
|
|
|
case block_handler_type_proc:
|
|
|
|
procval = VM_BH_TO_PROC(block_handler);
|
2008-12-24 22:51:35 -05:00
|
|
|
|
2016-07-28 07:02:30 -04:00
|
|
|
return_existing_proc:
|
|
|
|
if (RBASIC_CLASS(procval) == klass) {
|
2008-12-24 22:51:35 -05:00
|
|
|
return procval;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
VALUE newprocval = proc_dup(procval);
|
* include/ruby/ruby.h: constify RBasic::klass and add
RBASIC_CLASS(obj) macro which returns a class of `obj'.
This change is a part of RGENGC branch [ruby-trunk - Feature #8339].
* object.c: add new function rb_obj_reveal().
This function reveal interal (hidden) object by rb_obj_hide().
Note that do not change class before and after hiding.
Only permitted example is:
klass = RBASIC_CLASS(obj);
rb_obj_hide(obj);
....
rb_obj_reveal(obj, klass);
TODO: API design. rb_obj_reveal() should be replaced with others.
TODO: modify constified variables using cast may be harmful for
compiler's analysis and optimizaton.
Any idea to prohibt inserting RBasic::klass directly?
If rename RBasic::klass and force to use RBASIC_CLASS(obj),
then all codes such as `RBASIC(obj)->klass' will be
compilation error. Is it acceptable? (We have similar
experience at Ruby 1.9,
for example "RARRAY(ary)->ptr" to "RARRAY_PTR(ary)".
* internal.h: add some macros.
* RBASIC_CLEAR_CLASS(obj) clear RBasic::klass to make it internal
object.
* RBASIC_SET_CLASS(obj, cls) set RBasic::klass.
* RBASIC_SET_CLASS_RAW(obj, cls) same as RBASIC_SET_CLASS
without write barrier (planned).
* RCLASS_SET_SUPER(a, b) set super class of a.
* array.c, class.c, compile.c, encoding.c, enum.c, error.c, eval.c,
file.c, gc.c, hash.c, io.c, iseq.c, marshal.c, object.c,
parse.y, proc.c, process.c, random.c, ruby.c, sprintf.c,
string.c, thread.c, transcode.c, vm.c, vm_eval.c, win32/file.c:
Use above macros and functions to access RBasic::klass.
* ext/coverage/coverage.c, ext/readline/readline.c,
ext/socket/ancdata.c, ext/socket/init.c,
* ext/zlib/zlib.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@40691 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2013-05-13 06:49:11 -04:00
|
|
|
RBASIC_SET_CLASS(newprocval, klass);
|
2008-12-24 22:51:35 -05:00
|
|
|
return newprocval;
|
|
|
|
}
|
2016-07-28 07:02:30 -04:00
|
|
|
break;
|
2007-06-29 03:42:16 -04:00
|
|
|
|
2016-07-28 07:02:30 -04:00
|
|
|
case block_handler_type_symbol:
|
|
|
|
return (klass != rb_cProc) ?
|
|
|
|
sym_proc_new(klass, VM_BH_TO_SYMBOL(block_handler)) :
|
|
|
|
rb_sym_to_proc(VM_BH_TO_SYMBOL(block_handler));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case block_handler_type_ifunc:
|
|
|
|
case block_handler_type_iseq:
|
|
|
|
return rb_vm_make_proc_lambda(th, VM_BH_TO_CAPT_BLOCK(block_handler), klass, is_lambda);
|
|
|
|
}
|
|
|
|
VM_UNREACHABLE(proc_new);
|
|
|
|
return Qnil;
|
2007-02-02 10:21:41 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-17 17:07:33 -04:00
|
|
|
* Proc.new {|...| block } -> a_proc
|
|
|
|
* Proc.new -> a_proc
|
2009-02-22 09:23:33 -05:00
|
|
|
*
|
2007-02-02 10:21:41 -05:00
|
|
|
* Creates a new <code>Proc</code> object, bound to the current
|
|
|
|
* context. <code>Proc::new</code> may be called without a block only
|
|
|
|
* within a method with an attached block, in which case that block is
|
|
|
|
* converted to the <code>Proc</code> object.
|
2009-02-22 09:23:33 -05:00
|
|
|
*
|
2007-02-02 10:21:41 -05:00
|
|
|
* def proc_from
|
|
|
|
* Proc.new
|
|
|
|
* end
|
|
|
|
* proc = proc_from { "hello" }
|
|
|
|
* proc.call #=> "hello"
|
|
|
|
*/
|
|
|
|
|
|
|
|
static VALUE
|
2007-12-05 02:18:52 -05:00
|
|
|
rb_proc_s_new(int argc, VALUE *argv, VALUE klass)
|
2007-02-02 10:21:41 -05:00
|
|
|
{
|
2009-07-18 04:05:32 -04:00
|
|
|
VALUE block = proc_new(klass, FALSE);
|
2007-12-05 02:18:52 -05:00
|
|
|
|
|
|
|
rb_obj_call_init(block, argc, argv);
|
|
|
|
return block;
|
2007-02-02 10:21:41 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-17 17:07:33 -04:00
|
|
|
* proc { |...| block } -> a_proc
|
2007-02-02 10:21:41 -05:00
|
|
|
*
|
|
|
|
* Equivalent to <code>Proc.new</code>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
VALUE
|
|
|
|
rb_block_proc(void)
|
|
|
|
{
|
2009-07-18 04:05:32 -04:00
|
|
|
return proc_new(rb_cProc, FALSE);
|
2007-02-02 10:21:41 -05:00
|
|
|
}
|
|
|
|
|
2013-04-29 23:30:21 -04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* lambda { |...| block } -> a_proc
|
|
|
|
*
|
|
|
|
* Equivalent to <code>Proc.new</code>, except the resulting Proc objects
|
|
|
|
* check the number of parameters passed when called.
|
|
|
|
*/
|
|
|
|
|
2007-02-02 10:21:41 -05:00
|
|
|
VALUE
|
|
|
|
rb_block_lambda(void)
|
|
|
|
{
|
2009-07-18 04:05:32 -04:00
|
|
|
return proc_new(rb_cProc, TRUE);
|
2007-02-02 10:21:41 -05:00
|
|
|
}
|
|
|
|
|
2011-10-03 18:59:45 -04:00
|
|
|
/* Document-method: ===
|
|
|
|
*
|
|
|
|
* call-seq:
|
|
|
|
* proc === obj -> result_of_proc
|
|
|
|
*
|
|
|
|
* Invokes the block with +obj+ as the proc's parameter like Proc#call. It
|
|
|
|
* is to allow a proc object to be a target of +when+ clause in a case
|
|
|
|
* statement.
|
|
|
|
*/
|
|
|
|
|
2007-02-02 10:21:41 -05:00
|
|
|
/* CHECKME: are the argument checking semantics correct? */
|
|
|
|
|
|
|
|
/*
|
2016-05-15 17:11:33 -04:00
|
|
|
* Document-method: call
|
|
|
|
* Document-method: []
|
|
|
|
* Document-method: yield
|
|
|
|
*
|
2007-02-02 10:21:41 -05:00
|
|
|
* call-seq:
|
2010-05-17 17:07:33 -04:00
|
|
|
* prc.call(params,...) -> obj
|
|
|
|
* prc[params,...] -> obj
|
|
|
|
* prc.(params,...) -> obj
|
2009-02-22 09:23:33 -05:00
|
|
|
*
|
2007-02-02 10:21:41 -05:00
|
|
|
* Invokes the block, setting the block's parameters to the values in
|
|
|
|
* <i>params</i> using something close to method calling semantics.
|
|
|
|
* Generates a warning if multiple values are passed to a proc that
|
|
|
|
* expects just one (previously this silently converted the parameters
|
2015-02-11 20:46:33 -05:00
|
|
|
* to an array). Note that <code>prc.()</code> invokes
|
|
|
|
* <code>prc.call()</code> with the parameters given. It's a syntax sugar to
|
|
|
|
* hide "call".
|
|
|
|
*
|
|
|
|
* Returns the value of the last expression evaluated in the block. See
|
|
|
|
* also Proc#yield.
|
|
|
|
*
|
|
|
|
* a_proc = Proc.new { |scalar, *values| values.collect { |value| value*scalar } }
|
|
|
|
* a_proc.call(9, 1, 2, 3) #=> [9, 18, 27]
|
|
|
|
* a_proc[9, 1, 2, 3] #=> [9, 18, 27]
|
|
|
|
* a_proc.(9, 1, 2, 3) #=> [9, 18, 27]
|
2007-02-02 10:21:41 -05:00
|
|
|
*
|
2011-10-03 19:16:11 -04:00
|
|
|
* For procs created using <code>lambda</code> or <code>->()</code> an error
|
|
|
|
* is generated if the wrong number of parameters are passed to a Proc with
|
|
|
|
* multiple parameters. For procs created using <code>Proc.new</code> or
|
|
|
|
* <code>Kernel.proc</code>, extra parameters are silently discarded.
|
2007-02-02 10:21:41 -05:00
|
|
|
*
|
2011-10-03 19:16:11 -04:00
|
|
|
* a_proc = lambda {|a,b| a}
|
2007-02-02 10:21:41 -05:00
|
|
|
* a_proc.call(1,2,3)
|
2009-02-22 09:23:33 -05:00
|
|
|
*
|
2007-02-02 10:21:41 -05:00
|
|
|
* <em>produces:</em>
|
2009-02-22 09:23:33 -05:00
|
|
|
*
|
2015-10-24 20:39:29 -04:00
|
|
|
* prog.rb:4:in `block in <main>': wrong number of arguments (given 3, expected 2) (ArgumentError)
|
2011-10-03 19:16:11 -04:00
|
|
|
* from prog.rb:5:in `call'
|
|
|
|
* from prog.rb:5:in `<main>'
|
|
|
|
*
|
2007-02-02 10:21:41 -05:00
|
|
|
*/
|
2015-10-05 17:34:24 -04:00
|
|
|
#if 0
|
2007-02-02 10:21:41 -05:00
|
|
|
static VALUE
|
|
|
|
proc_call(int argc, VALUE *argv, VALUE procval)
|
|
|
|
{
|
2016-07-28 07:02:30 -04:00
|
|
|
/* removed */
|
2007-02-02 10:21:41 -05:00
|
|
|
}
|
2015-10-05 17:34:24 -04:00
|
|
|
#endif
|
2007-02-02 10:21:41 -05:00
|
|
|
|
2009-03-14 05:05:09 -04:00
|
|
|
#if SIZEOF_LONG > SIZEOF_INT
|
2009-03-14 05:18:09 -04:00
|
|
|
static inline int
|
|
|
|
check_argc(long argc)
|
|
|
|
{
|
2009-03-14 05:05:09 -04:00
|
|
|
if (argc > INT_MAX || argc < 0) {
|
|
|
|
rb_raise(rb_eArgError, "too many arguments (%lu)",
|
|
|
|
(unsigned long)argc);
|
|
|
|
}
|
2009-03-14 05:18:09 -04:00
|
|
|
return (int)argc;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
#define check_argc(argc) (argc)
|
2009-03-14 05:05:09 -04:00
|
|
|
#endif
|
2009-03-14 05:18:09 -04:00
|
|
|
|
|
|
|
VALUE
|
|
|
|
rb_proc_call(VALUE self, VALUE args)
|
|
|
|
{
|
2011-12-23 22:38:56 -05:00
|
|
|
VALUE vret;
|
2009-03-14 05:18:09 -04:00
|
|
|
rb_proc_t *proc;
|
|
|
|
GetProcPtr(self, proc);
|
2016-07-28 07:02:30 -04:00
|
|
|
vret = rb_vm_invoke_proc(GET_THREAD(), proc,
|
|
|
|
check_argc(RARRAY_LEN(args)), RARRAY_CONST_PTR(args),
|
|
|
|
VM_BLOCK_HANDLER_NONE);
|
2011-12-23 22:38:56 -05:00
|
|
|
RB_GC_GUARD(self);
|
|
|
|
RB_GC_GUARD(args);
|
|
|
|
return vret;
|
2008-06-10 12:33:51 -04:00
|
|
|
}
|
|
|
|
|
2016-07-28 07:02:30 -04:00
|
|
|
static VALUE
|
|
|
|
proc_to_block_handler(VALUE procval)
|
|
|
|
{
|
|
|
|
return NIL_P(procval) ? VM_BLOCK_HANDLER_NONE : procval;
|
|
|
|
}
|
|
|
|
|
2008-06-10 12:33:51 -04:00
|
|
|
VALUE
|
2016-07-28 07:02:30 -04:00
|
|
|
rb_proc_call_with_block(VALUE self, int argc, const VALUE *argv, VALUE passed_procval)
|
2008-06-10 12:33:51 -04:00
|
|
|
{
|
2016-07-28 07:02:30 -04:00
|
|
|
rb_thread_t *th = GET_THREAD();
|
2011-12-23 22:38:56 -05:00
|
|
|
VALUE vret;
|
2008-07-31 12:02:18 -04:00
|
|
|
rb_proc_t *proc;
|
2008-06-10 12:33:51 -04:00
|
|
|
GetProcPtr(self, proc);
|
2016-07-28 07:02:30 -04:00
|
|
|
vret = rb_vm_invoke_proc(th, proc, argc, argv, proc_to_block_handler(passed_procval));
|
2011-12-23 22:38:56 -05:00
|
|
|
RB_GC_GUARD(self);
|
|
|
|
return vret;
|
2007-02-02 10:21:41 -05:00
|
|
|
}
|
|
|
|
|
2013-07-15 00:26:58 -04:00
|
|
|
|
2007-02-02 10:21:41 -05:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2016-09-08 00:57:49 -04:00
|
|
|
* prc.arity -> integer
|
2009-02-22 09:23:33 -05:00
|
|
|
*
|
2014-03-01 20:55:33 -05:00
|
|
|
* Returns the number of mandatory arguments. If the block
|
2007-02-02 10:21:41 -05:00
|
|
|
* is declared to take no arguments, returns 0. If the block is known
|
2014-03-01 20:55:33 -05:00
|
|
|
* to take exactly n arguments, returns n.
|
|
|
|
* If the block has optional arguments, returns -n-1, where n is the
|
|
|
|
* number of mandatory arguments, with the exception for blocks that
|
|
|
|
* are not lambdas and have only a finite number of optional arguments;
|
|
|
|
* in this latter case, returns n.
|
2014-03-01 20:55:49 -05:00
|
|
|
* Keywords arguments will considered as a single additional argument,
|
|
|
|
* that argument being mandatory if any keyword argument is mandatory.
|
2014-03-01 20:55:33 -05:00
|
|
|
* A <code>proc</code> with no argument declarations
|
2014-11-30 21:05:53 -05:00
|
|
|
* is the same as a block declaring <code>||</code> as its arguments.
|
2009-02-22 09:23:33 -05:00
|
|
|
*
|
2014-03-01 20:55:17 -05:00
|
|
|
* proc {}.arity #=> 0
|
|
|
|
* proc { || }.arity #=> 0
|
|
|
|
* proc { |a| }.arity #=> 1
|
|
|
|
* proc { |a, b| }.arity #=> 2
|
|
|
|
* proc { |a, b, c| }.arity #=> 3
|
|
|
|
* proc { |*a| }.arity #=> -1
|
|
|
|
* proc { |a, *b| }.arity #=> -2
|
|
|
|
* proc { |a, *b, c| }.arity #=> -3
|
2014-03-01 20:55:49 -05:00
|
|
|
* proc { |x:, y:, z:0| }.arity #=> 1
|
|
|
|
* proc { |*a, x:, y:0| }.arity #=> -2
|
2014-03-01 20:55:17 -05:00
|
|
|
*
|
|
|
|
* proc { |x=0| }.arity #=> 0
|
|
|
|
* lambda { |x=0| }.arity #=> -1
|
|
|
|
* proc { |x=0, y| }.arity #=> 1
|
2012-07-17 14:08:40 -04:00
|
|
|
* lambda { |x=0, y| }.arity #=> -2
|
2014-03-01 20:55:17 -05:00
|
|
|
* proc { |x=0, y=0| }.arity #=> 0
|
2012-07-17 14:08:40 -04:00
|
|
|
* lambda { |x=0, y=0| }.arity #=> -1
|
2014-03-01 20:55:17 -05:00
|
|
|
* proc { |x, y=0| }.arity #=> 1
|
2012-07-17 14:08:40 -04:00
|
|
|
* lambda { |x, y=0| }.arity #=> -2
|
2014-03-01 20:55:17 -05:00
|
|
|
* proc { |(x, y), z=0| }.arity #=> 1
|
2012-07-17 14:08:40 -04:00
|
|
|
* lambda { |(x, y), z=0| }.arity #=> -2
|
2014-03-01 20:55:49 -05:00
|
|
|
* proc { |a, x:0, y:0| }.arity #=> 1
|
|
|
|
* lambda { |a, x:0, y:0| }.arity #=> -2
|
2007-02-02 10:21:41 -05:00
|
|
|
*/
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
proc_arity(VALUE self)
|
2009-03-14 04:59:12 -04:00
|
|
|
{
|
|
|
|
int arity = rb_proc_arity(self);
|
|
|
|
return INT2FIX(arity);
|
|
|
|
}
|
|
|
|
|
2013-02-01 17:46:07 -05:00
|
|
|
static inline int
|
|
|
|
rb_iseq_min_max_arity(const rb_iseq_t *iseq, int *max)
|
|
|
|
{
|
2015-07-21 18:52:59 -04:00
|
|
|
*max = iseq->body->param.flags.has_rest == FALSE ?
|
|
|
|
iseq->body->param.lead_num + iseq->body->param.opt_num + iseq->body->param.post_num +
|
|
|
|
(iseq->body->param.flags.has_kw == TRUE || iseq->body->param.flags.has_kwrest == TRUE)
|
2013-02-01 17:46:07 -05:00
|
|
|
: UNLIMITED_ARGUMENTS;
|
2015-07-21 18:52:59 -04:00
|
|
|
return iseq->body->param.lead_num + iseq->body->param.post_num + (iseq->body->param.flags.has_kw && iseq->body->param.keyword->required_num > 0);
|
2013-02-01 17:46:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2016-07-28 07:02:30 -04:00
|
|
|
rb_block_min_max_arity(const struct rb_block *block, int *max)
|
|
|
|
{
|
|
|
|
switch (vm_block_type(block)) {
|
|
|
|
case block_type_iseq:
|
|
|
|
return rb_iseq_min_max_arity(block->as.captured.code.iseq, max);
|
|
|
|
case block_type_proc:
|
|
|
|
return rb_block_min_max_arity(vm_proc_block(block->as.proc), max);
|
|
|
|
case block_type_ifunc:
|
|
|
|
{
|
|
|
|
const struct vm_ifunc *ifunc = block->as.captured.code.ifunc;
|
|
|
|
if (IS_METHOD_PROC_IFUNC(ifunc)) {
|
2013-02-01 17:46:07 -05:00
|
|
|
/* e.g. method(:foo).to_proc.arity */
|
2015-03-11 09:31:11 -04:00
|
|
|
return method_min_max_arity((VALUE)ifunc->data, max);
|
2007-05-01 01:01:15 -04:00
|
|
|
}
|
2007-02-02 10:21:41 -05:00
|
|
|
}
|
2016-07-28 07:02:30 -04:00
|
|
|
/* fall through */
|
|
|
|
case block_type_symbol:
|
|
|
|
break;
|
2007-02-02 10:21:41 -05:00
|
|
|
}
|
2013-02-01 17:46:07 -05:00
|
|
|
*max = UNLIMITED_ARGUMENTS;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-07-15 00:26:58 -04:00
|
|
|
/*
|
|
|
|
* Returns the number of required parameters and stores the maximum
|
|
|
|
* number of parameters in max, or UNLIMITED_ARGUMENTS if no max.
|
|
|
|
* For non-lambda procs, the maximum is the number of non-ignored
|
|
|
|
* parameters even though there is no actual limit to the number of parameters
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
rb_proc_min_max_arity(VALUE self, int *max)
|
|
|
|
{
|
|
|
|
rb_proc_t *proc;
|
|
|
|
GetProcPtr(self, proc);
|
2016-09-25 09:33:08 -04:00
|
|
|
return rb_block_min_max_arity(&proc->block, max);
|
2013-07-15 00:26:58 -04:00
|
|
|
}
|
|
|
|
|
2013-02-01 17:46:07 -05:00
|
|
|
int
|
|
|
|
rb_proc_arity(VALUE self)
|
|
|
|
{
|
|
|
|
rb_proc_t *proc;
|
2016-09-25 09:33:08 -04:00
|
|
|
int max, min;
|
2013-02-01 17:46:07 -05:00
|
|
|
GetProcPtr(self, proc);
|
2016-09-25 09:33:08 -04:00
|
|
|
min = rb_block_min_max_arity(&proc->block, &max);
|
2013-02-01 17:46:07 -05:00
|
|
|
return (proc->is_lambda ? min == max : max != UNLIMITED_ARGUMENTS) ? min : -min-1;
|
2007-02-02 10:21:41 -05:00
|
|
|
}
|
|
|
|
|
2016-07-28 07:02:30 -04:00
|
|
|
static void
|
|
|
|
block_setup(struct rb_block *block, VALUE block_handler)
|
|
|
|
{
|
|
|
|
switch (vm_block_handler_type(block_handler)) {
|
|
|
|
case block_handler_type_iseq:
|
|
|
|
block->type = block_type_iseq;
|
|
|
|
block->as.captured = *VM_BH_TO_ISEQ_BLOCK(block_handler);
|
|
|
|
break;
|
|
|
|
case block_handler_type_ifunc:
|
|
|
|
block->type = block_type_ifunc;
|
|
|
|
block->as.captured = *VM_BH_TO_IFUNC_BLOCK(block_handler);
|
|
|
|
break;
|
|
|
|
case block_handler_type_symbol:
|
|
|
|
block->type = block_type_symbol;
|
|
|
|
block->as.symbol = VM_BH_TO_SYMBOL(block_handler);
|
|
|
|
break;
|
|
|
|
case block_handler_type_proc:
|
|
|
|
block->type = block_type_proc;
|
|
|
|
block->as.proc = VM_BH_TO_PROC(block_handler);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-15 00:26:58 -04:00
|
|
|
int
|
|
|
|
rb_block_arity(void)
|
|
|
|
{
|
|
|
|
int min, max;
|
|
|
|
rb_thread_t *th = GET_THREAD();
|
|
|
|
rb_control_frame_t *cfp = th->cfp;
|
2016-07-28 07:02:30 -04:00
|
|
|
VALUE block_handler = rb_vm_frame_block_handler(cfp);
|
|
|
|
struct rb_block block;
|
|
|
|
|
|
|
|
if (block_handler == VM_BLOCK_HANDLER_NONE) {
|
|
|
|
rb_raise(rb_eArgError, "no block given");
|
|
|
|
}
|
|
|
|
|
|
|
|
block_setup(&block, block_handler);
|
|
|
|
min = rb_block_min_max_arity(&block, &max);
|
|
|
|
|
|
|
|
switch (vm_block_type(&block)) {
|
|
|
|
case block_handler_type_symbol:
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
case block_handler_type_proc:
|
|
|
|
{
|
|
|
|
VALUE procval = block_handler;
|
2015-12-17 02:16:14 -05:00
|
|
|
rb_proc_t *proc;
|
2016-07-28 07:02:30 -04:00
|
|
|
GetProcPtr(procval, proc);
|
|
|
|
return (proc->is_lambda ? min == max : max != UNLIMITED_ARGUMENTS) ? min : -min-1;
|
|
|
|
/* fall through */
|
2015-12-17 02:16:14 -05:00
|
|
|
}
|
2016-07-28 07:02:30 -04:00
|
|
|
|
|
|
|
default:
|
|
|
|
return max != UNLIMITED_ARGUMENTS ? min : -min-1;
|
2013-07-15 00:26:58 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-21 05:01:44 -04:00
|
|
|
const rb_iseq_t *
|
2009-09-30 00:15:46 -04:00
|
|
|
rb_proc_get_iseq(VALUE self, int *is_proc)
|
2007-12-05 02:18:52 -05:00
|
|
|
{
|
2015-05-21 05:01:44 -04:00
|
|
|
const rb_proc_t *proc;
|
2016-07-28 07:02:30 -04:00
|
|
|
const struct rb_block *block;
|
2007-12-05 02:18:52 -05:00
|
|
|
|
|
|
|
GetProcPtr(self, proc);
|
2016-07-28 07:02:30 -04:00
|
|
|
block = &proc->block;
|
2008-12-04 23:05:48 -05:00
|
|
|
if (is_proc) *is_proc = !proc->is_lambda;
|
2016-07-28 07:02:30 -04:00
|
|
|
|
|
|
|
switch (vm_block_type(block)) {
|
|
|
|
case block_type_iseq:
|
|
|
|
return rb_iseq_check(block->as.captured.code.iseq);
|
|
|
|
case block_type_proc:
|
|
|
|
return rb_proc_get_iseq(block->as.proc, is_proc);
|
|
|
|
case block_type_ifunc:
|
|
|
|
{
|
|
|
|
const struct vm_ifunc *ifunc = block->as.captured.code.ifunc;
|
|
|
|
if (IS_METHOD_PROC_IFUNC(ifunc)) {
|
|
|
|
/* method(:foo).to_proc */
|
|
|
|
if (is_proc) *is_proc = 0;
|
|
|
|
return rb_method_iseq((VALUE)ifunc->data);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return NULL;
|
|
|
|
}
|
2008-11-27 23:19:37 -05:00
|
|
|
}
|
2016-07-28 07:02:30 -04:00
|
|
|
case block_type_symbol:
|
2015-12-19 10:29:01 -05:00
|
|
|
return NULL;
|
2015-10-15 23:21:10 -04:00
|
|
|
}
|
2016-07-28 07:02:30 -04:00
|
|
|
|
|
|
|
VM_UNREACHABLE(rb_proc_get_iseq);
|
|
|
|
return NULL;
|
2007-12-05 02:18:52 -05:00
|
|
|
}
|
|
|
|
|
2008-09-26 09:47:01 -04:00
|
|
|
static VALUE
|
2015-05-21 05:01:44 -04:00
|
|
|
iseq_location(const rb_iseq_t *iseq)
|
2007-12-05 02:18:52 -05:00
|
|
|
{
|
|
|
|
VALUE loc[2];
|
|
|
|
|
|
|
|
if (!iseq) return Qnil;
|
2015-12-08 08:58:50 -05:00
|
|
|
rb_iseq_check(iseq);
|
2015-07-21 18:52:59 -04:00
|
|
|
loc[0] = iseq->body->location.path;
|
|
|
|
if (iseq->body->line_info_table) {
|
|
|
|
loc[1] = rb_iseq_first_lineno(iseq);
|
2007-12-05 02:18:52 -05:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
loc[1] = Qnil;
|
|
|
|
}
|
|
|
|
return rb_ary_new4(2, loc);
|
|
|
|
}
|
|
|
|
|
2008-09-26 09:47:01 -04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2016-10-26 02:11:23 -04:00
|
|
|
* prc.source_location -> [String, Integer]
|
2008-09-26 09:47:01 -04:00
|
|
|
*
|
2011-05-27 09:55:43 -04:00
|
|
|
* Returns the Ruby source filename and line number containing this proc
|
|
|
|
* or +nil+ if this proc was not defined in Ruby (i.e. native)
|
2008-09-26 09:47:01 -04:00
|
|
|
*/
|
|
|
|
|
|
|
|
VALUE
|
|
|
|
rb_proc_location(VALUE self)
|
|
|
|
{
|
2015-12-19 10:14:50 -05:00
|
|
|
return iseq_location(rb_proc_get_iseq(self, 0));
|
2008-09-26 09:47:01 -04:00
|
|
|
}
|
|
|
|
|
2008-11-27 23:19:37 -05:00
|
|
|
static VALUE
|
|
|
|
unnamed_parameters(int arity)
|
|
|
|
{
|
|
|
|
VALUE a, param = rb_ary_new2((arity < 0) ? -arity : arity);
|
|
|
|
int n = (arity < 0) ? ~arity : arity;
|
|
|
|
ID req, rest;
|
|
|
|
CONST_ID(req, "req");
|
|
|
|
a = rb_ary_new3(1, ID2SYM(req));
|
|
|
|
OBJ_FREEZE(a);
|
|
|
|
for (; n; --n) {
|
|
|
|
rb_ary_push(param, a);
|
|
|
|
}
|
|
|
|
if (arity < 0) {
|
|
|
|
CONST_ID(rest, "rest");
|
|
|
|
rb_ary_store(param, ~arity, rb_ary_new3(1, ID2SYM(rest)));
|
|
|
|
}
|
|
|
|
return param;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
2011-05-27 09:55:43 -04:00
|
|
|
* prc.parameters -> array
|
2008-11-27 23:19:37 -05:00
|
|
|
*
|
2011-05-27 09:55:43 -04:00
|
|
|
* Returns the parameter information of this proc.
|
2010-05-17 17:07:46 -04:00
|
|
|
*
|
2011-05-27 09:55:43 -04:00
|
|
|
* prc = lambda{|x, y=42, *other|}
|
|
|
|
* prc.parameters #=> [[:req, :x], [:opt, :y], [:rest, :other]]
|
2008-11-27 23:19:37 -05:00
|
|
|
*/
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
rb_proc_parameters(VALUE self)
|
|
|
|
{
|
2008-12-04 23:05:48 -05:00
|
|
|
int is_proc;
|
2015-12-19 10:14:50 -05:00
|
|
|
const rb_iseq_t *iseq = rb_proc_get_iseq(self, &is_proc);
|
2008-11-27 23:19:37 -05:00
|
|
|
if (!iseq) {
|
2009-03-14 04:59:12 -04:00
|
|
|
return unnamed_parameters(rb_proc_arity(self));
|
2008-11-27 23:19:37 -05:00
|
|
|
}
|
2008-12-04 23:05:48 -05:00
|
|
|
return rb_iseq_parameters(iseq, is_proc);
|
2008-11-27 23:19:37 -05:00
|
|
|
}
|
|
|
|
|
2012-02-20 19:13:44 -05:00
|
|
|
st_index_t
|
|
|
|
rb_hash_proc(st_index_t hash, VALUE prc)
|
|
|
|
{
|
2012-02-21 02:08:21 -05:00
|
|
|
rb_proc_t *proc;
|
|
|
|
GetProcPtr(prc, proc);
|
2016-07-28 07:02:30 -04:00
|
|
|
hash = rb_hash_uint(hash, (st_index_t)proc->block.as.captured.code.val);
|
|
|
|
hash = rb_hash_uint(hash, (st_index_t)proc->block.as.captured.self);
|
|
|
|
return rb_hash_uint(hash, (st_index_t)proc->block.as.captured.ep >> 16);
|
2012-02-20 19:13:44 -05:00
|
|
|
}
|
|
|
|
|
2015-10-15 00:37:26 -04:00
|
|
|
VALUE
|
|
|
|
rb_sym_to_proc(VALUE sym)
|
|
|
|
{
|
|
|
|
static VALUE sym_proc_cache = Qfalse;
|
|
|
|
enum {SYM_PROC_CACHE_SIZE = 67};
|
|
|
|
VALUE proc;
|
|
|
|
long index;
|
|
|
|
ID id;
|
|
|
|
VALUE *aryp;
|
|
|
|
|
|
|
|
if (!sym_proc_cache) {
|
|
|
|
sym_proc_cache = rb_ary_tmp_new(SYM_PROC_CACHE_SIZE * 2);
|
|
|
|
rb_gc_register_mark_object(sym_proc_cache);
|
|
|
|
rb_ary_store(sym_proc_cache, SYM_PROC_CACHE_SIZE*2 - 1, Qnil);
|
|
|
|
}
|
|
|
|
|
|
|
|
id = SYM2ID(sym);
|
|
|
|
index = (id % SYM_PROC_CACHE_SIZE) << 1;
|
|
|
|
|
|
|
|
aryp = RARRAY_PTR(sym_proc_cache);
|
|
|
|
if (aryp[index] == sym) {
|
|
|
|
return aryp[index + 1];
|
|
|
|
}
|
|
|
|
else {
|
2015-10-15 23:21:10 -04:00
|
|
|
proc = sym_proc_new(rb_cProc, ID2SYM(id));
|
2015-10-15 00:37:26 -04:00
|
|
|
aryp[index] = sym;
|
|
|
|
aryp[index + 1] = proc;
|
|
|
|
return proc;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-02-02 10:21:41 -05:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-17 17:07:33 -04:00
|
|
|
* prc.hash -> integer
|
2007-02-02 10:21:41 -05:00
|
|
|
*
|
2011-05-27 09:55:43 -04:00
|
|
|
* Returns a hash value corresponding to proc body.
|
2014-03-13 21:27:43 -04:00
|
|
|
*
|
|
|
|
* See also Object#hash.
|
2007-02-02 10:21:41 -05:00
|
|
|
*/
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
proc_hash(VALUE self)
|
|
|
|
{
|
2009-09-08 09:10:04 -04:00
|
|
|
st_index_t hash;
|
2012-02-20 19:13:44 -05:00
|
|
|
hash = rb_hash_start(0);
|
2012-02-21 02:08:21 -05:00
|
|
|
hash = rb_hash_proc(hash, self);
|
2009-09-08 09:10:04 -04:00
|
|
|
hash = rb_hash_end(hash);
|
2016-10-04 12:25:01 -04:00
|
|
|
return ST2FIX(hash);
|
2007-02-02 10:21:41 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-17 17:07:33 -04:00
|
|
|
* prc.to_s -> string
|
2007-02-02 10:21:41 -05:00
|
|
|
*
|
2011-05-27 09:55:43 -04:00
|
|
|
* Returns the unique identifier for this proc, along with
|
2007-02-02 10:21:41 -05:00
|
|
|
* an indication of where the proc was defined.
|
|
|
|
*/
|
|
|
|
|
2016-11-20 05:52:28 -05:00
|
|
|
static VALUE
|
|
|
|
proc_to_s(VALUE self)
|
|
|
|
{
|
|
|
|
const rb_proc_t *proc;
|
|
|
|
GetProcPtr(self, proc);
|
|
|
|
return proc_to_s_(self, proc);
|
|
|
|
}
|
|
|
|
|
2007-02-02 10:21:41 -05:00
|
|
|
static VALUE
|
2016-07-28 07:02:30 -04:00
|
|
|
proc_to_s_(VALUE self, const rb_proc_t *proc)
|
2007-02-02 10:21:41 -05:00
|
|
|
{
|
|
|
|
VALUE str = 0;
|
2008-05-31 05:28:20 -04:00
|
|
|
const char *cname = rb_obj_classname(self);
|
2016-07-28 07:02:30 -04:00
|
|
|
const struct rb_block *block;
|
2007-06-24 03:35:49 -04:00
|
|
|
const char *is_lambda;
|
2009-02-22 09:23:33 -05:00
|
|
|
|
2016-07-28 07:02:30 -04:00
|
|
|
block = &proc->block;
|
2007-06-24 03:35:49 -04:00
|
|
|
is_lambda = proc->is_lambda ? " (lambda)" : "";
|
2007-02-02 10:21:41 -05:00
|
|
|
|
2016-07-28 07:02:30 -04:00
|
|
|
again:
|
|
|
|
switch (vm_block_type(block)) {
|
|
|
|
case block_type_proc:
|
|
|
|
block = vm_proc_block(block->as.proc);
|
|
|
|
goto again;
|
|
|
|
case block_type_iseq:
|
|
|
|
{
|
|
|
|
const rb_iseq_t *iseq = rb_iseq_check(block->as.captured.code.iseq);
|
|
|
|
int first_lineno = 0;
|
|
|
|
if (iseq->body->line_info_table) {
|
|
|
|
first_lineno = FIX2INT(rb_iseq_first_lineno(iseq));
|
|
|
|
}
|
|
|
|
str = rb_sprintf("#<%s:%p@%"PRIsVALUE":%d%s>", cname, (void *)self,
|
|
|
|
iseq->body->location.path, first_lineno, is_lambda);
|
2007-02-02 10:21:41 -05:00
|
|
|
}
|
2016-07-28 07:02:30 -04:00
|
|
|
break;
|
|
|
|
case block_type_symbol:
|
2015-09-30 02:36:11 -04:00
|
|
|
str = rb_sprintf("#<%s:%p(&%+"PRIsVALUE")%s>", cname, (void *)self,
|
2016-07-28 07:02:30 -04:00
|
|
|
block->as.symbol, is_lambda);
|
|
|
|
break;
|
|
|
|
case block_type_ifunc:
|
|
|
|
str = rb_sprintf("#<%s:%p%s>", cname, proc->block.as.captured.code.ifunc,
|
2007-06-24 03:10:46 -04:00
|
|
|
is_lambda);
|
2016-07-28 07:02:30 -04:00
|
|
|
break;
|
2007-02-02 10:21:41 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if (OBJ_TAINTED(self)) {
|
|
|
|
OBJ_TAINT(str);
|
|
|
|
}
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
2014-01-19 00:43:28 -05:00
|
|
|
* prc.to_proc -> proc
|
2009-02-22 09:23:33 -05:00
|
|
|
*
|
2007-02-02 10:21:41 -05:00
|
|
|
* Part of the protocol for converting objects to <code>Proc</code>
|
|
|
|
* objects. Instances of class <code>Proc</code> simply return
|
|
|
|
* themselves.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
proc_to_proc(VALUE self)
|
|
|
|
{
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2007-12-24 04:09:21 -05:00
|
|
|
static void
|
2009-07-08 04:13:41 -04:00
|
|
|
bm_mark(void *ptr)
|
2007-02-02 10:21:41 -05:00
|
|
|
{
|
2009-07-08 04:13:41 -04:00
|
|
|
struct METHOD *data = ptr;
|
2007-02-02 10:21:41 -05:00
|
|
|
rb_gc_mark(data->recv);
|
* method.h: introduce rb_callable_method_entry_t to remove
rb_control_frame_t::klass.
[Bug #11278], [Bug #11279]
rb_method_entry_t data belong to modules/classes.
rb_method_entry_t::owner points defined module or class.
module M
def foo; end
end
In this case, owner is M.
rb_callable_method_entry_t data belong to only classes.
For modules, MRI creates corresponding T_ICLASS internally.
rb_callable_method_entry_t can also belong to T_ICLASS.
rb_callable_method_entry_t::defined_class points T_CLASS or
T_ICLASS.
rb_method_entry_t data for classes (not for modules) are also
rb_callable_method_entry_t data because it is completely same data.
In this case, rb_method_entry_t::owner == rb_method_entry_t::defined_class.
For example, there are classes C and D, and incldues M,
class C; include M; end
class D; include M; end
then, two T_ICLASS objects for C's super class and D's super class
will be created.
When C.new.foo is called, then M#foo is searcheed and
rb_callable_method_t data is used by VM to invoke M#foo.
rb_method_entry_t data is only one for M#foo.
However, rb_callable_method_entry_t data are two (and can be more).
It is proportional to the number of including (and prepending)
classes (the number of T_ICLASS which point to the module).
Now, created rb_callable_method_entry_t are collected when
the original module M was modified. We can think it is a cache.
We need to select what kind of method entry data is needed.
To operate definition, then you need to use rb_method_entry_t.
You can access them by the following functions.
* rb_method_entry(VALUE klass, ID id);
* rb_method_entry_with_refinements(VALUE klass, ID id);
* rb_method_entry_without_refinements(VALUE klass, ID id);
* rb_resolve_refined_method(VALUE refinements, const rb_method_entry_t *me);
To invoke methods, then you need to use rb_callable_method_entry_t
which you can get by the following APIs corresponding to the
above listed functions.
* rb_callable_method_entry(VALUE klass, ID id);
* rb_callable_method_entry_with_refinements(VALUE klass, ID id);
* rb_callable_method_entry_without_refinements(VALUE klass, ID id);
* rb_resolve_refined_method_callable(VALUE refinements, const rb_callable_method_entry_t *me);
VM pushes rb_callable_method_entry_t, so that rb_vm_frame_method_entry()
returns rb_callable_method_entry_t.
You can check a super class of current method by
rb_callable_method_entry_t::defined_class.
* method.h: renamed from rb_method_entry_t::klass to
rb_method_entry_t::owner.
* internal.h: add rb_classext_struct::callable_m_tbl to cache
rb_callable_method_entry_t data.
We need to consider abotu this field again because it is only
active for T_ICLASS.
* class.c (method_entry_i): ditto.
* class.c (rb_define_attr): rb_method_entry() does not takes
defiend_class_ptr.
* gc.c (mark_method_entry): mark RCLASS_CALLABLE_M_TBL() for T_ICLASS.
* cont.c (fiber_init): rb_control_frame_t::klass is removed.
* proc.c: fix `struct METHOD' data structure because
rb_callable_method_t has all information.
* vm_core.h: remove several fields.
* rb_control_frame_t::klass.
* rb_block_t::klass.
And catch up changes.
* eval.c: catch up changes.
* gc.c: ditto.
* insns.def: ditto.
* vm.c: ditto.
* vm_args.c: ditto.
* vm_backtrace.c: ditto.
* vm_dump.c: ditto.
* vm_eval.c: ditto.
* vm_insnhelper.c: ditto.
* vm_method.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@51126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2015-07-03 07:24:50 -04:00
|
|
|
rb_gc_mark(data->klass);
|
2015-06-02 00:20:30 -04:00
|
|
|
rb_gc_mark((VALUE)data->me);
|
2009-08-27 22:45:41 -04:00
|
|
|
}
|
|
|
|
|
2009-07-08 04:13:41 -04:00
|
|
|
static size_t
|
2009-09-08 22:11:35 -04:00
|
|
|
bm_memsize(const void *ptr)
|
2009-07-08 04:13:41 -04:00
|
|
|
{
|
2015-12-08 19:38:32 -05:00
|
|
|
return sizeof(struct METHOD);
|
2009-07-08 04:13:41 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static const rb_data_type_t method_data_type = {
|
|
|
|
"method",
|
2010-07-18 03:31:54 -04:00
|
|
|
{
|
|
|
|
bm_mark,
|
2015-12-09 14:58:48 -05:00
|
|
|
RUBY_TYPED_DEFAULT_FREE,
|
2010-07-18 03:31:54 -04:00
|
|
|
bm_memsize,
|
|
|
|
},
|
2014-12-01 01:38:04 -05:00
|
|
|
0, 0, RUBY_TYPED_FREE_IMMEDIATELY
|
2009-07-08 04:13:41 -04:00
|
|
|
};
|
|
|
|
|
2010-12-07 08:05:26 -05:00
|
|
|
VALUE
|
2009-07-08 04:13:41 -04:00
|
|
|
rb_obj_is_method(VALUE m)
|
|
|
|
{
|
2010-12-07 08:05:26 -05:00
|
|
|
if (rb_typeddata_is_kind_of(m, &method_data_type)) {
|
|
|
|
return Qtrue;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return Qfalse;
|
|
|
|
}
|
2009-07-08 04:13:41 -04:00
|
|
|
}
|
|
|
|
|
2015-03-20 05:41:06 -04:00
|
|
|
static int
|
|
|
|
respond_to_missing_p(VALUE klass, VALUE obj, VALUE sym, int scope)
|
|
|
|
{
|
|
|
|
/* TODO: merge with obj_respond_to() */
|
|
|
|
ID rmiss = idRespond_to_missing;
|
|
|
|
|
|
|
|
if (obj == Qundef) return 0;
|
|
|
|
if (rb_method_basic_definition_p(klass, rmiss)) return 0;
|
|
|
|
return RTEST(rb_funcall(obj, rmiss, 2, sym, scope ? Qfalse : Qtrue));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static VALUE
|
2016-08-18 11:59:12 -04:00
|
|
|
mnew_missing(VALUE klass, VALUE obj, ID id, VALUE mclass)
|
2015-03-20 05:41:06 -04:00
|
|
|
{
|
|
|
|
struct METHOD *data;
|
|
|
|
VALUE method = TypedData_Make_Struct(mclass, struct METHOD, &method_data_type, data);
|
|
|
|
rb_method_entry_t *me;
|
|
|
|
rb_method_definition_t *def;
|
|
|
|
|
* method.h: introduce rb_callable_method_entry_t to remove
rb_control_frame_t::klass.
[Bug #11278], [Bug #11279]
rb_method_entry_t data belong to modules/classes.
rb_method_entry_t::owner points defined module or class.
module M
def foo; end
end
In this case, owner is M.
rb_callable_method_entry_t data belong to only classes.
For modules, MRI creates corresponding T_ICLASS internally.
rb_callable_method_entry_t can also belong to T_ICLASS.
rb_callable_method_entry_t::defined_class points T_CLASS or
T_ICLASS.
rb_method_entry_t data for classes (not for modules) are also
rb_callable_method_entry_t data because it is completely same data.
In this case, rb_method_entry_t::owner == rb_method_entry_t::defined_class.
For example, there are classes C and D, and incldues M,
class C; include M; end
class D; include M; end
then, two T_ICLASS objects for C's super class and D's super class
will be created.
When C.new.foo is called, then M#foo is searcheed and
rb_callable_method_t data is used by VM to invoke M#foo.
rb_method_entry_t data is only one for M#foo.
However, rb_callable_method_entry_t data are two (and can be more).
It is proportional to the number of including (and prepending)
classes (the number of T_ICLASS which point to the module).
Now, created rb_callable_method_entry_t are collected when
the original module M was modified. We can think it is a cache.
We need to select what kind of method entry data is needed.
To operate definition, then you need to use rb_method_entry_t.
You can access them by the following functions.
* rb_method_entry(VALUE klass, ID id);
* rb_method_entry_with_refinements(VALUE klass, ID id);
* rb_method_entry_without_refinements(VALUE klass, ID id);
* rb_resolve_refined_method(VALUE refinements, const rb_method_entry_t *me);
To invoke methods, then you need to use rb_callable_method_entry_t
which you can get by the following APIs corresponding to the
above listed functions.
* rb_callable_method_entry(VALUE klass, ID id);
* rb_callable_method_entry_with_refinements(VALUE klass, ID id);
* rb_callable_method_entry_without_refinements(VALUE klass, ID id);
* rb_resolve_refined_method_callable(VALUE refinements, const rb_callable_method_entry_t *me);
VM pushes rb_callable_method_entry_t, so that rb_vm_frame_method_entry()
returns rb_callable_method_entry_t.
You can check a super class of current method by
rb_callable_method_entry_t::defined_class.
* method.h: renamed from rb_method_entry_t::klass to
rb_method_entry_t::owner.
* internal.h: add rb_classext_struct::callable_m_tbl to cache
rb_callable_method_entry_t data.
We need to consider abotu this field again because it is only
active for T_ICLASS.
* class.c (method_entry_i): ditto.
* class.c (rb_define_attr): rb_method_entry() does not takes
defiend_class_ptr.
* gc.c (mark_method_entry): mark RCLASS_CALLABLE_M_TBL() for T_ICLASS.
* cont.c (fiber_init): rb_control_frame_t::klass is removed.
* proc.c: fix `struct METHOD' data structure because
rb_callable_method_t has all information.
* vm_core.h: remove several fields.
* rb_control_frame_t::klass.
* rb_block_t::klass.
And catch up changes.
* eval.c: catch up changes.
* gc.c: ditto.
* insns.def: ditto.
* vm.c: ditto.
* vm_args.c: ditto.
* vm_backtrace.c: ditto.
* vm_dump.c: ditto.
* vm_eval.c: ditto.
* vm_insnhelper.c: ditto.
* vm_method.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@51126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2015-07-03 07:24:50 -04:00
|
|
|
RB_OBJ_WRITE(method, &data->recv, obj);
|
|
|
|
RB_OBJ_WRITE(method, &data->klass, klass);
|
2015-03-20 05:41:06 -04:00
|
|
|
|
2015-06-02 00:20:30 -04:00
|
|
|
def = ZALLOC(rb_method_definition_t);
|
2015-03-20 05:41:06 -04:00
|
|
|
def->type = VM_METHOD_TYPE_MISSING;
|
|
|
|
def->original_id = id;
|
|
|
|
|
2015-06-06 06:19:48 -04:00
|
|
|
me = rb_method_entry_create(id, klass, METHOD_VISI_UNDEF, def);
|
|
|
|
|
2015-06-02 00:20:30 -04:00
|
|
|
RB_OBJ_WRITE(method, &data->me, me);
|
2015-03-20 05:41:06 -04:00
|
|
|
|
|
|
|
OBJ_INFECT(method, klass);
|
|
|
|
|
|
|
|
return method;
|
|
|
|
}
|
|
|
|
|
2007-02-02 10:21:41 -05:00
|
|
|
static VALUE
|
* method.h: introduce rb_callable_method_entry_t to remove
rb_control_frame_t::klass.
[Bug #11278], [Bug #11279]
rb_method_entry_t data belong to modules/classes.
rb_method_entry_t::owner points defined module or class.
module M
def foo; end
end
In this case, owner is M.
rb_callable_method_entry_t data belong to only classes.
For modules, MRI creates corresponding T_ICLASS internally.
rb_callable_method_entry_t can also belong to T_ICLASS.
rb_callable_method_entry_t::defined_class points T_CLASS or
T_ICLASS.
rb_method_entry_t data for classes (not for modules) are also
rb_callable_method_entry_t data because it is completely same data.
In this case, rb_method_entry_t::owner == rb_method_entry_t::defined_class.
For example, there are classes C and D, and incldues M,
class C; include M; end
class D; include M; end
then, two T_ICLASS objects for C's super class and D's super class
will be created.
When C.new.foo is called, then M#foo is searcheed and
rb_callable_method_t data is used by VM to invoke M#foo.
rb_method_entry_t data is only one for M#foo.
However, rb_callable_method_entry_t data are two (and can be more).
It is proportional to the number of including (and prepending)
classes (the number of T_ICLASS which point to the module).
Now, created rb_callable_method_entry_t are collected when
the original module M was modified. We can think it is a cache.
We need to select what kind of method entry data is needed.
To operate definition, then you need to use rb_method_entry_t.
You can access them by the following functions.
* rb_method_entry(VALUE klass, ID id);
* rb_method_entry_with_refinements(VALUE klass, ID id);
* rb_method_entry_without_refinements(VALUE klass, ID id);
* rb_resolve_refined_method(VALUE refinements, const rb_method_entry_t *me);
To invoke methods, then you need to use rb_callable_method_entry_t
which you can get by the following APIs corresponding to the
above listed functions.
* rb_callable_method_entry(VALUE klass, ID id);
* rb_callable_method_entry_with_refinements(VALUE klass, ID id);
* rb_callable_method_entry_without_refinements(VALUE klass, ID id);
* rb_resolve_refined_method_callable(VALUE refinements, const rb_callable_method_entry_t *me);
VM pushes rb_callable_method_entry_t, so that rb_vm_frame_method_entry()
returns rb_callable_method_entry_t.
You can check a super class of current method by
rb_callable_method_entry_t::defined_class.
* method.h: renamed from rb_method_entry_t::klass to
rb_method_entry_t::owner.
* internal.h: add rb_classext_struct::callable_m_tbl to cache
rb_callable_method_entry_t data.
We need to consider abotu this field again because it is only
active for T_ICLASS.
* class.c (method_entry_i): ditto.
* class.c (rb_define_attr): rb_method_entry() does not takes
defiend_class_ptr.
* gc.c (mark_method_entry): mark RCLASS_CALLABLE_M_TBL() for T_ICLASS.
* cont.c (fiber_init): rb_control_frame_t::klass is removed.
* proc.c: fix `struct METHOD' data structure because
rb_callable_method_t has all information.
* vm_core.h: remove several fields.
* rb_control_frame_t::klass.
* rb_block_t::klass.
And catch up changes.
* eval.c: catch up changes.
* gc.c: ditto.
* insns.def: ditto.
* vm.c: ditto.
* vm_args.c: ditto.
* vm_backtrace.c: ditto.
* vm_dump.c: ditto.
* vm_eval.c: ditto.
* vm_insnhelper.c: ditto.
* vm_method.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@51126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2015-07-03 07:24:50 -04:00
|
|
|
mnew_internal(const rb_method_entry_t *me, VALUE klass,
|
2014-07-26 12:22:41 -04:00
|
|
|
VALUE obj, ID id, VALUE mclass, int scope, int error)
|
2007-02-02 10:21:41 -05:00
|
|
|
{
|
2015-03-20 05:41:06 -04:00
|
|
|
struct METHOD *data;
|
|
|
|
VALUE method;
|
2015-06-02 21:39:16 -04:00
|
|
|
rb_method_visibility_t visi = METHOD_VISI_UNDEF;
|
2007-02-02 10:21:41 -05:00
|
|
|
|
|
|
|
again:
|
2009-08-27 22:45:41 -04:00
|
|
|
if (UNDEFINED_METHOD_ENTRY_P(me)) {
|
2015-03-20 05:41:06 -04:00
|
|
|
if (respond_to_missing_p(klass, obj, ID2SYM(id), scope)) {
|
2016-08-18 11:59:12 -04:00
|
|
|
return mnew_missing(klass, obj, id, mclass);
|
2009-09-24 00:42:28 -04:00
|
|
|
}
|
2014-07-26 12:22:41 -04:00
|
|
|
if (!error) return Qnil;
|
2016-01-02 23:59:54 -05:00
|
|
|
rb_print_undef(klass, id, METHOD_VISI_UNDEF);
|
2007-12-17 18:01:50 -05:00
|
|
|
}
|
2015-06-02 21:39:16 -04:00
|
|
|
if (visi == METHOD_VISI_UNDEF) {
|
2015-06-06 06:19:48 -04:00
|
|
|
visi = METHOD_ENTRY_VISI(me);
|
2015-06-02 21:39:16 -04:00
|
|
|
if (scope && (visi != METHOD_VISI_PUBLIC)) {
|
2014-07-26 12:22:41 -04:00
|
|
|
if (!error) return Qnil;
|
2015-06-02 21:39:16 -04:00
|
|
|
rb_print_inaccessible(klass, id, visi);
|
2010-01-08 09:40:38 -05:00
|
|
|
}
|
2007-02-02 10:21:41 -05:00
|
|
|
}
|
2015-06-06 06:19:48 -04:00
|
|
|
if (me->def->type == VM_METHOD_TYPE_ZSUPER) {
|
* method.h: introduce rb_callable_method_entry_t to remove
rb_control_frame_t::klass.
[Bug #11278], [Bug #11279]
rb_method_entry_t data belong to modules/classes.
rb_method_entry_t::owner points defined module or class.
module M
def foo; end
end
In this case, owner is M.
rb_callable_method_entry_t data belong to only classes.
For modules, MRI creates corresponding T_ICLASS internally.
rb_callable_method_entry_t can also belong to T_ICLASS.
rb_callable_method_entry_t::defined_class points T_CLASS or
T_ICLASS.
rb_method_entry_t data for classes (not for modules) are also
rb_callable_method_entry_t data because it is completely same data.
In this case, rb_method_entry_t::owner == rb_method_entry_t::defined_class.
For example, there are classes C and D, and incldues M,
class C; include M; end
class D; include M; end
then, two T_ICLASS objects for C's super class and D's super class
will be created.
When C.new.foo is called, then M#foo is searcheed and
rb_callable_method_t data is used by VM to invoke M#foo.
rb_method_entry_t data is only one for M#foo.
However, rb_callable_method_entry_t data are two (and can be more).
It is proportional to the number of including (and prepending)
classes (the number of T_ICLASS which point to the module).
Now, created rb_callable_method_entry_t are collected when
the original module M was modified. We can think it is a cache.
We need to select what kind of method entry data is needed.
To operate definition, then you need to use rb_method_entry_t.
You can access them by the following functions.
* rb_method_entry(VALUE klass, ID id);
* rb_method_entry_with_refinements(VALUE klass, ID id);
* rb_method_entry_without_refinements(VALUE klass, ID id);
* rb_resolve_refined_method(VALUE refinements, const rb_method_entry_t *me);
To invoke methods, then you need to use rb_callable_method_entry_t
which you can get by the following APIs corresponding to the
above listed functions.
* rb_callable_method_entry(VALUE klass, ID id);
* rb_callable_method_entry_with_refinements(VALUE klass, ID id);
* rb_callable_method_entry_without_refinements(VALUE klass, ID id);
* rb_resolve_refined_method_callable(VALUE refinements, const rb_callable_method_entry_t *me);
VM pushes rb_callable_method_entry_t, so that rb_vm_frame_method_entry()
returns rb_callable_method_entry_t.
You can check a super class of current method by
rb_callable_method_entry_t::defined_class.
* method.h: renamed from rb_method_entry_t::klass to
rb_method_entry_t::owner.
* internal.h: add rb_classext_struct::callable_m_tbl to cache
rb_callable_method_entry_t data.
We need to consider abotu this field again because it is only
active for T_ICLASS.
* class.c (method_entry_i): ditto.
* class.c (rb_define_attr): rb_method_entry() does not takes
defiend_class_ptr.
* gc.c (mark_method_entry): mark RCLASS_CALLABLE_M_TBL() for T_ICLASS.
* cont.c (fiber_init): rb_control_frame_t::klass is removed.
* proc.c: fix `struct METHOD' data structure because
rb_callable_method_t has all information.
* vm_core.h: remove several fields.
* rb_control_frame_t::klass.
* rb_block_t::klass.
And catch up changes.
* eval.c: catch up changes.
* gc.c: ditto.
* insns.def: ditto.
* vm.c: ditto.
* vm_args.c: ditto.
* vm_backtrace.c: ditto.
* vm_dump.c: ditto.
* vm_eval.c: ditto.
* vm_insnhelper.c: ditto.
* vm_method.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@51126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2015-07-03 07:24:50 -04:00
|
|
|
if (me->defined_class) {
|
2016-10-24 23:54:09 -04:00
|
|
|
VALUE klass = RCLASS_SUPER(RCLASS_ORIGIN(me->defined_class));
|
* method.h: introduce rb_callable_method_entry_t to remove
rb_control_frame_t::klass.
[Bug #11278], [Bug #11279]
rb_method_entry_t data belong to modules/classes.
rb_method_entry_t::owner points defined module or class.
module M
def foo; end
end
In this case, owner is M.
rb_callable_method_entry_t data belong to only classes.
For modules, MRI creates corresponding T_ICLASS internally.
rb_callable_method_entry_t can also belong to T_ICLASS.
rb_callable_method_entry_t::defined_class points T_CLASS or
T_ICLASS.
rb_method_entry_t data for classes (not for modules) are also
rb_callable_method_entry_t data because it is completely same data.
In this case, rb_method_entry_t::owner == rb_method_entry_t::defined_class.
For example, there are classes C and D, and incldues M,
class C; include M; end
class D; include M; end
then, two T_ICLASS objects for C's super class and D's super class
will be created.
When C.new.foo is called, then M#foo is searcheed and
rb_callable_method_t data is used by VM to invoke M#foo.
rb_method_entry_t data is only one for M#foo.
However, rb_callable_method_entry_t data are two (and can be more).
It is proportional to the number of including (and prepending)
classes (the number of T_ICLASS which point to the module).
Now, created rb_callable_method_entry_t are collected when
the original module M was modified. We can think it is a cache.
We need to select what kind of method entry data is needed.
To operate definition, then you need to use rb_method_entry_t.
You can access them by the following functions.
* rb_method_entry(VALUE klass, ID id);
* rb_method_entry_with_refinements(VALUE klass, ID id);
* rb_method_entry_without_refinements(VALUE klass, ID id);
* rb_resolve_refined_method(VALUE refinements, const rb_method_entry_t *me);
To invoke methods, then you need to use rb_callable_method_entry_t
which you can get by the following APIs corresponding to the
above listed functions.
* rb_callable_method_entry(VALUE klass, ID id);
* rb_callable_method_entry_with_refinements(VALUE klass, ID id);
* rb_callable_method_entry_without_refinements(VALUE klass, ID id);
* rb_resolve_refined_method_callable(VALUE refinements, const rb_callable_method_entry_t *me);
VM pushes rb_callable_method_entry_t, so that rb_vm_frame_method_entry()
returns rb_callable_method_entry_t.
You can check a super class of current method by
rb_callable_method_entry_t::defined_class.
* method.h: renamed from rb_method_entry_t::klass to
rb_method_entry_t::owner.
* internal.h: add rb_classext_struct::callable_m_tbl to cache
rb_callable_method_entry_t data.
We need to consider abotu this field again because it is only
active for T_ICLASS.
* class.c (method_entry_i): ditto.
* class.c (rb_define_attr): rb_method_entry() does not takes
defiend_class_ptr.
* gc.c (mark_method_entry): mark RCLASS_CALLABLE_M_TBL() for T_ICLASS.
* cont.c (fiber_init): rb_control_frame_t::klass is removed.
* proc.c: fix `struct METHOD' data structure because
rb_callable_method_t has all information.
* vm_core.h: remove several fields.
* rb_control_frame_t::klass.
* rb_block_t::klass.
And catch up changes.
* eval.c: catch up changes.
* gc.c: ditto.
* insns.def: ditto.
* vm.c: ditto.
* vm_args.c: ditto.
* vm_backtrace.c: ditto.
* vm_dump.c: ditto.
* vm_eval.c: ditto.
* vm_insnhelper.c: ditto.
* vm_method.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@51126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2015-07-03 07:24:50 -04:00
|
|
|
id = me->def->original_id;
|
|
|
|
me = (rb_method_entry_t *)rb_callable_method_entry_without_refinements(klass, id);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
VALUE klass = RCLASS_SUPER(me->owner);
|
|
|
|
id = me->def->original_id;
|
|
|
|
me = rb_method_entry_without_refinements(klass, id);
|
|
|
|
}
|
2007-02-02 10:21:41 -05:00
|
|
|
goto again;
|
|
|
|
}
|
|
|
|
|
* method.h: introduce rb_callable_method_entry_t to remove
rb_control_frame_t::klass.
[Bug #11278], [Bug #11279]
rb_method_entry_t data belong to modules/classes.
rb_method_entry_t::owner points defined module or class.
module M
def foo; end
end
In this case, owner is M.
rb_callable_method_entry_t data belong to only classes.
For modules, MRI creates corresponding T_ICLASS internally.
rb_callable_method_entry_t can also belong to T_ICLASS.
rb_callable_method_entry_t::defined_class points T_CLASS or
T_ICLASS.
rb_method_entry_t data for classes (not for modules) are also
rb_callable_method_entry_t data because it is completely same data.
In this case, rb_method_entry_t::owner == rb_method_entry_t::defined_class.
For example, there are classes C and D, and incldues M,
class C; include M; end
class D; include M; end
then, two T_ICLASS objects for C's super class and D's super class
will be created.
When C.new.foo is called, then M#foo is searcheed and
rb_callable_method_t data is used by VM to invoke M#foo.
rb_method_entry_t data is only one for M#foo.
However, rb_callable_method_entry_t data are two (and can be more).
It is proportional to the number of including (and prepending)
classes (the number of T_ICLASS which point to the module).
Now, created rb_callable_method_entry_t are collected when
the original module M was modified. We can think it is a cache.
We need to select what kind of method entry data is needed.
To operate definition, then you need to use rb_method_entry_t.
You can access them by the following functions.
* rb_method_entry(VALUE klass, ID id);
* rb_method_entry_with_refinements(VALUE klass, ID id);
* rb_method_entry_without_refinements(VALUE klass, ID id);
* rb_resolve_refined_method(VALUE refinements, const rb_method_entry_t *me);
To invoke methods, then you need to use rb_callable_method_entry_t
which you can get by the following APIs corresponding to the
above listed functions.
* rb_callable_method_entry(VALUE klass, ID id);
* rb_callable_method_entry_with_refinements(VALUE klass, ID id);
* rb_callable_method_entry_without_refinements(VALUE klass, ID id);
* rb_resolve_refined_method_callable(VALUE refinements, const rb_callable_method_entry_t *me);
VM pushes rb_callable_method_entry_t, so that rb_vm_frame_method_entry()
returns rb_callable_method_entry_t.
You can check a super class of current method by
rb_callable_method_entry_t::defined_class.
* method.h: renamed from rb_method_entry_t::klass to
rb_method_entry_t::owner.
* internal.h: add rb_classext_struct::callable_m_tbl to cache
rb_callable_method_entry_t data.
We need to consider abotu this field again because it is only
active for T_ICLASS.
* class.c (method_entry_i): ditto.
* class.c (rb_define_attr): rb_method_entry() does not takes
defiend_class_ptr.
* gc.c (mark_method_entry): mark RCLASS_CALLABLE_M_TBL() for T_ICLASS.
* cont.c (fiber_init): rb_control_frame_t::klass is removed.
* proc.c: fix `struct METHOD' data structure because
rb_callable_method_t has all information.
* vm_core.h: remove several fields.
* rb_control_frame_t::klass.
* rb_block_t::klass.
And catch up changes.
* eval.c: catch up changes.
* gc.c: ditto.
* insns.def: ditto.
* vm.c: ditto.
* vm_args.c: ditto.
* vm_backtrace.c: ditto.
* vm_dump.c: ditto.
* vm_eval.c: ditto.
* vm_insnhelper.c: ditto.
* vm_method.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@51126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2015-07-03 07:24:50 -04:00
|
|
|
while (klass != me->owner && (FL_TEST(klass, FL_SINGLETON) || RB_TYPE_P(klass, T_ICLASS))) {
|
|
|
|
klass = RCLASS_SUPER(klass);
|
2007-02-02 10:21:41 -05:00
|
|
|
}
|
2009-07-15 10:59:41 -04:00
|
|
|
|
2009-07-08 04:13:41 -04:00
|
|
|
method = TypedData_Make_Struct(mclass, struct METHOD, &method_data_type, data);
|
2007-02-02 10:21:41 -05:00
|
|
|
|
* method.h: introduce rb_callable_method_entry_t to remove
rb_control_frame_t::klass.
[Bug #11278], [Bug #11279]
rb_method_entry_t data belong to modules/classes.
rb_method_entry_t::owner points defined module or class.
module M
def foo; end
end
In this case, owner is M.
rb_callable_method_entry_t data belong to only classes.
For modules, MRI creates corresponding T_ICLASS internally.
rb_callable_method_entry_t can also belong to T_ICLASS.
rb_callable_method_entry_t::defined_class points T_CLASS or
T_ICLASS.
rb_method_entry_t data for classes (not for modules) are also
rb_callable_method_entry_t data because it is completely same data.
In this case, rb_method_entry_t::owner == rb_method_entry_t::defined_class.
For example, there are classes C and D, and incldues M,
class C; include M; end
class D; include M; end
then, two T_ICLASS objects for C's super class and D's super class
will be created.
When C.new.foo is called, then M#foo is searcheed and
rb_callable_method_t data is used by VM to invoke M#foo.
rb_method_entry_t data is only one for M#foo.
However, rb_callable_method_entry_t data are two (and can be more).
It is proportional to the number of including (and prepending)
classes (the number of T_ICLASS which point to the module).
Now, created rb_callable_method_entry_t are collected when
the original module M was modified. We can think it is a cache.
We need to select what kind of method entry data is needed.
To operate definition, then you need to use rb_method_entry_t.
You can access them by the following functions.
* rb_method_entry(VALUE klass, ID id);
* rb_method_entry_with_refinements(VALUE klass, ID id);
* rb_method_entry_without_refinements(VALUE klass, ID id);
* rb_resolve_refined_method(VALUE refinements, const rb_method_entry_t *me);
To invoke methods, then you need to use rb_callable_method_entry_t
which you can get by the following APIs corresponding to the
above listed functions.
* rb_callable_method_entry(VALUE klass, ID id);
* rb_callable_method_entry_with_refinements(VALUE klass, ID id);
* rb_callable_method_entry_without_refinements(VALUE klass, ID id);
* rb_resolve_refined_method_callable(VALUE refinements, const rb_callable_method_entry_t *me);
VM pushes rb_callable_method_entry_t, so that rb_vm_frame_method_entry()
returns rb_callable_method_entry_t.
You can check a super class of current method by
rb_callable_method_entry_t::defined_class.
* method.h: renamed from rb_method_entry_t::klass to
rb_method_entry_t::owner.
* internal.h: add rb_classext_struct::callable_m_tbl to cache
rb_callable_method_entry_t data.
We need to consider abotu this field again because it is only
active for T_ICLASS.
* class.c (method_entry_i): ditto.
* class.c (rb_define_attr): rb_method_entry() does not takes
defiend_class_ptr.
* gc.c (mark_method_entry): mark RCLASS_CALLABLE_M_TBL() for T_ICLASS.
* cont.c (fiber_init): rb_control_frame_t::klass is removed.
* proc.c: fix `struct METHOD' data structure because
rb_callable_method_t has all information.
* vm_core.h: remove several fields.
* rb_control_frame_t::klass.
* rb_block_t::klass.
And catch up changes.
* eval.c: catch up changes.
* gc.c: ditto.
* insns.def: ditto.
* vm.c: ditto.
* vm_args.c: ditto.
* vm_backtrace.c: ditto.
* vm_dump.c: ditto.
* vm_eval.c: ditto.
* vm_insnhelper.c: ditto.
* vm_method.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@51126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2015-07-03 07:24:50 -04:00
|
|
|
RB_OBJ_WRITE(method, &data->recv, obj);
|
|
|
|
RB_OBJ_WRITE(method, &data->klass, klass);
|
|
|
|
RB_OBJ_WRITE(method, &data->me, me);
|
|
|
|
|
2007-02-02 10:21:41 -05:00
|
|
|
OBJ_INFECT(method, klass);
|
|
|
|
return method;
|
|
|
|
}
|
|
|
|
|
2014-07-26 12:22:41 -04:00
|
|
|
static VALUE
|
* method.h: introduce rb_callable_method_entry_t to remove
rb_control_frame_t::klass.
[Bug #11278], [Bug #11279]
rb_method_entry_t data belong to modules/classes.
rb_method_entry_t::owner points defined module or class.
module M
def foo; end
end
In this case, owner is M.
rb_callable_method_entry_t data belong to only classes.
For modules, MRI creates corresponding T_ICLASS internally.
rb_callable_method_entry_t can also belong to T_ICLASS.
rb_callable_method_entry_t::defined_class points T_CLASS or
T_ICLASS.
rb_method_entry_t data for classes (not for modules) are also
rb_callable_method_entry_t data because it is completely same data.
In this case, rb_method_entry_t::owner == rb_method_entry_t::defined_class.
For example, there are classes C and D, and incldues M,
class C; include M; end
class D; include M; end
then, two T_ICLASS objects for C's super class and D's super class
will be created.
When C.new.foo is called, then M#foo is searcheed and
rb_callable_method_t data is used by VM to invoke M#foo.
rb_method_entry_t data is only one for M#foo.
However, rb_callable_method_entry_t data are two (and can be more).
It is proportional to the number of including (and prepending)
classes (the number of T_ICLASS which point to the module).
Now, created rb_callable_method_entry_t are collected when
the original module M was modified. We can think it is a cache.
We need to select what kind of method entry data is needed.
To operate definition, then you need to use rb_method_entry_t.
You can access them by the following functions.
* rb_method_entry(VALUE klass, ID id);
* rb_method_entry_with_refinements(VALUE klass, ID id);
* rb_method_entry_without_refinements(VALUE klass, ID id);
* rb_resolve_refined_method(VALUE refinements, const rb_method_entry_t *me);
To invoke methods, then you need to use rb_callable_method_entry_t
which you can get by the following APIs corresponding to the
above listed functions.
* rb_callable_method_entry(VALUE klass, ID id);
* rb_callable_method_entry_with_refinements(VALUE klass, ID id);
* rb_callable_method_entry_without_refinements(VALUE klass, ID id);
* rb_resolve_refined_method_callable(VALUE refinements, const rb_callable_method_entry_t *me);
VM pushes rb_callable_method_entry_t, so that rb_vm_frame_method_entry()
returns rb_callable_method_entry_t.
You can check a super class of current method by
rb_callable_method_entry_t::defined_class.
* method.h: renamed from rb_method_entry_t::klass to
rb_method_entry_t::owner.
* internal.h: add rb_classext_struct::callable_m_tbl to cache
rb_callable_method_entry_t data.
We need to consider abotu this field again because it is only
active for T_ICLASS.
* class.c (method_entry_i): ditto.
* class.c (rb_define_attr): rb_method_entry() does not takes
defiend_class_ptr.
* gc.c (mark_method_entry): mark RCLASS_CALLABLE_M_TBL() for T_ICLASS.
* cont.c (fiber_init): rb_control_frame_t::klass is removed.
* proc.c: fix `struct METHOD' data structure because
rb_callable_method_t has all information.
* vm_core.h: remove several fields.
* rb_control_frame_t::klass.
* rb_block_t::klass.
And catch up changes.
* eval.c: catch up changes.
* gc.c: ditto.
* insns.def: ditto.
* vm.c: ditto.
* vm_args.c: ditto.
* vm_backtrace.c: ditto.
* vm_dump.c: ditto.
* vm_eval.c: ditto.
* vm_insnhelper.c: ditto.
* vm_method.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@51126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2015-07-03 07:24:50 -04:00
|
|
|
mnew_from_me(const rb_method_entry_t *me, VALUE klass,
|
2014-07-26 12:22:41 -04:00
|
|
|
VALUE obj, ID id, VALUE mclass, int scope)
|
|
|
|
{
|
* method.h: introduce rb_callable_method_entry_t to remove
rb_control_frame_t::klass.
[Bug #11278], [Bug #11279]
rb_method_entry_t data belong to modules/classes.
rb_method_entry_t::owner points defined module or class.
module M
def foo; end
end
In this case, owner is M.
rb_callable_method_entry_t data belong to only classes.
For modules, MRI creates corresponding T_ICLASS internally.
rb_callable_method_entry_t can also belong to T_ICLASS.
rb_callable_method_entry_t::defined_class points T_CLASS or
T_ICLASS.
rb_method_entry_t data for classes (not for modules) are also
rb_callable_method_entry_t data because it is completely same data.
In this case, rb_method_entry_t::owner == rb_method_entry_t::defined_class.
For example, there are classes C and D, and incldues M,
class C; include M; end
class D; include M; end
then, two T_ICLASS objects for C's super class and D's super class
will be created.
When C.new.foo is called, then M#foo is searcheed and
rb_callable_method_t data is used by VM to invoke M#foo.
rb_method_entry_t data is only one for M#foo.
However, rb_callable_method_entry_t data are two (and can be more).
It is proportional to the number of including (and prepending)
classes (the number of T_ICLASS which point to the module).
Now, created rb_callable_method_entry_t are collected when
the original module M was modified. We can think it is a cache.
We need to select what kind of method entry data is needed.
To operate definition, then you need to use rb_method_entry_t.
You can access them by the following functions.
* rb_method_entry(VALUE klass, ID id);
* rb_method_entry_with_refinements(VALUE klass, ID id);
* rb_method_entry_without_refinements(VALUE klass, ID id);
* rb_resolve_refined_method(VALUE refinements, const rb_method_entry_t *me);
To invoke methods, then you need to use rb_callable_method_entry_t
which you can get by the following APIs corresponding to the
above listed functions.
* rb_callable_method_entry(VALUE klass, ID id);
* rb_callable_method_entry_with_refinements(VALUE klass, ID id);
* rb_callable_method_entry_without_refinements(VALUE klass, ID id);
* rb_resolve_refined_method_callable(VALUE refinements, const rb_callable_method_entry_t *me);
VM pushes rb_callable_method_entry_t, so that rb_vm_frame_method_entry()
returns rb_callable_method_entry_t.
You can check a super class of current method by
rb_callable_method_entry_t::defined_class.
* method.h: renamed from rb_method_entry_t::klass to
rb_method_entry_t::owner.
* internal.h: add rb_classext_struct::callable_m_tbl to cache
rb_callable_method_entry_t data.
We need to consider abotu this field again because it is only
active for T_ICLASS.
* class.c (method_entry_i): ditto.
* class.c (rb_define_attr): rb_method_entry() does not takes
defiend_class_ptr.
* gc.c (mark_method_entry): mark RCLASS_CALLABLE_M_TBL() for T_ICLASS.
* cont.c (fiber_init): rb_control_frame_t::klass is removed.
* proc.c: fix `struct METHOD' data structure because
rb_callable_method_t has all information.
* vm_core.h: remove several fields.
* rb_control_frame_t::klass.
* rb_block_t::klass.
And catch up changes.
* eval.c: catch up changes.
* gc.c: ditto.
* insns.def: ditto.
* vm.c: ditto.
* vm_args.c: ditto.
* vm_backtrace.c: ditto.
* vm_dump.c: ditto.
* vm_eval.c: ditto.
* vm_insnhelper.c: ditto.
* vm_method.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@51126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2015-07-03 07:24:50 -04:00
|
|
|
return mnew_internal(me, klass, obj, id, mclass, scope, TRUE);
|
2014-07-26 12:22:41 -04:00
|
|
|
}
|
|
|
|
|
2013-05-13 01:52:03 -04:00
|
|
|
static VALUE
|
|
|
|
mnew(VALUE klass, VALUE obj, ID id, VALUE mclass, int scope)
|
|
|
|
{
|
* method.h: introduce rb_callable_method_entry_t to remove
rb_control_frame_t::klass.
[Bug #11278], [Bug #11279]
rb_method_entry_t data belong to modules/classes.
rb_method_entry_t::owner points defined module or class.
module M
def foo; end
end
In this case, owner is M.
rb_callable_method_entry_t data belong to only classes.
For modules, MRI creates corresponding T_ICLASS internally.
rb_callable_method_entry_t can also belong to T_ICLASS.
rb_callable_method_entry_t::defined_class points T_CLASS or
T_ICLASS.
rb_method_entry_t data for classes (not for modules) are also
rb_callable_method_entry_t data because it is completely same data.
In this case, rb_method_entry_t::owner == rb_method_entry_t::defined_class.
For example, there are classes C and D, and incldues M,
class C; include M; end
class D; include M; end
then, two T_ICLASS objects for C's super class and D's super class
will be created.
When C.new.foo is called, then M#foo is searcheed and
rb_callable_method_t data is used by VM to invoke M#foo.
rb_method_entry_t data is only one for M#foo.
However, rb_callable_method_entry_t data are two (and can be more).
It is proportional to the number of including (and prepending)
classes (the number of T_ICLASS which point to the module).
Now, created rb_callable_method_entry_t are collected when
the original module M was modified. We can think it is a cache.
We need to select what kind of method entry data is needed.
To operate definition, then you need to use rb_method_entry_t.
You can access them by the following functions.
* rb_method_entry(VALUE klass, ID id);
* rb_method_entry_with_refinements(VALUE klass, ID id);
* rb_method_entry_without_refinements(VALUE klass, ID id);
* rb_resolve_refined_method(VALUE refinements, const rb_method_entry_t *me);
To invoke methods, then you need to use rb_callable_method_entry_t
which you can get by the following APIs corresponding to the
above listed functions.
* rb_callable_method_entry(VALUE klass, ID id);
* rb_callable_method_entry_with_refinements(VALUE klass, ID id);
* rb_callable_method_entry_without_refinements(VALUE klass, ID id);
* rb_resolve_refined_method_callable(VALUE refinements, const rb_callable_method_entry_t *me);
VM pushes rb_callable_method_entry_t, so that rb_vm_frame_method_entry()
returns rb_callable_method_entry_t.
You can check a super class of current method by
rb_callable_method_entry_t::defined_class.
* method.h: renamed from rb_method_entry_t::klass to
rb_method_entry_t::owner.
* internal.h: add rb_classext_struct::callable_m_tbl to cache
rb_callable_method_entry_t data.
We need to consider abotu this field again because it is only
active for T_ICLASS.
* class.c (method_entry_i): ditto.
* class.c (rb_define_attr): rb_method_entry() does not takes
defiend_class_ptr.
* gc.c (mark_method_entry): mark RCLASS_CALLABLE_M_TBL() for T_ICLASS.
* cont.c (fiber_init): rb_control_frame_t::klass is removed.
* proc.c: fix `struct METHOD' data structure because
rb_callable_method_t has all information.
* vm_core.h: remove several fields.
* rb_control_frame_t::klass.
* rb_block_t::klass.
And catch up changes.
* eval.c: catch up changes.
* gc.c: ditto.
* insns.def: ditto.
* vm.c: ditto.
* vm_args.c: ditto.
* vm_backtrace.c: ditto.
* vm_dump.c: ditto.
* vm_eval.c: ditto.
* vm_insnhelper.c: ditto.
* vm_method.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@51126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2015-07-03 07:24:50 -04:00
|
|
|
const rb_method_entry_t *me;
|
|
|
|
|
|
|
|
if (obj == Qundef) { /* UnboundMethod */
|
|
|
|
me = rb_method_entry_without_refinements(klass, id);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
me = (rb_method_entry_t *)rb_callable_method_entry_without_refinements(klass, id);
|
|
|
|
}
|
|
|
|
return mnew_from_me(me, klass, obj, id, mclass, scope);
|
2013-05-13 01:52:03 -04:00
|
|
|
}
|
|
|
|
|
2015-08-07 04:07:58 -04:00
|
|
|
static inline VALUE
|
|
|
|
method_entry_defined_class(const rb_method_entry_t *me)
|
|
|
|
{
|
|
|
|
VALUE defined_class = me->defined_class;
|
|
|
|
return defined_class ? defined_class : me->owner;
|
|
|
|
}
|
2007-02-02 10:21:41 -05:00
|
|
|
|
|
|
|
/**********************************************************************
|
|
|
|
*
|
|
|
|
* Document-class : Method
|
|
|
|
*
|
|
|
|
* Method objects are created by <code>Object#method</code>, and are
|
|
|
|
* associated with a particular object (not just with a class). They
|
|
|
|
* may be used to invoke the method within the object, and as a block
|
|
|
|
* associated with an iterator. They may also be unbound from one
|
|
|
|
* object (creating an <code>UnboundMethod</code>) and bound to
|
|
|
|
* another.
|
2009-02-22 09:23:33 -05:00
|
|
|
*
|
2007-02-02 10:21:41 -05:00
|
|
|
* class Thing
|
|
|
|
* def square(n)
|
|
|
|
* n*n
|
|
|
|
* end
|
|
|
|
* end
|
|
|
|
* thing = Thing.new
|
|
|
|
* meth = thing.method(:square)
|
2009-02-22 09:23:33 -05:00
|
|
|
*
|
2007-02-02 10:21:41 -05:00
|
|
|
* meth.call(9) #=> 81
|
|
|
|
* [ 1, 2, 3 ].collect(&meth) #=> [1, 4, 9]
|
2009-02-22 09:23:33 -05:00
|
|
|
*
|
2007-02-02 10:21:41 -05:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
2013-04-11 22:59:07 -04:00
|
|
|
* meth.eql?(other_meth) -> true or false
|
2010-05-17 17:07:33 -04:00
|
|
|
* meth == other_meth -> true or false
|
2007-02-02 10:21:41 -05:00
|
|
|
*
|
2009-09-24 16:59:19 -04:00
|
|
|
* Two method objects are equal if they are bound to the same
|
2012-12-27 21:23:11 -05:00
|
|
|
* object and refer to the same method definition and their owners are the
|
|
|
|
* same class or module.
|
2007-02-02 10:21:41 -05:00
|
|
|
*/
|
|
|
|
|
|
|
|
static VALUE
|
2007-02-05 13:31:08 -05:00
|
|
|
method_eq(VALUE method, VALUE other)
|
2007-02-02 10:21:41 -05:00
|
|
|
{
|
|
|
|
struct METHOD *m1, *m2;
|
* method.h: introduce rb_callable_method_entry_t to remove
rb_control_frame_t::klass.
[Bug #11278], [Bug #11279]
rb_method_entry_t data belong to modules/classes.
rb_method_entry_t::owner points defined module or class.
module M
def foo; end
end
In this case, owner is M.
rb_callable_method_entry_t data belong to only classes.
For modules, MRI creates corresponding T_ICLASS internally.
rb_callable_method_entry_t can also belong to T_ICLASS.
rb_callable_method_entry_t::defined_class points T_CLASS or
T_ICLASS.
rb_method_entry_t data for classes (not for modules) are also
rb_callable_method_entry_t data because it is completely same data.
In this case, rb_method_entry_t::owner == rb_method_entry_t::defined_class.
For example, there are classes C and D, and incldues M,
class C; include M; end
class D; include M; end
then, two T_ICLASS objects for C's super class and D's super class
will be created.
When C.new.foo is called, then M#foo is searcheed and
rb_callable_method_t data is used by VM to invoke M#foo.
rb_method_entry_t data is only one for M#foo.
However, rb_callable_method_entry_t data are two (and can be more).
It is proportional to the number of including (and prepending)
classes (the number of T_ICLASS which point to the module).
Now, created rb_callable_method_entry_t are collected when
the original module M was modified. We can think it is a cache.
We need to select what kind of method entry data is needed.
To operate definition, then you need to use rb_method_entry_t.
You can access them by the following functions.
* rb_method_entry(VALUE klass, ID id);
* rb_method_entry_with_refinements(VALUE klass, ID id);
* rb_method_entry_without_refinements(VALUE klass, ID id);
* rb_resolve_refined_method(VALUE refinements, const rb_method_entry_t *me);
To invoke methods, then you need to use rb_callable_method_entry_t
which you can get by the following APIs corresponding to the
above listed functions.
* rb_callable_method_entry(VALUE klass, ID id);
* rb_callable_method_entry_with_refinements(VALUE klass, ID id);
* rb_callable_method_entry_without_refinements(VALUE klass, ID id);
* rb_resolve_refined_method_callable(VALUE refinements, const rb_callable_method_entry_t *me);
VM pushes rb_callable_method_entry_t, so that rb_vm_frame_method_entry()
returns rb_callable_method_entry_t.
You can check a super class of current method by
rb_callable_method_entry_t::defined_class.
* method.h: renamed from rb_method_entry_t::klass to
rb_method_entry_t::owner.
* internal.h: add rb_classext_struct::callable_m_tbl to cache
rb_callable_method_entry_t data.
We need to consider abotu this field again because it is only
active for T_ICLASS.
* class.c (method_entry_i): ditto.
* class.c (rb_define_attr): rb_method_entry() does not takes
defiend_class_ptr.
* gc.c (mark_method_entry): mark RCLASS_CALLABLE_M_TBL() for T_ICLASS.
* cont.c (fiber_init): rb_control_frame_t::klass is removed.
* proc.c: fix `struct METHOD' data structure because
rb_callable_method_t has all information.
* vm_core.h: remove several fields.
* rb_control_frame_t::klass.
* rb_block_t::klass.
And catch up changes.
* eval.c: catch up changes.
* gc.c: ditto.
* insns.def: ditto.
* vm.c: ditto.
* vm_args.c: ditto.
* vm_backtrace.c: ditto.
* vm_dump.c: ditto.
* vm_eval.c: ditto.
* vm_insnhelper.c: ditto.
* vm_method.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@51126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2015-07-03 07:24:50 -04:00
|
|
|
VALUE klass1, klass2;
|
2007-02-02 10:21:41 -05:00
|
|
|
|
2009-07-08 04:13:41 -04:00
|
|
|
if (!rb_obj_is_method(other))
|
2007-02-02 10:21:41 -05:00
|
|
|
return Qfalse;
|
|
|
|
if (CLASS_OF(method) != CLASS_OF(other))
|
|
|
|
return Qfalse;
|
|
|
|
|
2009-07-08 04:13:41 -04:00
|
|
|
Check_TypedStruct(method, &method_data_type);
|
|
|
|
m1 = (struct METHOD *)DATA_PTR(method);
|
|
|
|
m2 = (struct METHOD *)DATA_PTR(other);
|
2007-02-02 10:21:41 -05:00
|
|
|
|
2015-08-07 04:07:58 -04:00
|
|
|
klass1 = method_entry_defined_class(m1->me);
|
|
|
|
klass2 = method_entry_defined_class(m2->me);
|
* method.h: introduce rb_callable_method_entry_t to remove
rb_control_frame_t::klass.
[Bug #11278], [Bug #11279]
rb_method_entry_t data belong to modules/classes.
rb_method_entry_t::owner points defined module or class.
module M
def foo; end
end
In this case, owner is M.
rb_callable_method_entry_t data belong to only classes.
For modules, MRI creates corresponding T_ICLASS internally.
rb_callable_method_entry_t can also belong to T_ICLASS.
rb_callable_method_entry_t::defined_class points T_CLASS or
T_ICLASS.
rb_method_entry_t data for classes (not for modules) are also
rb_callable_method_entry_t data because it is completely same data.
In this case, rb_method_entry_t::owner == rb_method_entry_t::defined_class.
For example, there are classes C and D, and incldues M,
class C; include M; end
class D; include M; end
then, two T_ICLASS objects for C's super class and D's super class
will be created.
When C.new.foo is called, then M#foo is searcheed and
rb_callable_method_t data is used by VM to invoke M#foo.
rb_method_entry_t data is only one for M#foo.
However, rb_callable_method_entry_t data are two (and can be more).
It is proportional to the number of including (and prepending)
classes (the number of T_ICLASS which point to the module).
Now, created rb_callable_method_entry_t are collected when
the original module M was modified. We can think it is a cache.
We need to select what kind of method entry data is needed.
To operate definition, then you need to use rb_method_entry_t.
You can access them by the following functions.
* rb_method_entry(VALUE klass, ID id);
* rb_method_entry_with_refinements(VALUE klass, ID id);
* rb_method_entry_without_refinements(VALUE klass, ID id);
* rb_resolve_refined_method(VALUE refinements, const rb_method_entry_t *me);
To invoke methods, then you need to use rb_callable_method_entry_t
which you can get by the following APIs corresponding to the
above listed functions.
* rb_callable_method_entry(VALUE klass, ID id);
* rb_callable_method_entry_with_refinements(VALUE klass, ID id);
* rb_callable_method_entry_without_refinements(VALUE klass, ID id);
* rb_resolve_refined_method_callable(VALUE refinements, const rb_callable_method_entry_t *me);
VM pushes rb_callable_method_entry_t, so that rb_vm_frame_method_entry()
returns rb_callable_method_entry_t.
You can check a super class of current method by
rb_callable_method_entry_t::defined_class.
* method.h: renamed from rb_method_entry_t::klass to
rb_method_entry_t::owner.
* internal.h: add rb_classext_struct::callable_m_tbl to cache
rb_callable_method_entry_t data.
We need to consider abotu this field again because it is only
active for T_ICLASS.
* class.c (method_entry_i): ditto.
* class.c (rb_define_attr): rb_method_entry() does not takes
defiend_class_ptr.
* gc.c (mark_method_entry): mark RCLASS_CALLABLE_M_TBL() for T_ICLASS.
* cont.c (fiber_init): rb_control_frame_t::klass is removed.
* proc.c: fix `struct METHOD' data structure because
rb_callable_method_t has all information.
* vm_core.h: remove several fields.
* rb_control_frame_t::klass.
* rb_block_t::klass.
And catch up changes.
* eval.c: catch up changes.
* gc.c: ditto.
* insns.def: ditto.
* vm.c: ditto.
* vm_args.c: ditto.
* vm_backtrace.c: ditto.
* vm_dump.c: ditto.
* vm_eval.c: ditto.
* vm_insnhelper.c: ditto.
* vm_method.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@51126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2015-07-03 07:24:50 -04:00
|
|
|
|
2011-07-25 10:29:28 -04:00
|
|
|
if (!rb_method_entry_eq(m1->me, m2->me) ||
|
* method.h: introduce rb_callable_method_entry_t to remove
rb_control_frame_t::klass.
[Bug #11278], [Bug #11279]
rb_method_entry_t data belong to modules/classes.
rb_method_entry_t::owner points defined module or class.
module M
def foo; end
end
In this case, owner is M.
rb_callable_method_entry_t data belong to only classes.
For modules, MRI creates corresponding T_ICLASS internally.
rb_callable_method_entry_t can also belong to T_ICLASS.
rb_callable_method_entry_t::defined_class points T_CLASS or
T_ICLASS.
rb_method_entry_t data for classes (not for modules) are also
rb_callable_method_entry_t data because it is completely same data.
In this case, rb_method_entry_t::owner == rb_method_entry_t::defined_class.
For example, there are classes C and D, and incldues M,
class C; include M; end
class D; include M; end
then, two T_ICLASS objects for C's super class and D's super class
will be created.
When C.new.foo is called, then M#foo is searcheed and
rb_callable_method_t data is used by VM to invoke M#foo.
rb_method_entry_t data is only one for M#foo.
However, rb_callable_method_entry_t data are two (and can be more).
It is proportional to the number of including (and prepending)
classes (the number of T_ICLASS which point to the module).
Now, created rb_callable_method_entry_t are collected when
the original module M was modified. We can think it is a cache.
We need to select what kind of method entry data is needed.
To operate definition, then you need to use rb_method_entry_t.
You can access them by the following functions.
* rb_method_entry(VALUE klass, ID id);
* rb_method_entry_with_refinements(VALUE klass, ID id);
* rb_method_entry_without_refinements(VALUE klass, ID id);
* rb_resolve_refined_method(VALUE refinements, const rb_method_entry_t *me);
To invoke methods, then you need to use rb_callable_method_entry_t
which you can get by the following APIs corresponding to the
above listed functions.
* rb_callable_method_entry(VALUE klass, ID id);
* rb_callable_method_entry_with_refinements(VALUE klass, ID id);
* rb_callable_method_entry_without_refinements(VALUE klass, ID id);
* rb_resolve_refined_method_callable(VALUE refinements, const rb_callable_method_entry_t *me);
VM pushes rb_callable_method_entry_t, so that rb_vm_frame_method_entry()
returns rb_callable_method_entry_t.
You can check a super class of current method by
rb_callable_method_entry_t::defined_class.
* method.h: renamed from rb_method_entry_t::klass to
rb_method_entry_t::owner.
* internal.h: add rb_classext_struct::callable_m_tbl to cache
rb_callable_method_entry_t data.
We need to consider abotu this field again because it is only
active for T_ICLASS.
* class.c (method_entry_i): ditto.
* class.c (rb_define_attr): rb_method_entry() does not takes
defiend_class_ptr.
* gc.c (mark_method_entry): mark RCLASS_CALLABLE_M_TBL() for T_ICLASS.
* cont.c (fiber_init): rb_control_frame_t::klass is removed.
* proc.c: fix `struct METHOD' data structure because
rb_callable_method_t has all information.
* vm_core.h: remove several fields.
* rb_control_frame_t::klass.
* rb_block_t::klass.
And catch up changes.
* eval.c: catch up changes.
* gc.c: ditto.
* insns.def: ditto.
* vm.c: ditto.
* vm_args.c: ditto.
* vm_backtrace.c: ditto.
* vm_dump.c: ditto.
* vm_eval.c: ditto.
* vm_insnhelper.c: ditto.
* vm_method.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@51126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2015-07-03 07:24:50 -04:00
|
|
|
klass1 != klass2 ||
|
|
|
|
m1->klass != m2->klass ||
|
2009-07-15 10:59:41 -04:00
|
|
|
m1->recv != m2->recv) {
|
2007-02-02 10:21:41 -05:00
|
|
|
return Qfalse;
|
2009-07-15 10:59:41 -04:00
|
|
|
}
|
2007-02-02 10:21:41 -05:00
|
|
|
|
|
|
|
return Qtrue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-17 17:07:33 -04:00
|
|
|
* meth.hash -> integer
|
2007-02-02 10:21:41 -05:00
|
|
|
*
|
2011-05-27 09:55:43 -04:00
|
|
|
* Returns a hash value corresponding to the method object.
|
2014-03-13 21:27:43 -04:00
|
|
|
*
|
|
|
|
* See also Object#hash.
|
2007-02-02 10:21:41 -05:00
|
|
|
*/
|
|
|
|
|
|
|
|
static VALUE
|
2007-02-05 13:31:08 -05:00
|
|
|
method_hash(VALUE method)
|
2007-02-02 10:21:41 -05:00
|
|
|
{
|
|
|
|
struct METHOD *m;
|
2009-09-08 09:10:04 -04:00
|
|
|
st_index_t hash;
|
2007-02-02 10:21:41 -05:00
|
|
|
|
2009-07-08 04:13:41 -04:00
|
|
|
TypedData_Get_Struct(method, struct METHOD, &method_data_type, m);
|
* method.h: introduce rb_callable_method_entry_t to remove
rb_control_frame_t::klass.
[Bug #11278], [Bug #11279]
rb_method_entry_t data belong to modules/classes.
rb_method_entry_t::owner points defined module or class.
module M
def foo; end
end
In this case, owner is M.
rb_callable_method_entry_t data belong to only classes.
For modules, MRI creates corresponding T_ICLASS internally.
rb_callable_method_entry_t can also belong to T_ICLASS.
rb_callable_method_entry_t::defined_class points T_CLASS or
T_ICLASS.
rb_method_entry_t data for classes (not for modules) are also
rb_callable_method_entry_t data because it is completely same data.
In this case, rb_method_entry_t::owner == rb_method_entry_t::defined_class.
For example, there are classes C and D, and incldues M,
class C; include M; end
class D; include M; end
then, two T_ICLASS objects for C's super class and D's super class
will be created.
When C.new.foo is called, then M#foo is searcheed and
rb_callable_method_t data is used by VM to invoke M#foo.
rb_method_entry_t data is only one for M#foo.
However, rb_callable_method_entry_t data are two (and can be more).
It is proportional to the number of including (and prepending)
classes (the number of T_ICLASS which point to the module).
Now, created rb_callable_method_entry_t are collected when
the original module M was modified. We can think it is a cache.
We need to select what kind of method entry data is needed.
To operate definition, then you need to use rb_method_entry_t.
You can access them by the following functions.
* rb_method_entry(VALUE klass, ID id);
* rb_method_entry_with_refinements(VALUE klass, ID id);
* rb_method_entry_without_refinements(VALUE klass, ID id);
* rb_resolve_refined_method(VALUE refinements, const rb_method_entry_t *me);
To invoke methods, then you need to use rb_callable_method_entry_t
which you can get by the following APIs corresponding to the
above listed functions.
* rb_callable_method_entry(VALUE klass, ID id);
* rb_callable_method_entry_with_refinements(VALUE klass, ID id);
* rb_callable_method_entry_without_refinements(VALUE klass, ID id);
* rb_resolve_refined_method_callable(VALUE refinements, const rb_callable_method_entry_t *me);
VM pushes rb_callable_method_entry_t, so that rb_vm_frame_method_entry()
returns rb_callable_method_entry_t.
You can check a super class of current method by
rb_callable_method_entry_t::defined_class.
* method.h: renamed from rb_method_entry_t::klass to
rb_method_entry_t::owner.
* internal.h: add rb_classext_struct::callable_m_tbl to cache
rb_callable_method_entry_t data.
We need to consider abotu this field again because it is only
active for T_ICLASS.
* class.c (method_entry_i): ditto.
* class.c (rb_define_attr): rb_method_entry() does not takes
defiend_class_ptr.
* gc.c (mark_method_entry): mark RCLASS_CALLABLE_M_TBL() for T_ICLASS.
* cont.c (fiber_init): rb_control_frame_t::klass is removed.
* proc.c: fix `struct METHOD' data structure because
rb_callable_method_t has all information.
* vm_core.h: remove several fields.
* rb_control_frame_t::klass.
* rb_block_t::klass.
And catch up changes.
* eval.c: catch up changes.
* gc.c: ditto.
* insns.def: ditto.
* vm.c: ditto.
* vm_args.c: ditto.
* vm_backtrace.c: ditto.
* vm_dump.c: ditto.
* vm_eval.c: ditto.
* vm_insnhelper.c: ditto.
* vm_method.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@51126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2015-07-03 07:24:50 -04:00
|
|
|
hash = rb_hash_start((st_index_t)m->recv);
|
2012-02-20 19:13:44 -05:00
|
|
|
hash = rb_hash_method_entry(hash, m->me);
|
2009-09-08 09:10:04 -04:00
|
|
|
hash = rb_hash_end(hash);
|
2007-02-02 10:21:41 -05:00
|
|
|
|
|
|
|
return INT2FIX(hash);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-17 17:07:33 -04:00
|
|
|
* meth.unbind -> unbound_method
|
2009-02-22 09:23:33 -05:00
|
|
|
*
|
2011-05-27 09:55:43 -04:00
|
|
|
* Dissociates <i>meth</i> from its current receiver. The resulting
|
2007-02-02 10:21:41 -05:00
|
|
|
* <code>UnboundMethod</code> can subsequently be bound to a new object
|
|
|
|
* of the same class (see <code>UnboundMethod</code>).
|
|
|
|
*/
|
|
|
|
|
|
|
|
static VALUE
|
2007-02-05 13:31:08 -05:00
|
|
|
method_unbind(VALUE obj)
|
2007-02-02 10:21:41 -05:00
|
|
|
{
|
|
|
|
VALUE method;
|
|
|
|
struct METHOD *orig, *data;
|
|
|
|
|
2009-07-08 04:13:41 -04:00
|
|
|
TypedData_Get_Struct(obj, struct METHOD, &method_data_type, orig);
|
2009-07-15 10:59:41 -04:00
|
|
|
method = TypedData_Make_Struct(rb_cUnboundMethod, struct METHOD,
|
|
|
|
&method_data_type, data);
|
* method.h: introduce rb_callable_method_entry_t to remove
rb_control_frame_t::klass.
[Bug #11278], [Bug #11279]
rb_method_entry_t data belong to modules/classes.
rb_method_entry_t::owner points defined module or class.
module M
def foo; end
end
In this case, owner is M.
rb_callable_method_entry_t data belong to only classes.
For modules, MRI creates corresponding T_ICLASS internally.
rb_callable_method_entry_t can also belong to T_ICLASS.
rb_callable_method_entry_t::defined_class points T_CLASS or
T_ICLASS.
rb_method_entry_t data for classes (not for modules) are also
rb_callable_method_entry_t data because it is completely same data.
In this case, rb_method_entry_t::owner == rb_method_entry_t::defined_class.
For example, there are classes C and D, and incldues M,
class C; include M; end
class D; include M; end
then, two T_ICLASS objects for C's super class and D's super class
will be created.
When C.new.foo is called, then M#foo is searcheed and
rb_callable_method_t data is used by VM to invoke M#foo.
rb_method_entry_t data is only one for M#foo.
However, rb_callable_method_entry_t data are two (and can be more).
It is proportional to the number of including (and prepending)
classes (the number of T_ICLASS which point to the module).
Now, created rb_callable_method_entry_t are collected when
the original module M was modified. We can think it is a cache.
We need to select what kind of method entry data is needed.
To operate definition, then you need to use rb_method_entry_t.
You can access them by the following functions.
* rb_method_entry(VALUE klass, ID id);
* rb_method_entry_with_refinements(VALUE klass, ID id);
* rb_method_entry_without_refinements(VALUE klass, ID id);
* rb_resolve_refined_method(VALUE refinements, const rb_method_entry_t *me);
To invoke methods, then you need to use rb_callable_method_entry_t
which you can get by the following APIs corresponding to the
above listed functions.
* rb_callable_method_entry(VALUE klass, ID id);
* rb_callable_method_entry_with_refinements(VALUE klass, ID id);
* rb_callable_method_entry_without_refinements(VALUE klass, ID id);
* rb_resolve_refined_method_callable(VALUE refinements, const rb_callable_method_entry_t *me);
VM pushes rb_callable_method_entry_t, so that rb_vm_frame_method_entry()
returns rb_callable_method_entry_t.
You can check a super class of current method by
rb_callable_method_entry_t::defined_class.
* method.h: renamed from rb_method_entry_t::klass to
rb_method_entry_t::owner.
* internal.h: add rb_classext_struct::callable_m_tbl to cache
rb_callable_method_entry_t data.
We need to consider abotu this field again because it is only
active for T_ICLASS.
* class.c (method_entry_i): ditto.
* class.c (rb_define_attr): rb_method_entry() does not takes
defiend_class_ptr.
* gc.c (mark_method_entry): mark RCLASS_CALLABLE_M_TBL() for T_ICLASS.
* cont.c (fiber_init): rb_control_frame_t::klass is removed.
* proc.c: fix `struct METHOD' data structure because
rb_callable_method_t has all information.
* vm_core.h: remove several fields.
* rb_control_frame_t::klass.
* rb_block_t::klass.
And catch up changes.
* eval.c: catch up changes.
* gc.c: ditto.
* insns.def: ditto.
* vm.c: ditto.
* vm_args.c: ditto.
* vm_backtrace.c: ditto.
* vm_dump.c: ditto.
* vm_eval.c: ditto.
* vm_insnhelper.c: ditto.
* vm_method.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@51126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2015-07-03 07:24:50 -04:00
|
|
|
RB_OBJ_WRITE(method, &data->recv, Qundef);
|
|
|
|
RB_OBJ_WRITE(method, &data->klass, orig->klass);
|
2015-06-02 00:20:30 -04:00
|
|
|
RB_OBJ_WRITE(method, &data->me, rb_method_entry_clone(orig->me));
|
2007-02-02 10:21:41 -05:00
|
|
|
OBJ_INFECT(method, obj);
|
|
|
|
|
|
|
|
return method;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-17 17:07:33 -04:00
|
|
|
* meth.receiver -> object
|
2009-02-22 09:23:33 -05:00
|
|
|
*
|
2007-02-02 10:21:41 -05:00
|
|
|
* Returns the bound receiver of the method object.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
method_receiver(VALUE obj)
|
|
|
|
{
|
|
|
|
struct METHOD *data;
|
|
|
|
|
2009-07-08 04:13:41 -04:00
|
|
|
TypedData_Get_Struct(obj, struct METHOD, &method_data_type, data);
|
2007-02-02 10:21:41 -05:00
|
|
|
return data->recv;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-17 17:07:33 -04:00
|
|
|
* meth.name -> symbol
|
2009-02-22 09:23:33 -05:00
|
|
|
*
|
2007-02-02 10:21:41 -05:00
|
|
|
* Returns the name of the method.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
method_name(VALUE obj)
|
|
|
|
{
|
|
|
|
struct METHOD *data;
|
|
|
|
|
2009-07-08 04:13:41 -04:00
|
|
|
TypedData_Get_Struct(obj, struct METHOD, &method_data_type, data);
|
* method.h: introduce rb_callable_method_entry_t to remove
rb_control_frame_t::klass.
[Bug #11278], [Bug #11279]
rb_method_entry_t data belong to modules/classes.
rb_method_entry_t::owner points defined module or class.
module M
def foo; end
end
In this case, owner is M.
rb_callable_method_entry_t data belong to only classes.
For modules, MRI creates corresponding T_ICLASS internally.
rb_callable_method_entry_t can also belong to T_ICLASS.
rb_callable_method_entry_t::defined_class points T_CLASS or
T_ICLASS.
rb_method_entry_t data for classes (not for modules) are also
rb_callable_method_entry_t data because it is completely same data.
In this case, rb_method_entry_t::owner == rb_method_entry_t::defined_class.
For example, there are classes C and D, and incldues M,
class C; include M; end
class D; include M; end
then, two T_ICLASS objects for C's super class and D's super class
will be created.
When C.new.foo is called, then M#foo is searcheed and
rb_callable_method_t data is used by VM to invoke M#foo.
rb_method_entry_t data is only one for M#foo.
However, rb_callable_method_entry_t data are two (and can be more).
It is proportional to the number of including (and prepending)
classes (the number of T_ICLASS which point to the module).
Now, created rb_callable_method_entry_t are collected when
the original module M was modified. We can think it is a cache.
We need to select what kind of method entry data is needed.
To operate definition, then you need to use rb_method_entry_t.
You can access them by the following functions.
* rb_method_entry(VALUE klass, ID id);
* rb_method_entry_with_refinements(VALUE klass, ID id);
* rb_method_entry_without_refinements(VALUE klass, ID id);
* rb_resolve_refined_method(VALUE refinements, const rb_method_entry_t *me);
To invoke methods, then you need to use rb_callable_method_entry_t
which you can get by the following APIs corresponding to the
above listed functions.
* rb_callable_method_entry(VALUE klass, ID id);
* rb_callable_method_entry_with_refinements(VALUE klass, ID id);
* rb_callable_method_entry_without_refinements(VALUE klass, ID id);
* rb_resolve_refined_method_callable(VALUE refinements, const rb_callable_method_entry_t *me);
VM pushes rb_callable_method_entry_t, so that rb_vm_frame_method_entry()
returns rb_callable_method_entry_t.
You can check a super class of current method by
rb_callable_method_entry_t::defined_class.
* method.h: renamed from rb_method_entry_t::klass to
rb_method_entry_t::owner.
* internal.h: add rb_classext_struct::callable_m_tbl to cache
rb_callable_method_entry_t data.
We need to consider abotu this field again because it is only
active for T_ICLASS.
* class.c (method_entry_i): ditto.
* class.c (rb_define_attr): rb_method_entry() does not takes
defiend_class_ptr.
* gc.c (mark_method_entry): mark RCLASS_CALLABLE_M_TBL() for T_ICLASS.
* cont.c (fiber_init): rb_control_frame_t::klass is removed.
* proc.c: fix `struct METHOD' data structure because
rb_callable_method_t has all information.
* vm_core.h: remove several fields.
* rb_control_frame_t::klass.
* rb_block_t::klass.
And catch up changes.
* eval.c: catch up changes.
* gc.c: ditto.
* insns.def: ditto.
* vm.c: ditto.
* vm_args.c: ditto.
* vm_backtrace.c: ditto.
* vm_dump.c: ditto.
* vm_eval.c: ditto.
* vm_insnhelper.c: ditto.
* vm_method.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@51126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2015-07-03 07:24:50 -04:00
|
|
|
return ID2SYM(data->me->called_id);
|
2007-02-02 10:21:41 -05:00
|
|
|
}
|
|
|
|
|
2013-02-13 04:12:04 -05:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* meth.original_name -> symbol
|
|
|
|
*
|
|
|
|
* Returns the original name of the method.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
method_original_name(VALUE obj)
|
|
|
|
{
|
|
|
|
struct METHOD *data;
|
|
|
|
|
|
|
|
TypedData_Get_Struct(obj, struct METHOD, &method_data_type, data);
|
|
|
|
return ID2SYM(data->me->def->original_id);
|
|
|
|
}
|
|
|
|
|
2007-02-02 10:21:41 -05:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-17 17:07:33 -04:00
|
|
|
* meth.owner -> class_or_module
|
2009-02-22 09:23:33 -05:00
|
|
|
*
|
2007-02-02 10:21:41 -05:00
|
|
|
* Returns the class or module that defines the method.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
method_owner(VALUE obj)
|
|
|
|
{
|
|
|
|
struct METHOD *data;
|
2009-07-08 04:13:41 -04:00
|
|
|
TypedData_Get_Struct(obj, struct METHOD, &method_data_type, data);
|
* method.h: introduce rb_callable_method_entry_t to remove
rb_control_frame_t::klass.
[Bug #11278], [Bug #11279]
rb_method_entry_t data belong to modules/classes.
rb_method_entry_t::owner points defined module or class.
module M
def foo; end
end
In this case, owner is M.
rb_callable_method_entry_t data belong to only classes.
For modules, MRI creates corresponding T_ICLASS internally.
rb_callable_method_entry_t can also belong to T_ICLASS.
rb_callable_method_entry_t::defined_class points T_CLASS or
T_ICLASS.
rb_method_entry_t data for classes (not for modules) are also
rb_callable_method_entry_t data because it is completely same data.
In this case, rb_method_entry_t::owner == rb_method_entry_t::defined_class.
For example, there are classes C and D, and incldues M,
class C; include M; end
class D; include M; end
then, two T_ICLASS objects for C's super class and D's super class
will be created.
When C.new.foo is called, then M#foo is searcheed and
rb_callable_method_t data is used by VM to invoke M#foo.
rb_method_entry_t data is only one for M#foo.
However, rb_callable_method_entry_t data are two (and can be more).
It is proportional to the number of including (and prepending)
classes (the number of T_ICLASS which point to the module).
Now, created rb_callable_method_entry_t are collected when
the original module M was modified. We can think it is a cache.
We need to select what kind of method entry data is needed.
To operate definition, then you need to use rb_method_entry_t.
You can access them by the following functions.
* rb_method_entry(VALUE klass, ID id);
* rb_method_entry_with_refinements(VALUE klass, ID id);
* rb_method_entry_without_refinements(VALUE klass, ID id);
* rb_resolve_refined_method(VALUE refinements, const rb_method_entry_t *me);
To invoke methods, then you need to use rb_callable_method_entry_t
which you can get by the following APIs corresponding to the
above listed functions.
* rb_callable_method_entry(VALUE klass, ID id);
* rb_callable_method_entry_with_refinements(VALUE klass, ID id);
* rb_callable_method_entry_without_refinements(VALUE klass, ID id);
* rb_resolve_refined_method_callable(VALUE refinements, const rb_callable_method_entry_t *me);
VM pushes rb_callable_method_entry_t, so that rb_vm_frame_method_entry()
returns rb_callable_method_entry_t.
You can check a super class of current method by
rb_callable_method_entry_t::defined_class.
* method.h: renamed from rb_method_entry_t::klass to
rb_method_entry_t::owner.
* internal.h: add rb_classext_struct::callable_m_tbl to cache
rb_callable_method_entry_t data.
We need to consider abotu this field again because it is only
active for T_ICLASS.
* class.c (method_entry_i): ditto.
* class.c (rb_define_attr): rb_method_entry() does not takes
defiend_class_ptr.
* gc.c (mark_method_entry): mark RCLASS_CALLABLE_M_TBL() for T_ICLASS.
* cont.c (fiber_init): rb_control_frame_t::klass is removed.
* proc.c: fix `struct METHOD' data structure because
rb_callable_method_t has all information.
* vm_core.h: remove several fields.
* rb_control_frame_t::klass.
* rb_block_t::klass.
And catch up changes.
* eval.c: catch up changes.
* gc.c: ditto.
* insns.def: ditto.
* vm.c: ditto.
* vm_args.c: ditto.
* vm_backtrace.c: ditto.
* vm_dump.c: ditto.
* vm_eval.c: ditto.
* vm_insnhelper.c: ditto.
* vm_method.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@51126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2015-07-03 07:24:50 -04:00
|
|
|
return data->me->owner;
|
2007-02-02 10:21:41 -05:00
|
|
|
}
|
|
|
|
|
2011-07-26 12:05:35 -04:00
|
|
|
void
|
2011-07-23 11:05:03 -04:00
|
|
|
rb_method_name_error(VALUE klass, VALUE str)
|
|
|
|
{
|
2015-10-28 02:24:12 -04:00
|
|
|
#define MSG(s) rb_fstring_cstr("undefined method `%1$s' for"s" `%2$s'")
|
2011-07-23 11:05:03 -04:00
|
|
|
VALUE c = klass;
|
2015-10-28 02:24:12 -04:00
|
|
|
VALUE s;
|
2011-07-23 11:05:03 -04:00
|
|
|
|
|
|
|
if (FL_TEST(c, FL_SINGLETON)) {
|
|
|
|
VALUE obj = rb_ivar_get(klass, attached);
|
|
|
|
|
2015-10-28 02:24:12 -04:00
|
|
|
switch (BUILTIN_TYPE(obj)) {
|
2011-07-23 11:05:03 -04:00
|
|
|
case T_MODULE:
|
|
|
|
case T_CLASS:
|
|
|
|
c = obj;
|
2015-10-28 02:24:12 -04:00
|
|
|
s = MSG("");
|
2011-07-23 11:05:03 -04:00
|
|
|
}
|
2015-10-28 02:24:12 -04:00
|
|
|
goto normal_class;
|
2011-07-23 11:05:03 -04:00
|
|
|
}
|
|
|
|
else if (RB_TYPE_P(c, T_MODULE)) {
|
2015-10-28 02:24:12 -04:00
|
|
|
s = MSG(" module");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
normal_class:
|
|
|
|
s = MSG(" class");
|
2011-07-23 11:05:03 -04:00
|
|
|
}
|
2015-10-28 02:24:12 -04:00
|
|
|
rb_name_err_raise_str(s, c, str);
|
|
|
|
#undef MSG
|
2011-07-23 11:05:03 -04:00
|
|
|
}
|
|
|
|
|
2015-03-20 05:41:06 -04:00
|
|
|
static VALUE
|
|
|
|
obj_method(VALUE obj, VALUE vid, int scope)
|
|
|
|
{
|
|
|
|
ID id = rb_check_id(&vid);
|
|
|
|
const VALUE klass = CLASS_OF(obj);
|
|
|
|
const VALUE mclass = rb_cMethod;
|
|
|
|
|
|
|
|
if (!id) {
|
|
|
|
if (respond_to_missing_p(klass, obj, vid, scope)) {
|
|
|
|
id = rb_intern_str(vid);
|
2016-08-18 11:59:12 -04:00
|
|
|
return mnew_missing(klass, obj, id, mclass);
|
2015-03-20 05:41:06 -04:00
|
|
|
}
|
|
|
|
rb_method_name_error(klass, vid);
|
|
|
|
}
|
|
|
|
return mnew(klass, obj, id, mclass, scope);
|
|
|
|
}
|
|
|
|
|
2007-02-02 10:21:41 -05:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-17 17:07:33 -04:00
|
|
|
* obj.method(sym) -> method
|
2009-02-22 09:23:33 -05:00
|
|
|
*
|
2007-02-02 10:21:41 -05:00
|
|
|
* Looks up the named method as a receiver in <i>obj</i>, returning a
|
|
|
|
* <code>Method</code> object (or raising <code>NameError</code>). The
|
|
|
|
* <code>Method</code> object acts as a closure in <i>obj</i>'s object
|
|
|
|
* instance, so instance variables and the value of <code>self</code>
|
|
|
|
* remain available.
|
2009-02-22 09:23:33 -05:00
|
|
|
*
|
2007-02-02 10:21:41 -05:00
|
|
|
* class Demo
|
|
|
|
* def initialize(n)
|
|
|
|
* @iv = n
|
|
|
|
* end
|
|
|
|
* def hello()
|
|
|
|
* "Hello, @iv = #{@iv}"
|
|
|
|
* end
|
|
|
|
* end
|
2009-02-22 09:23:33 -05:00
|
|
|
*
|
2007-02-02 10:21:41 -05:00
|
|
|
* k = Demo.new(99)
|
|
|
|
* m = k.method(:hello)
|
|
|
|
* m.call #=> "Hello, @iv = 99"
|
2009-02-22 09:23:33 -05:00
|
|
|
*
|
2007-02-02 10:21:41 -05:00
|
|
|
* l = Demo.new('Fred')
|
|
|
|
* m = l.method("hello")
|
|
|
|
* m.call #=> "Hello, @iv = Fred"
|
|
|
|
*/
|
|
|
|
|
|
|
|
VALUE
|
2007-02-05 13:31:08 -05:00
|
|
|
rb_obj_method(VALUE obj, VALUE vid)
|
2007-02-02 10:21:41 -05:00
|
|
|
{
|
2015-03-20 05:41:06 -04:00
|
|
|
return obj_method(obj, vid, FALSE);
|
2007-12-17 18:01:50 -05:00
|
|
|
}
|
|
|
|
|
2009-09-21 07:06:32 -04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-17 17:07:33 -04:00
|
|
|
* obj.public_method(sym) -> method
|
2009-09-21 07:06:32 -04:00
|
|
|
*
|
2009-11-03 12:46:28 -05:00
|
|
|
* Similar to _method_, searches public method only.
|
2009-09-21 07:06:32 -04:00
|
|
|
*/
|
|
|
|
|
2007-12-17 18:01:50 -05:00
|
|
|
VALUE
|
|
|
|
rb_obj_public_method(VALUE obj, VALUE vid)
|
|
|
|
{
|
2015-03-20 05:41:06 -04:00
|
|
|
return obj_method(obj, vid, TRUE);
|
2007-02-02 10:21:41 -05:00
|
|
|
}
|
|
|
|
|
2013-05-13 01:52:03 -04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* obj.singleton_method(sym) -> method
|
|
|
|
*
|
|
|
|
* Similar to _method_, searches singleton method only.
|
|
|
|
*
|
|
|
|
* class Demo
|
|
|
|
* def initialize(n)
|
|
|
|
* @iv = n
|
|
|
|
* end
|
|
|
|
* def hello()
|
|
|
|
* "Hello, @iv = #{@iv}"
|
|
|
|
* end
|
|
|
|
* end
|
|
|
|
*
|
|
|
|
* k = Demo.new(99)
|
|
|
|
* def k.hi
|
|
|
|
* "Hi, @iv = #{@iv}"
|
|
|
|
* end
|
|
|
|
* m = k.singleton_method(:hi)
|
|
|
|
* m.call #=> "Hi, @iv = 99"
|
|
|
|
* m = k.singleton_method(:hello) #=> NameError
|
|
|
|
*/
|
|
|
|
|
|
|
|
VALUE
|
|
|
|
rb_obj_singleton_method(VALUE obj, VALUE vid)
|
|
|
|
{
|
2015-05-21 05:01:44 -04:00
|
|
|
const rb_method_entry_t *me;
|
2013-05-13 01:52:03 -04:00
|
|
|
VALUE klass;
|
|
|
|
ID id = rb_check_id(&vid);
|
2015-05-21 05:01:44 -04:00
|
|
|
|
2013-05-13 01:52:03 -04:00
|
|
|
if (!id) {
|
2015-03-20 05:41:06 -04:00
|
|
|
if (!NIL_P(klass = rb_singleton_class_get(obj)) &&
|
|
|
|
respond_to_missing_p(klass, obj, vid, FALSE)) {
|
|
|
|
id = rb_intern_str(vid);
|
2016-08-18 11:59:12 -04:00
|
|
|
return mnew_missing(klass, obj, id, rb_cMethod);
|
2015-03-20 05:41:06 -04:00
|
|
|
}
|
2015-10-28 02:24:12 -04:00
|
|
|
undef:
|
|
|
|
rb_name_err_raise("undefined singleton method `%1$s' for `%2$s'",
|
|
|
|
obj, vid);
|
2013-05-13 01:52:03 -04:00
|
|
|
}
|
|
|
|
if (NIL_P(klass = rb_singleton_class_get(obj)) ||
|
2015-01-15 07:35:00 -05:00
|
|
|
UNDEFINED_METHOD_ENTRY_P(me = rb_method_entry_at(klass, id)) ||
|
|
|
|
UNDEFINED_REFINED_METHOD_P(me->def)) {
|
2015-10-28 02:24:12 -04:00
|
|
|
vid = ID2SYM(id);
|
|
|
|
goto undef;
|
2013-05-13 01:52:03 -04:00
|
|
|
}
|
* method.h: introduce rb_callable_method_entry_t to remove
rb_control_frame_t::klass.
[Bug #11278], [Bug #11279]
rb_method_entry_t data belong to modules/classes.
rb_method_entry_t::owner points defined module or class.
module M
def foo; end
end
In this case, owner is M.
rb_callable_method_entry_t data belong to only classes.
For modules, MRI creates corresponding T_ICLASS internally.
rb_callable_method_entry_t can also belong to T_ICLASS.
rb_callable_method_entry_t::defined_class points T_CLASS or
T_ICLASS.
rb_method_entry_t data for classes (not for modules) are also
rb_callable_method_entry_t data because it is completely same data.
In this case, rb_method_entry_t::owner == rb_method_entry_t::defined_class.
For example, there are classes C and D, and incldues M,
class C; include M; end
class D; include M; end
then, two T_ICLASS objects for C's super class and D's super class
will be created.
When C.new.foo is called, then M#foo is searcheed and
rb_callable_method_t data is used by VM to invoke M#foo.
rb_method_entry_t data is only one for M#foo.
However, rb_callable_method_entry_t data are two (and can be more).
It is proportional to the number of including (and prepending)
classes (the number of T_ICLASS which point to the module).
Now, created rb_callable_method_entry_t are collected when
the original module M was modified. We can think it is a cache.
We need to select what kind of method entry data is needed.
To operate definition, then you need to use rb_method_entry_t.
You can access them by the following functions.
* rb_method_entry(VALUE klass, ID id);
* rb_method_entry_with_refinements(VALUE klass, ID id);
* rb_method_entry_without_refinements(VALUE klass, ID id);
* rb_resolve_refined_method(VALUE refinements, const rb_method_entry_t *me);
To invoke methods, then you need to use rb_callable_method_entry_t
which you can get by the following APIs corresponding to the
above listed functions.
* rb_callable_method_entry(VALUE klass, ID id);
* rb_callable_method_entry_with_refinements(VALUE klass, ID id);
* rb_callable_method_entry_without_refinements(VALUE klass, ID id);
* rb_resolve_refined_method_callable(VALUE refinements, const rb_callable_method_entry_t *me);
VM pushes rb_callable_method_entry_t, so that rb_vm_frame_method_entry()
returns rb_callable_method_entry_t.
You can check a super class of current method by
rb_callable_method_entry_t::defined_class.
* method.h: renamed from rb_method_entry_t::klass to
rb_method_entry_t::owner.
* internal.h: add rb_classext_struct::callable_m_tbl to cache
rb_callable_method_entry_t data.
We need to consider abotu this field again because it is only
active for T_ICLASS.
* class.c (method_entry_i): ditto.
* class.c (rb_define_attr): rb_method_entry() does not takes
defiend_class_ptr.
* gc.c (mark_method_entry): mark RCLASS_CALLABLE_M_TBL() for T_ICLASS.
* cont.c (fiber_init): rb_control_frame_t::klass is removed.
* proc.c: fix `struct METHOD' data structure because
rb_callable_method_t has all information.
* vm_core.h: remove several fields.
* rb_control_frame_t::klass.
* rb_block_t::klass.
And catch up changes.
* eval.c: catch up changes.
* gc.c: ditto.
* insns.def: ditto.
* vm.c: ditto.
* vm_args.c: ditto.
* vm_backtrace.c: ditto.
* vm_dump.c: ditto.
* vm_eval.c: ditto.
* vm_insnhelper.c: ditto.
* vm_method.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@51126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2015-07-03 07:24:50 -04:00
|
|
|
return mnew_from_me(me, klass, obj, id, rb_cMethod, FALSE);
|
2013-05-13 01:52:03 -04:00
|
|
|
}
|
|
|
|
|
2007-02-02 10:21:41 -05:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-17 17:07:33 -04:00
|
|
|
* mod.instance_method(symbol) -> unbound_method
|
2009-02-22 09:23:33 -05:00
|
|
|
*
|
2007-02-02 10:21:41 -05:00
|
|
|
* Returns an +UnboundMethod+ representing the given
|
|
|
|
* instance method in _mod_.
|
2009-02-22 09:23:33 -05:00
|
|
|
*
|
2007-02-02 10:21:41 -05:00
|
|
|
* class Interpreter
|
|
|
|
* def do_a() print "there, "; end
|
|
|
|
* def do_d() print "Hello "; end
|
|
|
|
* def do_e() print "!\n"; end
|
|
|
|
* def do_v() print "Dave"; end
|
|
|
|
* Dispatcher = {
|
2009-12-07 14:06:11 -05:00
|
|
|
* "a" => instance_method(:do_a),
|
|
|
|
* "d" => instance_method(:do_d),
|
|
|
|
* "e" => instance_method(:do_e),
|
|
|
|
* "v" => instance_method(:do_v)
|
2007-02-02 10:21:41 -05:00
|
|
|
* }
|
|
|
|
* def interpret(string)
|
2009-12-07 14:06:11 -05:00
|
|
|
* string.each_char {|b| Dispatcher[b].bind(self).call }
|
2007-02-02 10:21:41 -05:00
|
|
|
* end
|
|
|
|
* end
|
2009-02-22 09:23:33 -05:00
|
|
|
*
|
2007-02-02 10:21:41 -05:00
|
|
|
* interpreter = Interpreter.new
|
|
|
|
* interpreter.interpret('dave')
|
2009-02-22 09:23:33 -05:00
|
|
|
*
|
2007-02-02 10:21:41 -05:00
|
|
|
* <em>produces:</em>
|
2009-02-22 09:23:33 -05:00
|
|
|
*
|
2007-02-02 10:21:41 -05:00
|
|
|
* Hello there, Dave!
|
|
|
|
*/
|
|
|
|
|
|
|
|
static VALUE
|
2007-12-17 18:01:50 -05:00
|
|
|
rb_mod_instance_method(VALUE mod, VALUE vid)
|
|
|
|
{
|
2011-07-26 12:05:27 -04:00
|
|
|
ID id = rb_check_id(&vid);
|
2011-07-23 11:05:03 -04:00
|
|
|
if (!id) {
|
|
|
|
rb_method_name_error(mod, vid);
|
|
|
|
}
|
|
|
|
return mnew(mod, Qundef, id, rb_cUnboundMethod, FALSE);
|
2007-12-17 18:01:50 -05:00
|
|
|
}
|
|
|
|
|
2009-09-21 07:06:32 -04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-17 17:07:33 -04:00
|
|
|
* mod.public_instance_method(symbol) -> unbound_method
|
2009-09-21 07:06:32 -04:00
|
|
|
*
|
2009-11-03 12:46:28 -05:00
|
|
|
* Similar to _instance_method_, searches public method only.
|
2009-09-21 07:06:32 -04:00
|
|
|
*/
|
|
|
|
|
2007-12-17 18:01:50 -05:00
|
|
|
static VALUE
|
|
|
|
rb_mod_public_instance_method(VALUE mod, VALUE vid)
|
2007-02-02 10:21:41 -05:00
|
|
|
{
|
2011-07-26 12:05:27 -04:00
|
|
|
ID id = rb_check_id(&vid);
|
2011-07-23 11:05:03 -04:00
|
|
|
if (!id) {
|
|
|
|
rb_method_name_error(mod, vid);
|
|
|
|
}
|
|
|
|
return mnew(mod, Qundef, id, rb_cUnboundMethod, TRUE);
|
2007-02-02 10:21:41 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
2013-08-14 01:35:21 -04:00
|
|
|
* define_method(symbol, method) -> symbol
|
|
|
|
* define_method(symbol) { block } -> symbol
|
2009-02-22 09:23:33 -05:00
|
|
|
*
|
2007-02-02 10:21:41 -05:00
|
|
|
* Defines an instance method in the receiver. The _method_
|
2009-09-22 14:53:25 -04:00
|
|
|
* parameter can be a +Proc+, a +Method+ or an +UnboundMethod+ object.
|
2007-02-02 10:21:41 -05:00
|
|
|
* If a block is specified, it is used as the method body. This block
|
|
|
|
* is evaluated using <code>instance_eval</code>, a point that is
|
|
|
|
* tricky to demonstrate because <code>define_method</code> is private.
|
|
|
|
* (This is why we resort to the +send+ hack in this example.)
|
2009-02-22 09:23:33 -05:00
|
|
|
*
|
2007-02-02 10:21:41 -05:00
|
|
|
* class A
|
|
|
|
* def fred
|
|
|
|
* puts "In Fred"
|
|
|
|
* end
|
|
|
|
* def create_method(name, &block)
|
|
|
|
* self.class.send(:define_method, name, &block)
|
|
|
|
* end
|
|
|
|
* define_method(:wilma) { puts "Charge it!" }
|
|
|
|
* end
|
|
|
|
* class B < A
|
|
|
|
* define_method(:barney, instance_method(:fred))
|
|
|
|
* end
|
|
|
|
* a = B.new
|
|
|
|
* a.barney
|
|
|
|
* a.wilma
|
|
|
|
* a.create_method(:betty) { p self }
|
|
|
|
* a.betty
|
2009-02-22 09:23:33 -05:00
|
|
|
*
|
2007-02-02 10:21:41 -05:00
|
|
|
* <em>produces:</em>
|
2009-02-22 09:23:33 -05:00
|
|
|
*
|
2007-02-02 10:21:41 -05:00
|
|
|
* In Fred
|
|
|
|
* Charge it!
|
|
|
|
* #<B:0x401b39e8>
|
|
|
|
*/
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
rb_mod_define_method(int argc, VALUE *argv, VALUE mod)
|
|
|
|
{
|
|
|
|
ID id;
|
|
|
|
VALUE body;
|
2015-06-19 00:55:01 -04:00
|
|
|
VALUE name;
|
2015-03-08 17:22:43 -04:00
|
|
|
const rb_cref_t *cref = rb_vm_cref_in_context(mod, mod);
|
2015-06-02 21:39:16 -04:00
|
|
|
const rb_scope_visibility_t default_scope_visi = {METHOD_VISI_PUBLIC, FALSE};
|
|
|
|
const rb_scope_visibility_t *scope_visi = &default_scope_visi;
|
2015-06-15 04:18:18 -04:00
|
|
|
int is_method = FALSE;
|
2013-12-24 02:28:11 -05:00
|
|
|
|
2013-12-24 09:04:31 -05:00
|
|
|
if (cref) {
|
2015-06-02 21:39:16 -04:00
|
|
|
scope_visi = CREF_SCOPE_VISI(cref);
|
2013-12-24 02:28:11 -05:00
|
|
|
}
|
2007-02-02 10:21:41 -05:00
|
|
|
|
2015-06-19 00:55:01 -04:00
|
|
|
rb_check_arity(argc, 1, 2);
|
|
|
|
name = argv[0];
|
|
|
|
id = rb_check_id(&name);
|
2007-02-02 10:21:41 -05:00
|
|
|
if (argc == 1) {
|
2015-06-19 01:53:41 -04:00
|
|
|
#if PROC_NEW_REQUIRES_BLOCK
|
2007-02-02 10:21:41 -05:00
|
|
|
body = rb_block_lambda();
|
2015-06-19 01:53:41 -04:00
|
|
|
#else
|
|
|
|
rb_thread_t *th = GET_THREAD();
|
2016-07-28 07:02:30 -04:00
|
|
|
VALUE block_handler = rb_vm_frame_block_handler(th->cfp);
|
|
|
|
if (block_handler == VM_BLOCK_HANDLER_NONE) rb_raise(rb_eArgError, proc_without_block);
|
2015-12-19 21:14:57 -05:00
|
|
|
|
2016-07-28 07:02:30 -04:00
|
|
|
switch (vm_block_handler_type(block_handler)) {
|
|
|
|
case block_handler_type_proc:
|
|
|
|
body = VM_BH_TO_PROC(block_handler);
|
|
|
|
break;
|
|
|
|
case block_handler_type_symbol:
|
|
|
|
body = rb_sym_to_proc(VM_BH_TO_SYMBOL(block_handler));
|
|
|
|
break;
|
|
|
|
case block_handler_type_iseq:
|
|
|
|
case block_handler_type_ifunc:
|
|
|
|
body = rb_vm_make_proc_lambda(th, VM_BH_TO_CAPT_BLOCK(block_handler), rb_cProc, TRUE);
|
2015-06-19 01:53:41 -04:00
|
|
|
}
|
|
|
|
#endif
|
2007-02-02 10:21:41 -05:00
|
|
|
}
|
2012-03-14 17:10:34 -04:00
|
|
|
else {
|
2007-02-02 10:21:41 -05:00
|
|
|
body = argv[1];
|
2015-12-19 21:02:55 -05:00
|
|
|
|
|
|
|
if (rb_obj_is_method(body)) {
|
|
|
|
is_method = TRUE;
|
|
|
|
}
|
|
|
|
else if (rb_obj_is_proc(body)) {
|
|
|
|
is_method = FALSE;
|
|
|
|
}
|
|
|
|
else {
|
2007-02-02 10:21:41 -05:00
|
|
|
rb_raise(rb_eTypeError,
|
|
|
|
"wrong argument type %s (expected Proc/Method)",
|
|
|
|
rb_obj_classname(body));
|
|
|
|
}
|
|
|
|
}
|
2015-06-19 00:55:01 -04:00
|
|
|
if (!id) id = rb_to_id(name);
|
2007-02-02 10:21:41 -05:00
|
|
|
|
2015-06-15 04:18:18 -04:00
|
|
|
if (is_method) {
|
2007-02-02 10:21:41 -05:00
|
|
|
struct METHOD *method = (struct METHOD *)DATA_PTR(body);
|
* method.h: introduce rb_callable_method_entry_t to remove
rb_control_frame_t::klass.
[Bug #11278], [Bug #11279]
rb_method_entry_t data belong to modules/classes.
rb_method_entry_t::owner points defined module or class.
module M
def foo; end
end
In this case, owner is M.
rb_callable_method_entry_t data belong to only classes.
For modules, MRI creates corresponding T_ICLASS internally.
rb_callable_method_entry_t can also belong to T_ICLASS.
rb_callable_method_entry_t::defined_class points T_CLASS or
T_ICLASS.
rb_method_entry_t data for classes (not for modules) are also
rb_callable_method_entry_t data because it is completely same data.
In this case, rb_method_entry_t::owner == rb_method_entry_t::defined_class.
For example, there are classes C and D, and incldues M,
class C; include M; end
class D; include M; end
then, two T_ICLASS objects for C's super class and D's super class
will be created.
When C.new.foo is called, then M#foo is searcheed and
rb_callable_method_t data is used by VM to invoke M#foo.
rb_method_entry_t data is only one for M#foo.
However, rb_callable_method_entry_t data are two (and can be more).
It is proportional to the number of including (and prepending)
classes (the number of T_ICLASS which point to the module).
Now, created rb_callable_method_entry_t are collected when
the original module M was modified. We can think it is a cache.
We need to select what kind of method entry data is needed.
To operate definition, then you need to use rb_method_entry_t.
You can access them by the following functions.
* rb_method_entry(VALUE klass, ID id);
* rb_method_entry_with_refinements(VALUE klass, ID id);
* rb_method_entry_without_refinements(VALUE klass, ID id);
* rb_resolve_refined_method(VALUE refinements, const rb_method_entry_t *me);
To invoke methods, then you need to use rb_callable_method_entry_t
which you can get by the following APIs corresponding to the
above listed functions.
* rb_callable_method_entry(VALUE klass, ID id);
* rb_callable_method_entry_with_refinements(VALUE klass, ID id);
* rb_callable_method_entry_without_refinements(VALUE klass, ID id);
* rb_resolve_refined_method_callable(VALUE refinements, const rb_callable_method_entry_t *me);
VM pushes rb_callable_method_entry_t, so that rb_vm_frame_method_entry()
returns rb_callable_method_entry_t.
You can check a super class of current method by
rb_callable_method_entry_t::defined_class.
* method.h: renamed from rb_method_entry_t::klass to
rb_method_entry_t::owner.
* internal.h: add rb_classext_struct::callable_m_tbl to cache
rb_callable_method_entry_t data.
We need to consider abotu this field again because it is only
active for T_ICLASS.
* class.c (method_entry_i): ditto.
* class.c (rb_define_attr): rb_method_entry() does not takes
defiend_class_ptr.
* gc.c (mark_method_entry): mark RCLASS_CALLABLE_M_TBL() for T_ICLASS.
* cont.c (fiber_init): rb_control_frame_t::klass is removed.
* proc.c: fix `struct METHOD' data structure because
rb_callable_method_t has all information.
* vm_core.h: remove several fields.
* rb_control_frame_t::klass.
* rb_block_t::klass.
And catch up changes.
* eval.c: catch up changes.
* gc.c: ditto.
* insns.def: ditto.
* vm.c: ditto.
* vm_args.c: ditto.
* vm_backtrace.c: ditto.
* vm_dump.c: ditto.
* vm_eval.c: ditto.
* vm_insnhelper.c: ditto.
* vm_method.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@51126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2015-07-03 07:24:50 -04:00
|
|
|
if (method->me->owner != mod && !RB_TYPE_P(method->me->owner, T_MODULE) &&
|
|
|
|
!RTEST(rb_class_inherited_p(mod, method->me->owner))) {
|
|
|
|
if (FL_TEST(method->me->owner, FL_SINGLETON)) {
|
2007-02-02 10:21:41 -05:00
|
|
|
rb_raise(rb_eTypeError,
|
|
|
|
"can't bind singleton method to a different class");
|
|
|
|
}
|
2009-09-22 16:04:59 -04:00
|
|
|
else {
|
2007-02-02 10:21:41 -05:00
|
|
|
rb_raise(rb_eTypeError,
|
2013-08-31 00:30:30 -04:00
|
|
|
"bind argument must be a subclass of % "PRIsVALUE,
|
* method.h: introduce rb_callable_method_entry_t to remove
rb_control_frame_t::klass.
[Bug #11278], [Bug #11279]
rb_method_entry_t data belong to modules/classes.
rb_method_entry_t::owner points defined module or class.
module M
def foo; end
end
In this case, owner is M.
rb_callable_method_entry_t data belong to only classes.
For modules, MRI creates corresponding T_ICLASS internally.
rb_callable_method_entry_t can also belong to T_ICLASS.
rb_callable_method_entry_t::defined_class points T_CLASS or
T_ICLASS.
rb_method_entry_t data for classes (not for modules) are also
rb_callable_method_entry_t data because it is completely same data.
In this case, rb_method_entry_t::owner == rb_method_entry_t::defined_class.
For example, there are classes C and D, and incldues M,
class C; include M; end
class D; include M; end
then, two T_ICLASS objects for C's super class and D's super class
will be created.
When C.new.foo is called, then M#foo is searcheed and
rb_callable_method_t data is used by VM to invoke M#foo.
rb_method_entry_t data is only one for M#foo.
However, rb_callable_method_entry_t data are two (and can be more).
It is proportional to the number of including (and prepending)
classes (the number of T_ICLASS which point to the module).
Now, created rb_callable_method_entry_t are collected when
the original module M was modified. We can think it is a cache.
We need to select what kind of method entry data is needed.
To operate definition, then you need to use rb_method_entry_t.
You can access them by the following functions.
* rb_method_entry(VALUE klass, ID id);
* rb_method_entry_with_refinements(VALUE klass, ID id);
* rb_method_entry_without_refinements(VALUE klass, ID id);
* rb_resolve_refined_method(VALUE refinements, const rb_method_entry_t *me);
To invoke methods, then you need to use rb_callable_method_entry_t
which you can get by the following APIs corresponding to the
above listed functions.
* rb_callable_method_entry(VALUE klass, ID id);
* rb_callable_method_entry_with_refinements(VALUE klass, ID id);
* rb_callable_method_entry_without_refinements(VALUE klass, ID id);
* rb_resolve_refined_method_callable(VALUE refinements, const rb_callable_method_entry_t *me);
VM pushes rb_callable_method_entry_t, so that rb_vm_frame_method_entry()
returns rb_callable_method_entry_t.
You can check a super class of current method by
rb_callable_method_entry_t::defined_class.
* method.h: renamed from rb_method_entry_t::klass to
rb_method_entry_t::owner.
* internal.h: add rb_classext_struct::callable_m_tbl to cache
rb_callable_method_entry_t data.
We need to consider abotu this field again because it is only
active for T_ICLASS.
* class.c (method_entry_i): ditto.
* class.c (rb_define_attr): rb_method_entry() does not takes
defiend_class_ptr.
* gc.c (mark_method_entry): mark RCLASS_CALLABLE_M_TBL() for T_ICLASS.
* cont.c (fiber_init): rb_control_frame_t::klass is removed.
* proc.c: fix `struct METHOD' data structure because
rb_callable_method_t has all information.
* vm_core.h: remove several fields.
* rb_control_frame_t::klass.
* rb_block_t::klass.
And catch up changes.
* eval.c: catch up changes.
* gc.c: ditto.
* insns.def: ditto.
* vm.c: ditto.
* vm_args.c: ditto.
* vm_backtrace.c: ditto.
* vm_dump.c: ditto.
* vm_eval.c: ditto.
* vm_insnhelper.c: ditto.
* vm_method.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@51126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2015-07-03 07:24:50 -04:00
|
|
|
rb_class_name(method->me->owner));
|
2007-02-02 10:21:41 -05:00
|
|
|
}
|
|
|
|
}
|
2015-06-02 21:39:16 -04:00
|
|
|
rb_method_entry_set(mod, id, method->me, scope_visi->method_visi);
|
|
|
|
if (scope_visi->module_func) {
|
|
|
|
rb_method_entry_set(rb_singleton_class(mod), id, method->me, METHOD_VISI_PUBLIC);
|
2013-03-31 04:18:09 -04:00
|
|
|
}
|
2014-10-12 09:24:35 -04:00
|
|
|
RB_GC_GUARD(body);
|
2007-02-02 10:21:41 -05:00
|
|
|
}
|
2015-06-15 04:18:18 -04:00
|
|
|
else {
|
2016-07-28 07:02:30 -04:00
|
|
|
VALUE procval = proc_dup(body);
|
|
|
|
if (vm_proc_iseq(procval) != NULL) {
|
|
|
|
rb_proc_t *proc;
|
|
|
|
GetProcPtr(procval, proc);
|
2009-07-18 04:05:32 -04:00
|
|
|
proc->is_lambda = TRUE;
|
|
|
|
proc->is_from_method = TRUE;
|
2007-02-02 10:21:41 -05:00
|
|
|
}
|
2016-07-28 07:02:30 -04:00
|
|
|
rb_add_method(mod, id, VM_METHOD_TYPE_BMETHOD, (void *)procval, scope_visi->method_visi);
|
2015-06-02 21:39:16 -04:00
|
|
|
if (scope_visi->module_func) {
|
|
|
|
rb_add_method(rb_singleton_class(mod), id, VM_METHOD_TYPE_BMETHOD, (void *)body, METHOD_VISI_PUBLIC);
|
2013-03-31 04:18:09 -04:00
|
|
|
}
|
2007-02-02 10:21:41 -05:00
|
|
|
}
|
|
|
|
|
2013-08-14 01:35:21 -04:00
|
|
|
return ID2SYM(id);
|
2007-02-02 10:21:41 -05:00
|
|
|
}
|
|
|
|
|
2009-07-13 08:41:24 -04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2016-09-03 11:21:43 -04:00
|
|
|
* define_singleton_method(symbol, method) -> symbol
|
|
|
|
* define_singleton_method(symbol) { block } -> symbol
|
2009-07-13 08:41:24 -04:00
|
|
|
*
|
|
|
|
* Defines a singleton method in the receiver. The _method_
|
2009-09-22 15:02:42 -04:00
|
|
|
* parameter can be a +Proc+, a +Method+ or an +UnboundMethod+ object.
|
2010-05-29 14:51:39 -04:00
|
|
|
* If a block is specified, it is used as the method body.
|
2009-07-13 08:41:24 -04:00
|
|
|
*
|
|
|
|
* class A
|
|
|
|
* class << self
|
|
|
|
* def class_name
|
|
|
|
* to_s
|
|
|
|
* end
|
|
|
|
* end
|
|
|
|
* end
|
|
|
|
* A.define_singleton_method(:who_am_i) do
|
|
|
|
* "I am: #{class_name}"
|
|
|
|
* end
|
|
|
|
* A.who_am_i # ==> "I am: A"
|
|
|
|
*
|
|
|
|
* guy = "Bob"
|
|
|
|
* guy.define_singleton_method(:hello) { "#{self}: Hello there!" }
|
2010-05-17 17:07:33 -04:00
|
|
|
* guy.hello #=> "Bob: Hello there!"
|
2009-07-13 08:41:24 -04:00
|
|
|
*/
|
|
|
|
|
2007-08-30 01:06:52 -04:00
|
|
|
static VALUE
|
|
|
|
rb_obj_define_method(int argc, VALUE *argv, VALUE obj)
|
|
|
|
{
|
|
|
|
VALUE klass = rb_singleton_class(obj);
|
|
|
|
|
|
|
|
return rb_mod_define_method(argc, argv, klass);
|
|
|
|
}
|
|
|
|
|
2012-11-01 19:24:33 -04:00
|
|
|
/*
|
2016-09-03 11:21:43 -04:00
|
|
|
* define_method(symbol, method) -> symbol
|
|
|
|
* define_method(symbol) { block } -> symbol
|
2012-11-01 19:24:33 -04:00
|
|
|
*
|
|
|
|
* Defines a global function by _method_ or the block.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
top_define_method(int argc, VALUE *argv, VALUE obj)
|
|
|
|
{
|
|
|
|
rb_thread_t *th = GET_THREAD();
|
|
|
|
VALUE klass;
|
|
|
|
|
|
|
|
klass = th->top_wrapper;
|
|
|
|
if (klass) {
|
|
|
|
rb_warning("main.define_method in the wrapped load is effective only in wrapper module");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
klass = rb_cObject;
|
|
|
|
}
|
|
|
|
return rb_mod_define_method(argc, argv, klass);
|
|
|
|
}
|
|
|
|
|
2007-02-02 10:21:41 -05:00
|
|
|
/*
|
2012-08-07 16:51:48 -04:00
|
|
|
* call-seq:
|
|
|
|
* method.clone -> new_method
|
|
|
|
*
|
|
|
|
* Returns a clone of this method.
|
|
|
|
*
|
|
|
|
* class A
|
|
|
|
* def foo
|
|
|
|
* return "bar"
|
|
|
|
* end
|
|
|
|
* end
|
|
|
|
*
|
|
|
|
* m = A.new.method(:foo)
|
|
|
|
* m.call # => "bar"
|
|
|
|
* n = m.clone.call # => "bar"
|
2007-02-02 10:21:41 -05:00
|
|
|
*/
|
|
|
|
|
|
|
|
static VALUE
|
2007-02-05 13:31:08 -05:00
|
|
|
method_clone(VALUE self)
|
2007-02-02 10:21:41 -05:00
|
|
|
{
|
|
|
|
VALUE clone;
|
|
|
|
struct METHOD *orig, *data;
|
|
|
|
|
2009-07-08 04:13:41 -04:00
|
|
|
TypedData_Get_Struct(self, struct METHOD, &method_data_type, orig);
|
|
|
|
clone = TypedData_Make_Struct(CLASS_OF(self), struct METHOD, &method_data_type, data);
|
2007-02-02 10:21:41 -05:00
|
|
|
CLONESETUP(clone, self);
|
* method.h: introduce rb_callable_method_entry_t to remove
rb_control_frame_t::klass.
[Bug #11278], [Bug #11279]
rb_method_entry_t data belong to modules/classes.
rb_method_entry_t::owner points defined module or class.
module M
def foo; end
end
In this case, owner is M.
rb_callable_method_entry_t data belong to only classes.
For modules, MRI creates corresponding T_ICLASS internally.
rb_callable_method_entry_t can also belong to T_ICLASS.
rb_callable_method_entry_t::defined_class points T_CLASS or
T_ICLASS.
rb_method_entry_t data for classes (not for modules) are also
rb_callable_method_entry_t data because it is completely same data.
In this case, rb_method_entry_t::owner == rb_method_entry_t::defined_class.
For example, there are classes C and D, and incldues M,
class C; include M; end
class D; include M; end
then, two T_ICLASS objects for C's super class and D's super class
will be created.
When C.new.foo is called, then M#foo is searcheed and
rb_callable_method_t data is used by VM to invoke M#foo.
rb_method_entry_t data is only one for M#foo.
However, rb_callable_method_entry_t data are two (and can be more).
It is proportional to the number of including (and prepending)
classes (the number of T_ICLASS which point to the module).
Now, created rb_callable_method_entry_t are collected when
the original module M was modified. We can think it is a cache.
We need to select what kind of method entry data is needed.
To operate definition, then you need to use rb_method_entry_t.
You can access them by the following functions.
* rb_method_entry(VALUE klass, ID id);
* rb_method_entry_with_refinements(VALUE klass, ID id);
* rb_method_entry_without_refinements(VALUE klass, ID id);
* rb_resolve_refined_method(VALUE refinements, const rb_method_entry_t *me);
To invoke methods, then you need to use rb_callable_method_entry_t
which you can get by the following APIs corresponding to the
above listed functions.
* rb_callable_method_entry(VALUE klass, ID id);
* rb_callable_method_entry_with_refinements(VALUE klass, ID id);
* rb_callable_method_entry_without_refinements(VALUE klass, ID id);
* rb_resolve_refined_method_callable(VALUE refinements, const rb_callable_method_entry_t *me);
VM pushes rb_callable_method_entry_t, so that rb_vm_frame_method_entry()
returns rb_callable_method_entry_t.
You can check a super class of current method by
rb_callable_method_entry_t::defined_class.
* method.h: renamed from rb_method_entry_t::klass to
rb_method_entry_t::owner.
* internal.h: add rb_classext_struct::callable_m_tbl to cache
rb_callable_method_entry_t data.
We need to consider abotu this field again because it is only
active for T_ICLASS.
* class.c (method_entry_i): ditto.
* class.c (rb_define_attr): rb_method_entry() does not takes
defiend_class_ptr.
* gc.c (mark_method_entry): mark RCLASS_CALLABLE_M_TBL() for T_ICLASS.
* cont.c (fiber_init): rb_control_frame_t::klass is removed.
* proc.c: fix `struct METHOD' data structure because
rb_callable_method_t has all information.
* vm_core.h: remove several fields.
* rb_control_frame_t::klass.
* rb_block_t::klass.
And catch up changes.
* eval.c: catch up changes.
* gc.c: ditto.
* insns.def: ditto.
* vm.c: ditto.
* vm_args.c: ditto.
* vm_backtrace.c: ditto.
* vm_dump.c: ditto.
* vm_eval.c: ditto.
* vm_insnhelper.c: ditto.
* vm_method.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@51126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2015-07-03 07:24:50 -04:00
|
|
|
RB_OBJ_WRITE(clone, &data->recv, orig->recv);
|
|
|
|
RB_OBJ_WRITE(clone, &data->klass, orig->klass);
|
2015-06-02 00:20:30 -04:00
|
|
|
RB_OBJ_WRITE(clone, &data->me, rb_method_entry_clone(orig->me));
|
2007-02-02 10:21:41 -05:00
|
|
|
return clone;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-17 17:07:33 -04:00
|
|
|
* meth.call(args, ...) -> obj
|
|
|
|
* meth[args, ...] -> obj
|
2009-02-22 09:23:33 -05:00
|
|
|
*
|
2007-02-02 10:21:41 -05:00
|
|
|
* Invokes the <i>meth</i> with the specified arguments, returning the
|
|
|
|
* method's return value.
|
2009-02-22 09:23:33 -05:00
|
|
|
*
|
2007-02-02 10:21:41 -05:00
|
|
|
* m = 12.method("+")
|
|
|
|
* m.call(3) #=> 15
|
|
|
|
* m.call(20) #=> 32
|
|
|
|
*/
|
|
|
|
|
|
|
|
VALUE
|
2014-06-18 02:16:39 -04:00
|
|
|
rb_method_call(int argc, const VALUE *argv, VALUE method)
|
2013-06-17 08:38:52 -04:00
|
|
|
{
|
2016-07-28 07:02:30 -04:00
|
|
|
VALUE procval = rb_block_given_p() ? rb_block_proc() : Qnil;
|
|
|
|
return rb_method_call_with_block(argc, argv, method, procval);
|
2013-06-17 08:38:52 -04:00
|
|
|
}
|
|
|
|
|
* method.h: introduce rb_callable_method_entry_t to remove
rb_control_frame_t::klass.
[Bug #11278], [Bug #11279]
rb_method_entry_t data belong to modules/classes.
rb_method_entry_t::owner points defined module or class.
module M
def foo; end
end
In this case, owner is M.
rb_callable_method_entry_t data belong to only classes.
For modules, MRI creates corresponding T_ICLASS internally.
rb_callable_method_entry_t can also belong to T_ICLASS.
rb_callable_method_entry_t::defined_class points T_CLASS or
T_ICLASS.
rb_method_entry_t data for classes (not for modules) are also
rb_callable_method_entry_t data because it is completely same data.
In this case, rb_method_entry_t::owner == rb_method_entry_t::defined_class.
For example, there are classes C and D, and incldues M,
class C; include M; end
class D; include M; end
then, two T_ICLASS objects for C's super class and D's super class
will be created.
When C.new.foo is called, then M#foo is searcheed and
rb_callable_method_t data is used by VM to invoke M#foo.
rb_method_entry_t data is only one for M#foo.
However, rb_callable_method_entry_t data are two (and can be more).
It is proportional to the number of including (and prepending)
classes (the number of T_ICLASS which point to the module).
Now, created rb_callable_method_entry_t are collected when
the original module M was modified. We can think it is a cache.
We need to select what kind of method entry data is needed.
To operate definition, then you need to use rb_method_entry_t.
You can access them by the following functions.
* rb_method_entry(VALUE klass, ID id);
* rb_method_entry_with_refinements(VALUE klass, ID id);
* rb_method_entry_without_refinements(VALUE klass, ID id);
* rb_resolve_refined_method(VALUE refinements, const rb_method_entry_t *me);
To invoke methods, then you need to use rb_callable_method_entry_t
which you can get by the following APIs corresponding to the
above listed functions.
* rb_callable_method_entry(VALUE klass, ID id);
* rb_callable_method_entry_with_refinements(VALUE klass, ID id);
* rb_callable_method_entry_without_refinements(VALUE klass, ID id);
* rb_resolve_refined_method_callable(VALUE refinements, const rb_callable_method_entry_t *me);
VM pushes rb_callable_method_entry_t, so that rb_vm_frame_method_entry()
returns rb_callable_method_entry_t.
You can check a super class of current method by
rb_callable_method_entry_t::defined_class.
* method.h: renamed from rb_method_entry_t::klass to
rb_method_entry_t::owner.
* internal.h: add rb_classext_struct::callable_m_tbl to cache
rb_callable_method_entry_t data.
We need to consider abotu this field again because it is only
active for T_ICLASS.
* class.c (method_entry_i): ditto.
* class.c (rb_define_attr): rb_method_entry() does not takes
defiend_class_ptr.
* gc.c (mark_method_entry): mark RCLASS_CALLABLE_M_TBL() for T_ICLASS.
* cont.c (fiber_init): rb_control_frame_t::klass is removed.
* proc.c: fix `struct METHOD' data structure because
rb_callable_method_t has all information.
* vm_core.h: remove several fields.
* rb_control_frame_t::klass.
* rb_block_t::klass.
And catch up changes.
* eval.c: catch up changes.
* gc.c: ditto.
* insns.def: ditto.
* vm.c: ditto.
* vm_args.c: ditto.
* vm_backtrace.c: ditto.
* vm_dump.c: ditto.
* vm_eval.c: ditto.
* vm_insnhelper.c: ditto.
* vm_method.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@51126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2015-07-03 07:24:50 -04:00
|
|
|
static const rb_callable_method_entry_t *
|
2016-05-04 06:10:05 -04:00
|
|
|
method_callable_method_entry(const struct METHOD *data)
|
* method.h: introduce rb_callable_method_entry_t to remove
rb_control_frame_t::klass.
[Bug #11278], [Bug #11279]
rb_method_entry_t data belong to modules/classes.
rb_method_entry_t::owner points defined module or class.
module M
def foo; end
end
In this case, owner is M.
rb_callable_method_entry_t data belong to only classes.
For modules, MRI creates corresponding T_ICLASS internally.
rb_callable_method_entry_t can also belong to T_ICLASS.
rb_callable_method_entry_t::defined_class points T_CLASS or
T_ICLASS.
rb_method_entry_t data for classes (not for modules) are also
rb_callable_method_entry_t data because it is completely same data.
In this case, rb_method_entry_t::owner == rb_method_entry_t::defined_class.
For example, there are classes C and D, and incldues M,
class C; include M; end
class D; include M; end
then, two T_ICLASS objects for C's super class and D's super class
will be created.
When C.new.foo is called, then M#foo is searcheed and
rb_callable_method_t data is used by VM to invoke M#foo.
rb_method_entry_t data is only one for M#foo.
However, rb_callable_method_entry_t data are two (and can be more).
It is proportional to the number of including (and prepending)
classes (the number of T_ICLASS which point to the module).
Now, created rb_callable_method_entry_t are collected when
the original module M was modified. We can think it is a cache.
We need to select what kind of method entry data is needed.
To operate definition, then you need to use rb_method_entry_t.
You can access them by the following functions.
* rb_method_entry(VALUE klass, ID id);
* rb_method_entry_with_refinements(VALUE klass, ID id);
* rb_method_entry_without_refinements(VALUE klass, ID id);
* rb_resolve_refined_method(VALUE refinements, const rb_method_entry_t *me);
To invoke methods, then you need to use rb_callable_method_entry_t
which you can get by the following APIs corresponding to the
above listed functions.
* rb_callable_method_entry(VALUE klass, ID id);
* rb_callable_method_entry_with_refinements(VALUE klass, ID id);
* rb_callable_method_entry_without_refinements(VALUE klass, ID id);
* rb_resolve_refined_method_callable(VALUE refinements, const rb_callable_method_entry_t *me);
VM pushes rb_callable_method_entry_t, so that rb_vm_frame_method_entry()
returns rb_callable_method_entry_t.
You can check a super class of current method by
rb_callable_method_entry_t::defined_class.
* method.h: renamed from rb_method_entry_t::klass to
rb_method_entry_t::owner.
* internal.h: add rb_classext_struct::callable_m_tbl to cache
rb_callable_method_entry_t data.
We need to consider abotu this field again because it is only
active for T_ICLASS.
* class.c (method_entry_i): ditto.
* class.c (rb_define_attr): rb_method_entry() does not takes
defiend_class_ptr.
* gc.c (mark_method_entry): mark RCLASS_CALLABLE_M_TBL() for T_ICLASS.
* cont.c (fiber_init): rb_control_frame_t::klass is removed.
* proc.c: fix `struct METHOD' data structure because
rb_callable_method_t has all information.
* vm_core.h: remove several fields.
* rb_control_frame_t::klass.
* rb_block_t::klass.
And catch up changes.
* eval.c: catch up changes.
* gc.c: ditto.
* insns.def: ditto.
* vm.c: ditto.
* vm_args.c: ditto.
* vm_backtrace.c: ditto.
* vm_dump.c: ditto.
* vm_eval.c: ditto.
* vm_insnhelper.c: ditto.
* vm_method.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@51126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2015-07-03 07:24:50 -04:00
|
|
|
{
|
2015-07-06 22:52:34 -04:00
|
|
|
if (data->me->defined_class == 0) rb_bug("method_callable_method_entry: not callable.");
|
* method.h: introduce rb_callable_method_entry_t to remove
rb_control_frame_t::klass.
[Bug #11278], [Bug #11279]
rb_method_entry_t data belong to modules/classes.
rb_method_entry_t::owner points defined module or class.
module M
def foo; end
end
In this case, owner is M.
rb_callable_method_entry_t data belong to only classes.
For modules, MRI creates corresponding T_ICLASS internally.
rb_callable_method_entry_t can also belong to T_ICLASS.
rb_callable_method_entry_t::defined_class points T_CLASS or
T_ICLASS.
rb_method_entry_t data for classes (not for modules) are also
rb_callable_method_entry_t data because it is completely same data.
In this case, rb_method_entry_t::owner == rb_method_entry_t::defined_class.
For example, there are classes C and D, and incldues M,
class C; include M; end
class D; include M; end
then, two T_ICLASS objects for C's super class and D's super class
will be created.
When C.new.foo is called, then M#foo is searcheed and
rb_callable_method_t data is used by VM to invoke M#foo.
rb_method_entry_t data is only one for M#foo.
However, rb_callable_method_entry_t data are two (and can be more).
It is proportional to the number of including (and prepending)
classes (the number of T_ICLASS which point to the module).
Now, created rb_callable_method_entry_t are collected when
the original module M was modified. We can think it is a cache.
We need to select what kind of method entry data is needed.
To operate definition, then you need to use rb_method_entry_t.
You can access them by the following functions.
* rb_method_entry(VALUE klass, ID id);
* rb_method_entry_with_refinements(VALUE klass, ID id);
* rb_method_entry_without_refinements(VALUE klass, ID id);
* rb_resolve_refined_method(VALUE refinements, const rb_method_entry_t *me);
To invoke methods, then you need to use rb_callable_method_entry_t
which you can get by the following APIs corresponding to the
above listed functions.
* rb_callable_method_entry(VALUE klass, ID id);
* rb_callable_method_entry_with_refinements(VALUE klass, ID id);
* rb_callable_method_entry_without_refinements(VALUE klass, ID id);
* rb_resolve_refined_method_callable(VALUE refinements, const rb_callable_method_entry_t *me);
VM pushes rb_callable_method_entry_t, so that rb_vm_frame_method_entry()
returns rb_callable_method_entry_t.
You can check a super class of current method by
rb_callable_method_entry_t::defined_class.
* method.h: renamed from rb_method_entry_t::klass to
rb_method_entry_t::owner.
* internal.h: add rb_classext_struct::callable_m_tbl to cache
rb_callable_method_entry_t data.
We need to consider abotu this field again because it is only
active for T_ICLASS.
* class.c (method_entry_i): ditto.
* class.c (rb_define_attr): rb_method_entry() does not takes
defiend_class_ptr.
* gc.c (mark_method_entry): mark RCLASS_CALLABLE_M_TBL() for T_ICLASS.
* cont.c (fiber_init): rb_control_frame_t::klass is removed.
* proc.c: fix `struct METHOD' data structure because
rb_callable_method_t has all information.
* vm_core.h: remove several fields.
* rb_control_frame_t::klass.
* rb_block_t::klass.
And catch up changes.
* eval.c: catch up changes.
* gc.c: ditto.
* insns.def: ditto.
* vm.c: ditto.
* vm_args.c: ditto.
* vm_backtrace.c: ditto.
* vm_dump.c: ditto.
* vm_eval.c: ditto.
* vm_insnhelper.c: ditto.
* vm_method.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@51126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2015-07-03 07:24:50 -04:00
|
|
|
return (const rb_callable_method_entry_t *)data->me;
|
|
|
|
}
|
|
|
|
|
2016-05-04 06:10:07 -04:00
|
|
|
static inline VALUE
|
|
|
|
call_method_data(rb_thread_t *th, const struct METHOD *data,
|
2016-07-28 07:02:30 -04:00
|
|
|
int argc, const VALUE *argv, VALUE passed_procval)
|
2016-05-04 06:10:07 -04:00
|
|
|
{
|
2016-07-28 07:02:30 -04:00
|
|
|
vm_passed_block_handler_set(th, proc_to_block_handler(passed_procval));
|
2016-05-04 06:10:07 -04:00
|
|
|
return rb_vm_call(th, data->recv, data->me->called_id, argc, argv,
|
|
|
|
method_callable_method_entry(data));
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
call_method_data_safe(rb_thread_t *th, const struct METHOD *data,
|
2016-07-28 07:02:30 -04:00
|
|
|
int argc, const VALUE *argv, VALUE passed_procval,
|
2016-05-04 06:10:07 -04:00
|
|
|
int safe)
|
2007-02-02 10:21:41 -05:00
|
|
|
{
|
|
|
|
VALUE result = Qnil; /* OK */
|
|
|
|
int state;
|
2016-05-04 06:10:07 -04:00
|
|
|
|
|
|
|
TH_PUSH_TAG(th);
|
|
|
|
if ((state = TH_EXEC_TAG()) == 0) {
|
2016-05-05 03:11:34 -04:00
|
|
|
/* result is used only if state == 0, no exceptions is caught. */
|
|
|
|
/* otherwise it doesn't matter even if clobbered. */
|
2016-07-28 07:02:30 -04:00
|
|
|
NO_CLOBBERED(result) = call_method_data(th, data, argc, argv, passed_procval);
|
2016-05-04 06:10:07 -04:00
|
|
|
}
|
|
|
|
TH_POP_TAG();
|
|
|
|
rb_set_safe_level_force(safe);
|
|
|
|
if (state)
|
2016-05-04 23:22:20 -04:00
|
|
|
TH_JUMP_TAG(th, state);
|
2016-05-04 06:10:07 -04:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
2016-07-28 07:02:30 -04:00
|
|
|
rb_method_call_with_block(int argc, const VALUE *argv, VALUE method, VALUE passed_procval)
|
2016-05-04 06:10:07 -04:00
|
|
|
{
|
|
|
|
const struct METHOD *data;
|
|
|
|
rb_thread_t *const th = GET_THREAD();
|
2007-02-02 10:21:41 -05:00
|
|
|
|
2009-07-08 04:13:41 -04:00
|
|
|
TypedData_Get_Struct(method, struct METHOD, &method_data_type, data);
|
2007-02-02 10:21:41 -05:00
|
|
|
if (data->recv == Qundef) {
|
|
|
|
rb_raise(rb_eTypeError, "can't call unbound method; bind first");
|
|
|
|
}
|
|
|
|
if (OBJ_TAINTED(method)) {
|
2014-11-26 03:01:56 -05:00
|
|
|
const int safe_level_to_run = RUBY_SAFE_LEVEL_MAX;
|
2016-05-04 06:10:07 -04:00
|
|
|
int safe = rb_safe_level();
|
2014-11-26 03:01:56 -05:00
|
|
|
if (safe < safe_level_to_run) {
|
2012-12-03 05:22:15 -05:00
|
|
|
rb_set_safe_level_force(safe_level_to_run);
|
2016-07-28 07:02:30 -04:00
|
|
|
return call_method_data_safe(th, data, argc, argv, passed_procval, safe);
|
2007-02-02 10:21:41 -05:00
|
|
|
}
|
|
|
|
}
|
2016-07-28 07:02:30 -04:00
|
|
|
return call_method_data(th, data, argc, argv, passed_procval);
|
2007-02-02 10:21:41 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**********************************************************************
|
|
|
|
*
|
|
|
|
* Document-class: UnboundMethod
|
|
|
|
*
|
|
|
|
* Ruby supports two forms of objectified methods. Class
|
|
|
|
* <code>Method</code> is used to represent methods that are associated
|
|
|
|
* with a particular object: these method objects are bound to that
|
|
|
|
* object. Bound method objects for an object can be created using
|
|
|
|
* <code>Object#method</code>.
|
2009-02-22 09:23:33 -05:00
|
|
|
*
|
2007-02-02 10:21:41 -05:00
|
|
|
* Ruby also supports unbound methods; methods objects that are not
|
|
|
|
* associated with a particular object. These can be created either by
|
|
|
|
* calling <code>Module#instance_method</code> or by calling
|
|
|
|
* <code>unbind</code> on a bound method object. The result of both of
|
|
|
|
* these is an <code>UnboundMethod</code> object.
|
2009-02-22 09:23:33 -05:00
|
|
|
*
|
2007-02-02 10:21:41 -05:00
|
|
|
* Unbound methods can only be called after they are bound to an
|
2015-08-03 20:35:26 -04:00
|
|
|
* object. That object must be a kind_of? the method's original
|
2007-02-02 10:21:41 -05:00
|
|
|
* class.
|
2009-02-22 09:23:33 -05:00
|
|
|
*
|
2007-02-02 10:21:41 -05:00
|
|
|
* class Square
|
|
|
|
* def area
|
|
|
|
* @side * @side
|
|
|
|
* end
|
|
|
|
* def initialize(side)
|
|
|
|
* @side = side
|
|
|
|
* end
|
|
|
|
* end
|
2009-02-22 09:23:33 -05:00
|
|
|
*
|
2007-02-02 10:21:41 -05:00
|
|
|
* area_un = Square.instance_method(:area)
|
2009-02-22 09:23:33 -05:00
|
|
|
*
|
2007-02-02 10:21:41 -05:00
|
|
|
* s = Square.new(12)
|
|
|
|
* area = area_un.bind(s)
|
|
|
|
* area.call #=> 144
|
2009-02-22 09:23:33 -05:00
|
|
|
*
|
2007-02-02 10:21:41 -05:00
|
|
|
* Unbound methods are a reference to the method at the time it was
|
|
|
|
* objectified: subsequent changes to the underlying class will not
|
|
|
|
* affect the unbound method.
|
2009-02-22 09:23:33 -05:00
|
|
|
*
|
2007-02-02 10:21:41 -05:00
|
|
|
* class Test
|
|
|
|
* def test
|
|
|
|
* :original
|
|
|
|
* end
|
|
|
|
* end
|
|
|
|
* um = Test.instance_method(:test)
|
|
|
|
* class Test
|
|
|
|
* def test
|
|
|
|
* :modified
|
|
|
|
* end
|
|
|
|
* end
|
|
|
|
* t = Test.new
|
|
|
|
* t.test #=> :modified
|
|
|
|
* um.bind(t).call #=> :original
|
2009-02-22 09:23:33 -05:00
|
|
|
*
|
2007-02-02 10:21:41 -05:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* umeth.bind(obj) -> method
|
2009-02-22 09:23:33 -05:00
|
|
|
*
|
2007-02-02 10:21:41 -05:00
|
|
|
* Bind <i>umeth</i> to <i>obj</i>. If <code>Klass</code> was the class
|
|
|
|
* from which <i>umeth</i> was obtained,
|
|
|
|
* <code>obj.kind_of?(Klass)</code> must be true.
|
2009-02-22 09:23:33 -05:00
|
|
|
*
|
2007-02-02 10:21:41 -05:00
|
|
|
* class A
|
|
|
|
* def test
|
|
|
|
* puts "In test, class = #{self.class}"
|
|
|
|
* end
|
|
|
|
* end
|
|
|
|
* class B < A
|
|
|
|
* end
|
|
|
|
* class C < B
|
|
|
|
* end
|
2009-02-22 09:23:33 -05:00
|
|
|
*
|
|
|
|
*
|
2007-02-02 10:21:41 -05:00
|
|
|
* um = B.instance_method(:test)
|
|
|
|
* bm = um.bind(C.new)
|
|
|
|
* bm.call
|
|
|
|
* bm = um.bind(B.new)
|
|
|
|
* bm.call
|
|
|
|
* bm = um.bind(A.new)
|
|
|
|
* bm.call
|
2009-02-22 09:23:33 -05:00
|
|
|
*
|
2007-02-02 10:21:41 -05:00
|
|
|
* <em>produces:</em>
|
2009-02-22 09:23:33 -05:00
|
|
|
*
|
2007-02-02 10:21:41 -05:00
|
|
|
* In test, class = C
|
|
|
|
* In test, class = B
|
|
|
|
* prog.rb:16:in `bind': bind argument must be an instance of B (TypeError)
|
|
|
|
* from prog.rb:16
|
|
|
|
*/
|
|
|
|
|
|
|
|
static VALUE
|
2007-02-05 13:31:08 -05:00
|
|
|
umethod_bind(VALUE method, VALUE recv)
|
2007-02-02 10:21:41 -05:00
|
|
|
{
|
|
|
|
struct METHOD *data, *bound;
|
* method.h: introduce rb_callable_method_entry_t to remove
rb_control_frame_t::klass.
[Bug #11278], [Bug #11279]
rb_method_entry_t data belong to modules/classes.
rb_method_entry_t::owner points defined module or class.
module M
def foo; end
end
In this case, owner is M.
rb_callable_method_entry_t data belong to only classes.
For modules, MRI creates corresponding T_ICLASS internally.
rb_callable_method_entry_t can also belong to T_ICLASS.
rb_callable_method_entry_t::defined_class points T_CLASS or
T_ICLASS.
rb_method_entry_t data for classes (not for modules) are also
rb_callable_method_entry_t data because it is completely same data.
In this case, rb_method_entry_t::owner == rb_method_entry_t::defined_class.
For example, there are classes C and D, and incldues M,
class C; include M; end
class D; include M; end
then, two T_ICLASS objects for C's super class and D's super class
will be created.
When C.new.foo is called, then M#foo is searcheed and
rb_callable_method_t data is used by VM to invoke M#foo.
rb_method_entry_t data is only one for M#foo.
However, rb_callable_method_entry_t data are two (and can be more).
It is proportional to the number of including (and prepending)
classes (the number of T_ICLASS which point to the module).
Now, created rb_callable_method_entry_t are collected when
the original module M was modified. We can think it is a cache.
We need to select what kind of method entry data is needed.
To operate definition, then you need to use rb_method_entry_t.
You can access them by the following functions.
* rb_method_entry(VALUE klass, ID id);
* rb_method_entry_with_refinements(VALUE klass, ID id);
* rb_method_entry_without_refinements(VALUE klass, ID id);
* rb_resolve_refined_method(VALUE refinements, const rb_method_entry_t *me);
To invoke methods, then you need to use rb_callable_method_entry_t
which you can get by the following APIs corresponding to the
above listed functions.
* rb_callable_method_entry(VALUE klass, ID id);
* rb_callable_method_entry_with_refinements(VALUE klass, ID id);
* rb_callable_method_entry_without_refinements(VALUE klass, ID id);
* rb_resolve_refined_method_callable(VALUE refinements, const rb_callable_method_entry_t *me);
VM pushes rb_callable_method_entry_t, so that rb_vm_frame_method_entry()
returns rb_callable_method_entry_t.
You can check a super class of current method by
rb_callable_method_entry_t::defined_class.
* method.h: renamed from rb_method_entry_t::klass to
rb_method_entry_t::owner.
* internal.h: add rb_classext_struct::callable_m_tbl to cache
rb_callable_method_entry_t data.
We need to consider abotu this field again because it is only
active for T_ICLASS.
* class.c (method_entry_i): ditto.
* class.c (rb_define_attr): rb_method_entry() does not takes
defiend_class_ptr.
* gc.c (mark_method_entry): mark RCLASS_CALLABLE_M_TBL() for T_ICLASS.
* cont.c (fiber_init): rb_control_frame_t::klass is removed.
* proc.c: fix `struct METHOD' data structure because
rb_callable_method_t has all information.
* vm_core.h: remove several fields.
* rb_control_frame_t::klass.
* rb_block_t::klass.
And catch up changes.
* eval.c: catch up changes.
* gc.c: ditto.
* insns.def: ditto.
* vm.c: ditto.
* vm_args.c: ditto.
* vm_backtrace.c: ditto.
* vm_dump.c: ditto.
* vm_eval.c: ditto.
* vm_insnhelper.c: ditto.
* vm_method.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@51126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2015-07-03 07:24:50 -04:00
|
|
|
VALUE methclass, klass;
|
2007-02-02 10:21:41 -05:00
|
|
|
|
2009-07-08 04:13:41 -04:00
|
|
|
TypedData_Get_Struct(method, struct METHOD, &method_data_type, data);
|
2009-09-22 16:04:59 -04:00
|
|
|
|
* method.h: introduce rb_callable_method_entry_t to remove
rb_control_frame_t::klass.
[Bug #11278], [Bug #11279]
rb_method_entry_t data belong to modules/classes.
rb_method_entry_t::owner points defined module or class.
module M
def foo; end
end
In this case, owner is M.
rb_callable_method_entry_t data belong to only classes.
For modules, MRI creates corresponding T_ICLASS internally.
rb_callable_method_entry_t can also belong to T_ICLASS.
rb_callable_method_entry_t::defined_class points T_CLASS or
T_ICLASS.
rb_method_entry_t data for classes (not for modules) are also
rb_callable_method_entry_t data because it is completely same data.
In this case, rb_method_entry_t::owner == rb_method_entry_t::defined_class.
For example, there are classes C and D, and incldues M,
class C; include M; end
class D; include M; end
then, two T_ICLASS objects for C's super class and D's super class
will be created.
When C.new.foo is called, then M#foo is searcheed and
rb_callable_method_t data is used by VM to invoke M#foo.
rb_method_entry_t data is only one for M#foo.
However, rb_callable_method_entry_t data are two (and can be more).
It is proportional to the number of including (and prepending)
classes (the number of T_ICLASS which point to the module).
Now, created rb_callable_method_entry_t are collected when
the original module M was modified. We can think it is a cache.
We need to select what kind of method entry data is needed.
To operate definition, then you need to use rb_method_entry_t.
You can access them by the following functions.
* rb_method_entry(VALUE klass, ID id);
* rb_method_entry_with_refinements(VALUE klass, ID id);
* rb_method_entry_without_refinements(VALUE klass, ID id);
* rb_resolve_refined_method(VALUE refinements, const rb_method_entry_t *me);
To invoke methods, then you need to use rb_callable_method_entry_t
which you can get by the following APIs corresponding to the
above listed functions.
* rb_callable_method_entry(VALUE klass, ID id);
* rb_callable_method_entry_with_refinements(VALUE klass, ID id);
* rb_callable_method_entry_without_refinements(VALUE klass, ID id);
* rb_resolve_refined_method_callable(VALUE refinements, const rb_callable_method_entry_t *me);
VM pushes rb_callable_method_entry_t, so that rb_vm_frame_method_entry()
returns rb_callable_method_entry_t.
You can check a super class of current method by
rb_callable_method_entry_t::defined_class.
* method.h: renamed from rb_method_entry_t::klass to
rb_method_entry_t::owner.
* internal.h: add rb_classext_struct::callable_m_tbl to cache
rb_callable_method_entry_t data.
We need to consider abotu this field again because it is only
active for T_ICLASS.
* class.c (method_entry_i): ditto.
* class.c (rb_define_attr): rb_method_entry() does not takes
defiend_class_ptr.
* gc.c (mark_method_entry): mark RCLASS_CALLABLE_M_TBL() for T_ICLASS.
* cont.c (fiber_init): rb_control_frame_t::klass is removed.
* proc.c: fix `struct METHOD' data structure because
rb_callable_method_t has all information.
* vm_core.h: remove several fields.
* rb_control_frame_t::klass.
* rb_block_t::klass.
And catch up changes.
* eval.c: catch up changes.
* gc.c: ditto.
* insns.def: ditto.
* vm.c: ditto.
* vm_args.c: ditto.
* vm_backtrace.c: ditto.
* vm_dump.c: ditto.
* vm_eval.c: ditto.
* vm_insnhelper.c: ditto.
* vm_method.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@51126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2015-07-03 07:24:50 -04:00
|
|
|
methclass = data->me->owner;
|
|
|
|
|
2013-08-31 00:30:28 -04:00
|
|
|
if (!RB_TYPE_P(methclass, T_MODULE) &&
|
|
|
|
methclass != CLASS_OF(recv) && !rb_obj_is_kind_of(recv, methclass)) {
|
|
|
|
if (FL_TEST(methclass, FL_SINGLETON)) {
|
2007-02-02 10:21:41 -05:00
|
|
|
rb_raise(rb_eTypeError,
|
|
|
|
"singleton method called for a different object");
|
|
|
|
}
|
2009-09-22 16:04:59 -04:00
|
|
|
else {
|
2013-08-31 00:30:30 -04:00
|
|
|
rb_raise(rb_eTypeError, "bind argument must be an instance of % "PRIsVALUE,
|
|
|
|
rb_class_name(methclass));
|
2007-02-02 10:21:41 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
* method.h: introduce rb_callable_method_entry_t to remove
rb_control_frame_t::klass.
[Bug #11278], [Bug #11279]
rb_method_entry_t data belong to modules/classes.
rb_method_entry_t::owner points defined module or class.
module M
def foo; end
end
In this case, owner is M.
rb_callable_method_entry_t data belong to only classes.
For modules, MRI creates corresponding T_ICLASS internally.
rb_callable_method_entry_t can also belong to T_ICLASS.
rb_callable_method_entry_t::defined_class points T_CLASS or
T_ICLASS.
rb_method_entry_t data for classes (not for modules) are also
rb_callable_method_entry_t data because it is completely same data.
In this case, rb_method_entry_t::owner == rb_method_entry_t::defined_class.
For example, there are classes C and D, and incldues M,
class C; include M; end
class D; include M; end
then, two T_ICLASS objects for C's super class and D's super class
will be created.
When C.new.foo is called, then M#foo is searcheed and
rb_callable_method_t data is used by VM to invoke M#foo.
rb_method_entry_t data is only one for M#foo.
However, rb_callable_method_entry_t data are two (and can be more).
It is proportional to the number of including (and prepending)
classes (the number of T_ICLASS which point to the module).
Now, created rb_callable_method_entry_t are collected when
the original module M was modified. We can think it is a cache.
We need to select what kind of method entry data is needed.
To operate definition, then you need to use rb_method_entry_t.
You can access them by the following functions.
* rb_method_entry(VALUE klass, ID id);
* rb_method_entry_with_refinements(VALUE klass, ID id);
* rb_method_entry_without_refinements(VALUE klass, ID id);
* rb_resolve_refined_method(VALUE refinements, const rb_method_entry_t *me);
To invoke methods, then you need to use rb_callable_method_entry_t
which you can get by the following APIs corresponding to the
above listed functions.
* rb_callable_method_entry(VALUE klass, ID id);
* rb_callable_method_entry_with_refinements(VALUE klass, ID id);
* rb_callable_method_entry_without_refinements(VALUE klass, ID id);
* rb_resolve_refined_method_callable(VALUE refinements, const rb_callable_method_entry_t *me);
VM pushes rb_callable_method_entry_t, so that rb_vm_frame_method_entry()
returns rb_callable_method_entry_t.
You can check a super class of current method by
rb_callable_method_entry_t::defined_class.
* method.h: renamed from rb_method_entry_t::klass to
rb_method_entry_t::owner.
* internal.h: add rb_classext_struct::callable_m_tbl to cache
rb_callable_method_entry_t data.
We need to consider abotu this field again because it is only
active for T_ICLASS.
* class.c (method_entry_i): ditto.
* class.c (rb_define_attr): rb_method_entry() does not takes
defiend_class_ptr.
* gc.c (mark_method_entry): mark RCLASS_CALLABLE_M_TBL() for T_ICLASS.
* cont.c (fiber_init): rb_control_frame_t::klass is removed.
* proc.c: fix `struct METHOD' data structure because
rb_callable_method_t has all information.
* vm_core.h: remove several fields.
* rb_control_frame_t::klass.
* rb_block_t::klass.
And catch up changes.
* eval.c: catch up changes.
* gc.c: ditto.
* insns.def: ditto.
* vm.c: ditto.
* vm_args.c: ditto.
* vm_backtrace.c: ditto.
* vm_dump.c: ditto.
* vm_eval.c: ditto.
* vm_insnhelper.c: ditto.
* vm_method.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@51126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2015-07-03 07:24:50 -04:00
|
|
|
klass = CLASS_OF(recv);
|
|
|
|
|
2009-07-08 04:13:41 -04:00
|
|
|
method = TypedData_Make_Struct(rb_cMethod, struct METHOD, &method_data_type, bound);
|
* method.h: introduce rb_callable_method_entry_t to remove
rb_control_frame_t::klass.
[Bug #11278], [Bug #11279]
rb_method_entry_t data belong to modules/classes.
rb_method_entry_t::owner points defined module or class.
module M
def foo; end
end
In this case, owner is M.
rb_callable_method_entry_t data belong to only classes.
For modules, MRI creates corresponding T_ICLASS internally.
rb_callable_method_entry_t can also belong to T_ICLASS.
rb_callable_method_entry_t::defined_class points T_CLASS or
T_ICLASS.
rb_method_entry_t data for classes (not for modules) are also
rb_callable_method_entry_t data because it is completely same data.
In this case, rb_method_entry_t::owner == rb_method_entry_t::defined_class.
For example, there are classes C and D, and incldues M,
class C; include M; end
class D; include M; end
then, two T_ICLASS objects for C's super class and D's super class
will be created.
When C.new.foo is called, then M#foo is searcheed and
rb_callable_method_t data is used by VM to invoke M#foo.
rb_method_entry_t data is only one for M#foo.
However, rb_callable_method_entry_t data are two (and can be more).
It is proportional to the number of including (and prepending)
classes (the number of T_ICLASS which point to the module).
Now, created rb_callable_method_entry_t are collected when
the original module M was modified. We can think it is a cache.
We need to select what kind of method entry data is needed.
To operate definition, then you need to use rb_method_entry_t.
You can access them by the following functions.
* rb_method_entry(VALUE klass, ID id);
* rb_method_entry_with_refinements(VALUE klass, ID id);
* rb_method_entry_without_refinements(VALUE klass, ID id);
* rb_resolve_refined_method(VALUE refinements, const rb_method_entry_t *me);
To invoke methods, then you need to use rb_callable_method_entry_t
which you can get by the following APIs corresponding to the
above listed functions.
* rb_callable_method_entry(VALUE klass, ID id);
* rb_callable_method_entry_with_refinements(VALUE klass, ID id);
* rb_callable_method_entry_without_refinements(VALUE klass, ID id);
* rb_resolve_refined_method_callable(VALUE refinements, const rb_callable_method_entry_t *me);
VM pushes rb_callable_method_entry_t, so that rb_vm_frame_method_entry()
returns rb_callable_method_entry_t.
You can check a super class of current method by
rb_callable_method_entry_t::defined_class.
* method.h: renamed from rb_method_entry_t::klass to
rb_method_entry_t::owner.
* internal.h: add rb_classext_struct::callable_m_tbl to cache
rb_callable_method_entry_t data.
We need to consider abotu this field again because it is only
active for T_ICLASS.
* class.c (method_entry_i): ditto.
* class.c (rb_define_attr): rb_method_entry() does not takes
defiend_class_ptr.
* gc.c (mark_method_entry): mark RCLASS_CALLABLE_M_TBL() for T_ICLASS.
* cont.c (fiber_init): rb_control_frame_t::klass is removed.
* proc.c: fix `struct METHOD' data structure because
rb_callable_method_t has all information.
* vm_core.h: remove several fields.
* rb_control_frame_t::klass.
* rb_block_t::klass.
And catch up changes.
* eval.c: catch up changes.
* gc.c: ditto.
* insns.def: ditto.
* vm.c: ditto.
* vm_args.c: ditto.
* vm_backtrace.c: ditto.
* vm_dump.c: ditto.
* vm_eval.c: ditto.
* vm_insnhelper.c: ditto.
* vm_method.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@51126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2015-07-03 07:24:50 -04:00
|
|
|
RB_OBJ_WRITE(method, &bound->recv, recv);
|
|
|
|
RB_OBJ_WRITE(method, &bound->klass, data->klass);
|
2015-06-02 00:20:30 -04:00
|
|
|
RB_OBJ_WRITE(method, &bound->me, rb_method_entry_clone(data->me));
|
* method.h: introduce rb_callable_method_entry_t to remove
rb_control_frame_t::klass.
[Bug #11278], [Bug #11279]
rb_method_entry_t data belong to modules/classes.
rb_method_entry_t::owner points defined module or class.
module M
def foo; end
end
In this case, owner is M.
rb_callable_method_entry_t data belong to only classes.
For modules, MRI creates corresponding T_ICLASS internally.
rb_callable_method_entry_t can also belong to T_ICLASS.
rb_callable_method_entry_t::defined_class points T_CLASS or
T_ICLASS.
rb_method_entry_t data for classes (not for modules) are also
rb_callable_method_entry_t data because it is completely same data.
In this case, rb_method_entry_t::owner == rb_method_entry_t::defined_class.
For example, there are classes C and D, and incldues M,
class C; include M; end
class D; include M; end
then, two T_ICLASS objects for C's super class and D's super class
will be created.
When C.new.foo is called, then M#foo is searcheed and
rb_callable_method_t data is used by VM to invoke M#foo.
rb_method_entry_t data is only one for M#foo.
However, rb_callable_method_entry_t data are two (and can be more).
It is proportional to the number of including (and prepending)
classes (the number of T_ICLASS which point to the module).
Now, created rb_callable_method_entry_t are collected when
the original module M was modified. We can think it is a cache.
We need to select what kind of method entry data is needed.
To operate definition, then you need to use rb_method_entry_t.
You can access them by the following functions.
* rb_method_entry(VALUE klass, ID id);
* rb_method_entry_with_refinements(VALUE klass, ID id);
* rb_method_entry_without_refinements(VALUE klass, ID id);
* rb_resolve_refined_method(VALUE refinements, const rb_method_entry_t *me);
To invoke methods, then you need to use rb_callable_method_entry_t
which you can get by the following APIs corresponding to the
above listed functions.
* rb_callable_method_entry(VALUE klass, ID id);
* rb_callable_method_entry_with_refinements(VALUE klass, ID id);
* rb_callable_method_entry_without_refinements(VALUE klass, ID id);
* rb_resolve_refined_method_callable(VALUE refinements, const rb_callable_method_entry_t *me);
VM pushes rb_callable_method_entry_t, so that rb_vm_frame_method_entry()
returns rb_callable_method_entry_t.
You can check a super class of current method by
rb_callable_method_entry_t::defined_class.
* method.h: renamed from rb_method_entry_t::klass to
rb_method_entry_t::owner.
* internal.h: add rb_classext_struct::callable_m_tbl to cache
rb_callable_method_entry_t data.
We need to consider abotu this field again because it is only
active for T_ICLASS.
* class.c (method_entry_i): ditto.
* class.c (rb_define_attr): rb_method_entry() does not takes
defiend_class_ptr.
* gc.c (mark_method_entry): mark RCLASS_CALLABLE_M_TBL() for T_ICLASS.
* cont.c (fiber_init): rb_control_frame_t::klass is removed.
* proc.c: fix `struct METHOD' data structure because
rb_callable_method_t has all information.
* vm_core.h: remove several fields.
* rb_control_frame_t::klass.
* rb_block_t::klass.
And catch up changes.
* eval.c: catch up changes.
* gc.c: ditto.
* insns.def: ditto.
* vm.c: ditto.
* vm_args.c: ditto.
* vm_backtrace.c: ditto.
* vm_dump.c: ditto.
* vm_eval.c: ditto.
* vm_insnhelper.c: ditto.
* vm_method.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@51126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2015-07-03 07:24:50 -04:00
|
|
|
|
|
|
|
if (RB_TYPE_P(bound->me->owner, T_MODULE)) {
|
|
|
|
VALUE ic = rb_class_search_ancestor(klass, bound->me->owner);
|
2014-04-14 04:20:10 -04:00
|
|
|
if (ic) {
|
* method.h: introduce rb_callable_method_entry_t to remove
rb_control_frame_t::klass.
[Bug #11278], [Bug #11279]
rb_method_entry_t data belong to modules/classes.
rb_method_entry_t::owner points defined module or class.
module M
def foo; end
end
In this case, owner is M.
rb_callable_method_entry_t data belong to only classes.
For modules, MRI creates corresponding T_ICLASS internally.
rb_callable_method_entry_t can also belong to T_ICLASS.
rb_callable_method_entry_t::defined_class points T_CLASS or
T_ICLASS.
rb_method_entry_t data for classes (not for modules) are also
rb_callable_method_entry_t data because it is completely same data.
In this case, rb_method_entry_t::owner == rb_method_entry_t::defined_class.
For example, there are classes C and D, and incldues M,
class C; include M; end
class D; include M; end
then, two T_ICLASS objects for C's super class and D's super class
will be created.
When C.new.foo is called, then M#foo is searcheed and
rb_callable_method_t data is used by VM to invoke M#foo.
rb_method_entry_t data is only one for M#foo.
However, rb_callable_method_entry_t data are two (and can be more).
It is proportional to the number of including (and prepending)
classes (the number of T_ICLASS which point to the module).
Now, created rb_callable_method_entry_t are collected when
the original module M was modified. We can think it is a cache.
We need to select what kind of method entry data is needed.
To operate definition, then you need to use rb_method_entry_t.
You can access them by the following functions.
* rb_method_entry(VALUE klass, ID id);
* rb_method_entry_with_refinements(VALUE klass, ID id);
* rb_method_entry_without_refinements(VALUE klass, ID id);
* rb_resolve_refined_method(VALUE refinements, const rb_method_entry_t *me);
To invoke methods, then you need to use rb_callable_method_entry_t
which you can get by the following APIs corresponding to the
above listed functions.
* rb_callable_method_entry(VALUE klass, ID id);
* rb_callable_method_entry_with_refinements(VALUE klass, ID id);
* rb_callable_method_entry_without_refinements(VALUE klass, ID id);
* rb_resolve_refined_method_callable(VALUE refinements, const rb_callable_method_entry_t *me);
VM pushes rb_callable_method_entry_t, so that rb_vm_frame_method_entry()
returns rb_callable_method_entry_t.
You can check a super class of current method by
rb_callable_method_entry_t::defined_class.
* method.h: renamed from rb_method_entry_t::klass to
rb_method_entry_t::owner.
* internal.h: add rb_classext_struct::callable_m_tbl to cache
rb_callable_method_entry_t data.
We need to consider abotu this field again because it is only
active for T_ICLASS.
* class.c (method_entry_i): ditto.
* class.c (rb_define_attr): rb_method_entry() does not takes
defiend_class_ptr.
* gc.c (mark_method_entry): mark RCLASS_CALLABLE_M_TBL() for T_ICLASS.
* cont.c (fiber_init): rb_control_frame_t::klass is removed.
* proc.c: fix `struct METHOD' data structure because
rb_callable_method_t has all information.
* vm_core.h: remove several fields.
* rb_control_frame_t::klass.
* rb_block_t::klass.
And catch up changes.
* eval.c: catch up changes.
* gc.c: ditto.
* insns.def: ditto.
* vm.c: ditto.
* vm_args.c: ditto.
* vm_backtrace.c: ditto.
* vm_dump.c: ditto.
* vm_eval.c: ditto.
* vm_insnhelper.c: ditto.
* vm_method.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@51126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2015-07-03 07:24:50 -04:00
|
|
|
klass = ic;
|
2014-04-14 04:20:10 -04:00
|
|
|
}
|
|
|
|
else {
|
* method.h: introduce rb_callable_method_entry_t to remove
rb_control_frame_t::klass.
[Bug #11278], [Bug #11279]
rb_method_entry_t data belong to modules/classes.
rb_method_entry_t::owner points defined module or class.
module M
def foo; end
end
In this case, owner is M.
rb_callable_method_entry_t data belong to only classes.
For modules, MRI creates corresponding T_ICLASS internally.
rb_callable_method_entry_t can also belong to T_ICLASS.
rb_callable_method_entry_t::defined_class points T_CLASS or
T_ICLASS.
rb_method_entry_t data for classes (not for modules) are also
rb_callable_method_entry_t data because it is completely same data.
In this case, rb_method_entry_t::owner == rb_method_entry_t::defined_class.
For example, there are classes C and D, and incldues M,
class C; include M; end
class D; include M; end
then, two T_ICLASS objects for C's super class and D's super class
will be created.
When C.new.foo is called, then M#foo is searcheed and
rb_callable_method_t data is used by VM to invoke M#foo.
rb_method_entry_t data is only one for M#foo.
However, rb_callable_method_entry_t data are two (and can be more).
It is proportional to the number of including (and prepending)
classes (the number of T_ICLASS which point to the module).
Now, created rb_callable_method_entry_t are collected when
the original module M was modified. We can think it is a cache.
We need to select what kind of method entry data is needed.
To operate definition, then you need to use rb_method_entry_t.
You can access them by the following functions.
* rb_method_entry(VALUE klass, ID id);
* rb_method_entry_with_refinements(VALUE klass, ID id);
* rb_method_entry_without_refinements(VALUE klass, ID id);
* rb_resolve_refined_method(VALUE refinements, const rb_method_entry_t *me);
To invoke methods, then you need to use rb_callable_method_entry_t
which you can get by the following APIs corresponding to the
above listed functions.
* rb_callable_method_entry(VALUE klass, ID id);
* rb_callable_method_entry_with_refinements(VALUE klass, ID id);
* rb_callable_method_entry_without_refinements(VALUE klass, ID id);
* rb_resolve_refined_method_callable(VALUE refinements, const rb_callable_method_entry_t *me);
VM pushes rb_callable_method_entry_t, so that rb_vm_frame_method_entry()
returns rb_callable_method_entry_t.
You can check a super class of current method by
rb_callable_method_entry_t::defined_class.
* method.h: renamed from rb_method_entry_t::klass to
rb_method_entry_t::owner.
* internal.h: add rb_classext_struct::callable_m_tbl to cache
rb_callable_method_entry_t data.
We need to consider abotu this field again because it is only
active for T_ICLASS.
* class.c (method_entry_i): ditto.
* class.c (rb_define_attr): rb_method_entry() does not takes
defiend_class_ptr.
* gc.c (mark_method_entry): mark RCLASS_CALLABLE_M_TBL() for T_ICLASS.
* cont.c (fiber_init): rb_control_frame_t::klass is removed.
* proc.c: fix `struct METHOD' data structure because
rb_callable_method_t has all information.
* vm_core.h: remove several fields.
* rb_control_frame_t::klass.
* rb_block_t::klass.
And catch up changes.
* eval.c: catch up changes.
* gc.c: ditto.
* insns.def: ditto.
* vm.c: ditto.
* vm_args.c: ditto.
* vm_backtrace.c: ditto.
* vm_dump.c: ditto.
* vm_eval.c: ditto.
* vm_insnhelper.c: ditto.
* vm_method.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@51126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2015-07-03 07:24:50 -04:00
|
|
|
klass = rb_include_class_new(methclass, klass);
|
2014-04-14 04:20:10 -04:00
|
|
|
}
|
2016-11-05 09:15:26 -04:00
|
|
|
RB_OBJ_WRITE(method, &bound->me, rb_method_entry_complement_defined_class(bound->me, bound->me->called_id, klass));
|
2014-04-11 02:05:28 -04:00
|
|
|
}
|
2007-02-02 10:21:41 -05:00
|
|
|
|
|
|
|
return method;
|
|
|
|
}
|
|
|
|
|
2013-02-01 17:46:07 -05:00
|
|
|
/*
|
|
|
|
* Returns the number of required parameters and stores the maximum
|
|
|
|
* number of parameters in max, or UNLIMITED_ARGUMENTS
|
|
|
|
* if there is no maximum.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
rb_method_entry_min_max_arity(const rb_method_entry_t *me, int *max)
|
2007-02-02 10:21:41 -05:00
|
|
|
{
|
2009-08-27 22:45:41 -04:00
|
|
|
const rb_method_definition_t *def = me->def;
|
2015-05-21 05:01:44 -04:00
|
|
|
|
2013-02-01 17:46:07 -05:00
|
|
|
if (!def) return *max = 0;
|
2009-08-27 22:45:41 -04:00
|
|
|
switch (def->type) {
|
2009-07-15 10:59:41 -04:00
|
|
|
case VM_METHOD_TYPE_CFUNC:
|
2013-02-01 17:46:07 -05:00
|
|
|
if (def->body.cfunc.argc < 0) {
|
|
|
|
*max = UNLIMITED_ARGUMENTS;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return *max = check_argc(def->body.cfunc.argc);
|
2009-07-15 10:59:41 -04:00
|
|
|
case VM_METHOD_TYPE_ZSUPER:
|
2013-02-01 17:46:07 -05:00
|
|
|
*max = UNLIMITED_ARGUMENTS;
|
|
|
|
return 0;
|
2009-07-15 10:59:41 -04:00
|
|
|
case VM_METHOD_TYPE_ATTRSET:
|
2013-02-01 17:46:07 -05:00
|
|
|
return *max = 1;
|
2009-07-15 10:59:41 -04:00
|
|
|
case VM_METHOD_TYPE_IVAR:
|
2013-02-01 17:46:07 -05:00
|
|
|
return *max = 0;
|
2015-05-30 14:45:28 -04:00
|
|
|
case VM_METHOD_TYPE_ALIAS:
|
|
|
|
return rb_method_entry_min_max_arity(def->body.alias.original_me, max);
|
2009-07-15 10:59:41 -04:00
|
|
|
case VM_METHOD_TYPE_BMETHOD:
|
2013-02-01 17:46:07 -05:00
|
|
|
return rb_proc_min_max_arity(def->body.proc, max);
|
2009-07-15 10:59:41 -04:00
|
|
|
case VM_METHOD_TYPE_ISEQ: {
|
2015-12-08 08:58:50 -05:00
|
|
|
const rb_iseq_t *iseq = rb_iseq_check(def->body.iseq.iseqptr);
|
2013-02-01 17:46:07 -05:00
|
|
|
return rb_iseq_min_max_arity(iseq, max);
|
2009-07-15 10:59:41 -04:00
|
|
|
}
|
2009-07-28 14:14:11 -04:00
|
|
|
case VM_METHOD_TYPE_UNDEF:
|
|
|
|
case VM_METHOD_TYPE_NOTIMPLEMENTED:
|
2013-02-01 17:46:07 -05:00
|
|
|
return *max = 0;
|
2009-09-27 23:09:16 -04:00
|
|
|
case VM_METHOD_TYPE_MISSING:
|
2013-02-01 17:46:07 -05:00
|
|
|
*max = UNLIMITED_ARGUMENTS;
|
|
|
|
return 0;
|
2009-07-28 06:41:11 -04:00
|
|
|
case VM_METHOD_TYPE_OPTIMIZED: {
|
2009-08-27 22:45:41 -04:00
|
|
|
switch (def->body.optimize_type) {
|
2009-08-27 20:45:27 -04:00
|
|
|
case OPTIMIZED_METHOD_TYPE_SEND:
|
2013-02-01 17:46:07 -05:00
|
|
|
*max = UNLIMITED_ARGUMENTS;
|
|
|
|
return 0;
|
2015-10-06 10:44:06 -04:00
|
|
|
case OPTIMIZED_METHOD_TYPE_CALL:
|
|
|
|
*max = UNLIMITED_ARGUMENTS;
|
|
|
|
return 0;
|
2009-08-27 20:45:27 -04:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2013-05-25 05:55:17 -04:00
|
|
|
break;
|
2012-12-12 02:06:14 -05:00
|
|
|
}
|
* revised r37993 to avoid SEGV/ILL in tests. In r37993, a method
entry with VM_METHOD_TYPE_REFINED holds only the original method
definition, so ci->me is set to a method entry allocated in the
stack, and it causes SEGV/ILL. In this commit, a method entry
with VM_METHOD_TYPE_REFINED holds the whole original method entry.
Furthermore, rb_thread_mark() is changed to mark cfp->klass to
avoid GC for iclasses created by copy_refinement_iclass().
* vm_method.c (rb_method_entry_make): add a method entry with
VM_METHOD_TYPE_REFINED to the class refined by the refinement if
the target module is a refinement. When a method entry with
VM_METHOD_TYPE_UNDEF is invoked by vm_call_method(), a method with
the same name is searched in refinements. If such a method is
found, the method is invoked. Otherwise, the original method in
the refined class (rb_method_definition_t::body.orig_me) is
invoked. This change is made to simplify the normal method lookup
and to improve the performance of normal method calls.
* vm_method.c (EXPR1, search_method, rb_method_entry),
vm_eval.c (rb_call0, rb_search_method_entry): do not use
refinements for method lookup.
* vm_insnhelper.c (vm_call_method): search methods in refinements if
ci->me is VM_METHOD_TYPE_REFINED. If the method is called by
super (i.e., ci->call == vm_call_super_method), skip the same
method entry as the current method to avoid infinite call of the
same method.
* class.c (include_modules_at): add a refined method entry for each
method defined in a module included in a refinement.
* class.c (rb_prepend_module): set an empty table to
RCLASS_M_TBL(klass) to add refined method entries, because
refinements should have priority over prepended modules.
* proc.c (mnew): use rb_method_entry_with_refinements() to get
a refined method.
* vm.c (rb_thread_mark): mark cfp->klass for iclasses created by
copy_refinement_iclass().
* vm.c (Init_VM), cont.c (fiber_init): initialize th->cfp->klass.
* test/ruby/test_refinement.rb (test_inline_method_cache): do not skip
the test because it should pass successfully.
* test/ruby/test_refinement.rb (test_redefine_refined_method): new
test for the case a refined method is redefined.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@38236 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2012-12-06 08:08:41 -05:00
|
|
|
case VM_METHOD_TYPE_REFINED:
|
2013-02-01 17:46:07 -05:00
|
|
|
*max = UNLIMITED_ARGUMENTS;
|
|
|
|
return 0;
|
2007-02-02 10:21:41 -05:00
|
|
|
}
|
2013-02-01 17:46:07 -05:00
|
|
|
rb_bug("rb_method_entry_min_max_arity: invalid method entry type (%d)", def->type);
|
2012-04-13 19:45:37 -04:00
|
|
|
UNREACHABLE;
|
2007-02-02 10:21:41 -05:00
|
|
|
}
|
|
|
|
|
2013-02-01 17:46:07 -05:00
|
|
|
int
|
|
|
|
rb_method_entry_arity(const rb_method_entry_t *me)
|
|
|
|
{
|
|
|
|
int max, min = rb_method_entry_min_max_arity(me, &max);
|
|
|
|
return min == max ? min : -min-1;
|
|
|
|
}
|
|
|
|
|
2007-02-02 10:21:41 -05:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2016-09-08 00:57:49 -04:00
|
|
|
* meth.arity -> integer
|
2009-02-22 09:23:33 -05:00
|
|
|
*
|
2007-02-02 10:21:41 -05:00
|
|
|
* Returns an indication of the number of arguments accepted by a
|
|
|
|
* method. Returns a nonnegative integer for methods that take a fixed
|
|
|
|
* number of arguments. For Ruby methods that take a variable number of
|
|
|
|
* arguments, returns -n-1, where n is the number of required
|
|
|
|
* arguments. For methods written in C, returns -1 if the call takes a
|
|
|
|
* variable number of arguments.
|
2009-02-22 09:23:33 -05:00
|
|
|
*
|
2007-02-02 10:21:41 -05:00
|
|
|
* class C
|
|
|
|
* def one; end
|
|
|
|
* def two(a); end
|
|
|
|
* def three(*a); end
|
|
|
|
* def four(a, b); end
|
|
|
|
* def five(a, b, *c); end
|
|
|
|
* def six(a, b, *c, &d); end
|
|
|
|
* end
|
|
|
|
* c = C.new
|
|
|
|
* c.method(:one).arity #=> 0
|
|
|
|
* c.method(:two).arity #=> 1
|
|
|
|
* c.method(:three).arity #=> -1
|
|
|
|
* c.method(:four).arity #=> 2
|
|
|
|
* c.method(:five).arity #=> -3
|
|
|
|
* c.method(:six).arity #=> -3
|
2009-02-22 09:23:33 -05:00
|
|
|
*
|
2007-02-02 10:21:41 -05:00
|
|
|
* "cat".method(:size).arity #=> 0
|
|
|
|
* "cat".method(:replace).arity #=> 1
|
|
|
|
* "cat".method(:squeeze).arity #=> -1
|
|
|
|
* "cat".method(:count).arity #=> -1
|
|
|
|
*/
|
|
|
|
|
|
|
|
static VALUE
|
2007-02-05 13:31:08 -05:00
|
|
|
method_arity_m(VALUE method)
|
2007-02-02 10:21:41 -05:00
|
|
|
{
|
|
|
|
int n = method_arity(method);
|
|
|
|
return INT2FIX(n);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2007-02-05 13:31:08 -05:00
|
|
|
method_arity(VALUE method)
|
2007-02-02 10:21:41 -05:00
|
|
|
{
|
|
|
|
struct METHOD *data;
|
|
|
|
|
2009-07-08 04:13:41 -04:00
|
|
|
TypedData_Get_Struct(method, struct METHOD, &method_data_type, data);
|
2011-07-25 10:29:28 -04:00
|
|
|
return rb_method_entry_arity(data->me);
|
2007-02-02 10:21:41 -05:00
|
|
|
}
|
|
|
|
|
2015-05-21 05:01:44 -04:00
|
|
|
static const rb_method_entry_t *
|
2013-01-29 02:49:22 -05:00
|
|
|
original_method_entry(VALUE mod, ID id)
|
|
|
|
{
|
2015-05-21 05:01:44 -04:00
|
|
|
const rb_method_entry_t *me;
|
|
|
|
|
* method.h: introduce rb_callable_method_entry_t to remove
rb_control_frame_t::klass.
[Bug #11278], [Bug #11279]
rb_method_entry_t data belong to modules/classes.
rb_method_entry_t::owner points defined module or class.
module M
def foo; end
end
In this case, owner is M.
rb_callable_method_entry_t data belong to only classes.
For modules, MRI creates corresponding T_ICLASS internally.
rb_callable_method_entry_t can also belong to T_ICLASS.
rb_callable_method_entry_t::defined_class points T_CLASS or
T_ICLASS.
rb_method_entry_t data for classes (not for modules) are also
rb_callable_method_entry_t data because it is completely same data.
In this case, rb_method_entry_t::owner == rb_method_entry_t::defined_class.
For example, there are classes C and D, and incldues M,
class C; include M; end
class D; include M; end
then, two T_ICLASS objects for C's super class and D's super class
will be created.
When C.new.foo is called, then M#foo is searcheed and
rb_callable_method_t data is used by VM to invoke M#foo.
rb_method_entry_t data is only one for M#foo.
However, rb_callable_method_entry_t data are two (and can be more).
It is proportional to the number of including (and prepending)
classes (the number of T_ICLASS which point to the module).
Now, created rb_callable_method_entry_t are collected when
the original module M was modified. We can think it is a cache.
We need to select what kind of method entry data is needed.
To operate definition, then you need to use rb_method_entry_t.
You can access them by the following functions.
* rb_method_entry(VALUE klass, ID id);
* rb_method_entry_with_refinements(VALUE klass, ID id);
* rb_method_entry_without_refinements(VALUE klass, ID id);
* rb_resolve_refined_method(VALUE refinements, const rb_method_entry_t *me);
To invoke methods, then you need to use rb_callable_method_entry_t
which you can get by the following APIs corresponding to the
above listed functions.
* rb_callable_method_entry(VALUE klass, ID id);
* rb_callable_method_entry_with_refinements(VALUE klass, ID id);
* rb_callable_method_entry_without_refinements(VALUE klass, ID id);
* rb_resolve_refined_method_callable(VALUE refinements, const rb_callable_method_entry_t *me);
VM pushes rb_callable_method_entry_t, so that rb_vm_frame_method_entry()
returns rb_callable_method_entry_t.
You can check a super class of current method by
rb_callable_method_entry_t::defined_class.
* method.h: renamed from rb_method_entry_t::klass to
rb_method_entry_t::owner.
* internal.h: add rb_classext_struct::callable_m_tbl to cache
rb_callable_method_entry_t data.
We need to consider abotu this field again because it is only
active for T_ICLASS.
* class.c (method_entry_i): ditto.
* class.c (rb_define_attr): rb_method_entry() does not takes
defiend_class_ptr.
* gc.c (mark_method_entry): mark RCLASS_CALLABLE_M_TBL() for T_ICLASS.
* cont.c (fiber_init): rb_control_frame_t::klass is removed.
* proc.c: fix `struct METHOD' data structure because
rb_callable_method_t has all information.
* vm_core.h: remove several fields.
* rb_control_frame_t::klass.
* rb_block_t::klass.
And catch up changes.
* eval.c: catch up changes.
* gc.c: ditto.
* insns.def: ditto.
* vm.c: ditto.
* vm_args.c: ditto.
* vm_backtrace.c: ditto.
* vm_dump.c: ditto.
* vm_eval.c: ditto.
* vm_insnhelper.c: ditto.
* vm_method.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@51126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2015-07-03 07:24:50 -04:00
|
|
|
while ((me = rb_method_entry(mod, id)) != 0) {
|
2015-05-21 05:01:44 -04:00
|
|
|
const rb_method_definition_t *def = me->def;
|
2013-01-29 02:49:22 -05:00
|
|
|
if (def->type != VM_METHOD_TYPE_ZSUPER) break;
|
* method.h: introduce rb_callable_method_entry_t to remove
rb_control_frame_t::klass.
[Bug #11278], [Bug #11279]
rb_method_entry_t data belong to modules/classes.
rb_method_entry_t::owner points defined module or class.
module M
def foo; end
end
In this case, owner is M.
rb_callable_method_entry_t data belong to only classes.
For modules, MRI creates corresponding T_ICLASS internally.
rb_callable_method_entry_t can also belong to T_ICLASS.
rb_callable_method_entry_t::defined_class points T_CLASS or
T_ICLASS.
rb_method_entry_t data for classes (not for modules) are also
rb_callable_method_entry_t data because it is completely same data.
In this case, rb_method_entry_t::owner == rb_method_entry_t::defined_class.
For example, there are classes C and D, and incldues M,
class C; include M; end
class D; include M; end
then, two T_ICLASS objects for C's super class and D's super class
will be created.
When C.new.foo is called, then M#foo is searcheed and
rb_callable_method_t data is used by VM to invoke M#foo.
rb_method_entry_t data is only one for M#foo.
However, rb_callable_method_entry_t data are two (and can be more).
It is proportional to the number of including (and prepending)
classes (the number of T_ICLASS which point to the module).
Now, created rb_callable_method_entry_t are collected when
the original module M was modified. We can think it is a cache.
We need to select what kind of method entry data is needed.
To operate definition, then you need to use rb_method_entry_t.
You can access them by the following functions.
* rb_method_entry(VALUE klass, ID id);
* rb_method_entry_with_refinements(VALUE klass, ID id);
* rb_method_entry_without_refinements(VALUE klass, ID id);
* rb_resolve_refined_method(VALUE refinements, const rb_method_entry_t *me);
To invoke methods, then you need to use rb_callable_method_entry_t
which you can get by the following APIs corresponding to the
above listed functions.
* rb_callable_method_entry(VALUE klass, ID id);
* rb_callable_method_entry_with_refinements(VALUE klass, ID id);
* rb_callable_method_entry_without_refinements(VALUE klass, ID id);
* rb_resolve_refined_method_callable(VALUE refinements, const rb_callable_method_entry_t *me);
VM pushes rb_callable_method_entry_t, so that rb_vm_frame_method_entry()
returns rb_callable_method_entry_t.
You can check a super class of current method by
rb_callable_method_entry_t::defined_class.
* method.h: renamed from rb_method_entry_t::klass to
rb_method_entry_t::owner.
* internal.h: add rb_classext_struct::callable_m_tbl to cache
rb_callable_method_entry_t data.
We need to consider abotu this field again because it is only
active for T_ICLASS.
* class.c (method_entry_i): ditto.
* class.c (rb_define_attr): rb_method_entry() does not takes
defiend_class_ptr.
* gc.c (mark_method_entry): mark RCLASS_CALLABLE_M_TBL() for T_ICLASS.
* cont.c (fiber_init): rb_control_frame_t::klass is removed.
* proc.c: fix `struct METHOD' data structure because
rb_callable_method_t has all information.
* vm_core.h: remove several fields.
* rb_control_frame_t::klass.
* rb_block_t::klass.
And catch up changes.
* eval.c: catch up changes.
* gc.c: ditto.
* insns.def: ditto.
* vm.c: ditto.
* vm_args.c: ditto.
* vm_backtrace.c: ditto.
* vm_dump.c: ditto.
* vm_eval.c: ditto.
* vm_insnhelper.c: ditto.
* vm_method.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@51126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2015-07-03 07:24:50 -04:00
|
|
|
mod = RCLASS_SUPER(me->owner);
|
2013-01-29 02:49:22 -05:00
|
|
|
id = def->original_id;
|
|
|
|
}
|
|
|
|
return me;
|
|
|
|
}
|
|
|
|
|
2013-02-01 17:46:07 -05:00
|
|
|
static int
|
|
|
|
method_min_max_arity(VALUE method, int *max)
|
|
|
|
{
|
2015-05-21 05:01:44 -04:00
|
|
|
const struct METHOD *data;
|
2013-02-01 17:46:07 -05:00
|
|
|
|
|
|
|
TypedData_Get_Struct(method, struct METHOD, &method_data_type, data);
|
|
|
|
return rb_method_entry_min_max_arity(data->me, max);
|
|
|
|
}
|
|
|
|
|
2007-02-02 10:21:41 -05:00
|
|
|
int
|
2007-02-05 13:31:08 -05:00
|
|
|
rb_mod_method_arity(VALUE mod, ID id)
|
2007-02-02 10:21:41 -05:00
|
|
|
{
|
2015-05-21 05:01:44 -04:00
|
|
|
const rb_method_entry_t *me = original_method_entry(mod, id);
|
2013-01-29 02:49:22 -05:00
|
|
|
if (!me) return 0; /* should raise? */
|
2009-07-15 10:59:41 -04:00
|
|
|
return rb_method_entry_arity(me);
|
2007-02-02 10:21:41 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2007-02-05 13:31:08 -05:00
|
|
|
rb_obj_method_arity(VALUE obj, ID id)
|
2007-02-02 10:21:41 -05:00
|
|
|
{
|
|
|
|
return rb_mod_method_arity(CLASS_OF(obj), id);
|
|
|
|
}
|
|
|
|
|
2015-05-21 05:01:44 -04:00
|
|
|
static inline const rb_method_definition_t *
|
2015-05-21 04:03:58 -04:00
|
|
|
method_def(VALUE method)
|
2008-09-26 09:47:01 -04:00
|
|
|
{
|
2015-05-21 05:01:44 -04:00
|
|
|
const struct METHOD *data;
|
2008-09-26 09:47:01 -04:00
|
|
|
|
2009-07-08 04:13:41 -04:00
|
|
|
TypedData_Get_Struct(method, struct METHOD, &method_data_type, data);
|
2011-07-25 10:29:28 -04:00
|
|
|
return data->me->def;
|
2010-03-22 07:44:01 -04:00
|
|
|
}
|
2009-07-15 10:59:41 -04:00
|
|
|
|
2015-05-21 05:01:44 -04:00
|
|
|
static const rb_iseq_t *
|
|
|
|
method_def_iseq(const rb_method_definition_t *def)
|
2010-03-22 07:44:01 -04:00
|
|
|
{
|
2009-08-27 22:45:41 -04:00
|
|
|
switch (def->type) {
|
2009-07-15 10:59:41 -04:00
|
|
|
case VM_METHOD_TYPE_ISEQ:
|
2015-12-08 08:58:50 -05:00
|
|
|
return rb_iseq_check(def->body.iseq.iseqptr);
|
2015-05-30 14:45:28 -04:00
|
|
|
case VM_METHOD_TYPE_BMETHOD:
|
2015-12-19 10:14:50 -05:00
|
|
|
return rb_proc_get_iseq(def->body.proc, 0);
|
2015-05-30 14:45:28 -04:00
|
|
|
case VM_METHOD_TYPE_ALIAS:
|
|
|
|
return method_def_iseq(def->body.alias.original_me->def);
|
|
|
|
case VM_METHOD_TYPE_CFUNC:
|
|
|
|
case VM_METHOD_TYPE_ATTRSET:
|
|
|
|
case VM_METHOD_TYPE_IVAR:
|
|
|
|
case VM_METHOD_TYPE_ZSUPER:
|
|
|
|
case VM_METHOD_TYPE_UNDEF:
|
|
|
|
case VM_METHOD_TYPE_NOTIMPLEMENTED:
|
|
|
|
case VM_METHOD_TYPE_OPTIMIZED:
|
|
|
|
case VM_METHOD_TYPE_MISSING:
|
|
|
|
case VM_METHOD_TYPE_REFINED:
|
|
|
|
break;
|
* fix namespace issue on singleton class expressions. [Bug #10943]
* vm_core.h, method.h: remove rb_iseq_t::cref_stack. CREF is stored
to rb_method_definition_t::body.iseq_body.cref.
* vm_insnhelper.c: modify SVAR usage.
When calling ISEQ type method, push CREF information onto method
frame, SVAR located place. Before this fix, SVAR is simply nil.
After this patch, CREF (or NULL == Qfalse for not iseq methods)
is stored at the method invocation.
When SVAR is requierd, then put NODE_IF onto SVAR location,
and NDOE_IF::nd_reserved points CREF itself.
* vm.c (vm_cref_new, vm_cref_dump, vm_cref_new_toplevel): added.
* vm_insnhelper.c (vm_push_frame): accept CREF.
* method.h, vm_method.c (rb_add_method_iseq): added. This function
accepts iseq and CREF.
* class.c (clone_method): use rb_add_method_iseq().
* gc.c (mark_method_entry): mark method_entry::body.iseq_body.cref.
* iseq.c: remove CREF related codes.
* insns.def (getinlinecache/setinlinecache): CREF should be cache key
because a different CREF has a different namespace.
* node.c (rb_gc_mark_node): mark NODE_IF::nd_reserved for SVAR.
* proc.c: catch up changes.
* struct.c: ditto.
* insns.def: ditto.
* vm_args.c (raise_argument_error): ditto.
* vm_eval.c: ditto.
* test/ruby/test_class.rb: add a test.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@49874 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2015-03-06 07:24:58 -05:00
|
|
|
}
|
2015-05-30 14:45:28 -04:00
|
|
|
return NULL;
|
* fix namespace issue on singleton class expressions. [Bug #10943]
* vm_core.h, method.h: remove rb_iseq_t::cref_stack. CREF is stored
to rb_method_definition_t::body.iseq_body.cref.
* vm_insnhelper.c: modify SVAR usage.
When calling ISEQ type method, push CREF information onto method
frame, SVAR located place. Before this fix, SVAR is simply nil.
After this patch, CREF (or NULL == Qfalse for not iseq methods)
is stored at the method invocation.
When SVAR is requierd, then put NODE_IF onto SVAR location,
and NDOE_IF::nd_reserved points CREF itself.
* vm.c (vm_cref_new, vm_cref_dump, vm_cref_new_toplevel): added.
* vm_insnhelper.c (vm_push_frame): accept CREF.
* method.h, vm_method.c (rb_add_method_iseq): added. This function
accepts iseq and CREF.
* class.c (clone_method): use rb_add_method_iseq().
* gc.c (mark_method_entry): mark method_entry::body.iseq_body.cref.
* iseq.c: remove CREF related codes.
* insns.def (getinlinecache/setinlinecache): CREF should be cache key
because a different CREF has a different namespace.
* node.c (rb_gc_mark_node): mark NODE_IF::nd_reserved for SVAR.
* proc.c: catch up changes.
* struct.c: ditto.
* insns.def: ditto.
* vm_args.c (raise_argument_error): ditto.
* vm_eval.c: ditto.
* test/ruby/test_class.rb: add a test.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@49874 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2015-03-06 07:24:58 -05:00
|
|
|
}
|
|
|
|
|
2015-05-21 05:01:44 -04:00
|
|
|
const rb_iseq_t *
|
2015-05-21 04:03:58 -04:00
|
|
|
rb_method_iseq(VALUE method)
|
|
|
|
{
|
|
|
|
return method_def_iseq(method_def(method));
|
|
|
|
}
|
|
|
|
|
2015-03-08 17:22:43 -04:00
|
|
|
static const rb_cref_t *
|
2015-05-21 04:03:58 -04:00
|
|
|
method_cref(VALUE method)
|
* fix namespace issue on singleton class expressions. [Bug #10943]
* vm_core.h, method.h: remove rb_iseq_t::cref_stack. CREF is stored
to rb_method_definition_t::body.iseq_body.cref.
* vm_insnhelper.c: modify SVAR usage.
When calling ISEQ type method, push CREF information onto method
frame, SVAR located place. Before this fix, SVAR is simply nil.
After this patch, CREF (or NULL == Qfalse for not iseq methods)
is stored at the method invocation.
When SVAR is requierd, then put NODE_IF onto SVAR location,
and NDOE_IF::nd_reserved points CREF itself.
* vm.c (vm_cref_new, vm_cref_dump, vm_cref_new_toplevel): added.
* vm_insnhelper.c (vm_push_frame): accept CREF.
* method.h, vm_method.c (rb_add_method_iseq): added. This function
accepts iseq and CREF.
* class.c (clone_method): use rb_add_method_iseq().
* gc.c (mark_method_entry): mark method_entry::body.iseq_body.cref.
* iseq.c: remove CREF related codes.
* insns.def (getinlinecache/setinlinecache): CREF should be cache key
because a different CREF has a different namespace.
* node.c (rb_gc_mark_node): mark NODE_IF::nd_reserved for SVAR.
* proc.c: catch up changes.
* struct.c: ditto.
* insns.def: ditto.
* vm_args.c (raise_argument_error): ditto.
* vm_eval.c: ditto.
* test/ruby/test_class.rb: add a test.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@49874 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2015-03-06 07:24:58 -05:00
|
|
|
{
|
2015-05-21 05:01:44 -04:00
|
|
|
const rb_method_definition_t *def = method_def(method);
|
2015-05-21 04:03:58 -04:00
|
|
|
|
2015-05-30 14:45:28 -04:00
|
|
|
again:
|
* fix namespace issue on singleton class expressions. [Bug #10943]
* vm_core.h, method.h: remove rb_iseq_t::cref_stack. CREF is stored
to rb_method_definition_t::body.iseq_body.cref.
* vm_insnhelper.c: modify SVAR usage.
When calling ISEQ type method, push CREF information onto method
frame, SVAR located place. Before this fix, SVAR is simply nil.
After this patch, CREF (or NULL == Qfalse for not iseq methods)
is stored at the method invocation.
When SVAR is requierd, then put NODE_IF onto SVAR location,
and NDOE_IF::nd_reserved points CREF itself.
* vm.c (vm_cref_new, vm_cref_dump, vm_cref_new_toplevel): added.
* vm_insnhelper.c (vm_push_frame): accept CREF.
* method.h, vm_method.c (rb_add_method_iseq): added. This function
accepts iseq and CREF.
* class.c (clone_method): use rb_add_method_iseq().
* gc.c (mark_method_entry): mark method_entry::body.iseq_body.cref.
* iseq.c: remove CREF related codes.
* insns.def (getinlinecache/setinlinecache): CREF should be cache key
because a different CREF has a different namespace.
* node.c (rb_gc_mark_node): mark NODE_IF::nd_reserved for SVAR.
* proc.c: catch up changes.
* struct.c: ditto.
* insns.def: ditto.
* vm_args.c (raise_argument_error): ditto.
* vm_eval.c: ditto.
* test/ruby/test_class.rb: add a test.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@49874 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2015-03-06 07:24:58 -05:00
|
|
|
switch (def->type) {
|
|
|
|
case VM_METHOD_TYPE_ISEQ:
|
2015-06-02 00:20:30 -04:00
|
|
|
return def->body.iseq.cref;
|
2015-05-30 14:45:28 -04:00
|
|
|
case VM_METHOD_TYPE_ALIAS:
|
|
|
|
def = def->body.alias.original_me->def;
|
|
|
|
goto again;
|
* fix namespace issue on singleton class expressions. [Bug #10943]
* vm_core.h, method.h: remove rb_iseq_t::cref_stack. CREF is stored
to rb_method_definition_t::body.iseq_body.cref.
* vm_insnhelper.c: modify SVAR usage.
When calling ISEQ type method, push CREF information onto method
frame, SVAR located place. Before this fix, SVAR is simply nil.
After this patch, CREF (or NULL == Qfalse for not iseq methods)
is stored at the method invocation.
When SVAR is requierd, then put NODE_IF onto SVAR location,
and NDOE_IF::nd_reserved points CREF itself.
* vm.c (vm_cref_new, vm_cref_dump, vm_cref_new_toplevel): added.
* vm_insnhelper.c (vm_push_frame): accept CREF.
* method.h, vm_method.c (rb_add_method_iseq): added. This function
accepts iseq and CREF.
* class.c (clone_method): use rb_add_method_iseq().
* gc.c (mark_method_entry): mark method_entry::body.iseq_body.cref.
* iseq.c: remove CREF related codes.
* insns.def (getinlinecache/setinlinecache): CREF should be cache key
because a different CREF has a different namespace.
* node.c (rb_gc_mark_node): mark NODE_IF::nd_reserved for SVAR.
* proc.c: catch up changes.
* struct.c: ditto.
* insns.def: ditto.
* vm_args.c (raise_argument_error): ditto.
* vm_eval.c: ditto.
* test/ruby/test_class.rb: add a test.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@49874 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2015-03-06 07:24:58 -05:00
|
|
|
default:
|
|
|
|
return NULL;
|
2008-09-26 09:47:01 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-05 23:35:23 -05:00
|
|
|
static VALUE
|
2015-05-21 05:01:44 -04:00
|
|
|
method_def_location(const rb_method_definition_t *def)
|
2013-02-05 23:35:23 -05:00
|
|
|
{
|
|
|
|
if (def->type == VM_METHOD_TYPE_ATTRSET || def->type == VM_METHOD_TYPE_IVAR) {
|
|
|
|
if (!def->body.attr.location)
|
|
|
|
return Qnil;
|
|
|
|
return rb_ary_dup(def->body.attr.location);
|
|
|
|
}
|
2015-05-21 04:03:58 -04:00
|
|
|
return iseq_location(method_def_iseq(def));
|
2013-02-05 23:35:23 -05:00
|
|
|
}
|
|
|
|
|
2015-08-19 19:53:12 -04:00
|
|
|
VALUE
|
|
|
|
rb_method_entry_location(const rb_method_entry_t *me)
|
2013-02-05 23:35:23 -05:00
|
|
|
{
|
2015-06-03 07:10:16 -04:00
|
|
|
if (!me) return Qnil;
|
2013-02-05 23:35:23 -05:00
|
|
|
return method_def_location(me->def);
|
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
|
|
|
rb_mod_method_location(VALUE mod, ID id)
|
|
|
|
{
|
2015-05-21 05:01:44 -04:00
|
|
|
const rb_method_entry_t *me = original_method_entry(mod, id);
|
2015-08-19 19:53:12 -04:00
|
|
|
return rb_method_entry_location(me);
|
2013-02-05 23:35:23 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
|
|
|
rb_obj_method_location(VALUE obj, ID id)
|
|
|
|
{
|
|
|
|
return rb_mod_method_location(CLASS_OF(obj), id);
|
|
|
|
}
|
|
|
|
|
2008-09-26 09:47:01 -04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2016-10-26 02:11:23 -04:00
|
|
|
* meth.source_location -> [String, Integer]
|
2008-09-26 09:47:01 -04:00
|
|
|
*
|
2011-05-27 09:55:43 -04:00
|
|
|
* Returns the Ruby source filename and line number containing this method
|
|
|
|
* or nil if this method was not defined in Ruby (i.e. native)
|
2008-09-26 09:47:01 -04:00
|
|
|
*/
|
|
|
|
|
|
|
|
VALUE
|
|
|
|
rb_method_location(VALUE method)
|
|
|
|
{
|
2015-05-21 05:01:44 -04:00
|
|
|
return method_def_location(method_def(method));
|
2008-09-26 09:47:01 -04:00
|
|
|
}
|
|
|
|
|
2008-11-27 23:19:37 -05:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-17 17:07:33 -04:00
|
|
|
* meth.parameters -> array
|
2008-11-27 23:19:37 -05:00
|
|
|
*
|
2011-05-27 09:55:43 -04:00
|
|
|
* Returns the parameter information of this method.
|
2016-02-09 13:43:45 -05:00
|
|
|
*
|
|
|
|
* def foo(bar); end
|
|
|
|
* method(:foo).parameters #=> [[:req, :bar]]
|
|
|
|
*
|
|
|
|
* def foo(bar, baz, bat, &blk); end
|
|
|
|
* method(:foo).parameters #=> [[:req, :bar], [:req, :baz], [:req, :bat], [:block, :blk]]
|
|
|
|
*
|
|
|
|
* def foo(bar, *args); end
|
|
|
|
* method(:foo).parameters #=> [[:req, :bar], [:rest, :args]]
|
|
|
|
*
|
|
|
|
* def foo(bar, baz, *args, &blk); end
|
|
|
|
* method(:foo).parameters #=> [[:req, :bar], [:req, :baz], [:rest, :args], [:block, :blk]]
|
2008-11-27 23:19:37 -05:00
|
|
|
*/
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
rb_method_parameters(VALUE method)
|
|
|
|
{
|
2015-05-21 05:01:44 -04:00
|
|
|
const rb_iseq_t *iseq = rb_method_iseq(method);
|
2008-11-27 23:19:37 -05:00
|
|
|
if (!iseq) {
|
|
|
|
return unnamed_parameters(method_arity(method));
|
|
|
|
}
|
2008-12-04 23:05:48 -05:00
|
|
|
return rb_iseq_parameters(iseq, 0);
|
2008-11-27 23:19:37 -05:00
|
|
|
}
|
|
|
|
|
2007-02-02 10:21:41 -05:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-17 17:07:33 -04:00
|
|
|
* meth.to_s -> string
|
|
|
|
* meth.inspect -> string
|
2007-02-02 10:21:41 -05:00
|
|
|
*
|
2011-05-27 09:55:43 -04:00
|
|
|
* Returns the name of the underlying method.
|
2007-02-02 10:21:41 -05:00
|
|
|
*
|
|
|
|
* "cat".method(:count).inspect #=> "#<Method: String#count>"
|
|
|
|
*/
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
method_inspect(VALUE method)
|
|
|
|
{
|
|
|
|
struct METHOD *data;
|
|
|
|
VALUE str;
|
|
|
|
const char *s;
|
2008-05-31 05:28:20 -04:00
|
|
|
const char *sharp = "#";
|
2013-12-12 20:36:31 -05:00
|
|
|
VALUE mklass;
|
* method.h: introduce rb_callable_method_entry_t to remove
rb_control_frame_t::klass.
[Bug #11278], [Bug #11279]
rb_method_entry_t data belong to modules/classes.
rb_method_entry_t::owner points defined module or class.
module M
def foo; end
end
In this case, owner is M.
rb_callable_method_entry_t data belong to only classes.
For modules, MRI creates corresponding T_ICLASS internally.
rb_callable_method_entry_t can also belong to T_ICLASS.
rb_callable_method_entry_t::defined_class points T_CLASS or
T_ICLASS.
rb_method_entry_t data for classes (not for modules) are also
rb_callable_method_entry_t data because it is completely same data.
In this case, rb_method_entry_t::owner == rb_method_entry_t::defined_class.
For example, there are classes C and D, and incldues M,
class C; include M; end
class D; include M; end
then, two T_ICLASS objects for C's super class and D's super class
will be created.
When C.new.foo is called, then M#foo is searcheed and
rb_callable_method_t data is used by VM to invoke M#foo.
rb_method_entry_t data is only one for M#foo.
However, rb_callable_method_entry_t data are two (and can be more).
It is proportional to the number of including (and prepending)
classes (the number of T_ICLASS which point to the module).
Now, created rb_callable_method_entry_t are collected when
the original module M was modified. We can think it is a cache.
We need to select what kind of method entry data is needed.
To operate definition, then you need to use rb_method_entry_t.
You can access them by the following functions.
* rb_method_entry(VALUE klass, ID id);
* rb_method_entry_with_refinements(VALUE klass, ID id);
* rb_method_entry_without_refinements(VALUE klass, ID id);
* rb_resolve_refined_method(VALUE refinements, const rb_method_entry_t *me);
To invoke methods, then you need to use rb_callable_method_entry_t
which you can get by the following APIs corresponding to the
above listed functions.
* rb_callable_method_entry(VALUE klass, ID id);
* rb_callable_method_entry_with_refinements(VALUE klass, ID id);
* rb_callable_method_entry_without_refinements(VALUE klass, ID id);
* rb_resolve_refined_method_callable(VALUE refinements, const rb_callable_method_entry_t *me);
VM pushes rb_callable_method_entry_t, so that rb_vm_frame_method_entry()
returns rb_callable_method_entry_t.
You can check a super class of current method by
rb_callable_method_entry_t::defined_class.
* method.h: renamed from rb_method_entry_t::klass to
rb_method_entry_t::owner.
* internal.h: add rb_classext_struct::callable_m_tbl to cache
rb_callable_method_entry_t data.
We need to consider abotu this field again because it is only
active for T_ICLASS.
* class.c (method_entry_i): ditto.
* class.c (rb_define_attr): rb_method_entry() does not takes
defiend_class_ptr.
* gc.c (mark_method_entry): mark RCLASS_CALLABLE_M_TBL() for T_ICLASS.
* cont.c (fiber_init): rb_control_frame_t::klass is removed.
* proc.c: fix `struct METHOD' data structure because
rb_callable_method_t has all information.
* vm_core.h: remove several fields.
* rb_control_frame_t::klass.
* rb_block_t::klass.
And catch up changes.
* eval.c: catch up changes.
* gc.c: ditto.
* insns.def: ditto.
* vm.c: ditto.
* vm_args.c: ditto.
* vm_backtrace.c: ditto.
* vm_dump.c: ditto.
* vm_eval.c: ditto.
* vm_insnhelper.c: ditto.
* vm_method.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@51126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2015-07-03 07:24:50 -04:00
|
|
|
VALUE defined_class;
|
2007-02-02 10:21:41 -05:00
|
|
|
|
2009-07-08 04:13:41 -04:00
|
|
|
TypedData_Get_Struct(method, struct METHOD, &method_data_type, data);
|
2007-02-02 10:21:41 -05:00
|
|
|
str = rb_str_buf_new2("#<");
|
|
|
|
s = rb_obj_classname(method);
|
|
|
|
rb_str_buf_cat2(str, s);
|
|
|
|
rb_str_buf_cat2(str, ": ");
|
|
|
|
|
* method.h: introduce rb_callable_method_entry_t to remove
rb_control_frame_t::klass.
[Bug #11278], [Bug #11279]
rb_method_entry_t data belong to modules/classes.
rb_method_entry_t::owner points defined module or class.
module M
def foo; end
end
In this case, owner is M.
rb_callable_method_entry_t data belong to only classes.
For modules, MRI creates corresponding T_ICLASS internally.
rb_callable_method_entry_t can also belong to T_ICLASS.
rb_callable_method_entry_t::defined_class points T_CLASS or
T_ICLASS.
rb_method_entry_t data for classes (not for modules) are also
rb_callable_method_entry_t data because it is completely same data.
In this case, rb_method_entry_t::owner == rb_method_entry_t::defined_class.
For example, there are classes C and D, and incldues M,
class C; include M; end
class D; include M; end
then, two T_ICLASS objects for C's super class and D's super class
will be created.
When C.new.foo is called, then M#foo is searcheed and
rb_callable_method_t data is used by VM to invoke M#foo.
rb_method_entry_t data is only one for M#foo.
However, rb_callable_method_entry_t data are two (and can be more).
It is proportional to the number of including (and prepending)
classes (the number of T_ICLASS which point to the module).
Now, created rb_callable_method_entry_t are collected when
the original module M was modified. We can think it is a cache.
We need to select what kind of method entry data is needed.
To operate definition, then you need to use rb_method_entry_t.
You can access them by the following functions.
* rb_method_entry(VALUE klass, ID id);
* rb_method_entry_with_refinements(VALUE klass, ID id);
* rb_method_entry_without_refinements(VALUE klass, ID id);
* rb_resolve_refined_method(VALUE refinements, const rb_method_entry_t *me);
To invoke methods, then you need to use rb_callable_method_entry_t
which you can get by the following APIs corresponding to the
above listed functions.
* rb_callable_method_entry(VALUE klass, ID id);
* rb_callable_method_entry_with_refinements(VALUE klass, ID id);
* rb_callable_method_entry_without_refinements(VALUE klass, ID id);
* rb_resolve_refined_method_callable(VALUE refinements, const rb_callable_method_entry_t *me);
VM pushes rb_callable_method_entry_t, so that rb_vm_frame_method_entry()
returns rb_callable_method_entry_t.
You can check a super class of current method by
rb_callable_method_entry_t::defined_class.
* method.h: renamed from rb_method_entry_t::klass to
rb_method_entry_t::owner.
* internal.h: add rb_classext_struct::callable_m_tbl to cache
rb_callable_method_entry_t data.
We need to consider abotu this field again because it is only
active for T_ICLASS.
* class.c (method_entry_i): ditto.
* class.c (rb_define_attr): rb_method_entry() does not takes
defiend_class_ptr.
* gc.c (mark_method_entry): mark RCLASS_CALLABLE_M_TBL() for T_ICLASS.
* cont.c (fiber_init): rb_control_frame_t::klass is removed.
* proc.c: fix `struct METHOD' data structure because
rb_callable_method_t has all information.
* vm_core.h: remove several fields.
* rb_control_frame_t::klass.
* rb_block_t::klass.
And catch up changes.
* eval.c: catch up changes.
* gc.c: ditto.
* insns.def: ditto.
* vm.c: ditto.
* vm_args.c: ditto.
* vm_backtrace.c: ditto.
* vm_dump.c: ditto.
* vm_eval.c: ditto.
* vm_insnhelper.c: ditto.
* vm_method.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@51126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2015-07-03 07:24:50 -04:00
|
|
|
mklass = data->klass;
|
|
|
|
|
2015-07-06 22:52:34 -04:00
|
|
|
if (data->me->def->type == VM_METHOD_TYPE_ALIAS) {
|
* method.h: introduce rb_callable_method_entry_t to remove
rb_control_frame_t::klass.
[Bug #11278], [Bug #11279]
rb_method_entry_t data belong to modules/classes.
rb_method_entry_t::owner points defined module or class.
module M
def foo; end
end
In this case, owner is M.
rb_callable_method_entry_t data belong to only classes.
For modules, MRI creates corresponding T_ICLASS internally.
rb_callable_method_entry_t can also belong to T_ICLASS.
rb_callable_method_entry_t::defined_class points T_CLASS or
T_ICLASS.
rb_method_entry_t data for classes (not for modules) are also
rb_callable_method_entry_t data because it is completely same data.
In this case, rb_method_entry_t::owner == rb_method_entry_t::defined_class.
For example, there are classes C and D, and incldues M,
class C; include M; end
class D; include M; end
then, two T_ICLASS objects for C's super class and D's super class
will be created.
When C.new.foo is called, then M#foo is searcheed and
rb_callable_method_t data is used by VM to invoke M#foo.
rb_method_entry_t data is only one for M#foo.
However, rb_callable_method_entry_t data are two (and can be more).
It is proportional to the number of including (and prepending)
classes (the number of T_ICLASS which point to the module).
Now, created rb_callable_method_entry_t are collected when
the original module M was modified. We can think it is a cache.
We need to select what kind of method entry data is needed.
To operate definition, then you need to use rb_method_entry_t.
You can access them by the following functions.
* rb_method_entry(VALUE klass, ID id);
* rb_method_entry_with_refinements(VALUE klass, ID id);
* rb_method_entry_without_refinements(VALUE klass, ID id);
* rb_resolve_refined_method(VALUE refinements, const rb_method_entry_t *me);
To invoke methods, then you need to use rb_callable_method_entry_t
which you can get by the following APIs corresponding to the
above listed functions.
* rb_callable_method_entry(VALUE klass, ID id);
* rb_callable_method_entry_with_refinements(VALUE klass, ID id);
* rb_callable_method_entry_without_refinements(VALUE klass, ID id);
* rb_resolve_refined_method_callable(VALUE refinements, const rb_callable_method_entry_t *me);
VM pushes rb_callable_method_entry_t, so that rb_vm_frame_method_entry()
returns rb_callable_method_entry_t.
You can check a super class of current method by
rb_callable_method_entry_t::defined_class.
* method.h: renamed from rb_method_entry_t::klass to
rb_method_entry_t::owner.
* internal.h: add rb_classext_struct::callable_m_tbl to cache
rb_callable_method_entry_t data.
We need to consider abotu this field again because it is only
active for T_ICLASS.
* class.c (method_entry_i): ditto.
* class.c (rb_define_attr): rb_method_entry() does not takes
defiend_class_ptr.
* gc.c (mark_method_entry): mark RCLASS_CALLABLE_M_TBL() for T_ICLASS.
* cont.c (fiber_init): rb_control_frame_t::klass is removed.
* proc.c: fix `struct METHOD' data structure because
rb_callable_method_t has all information.
* vm_core.h: remove several fields.
* rb_control_frame_t::klass.
* rb_block_t::klass.
And catch up changes.
* eval.c: catch up changes.
* gc.c: ditto.
* insns.def: ditto.
* vm.c: ditto.
* vm_args.c: ditto.
* vm_backtrace.c: ditto.
* vm_dump.c: ditto.
* vm_eval.c: ditto.
* vm_insnhelper.c: ditto.
* vm_method.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@51126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2015-07-03 07:24:50 -04:00
|
|
|
defined_class = data->me->def->body.alias.original_me->owner;
|
|
|
|
}
|
|
|
|
else {
|
2015-08-07 04:07:58 -04:00
|
|
|
defined_class = method_entry_defined_class(data->me);
|
* method.h: introduce rb_callable_method_entry_t to remove
rb_control_frame_t::klass.
[Bug #11278], [Bug #11279]
rb_method_entry_t data belong to modules/classes.
rb_method_entry_t::owner points defined module or class.
module M
def foo; end
end
In this case, owner is M.
rb_callable_method_entry_t data belong to only classes.
For modules, MRI creates corresponding T_ICLASS internally.
rb_callable_method_entry_t can also belong to T_ICLASS.
rb_callable_method_entry_t::defined_class points T_CLASS or
T_ICLASS.
rb_method_entry_t data for classes (not for modules) are also
rb_callable_method_entry_t data because it is completely same data.
In this case, rb_method_entry_t::owner == rb_method_entry_t::defined_class.
For example, there are classes C and D, and incldues M,
class C; include M; end
class D; include M; end
then, two T_ICLASS objects for C's super class and D's super class
will be created.
When C.new.foo is called, then M#foo is searcheed and
rb_callable_method_t data is used by VM to invoke M#foo.
rb_method_entry_t data is only one for M#foo.
However, rb_callable_method_entry_t data are two (and can be more).
It is proportional to the number of including (and prepending)
classes (the number of T_ICLASS which point to the module).
Now, created rb_callable_method_entry_t are collected when
the original module M was modified. We can think it is a cache.
We need to select what kind of method entry data is needed.
To operate definition, then you need to use rb_method_entry_t.
You can access them by the following functions.
* rb_method_entry(VALUE klass, ID id);
* rb_method_entry_with_refinements(VALUE klass, ID id);
* rb_method_entry_without_refinements(VALUE klass, ID id);
* rb_resolve_refined_method(VALUE refinements, const rb_method_entry_t *me);
To invoke methods, then you need to use rb_callable_method_entry_t
which you can get by the following APIs corresponding to the
above listed functions.
* rb_callable_method_entry(VALUE klass, ID id);
* rb_callable_method_entry_with_refinements(VALUE klass, ID id);
* rb_callable_method_entry_without_refinements(VALUE klass, ID id);
* rb_resolve_refined_method_callable(VALUE refinements, const rb_callable_method_entry_t *me);
VM pushes rb_callable_method_entry_t, so that rb_vm_frame_method_entry()
returns rb_callable_method_entry_t.
You can check a super class of current method by
rb_callable_method_entry_t::defined_class.
* method.h: renamed from rb_method_entry_t::klass to
rb_method_entry_t::owner.
* internal.h: add rb_classext_struct::callable_m_tbl to cache
rb_callable_method_entry_t data.
We need to consider abotu this field again because it is only
active for T_ICLASS.
* class.c (method_entry_i): ditto.
* class.c (rb_define_attr): rb_method_entry() does not takes
defiend_class_ptr.
* gc.c (mark_method_entry): mark RCLASS_CALLABLE_M_TBL() for T_ICLASS.
* cont.c (fiber_init): rb_control_frame_t::klass is removed.
* proc.c: fix `struct METHOD' data structure because
rb_callable_method_t has all information.
* vm_core.h: remove several fields.
* rb_control_frame_t::klass.
* rb_block_t::klass.
And catch up changes.
* eval.c: catch up changes.
* gc.c: ditto.
* insns.def: ditto.
* vm.c: ditto.
* vm_args.c: ditto.
* vm_backtrace.c: ditto.
* vm_dump.c: ditto.
* vm_eval.c: ditto.
* vm_insnhelper.c: ditto.
* vm_method.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@51126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2015-07-03 07:24:50 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (RB_TYPE_P(defined_class, T_ICLASS)) {
|
|
|
|
defined_class = RBASIC_CLASS(defined_class);
|
|
|
|
}
|
|
|
|
|
2013-12-12 20:36:31 -05:00
|
|
|
if (FL_TEST(mklass, FL_SINGLETON)) {
|
|
|
|
VALUE v = rb_ivar_get(mklass, attached);
|
2007-02-02 10:21:41 -05:00
|
|
|
|
|
|
|
if (data->recv == Qundef) {
|
2013-12-12 20:36:31 -05:00
|
|
|
rb_str_buf_append(str, rb_inspect(mklass));
|
2007-02-02 10:21:41 -05:00
|
|
|
}
|
|
|
|
else if (data->recv == v) {
|
|
|
|
rb_str_buf_append(str, rb_inspect(v));
|
|
|
|
sharp = ".";
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
rb_str_buf_append(str, rb_inspect(data->recv));
|
|
|
|
rb_str_buf_cat2(str, "(");
|
|
|
|
rb_str_buf_append(str, rb_inspect(v));
|
|
|
|
rb_str_buf_cat2(str, ")");
|
|
|
|
sharp = ".";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
* method.h: introduce rb_callable_method_entry_t to remove
rb_control_frame_t::klass.
[Bug #11278], [Bug #11279]
rb_method_entry_t data belong to modules/classes.
rb_method_entry_t::owner points defined module or class.
module M
def foo; end
end
In this case, owner is M.
rb_callable_method_entry_t data belong to only classes.
For modules, MRI creates corresponding T_ICLASS internally.
rb_callable_method_entry_t can also belong to T_ICLASS.
rb_callable_method_entry_t::defined_class points T_CLASS or
T_ICLASS.
rb_method_entry_t data for classes (not for modules) are also
rb_callable_method_entry_t data because it is completely same data.
In this case, rb_method_entry_t::owner == rb_method_entry_t::defined_class.
For example, there are classes C and D, and incldues M,
class C; include M; end
class D; include M; end
then, two T_ICLASS objects for C's super class and D's super class
will be created.
When C.new.foo is called, then M#foo is searcheed and
rb_callable_method_t data is used by VM to invoke M#foo.
rb_method_entry_t data is only one for M#foo.
However, rb_callable_method_entry_t data are two (and can be more).
It is proportional to the number of including (and prepending)
classes (the number of T_ICLASS which point to the module).
Now, created rb_callable_method_entry_t are collected when
the original module M was modified. We can think it is a cache.
We need to select what kind of method entry data is needed.
To operate definition, then you need to use rb_method_entry_t.
You can access them by the following functions.
* rb_method_entry(VALUE klass, ID id);
* rb_method_entry_with_refinements(VALUE klass, ID id);
* rb_method_entry_without_refinements(VALUE klass, ID id);
* rb_resolve_refined_method(VALUE refinements, const rb_method_entry_t *me);
To invoke methods, then you need to use rb_callable_method_entry_t
which you can get by the following APIs corresponding to the
above listed functions.
* rb_callable_method_entry(VALUE klass, ID id);
* rb_callable_method_entry_with_refinements(VALUE klass, ID id);
* rb_callable_method_entry_without_refinements(VALUE klass, ID id);
* rb_resolve_refined_method_callable(VALUE refinements, const rb_callable_method_entry_t *me);
VM pushes rb_callable_method_entry_t, so that rb_vm_frame_method_entry()
returns rb_callable_method_entry_t.
You can check a super class of current method by
rb_callable_method_entry_t::defined_class.
* method.h: renamed from rb_method_entry_t::klass to
rb_method_entry_t::owner.
* internal.h: add rb_classext_struct::callable_m_tbl to cache
rb_callable_method_entry_t data.
We need to consider abotu this field again because it is only
active for T_ICLASS.
* class.c (method_entry_i): ditto.
* class.c (rb_define_attr): rb_method_entry() does not takes
defiend_class_ptr.
* gc.c (mark_method_entry): mark RCLASS_CALLABLE_M_TBL() for T_ICLASS.
* cont.c (fiber_init): rb_control_frame_t::klass is removed.
* proc.c: fix `struct METHOD' data structure because
rb_callable_method_t has all information.
* vm_core.h: remove several fields.
* rb_control_frame_t::klass.
* rb_block_t::klass.
And catch up changes.
* eval.c: catch up changes.
* gc.c: ditto.
* insns.def: ditto.
* vm.c: ditto.
* vm_args.c: ditto.
* vm_backtrace.c: ditto.
* vm_dump.c: ditto.
* vm_eval.c: ditto.
* vm_insnhelper.c: ditto.
* vm_method.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@51126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2015-07-03 07:24:50 -04:00
|
|
|
rb_str_buf_append(str, rb_class_name(mklass));
|
|
|
|
if (defined_class != mklass) {
|
2007-02-02 10:21:41 -05:00
|
|
|
rb_str_buf_cat2(str, "(");
|
* method.h: introduce rb_callable_method_entry_t to remove
rb_control_frame_t::klass.
[Bug #11278], [Bug #11279]
rb_method_entry_t data belong to modules/classes.
rb_method_entry_t::owner points defined module or class.
module M
def foo; end
end
In this case, owner is M.
rb_callable_method_entry_t data belong to only classes.
For modules, MRI creates corresponding T_ICLASS internally.
rb_callable_method_entry_t can also belong to T_ICLASS.
rb_callable_method_entry_t::defined_class points T_CLASS or
T_ICLASS.
rb_method_entry_t data for classes (not for modules) are also
rb_callable_method_entry_t data because it is completely same data.
In this case, rb_method_entry_t::owner == rb_method_entry_t::defined_class.
For example, there are classes C and D, and incldues M,
class C; include M; end
class D; include M; end
then, two T_ICLASS objects for C's super class and D's super class
will be created.
When C.new.foo is called, then M#foo is searcheed and
rb_callable_method_t data is used by VM to invoke M#foo.
rb_method_entry_t data is only one for M#foo.
However, rb_callable_method_entry_t data are two (and can be more).
It is proportional to the number of including (and prepending)
classes (the number of T_ICLASS which point to the module).
Now, created rb_callable_method_entry_t are collected when
the original module M was modified. We can think it is a cache.
We need to select what kind of method entry data is needed.
To operate definition, then you need to use rb_method_entry_t.
You can access them by the following functions.
* rb_method_entry(VALUE klass, ID id);
* rb_method_entry_with_refinements(VALUE klass, ID id);
* rb_method_entry_without_refinements(VALUE klass, ID id);
* rb_resolve_refined_method(VALUE refinements, const rb_method_entry_t *me);
To invoke methods, then you need to use rb_callable_method_entry_t
which you can get by the following APIs corresponding to the
above listed functions.
* rb_callable_method_entry(VALUE klass, ID id);
* rb_callable_method_entry_with_refinements(VALUE klass, ID id);
* rb_callable_method_entry_without_refinements(VALUE klass, ID id);
* rb_resolve_refined_method_callable(VALUE refinements, const rb_callable_method_entry_t *me);
VM pushes rb_callable_method_entry_t, so that rb_vm_frame_method_entry()
returns rb_callable_method_entry_t.
You can check a super class of current method by
rb_callable_method_entry_t::defined_class.
* method.h: renamed from rb_method_entry_t::klass to
rb_method_entry_t::owner.
* internal.h: add rb_classext_struct::callable_m_tbl to cache
rb_callable_method_entry_t data.
We need to consider abotu this field again because it is only
active for T_ICLASS.
* class.c (method_entry_i): ditto.
* class.c (rb_define_attr): rb_method_entry() does not takes
defiend_class_ptr.
* gc.c (mark_method_entry): mark RCLASS_CALLABLE_M_TBL() for T_ICLASS.
* cont.c (fiber_init): rb_control_frame_t::klass is removed.
* proc.c: fix `struct METHOD' data structure because
rb_callable_method_t has all information.
* vm_core.h: remove several fields.
* rb_control_frame_t::klass.
* rb_block_t::klass.
And catch up changes.
* eval.c: catch up changes.
* gc.c: ditto.
* insns.def: ditto.
* vm.c: ditto.
* vm_args.c: ditto.
* vm_backtrace.c: ditto.
* vm_dump.c: ditto.
* vm_eval.c: ditto.
* vm_insnhelper.c: ditto.
* vm_method.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@51126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2015-07-03 07:24:50 -04:00
|
|
|
rb_str_buf_append(str, rb_class_name(defined_class));
|
2007-02-02 10:21:41 -05:00
|
|
|
rb_str_buf_cat2(str, ")");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
rb_str_buf_cat2(str, sharp);
|
* method.h: introduce rb_callable_method_entry_t to remove
rb_control_frame_t::klass.
[Bug #11278], [Bug #11279]
rb_method_entry_t data belong to modules/classes.
rb_method_entry_t::owner points defined module or class.
module M
def foo; end
end
In this case, owner is M.
rb_callable_method_entry_t data belong to only classes.
For modules, MRI creates corresponding T_ICLASS internally.
rb_callable_method_entry_t can also belong to T_ICLASS.
rb_callable_method_entry_t::defined_class points T_CLASS or
T_ICLASS.
rb_method_entry_t data for classes (not for modules) are also
rb_callable_method_entry_t data because it is completely same data.
In this case, rb_method_entry_t::owner == rb_method_entry_t::defined_class.
For example, there are classes C and D, and incldues M,
class C; include M; end
class D; include M; end
then, two T_ICLASS objects for C's super class and D's super class
will be created.
When C.new.foo is called, then M#foo is searcheed and
rb_callable_method_t data is used by VM to invoke M#foo.
rb_method_entry_t data is only one for M#foo.
However, rb_callable_method_entry_t data are two (and can be more).
It is proportional to the number of including (and prepending)
classes (the number of T_ICLASS which point to the module).
Now, created rb_callable_method_entry_t are collected when
the original module M was modified. We can think it is a cache.
We need to select what kind of method entry data is needed.
To operate definition, then you need to use rb_method_entry_t.
You can access them by the following functions.
* rb_method_entry(VALUE klass, ID id);
* rb_method_entry_with_refinements(VALUE klass, ID id);
* rb_method_entry_without_refinements(VALUE klass, ID id);
* rb_resolve_refined_method(VALUE refinements, const rb_method_entry_t *me);
To invoke methods, then you need to use rb_callable_method_entry_t
which you can get by the following APIs corresponding to the
above listed functions.
* rb_callable_method_entry(VALUE klass, ID id);
* rb_callable_method_entry_with_refinements(VALUE klass, ID id);
* rb_callable_method_entry_without_refinements(VALUE klass, ID id);
* rb_resolve_refined_method_callable(VALUE refinements, const rb_callable_method_entry_t *me);
VM pushes rb_callable_method_entry_t, so that rb_vm_frame_method_entry()
returns rb_callable_method_entry_t.
You can check a super class of current method by
rb_callable_method_entry_t::defined_class.
* method.h: renamed from rb_method_entry_t::klass to
rb_method_entry_t::owner.
* internal.h: add rb_classext_struct::callable_m_tbl to cache
rb_callable_method_entry_t data.
We need to consider abotu this field again because it is only
active for T_ICLASS.
* class.c (method_entry_i): ditto.
* class.c (rb_define_attr): rb_method_entry() does not takes
defiend_class_ptr.
* gc.c (mark_method_entry): mark RCLASS_CALLABLE_M_TBL() for T_ICLASS.
* cont.c (fiber_init): rb_control_frame_t::klass is removed.
* proc.c: fix `struct METHOD' data structure because
rb_callable_method_t has all information.
* vm_core.h: remove several fields.
* rb_control_frame_t::klass.
* rb_block_t::klass.
And catch up changes.
* eval.c: catch up changes.
* gc.c: ditto.
* insns.def: ditto.
* vm.c: ditto.
* vm_args.c: ditto.
* vm_backtrace.c: ditto.
* vm_dump.c: ditto.
* vm_eval.c: ditto.
* vm_insnhelper.c: ditto.
* vm_method.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@51126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2015-07-03 07:24:50 -04:00
|
|
|
rb_str_append(str, rb_id2str(data->me->called_id));
|
|
|
|
if (data->me->called_id != data->me->def->original_id) {
|
2013-02-13 04:10:12 -05:00
|
|
|
rb_str_catf(str, "(%"PRIsVALUE")",
|
|
|
|
rb_id2str(data->me->def->original_id));
|
|
|
|
}
|
2011-07-25 10:29:28 -04:00
|
|
|
if (data->me->def->type == VM_METHOD_TYPE_NOTIMPLEMENTED) {
|
2009-04-16 10:17:14 -04:00
|
|
|
rb_str_buf_cat2(str, " (not-implemented)");
|
|
|
|
}
|
2007-02-02 10:21:41 -05:00
|
|
|
rb_str_buf_cat2(str, ">");
|
|
|
|
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
mproc(VALUE method)
|
|
|
|
{
|
2015-02-15 23:08:52 -05:00
|
|
|
return rb_funcallv(rb_mRubyVMFrozenCore, idProc, 0, 0);
|
2007-02-02 10:21:41 -05:00
|
|
|
}
|
|
|
|
|
2007-06-25 15:06:00 -04:00
|
|
|
static VALUE
|
|
|
|
mlambda(VALUE method)
|
|
|
|
{
|
2015-02-15 23:08:52 -05:00
|
|
|
return rb_funcallv(rb_mRubyVMFrozenCore, idLambda, 0, 0);
|
2007-06-25 15:06:00 -04:00
|
|
|
}
|
|
|
|
|
2007-02-02 10:21:41 -05:00
|
|
|
static VALUE
|
2013-06-17 08:38:52 -04:00
|
|
|
bmcall(VALUE args, VALUE method, int argc, VALUE *argv, VALUE passed_proc)
|
2007-02-02 10:21:41 -05:00
|
|
|
{
|
2016-05-05 03:27:10 -04:00
|
|
|
return rb_method_call_with_block(argc, argv, method, passed_proc);
|
2007-02-02 10:21:41 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
|
|
|
rb_proc_new(
|
|
|
|
VALUE (*func)(ANYARGS), /* VALUE yieldarg[, VALUE procarg] */
|
|
|
|
VALUE val)
|
|
|
|
{
|
2007-12-23 23:55:37 -05:00
|
|
|
VALUE procval = rb_iterate(mproc, 0, func, val);
|
2007-02-02 10:21:41 -05:00
|
|
|
return procval;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
2014-01-19 00:43:28 -05:00
|
|
|
* meth.to_proc -> proc
|
2009-02-22 09:23:33 -05:00
|
|
|
*
|
2007-02-02 10:21:41 -05:00
|
|
|
* Returns a <code>Proc</code> object corresponding to this method.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static VALUE
|
2015-05-20 22:00:31 -04:00
|
|
|
method_to_proc(VALUE method)
|
2007-02-02 10:21:41 -05:00
|
|
|
{
|
2007-08-19 00:02:21 -04:00
|
|
|
VALUE procval;
|
|
|
|
rb_proc_t *proc;
|
2014-10-27 04:17:26 -04:00
|
|
|
|
2007-02-02 10:21:41 -05:00
|
|
|
/*
|
|
|
|
* class Method
|
|
|
|
* def to_proc
|
2015-05-21 04:45:57 -04:00
|
|
|
* lambda{|*args|
|
2007-02-02 10:21:41 -05:00
|
|
|
* self.call(*args)
|
|
|
|
* }
|
|
|
|
* end
|
|
|
|
* end
|
|
|
|
*/
|
2007-12-23 23:55:37 -05:00
|
|
|
procval = rb_iterate(mlambda, 0, bmcall, method);
|
2007-08-19 00:02:21 -04:00
|
|
|
GetProcPtr(procval, proc);
|
|
|
|
proc->is_from_method = 1;
|
|
|
|
return procval;
|
2007-02-02 10:21:41 -05:00
|
|
|
}
|
|
|
|
|
2014-07-26 12:47:27 -04:00
|
|
|
/*
|
2015-11-16 02:00:56 -05:00
|
|
|
* call-seq:
|
|
|
|
* meth.super_method -> method
|
|
|
|
*
|
|
|
|
* Returns a Method of superclass which would be called when super is used
|
|
|
|
* or nil if there is no method on superclass.
|
2014-07-26 12:47:27 -04:00
|
|
|
*/
|
2015-11-16 02:00:56 -05:00
|
|
|
|
2014-07-26 12:22:41 -04:00
|
|
|
static VALUE
|
|
|
|
method_super_method(VALUE method)
|
|
|
|
{
|
2015-05-21 05:01:44 -04:00
|
|
|
const struct METHOD *data;
|
* method.h: introduce rb_callable_method_entry_t to remove
rb_control_frame_t::klass.
[Bug #11278], [Bug #11279]
rb_method_entry_t data belong to modules/classes.
rb_method_entry_t::owner points defined module or class.
module M
def foo; end
end
In this case, owner is M.
rb_callable_method_entry_t data belong to only classes.
For modules, MRI creates corresponding T_ICLASS internally.
rb_callable_method_entry_t can also belong to T_ICLASS.
rb_callable_method_entry_t::defined_class points T_CLASS or
T_ICLASS.
rb_method_entry_t data for classes (not for modules) are also
rb_callable_method_entry_t data because it is completely same data.
In this case, rb_method_entry_t::owner == rb_method_entry_t::defined_class.
For example, there are classes C and D, and incldues M,
class C; include M; end
class D; include M; end
then, two T_ICLASS objects for C's super class and D's super class
will be created.
When C.new.foo is called, then M#foo is searcheed and
rb_callable_method_t data is used by VM to invoke M#foo.
rb_method_entry_t data is only one for M#foo.
However, rb_callable_method_entry_t data are two (and can be more).
It is proportional to the number of including (and prepending)
classes (the number of T_ICLASS which point to the module).
Now, created rb_callable_method_entry_t are collected when
the original module M was modified. We can think it is a cache.
We need to select what kind of method entry data is needed.
To operate definition, then you need to use rb_method_entry_t.
You can access them by the following functions.
* rb_method_entry(VALUE klass, ID id);
* rb_method_entry_with_refinements(VALUE klass, ID id);
* rb_method_entry_without_refinements(VALUE klass, ID id);
* rb_resolve_refined_method(VALUE refinements, const rb_method_entry_t *me);
To invoke methods, then you need to use rb_callable_method_entry_t
which you can get by the following APIs corresponding to the
above listed functions.
* rb_callable_method_entry(VALUE klass, ID id);
* rb_callable_method_entry_with_refinements(VALUE klass, ID id);
* rb_callable_method_entry_without_refinements(VALUE klass, ID id);
* rb_resolve_refined_method_callable(VALUE refinements, const rb_callable_method_entry_t *me);
VM pushes rb_callable_method_entry_t, so that rb_vm_frame_method_entry()
returns rb_callable_method_entry_t.
You can check a super class of current method by
rb_callable_method_entry_t::defined_class.
* method.h: renamed from rb_method_entry_t::klass to
rb_method_entry_t::owner.
* internal.h: add rb_classext_struct::callable_m_tbl to cache
rb_callable_method_entry_t data.
We need to consider abotu this field again because it is only
active for T_ICLASS.
* class.c (method_entry_i): ditto.
* class.c (rb_define_attr): rb_method_entry() does not takes
defiend_class_ptr.
* gc.c (mark_method_entry): mark RCLASS_CALLABLE_M_TBL() for T_ICLASS.
* cont.c (fiber_init): rb_control_frame_t::klass is removed.
* proc.c: fix `struct METHOD' data structure because
rb_callable_method_t has all information.
* vm_core.h: remove several fields.
* rb_control_frame_t::klass.
* rb_block_t::klass.
And catch up changes.
* eval.c: catch up changes.
* gc.c: ditto.
* insns.def: ditto.
* vm.c: ditto.
* vm_args.c: ditto.
* vm_backtrace.c: ditto.
* vm_dump.c: ditto.
* vm_eval.c: ditto.
* vm_insnhelper.c: ditto.
* vm_method.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@51126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2015-07-03 07:24:50 -04:00
|
|
|
VALUE super_class;
|
2015-05-21 05:01:44 -04:00
|
|
|
const rb_method_entry_t *me;
|
2014-07-26 12:22:41 -04:00
|
|
|
|
|
|
|
TypedData_Get_Struct(method, struct METHOD, &method_data_type, data);
|
2015-08-07 04:07:58 -04:00
|
|
|
super_class = RCLASS_SUPER(method_entry_defined_class(data->me));
|
2014-07-26 12:22:41 -04:00
|
|
|
if (!super_class) return Qnil;
|
* method.h: introduce rb_callable_method_entry_t to remove
rb_control_frame_t::klass.
[Bug #11278], [Bug #11279]
rb_method_entry_t data belong to modules/classes.
rb_method_entry_t::owner points defined module or class.
module M
def foo; end
end
In this case, owner is M.
rb_callable_method_entry_t data belong to only classes.
For modules, MRI creates corresponding T_ICLASS internally.
rb_callable_method_entry_t can also belong to T_ICLASS.
rb_callable_method_entry_t::defined_class points T_CLASS or
T_ICLASS.
rb_method_entry_t data for classes (not for modules) are also
rb_callable_method_entry_t data because it is completely same data.
In this case, rb_method_entry_t::owner == rb_method_entry_t::defined_class.
For example, there are classes C and D, and incldues M,
class C; include M; end
class D; include M; end
then, two T_ICLASS objects for C's super class and D's super class
will be created.
When C.new.foo is called, then M#foo is searcheed and
rb_callable_method_t data is used by VM to invoke M#foo.
rb_method_entry_t data is only one for M#foo.
However, rb_callable_method_entry_t data are two (and can be more).
It is proportional to the number of including (and prepending)
classes (the number of T_ICLASS which point to the module).
Now, created rb_callable_method_entry_t are collected when
the original module M was modified. We can think it is a cache.
We need to select what kind of method entry data is needed.
To operate definition, then you need to use rb_method_entry_t.
You can access them by the following functions.
* rb_method_entry(VALUE klass, ID id);
* rb_method_entry_with_refinements(VALUE klass, ID id);
* rb_method_entry_without_refinements(VALUE klass, ID id);
* rb_resolve_refined_method(VALUE refinements, const rb_method_entry_t *me);
To invoke methods, then you need to use rb_callable_method_entry_t
which you can get by the following APIs corresponding to the
above listed functions.
* rb_callable_method_entry(VALUE klass, ID id);
* rb_callable_method_entry_with_refinements(VALUE klass, ID id);
* rb_callable_method_entry_without_refinements(VALUE klass, ID id);
* rb_resolve_refined_method_callable(VALUE refinements, const rb_callable_method_entry_t *me);
VM pushes rb_callable_method_entry_t, so that rb_vm_frame_method_entry()
returns rb_callable_method_entry_t.
You can check a super class of current method by
rb_callable_method_entry_t::defined_class.
* method.h: renamed from rb_method_entry_t::klass to
rb_method_entry_t::owner.
* internal.h: add rb_classext_struct::callable_m_tbl to cache
rb_callable_method_entry_t data.
We need to consider abotu this field again because it is only
active for T_ICLASS.
* class.c (method_entry_i): ditto.
* class.c (rb_define_attr): rb_method_entry() does not takes
defiend_class_ptr.
* gc.c (mark_method_entry): mark RCLASS_CALLABLE_M_TBL() for T_ICLASS.
* cont.c (fiber_init): rb_control_frame_t::klass is removed.
* proc.c: fix `struct METHOD' data structure because
rb_callable_method_t has all information.
* vm_core.h: remove several fields.
* rb_control_frame_t::klass.
* rb_block_t::klass.
And catch up changes.
* eval.c: catch up changes.
* gc.c: ditto.
* insns.def: ditto.
* vm.c: ditto.
* vm_args.c: ditto.
* vm_backtrace.c: ditto.
* vm_dump.c: ditto.
* vm_eval.c: ditto.
* vm_insnhelper.c: ditto.
* vm_method.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@51126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2015-07-03 07:24:50 -04:00
|
|
|
me = (rb_method_entry_t *)rb_callable_method_entry_without_refinements(super_class, data->me->called_id);
|
2014-07-26 12:22:41 -04:00
|
|
|
if (!me) return Qnil;
|
* method.h: introduce rb_callable_method_entry_t to remove
rb_control_frame_t::klass.
[Bug #11278], [Bug #11279]
rb_method_entry_t data belong to modules/classes.
rb_method_entry_t::owner points defined module or class.
module M
def foo; end
end
In this case, owner is M.
rb_callable_method_entry_t data belong to only classes.
For modules, MRI creates corresponding T_ICLASS internally.
rb_callable_method_entry_t can also belong to T_ICLASS.
rb_callable_method_entry_t::defined_class points T_CLASS or
T_ICLASS.
rb_method_entry_t data for classes (not for modules) are also
rb_callable_method_entry_t data because it is completely same data.
In this case, rb_method_entry_t::owner == rb_method_entry_t::defined_class.
For example, there are classes C and D, and incldues M,
class C; include M; end
class D; include M; end
then, two T_ICLASS objects for C's super class and D's super class
will be created.
When C.new.foo is called, then M#foo is searcheed and
rb_callable_method_t data is used by VM to invoke M#foo.
rb_method_entry_t data is only one for M#foo.
However, rb_callable_method_entry_t data are two (and can be more).
It is proportional to the number of including (and prepending)
classes (the number of T_ICLASS which point to the module).
Now, created rb_callable_method_entry_t are collected when
the original module M was modified. We can think it is a cache.
We need to select what kind of method entry data is needed.
To operate definition, then you need to use rb_method_entry_t.
You can access them by the following functions.
* rb_method_entry(VALUE klass, ID id);
* rb_method_entry_with_refinements(VALUE klass, ID id);
* rb_method_entry_without_refinements(VALUE klass, ID id);
* rb_resolve_refined_method(VALUE refinements, const rb_method_entry_t *me);
To invoke methods, then you need to use rb_callable_method_entry_t
which you can get by the following APIs corresponding to the
above listed functions.
* rb_callable_method_entry(VALUE klass, ID id);
* rb_callable_method_entry_with_refinements(VALUE klass, ID id);
* rb_callable_method_entry_without_refinements(VALUE klass, ID id);
* rb_resolve_refined_method_callable(VALUE refinements, const rb_callable_method_entry_t *me);
VM pushes rb_callable_method_entry_t, so that rb_vm_frame_method_entry()
returns rb_callable_method_entry_t.
You can check a super class of current method by
rb_callable_method_entry_t::defined_class.
* method.h: renamed from rb_method_entry_t::klass to
rb_method_entry_t::owner.
* internal.h: add rb_classext_struct::callable_m_tbl to cache
rb_callable_method_entry_t data.
We need to consider abotu this field again because it is only
active for T_ICLASS.
* class.c (method_entry_i): ditto.
* class.c (rb_define_attr): rb_method_entry() does not takes
defiend_class_ptr.
* gc.c (mark_method_entry): mark RCLASS_CALLABLE_M_TBL() for T_ICLASS.
* cont.c (fiber_init): rb_control_frame_t::klass is removed.
* proc.c: fix `struct METHOD' data structure because
rb_callable_method_t has all information.
* vm_core.h: remove several fields.
* rb_control_frame_t::klass.
* rb_block_t::klass.
And catch up changes.
* eval.c: catch up changes.
* gc.c: ditto.
* insns.def: ditto.
* vm.c: ditto.
* vm_args.c: ditto.
* vm_backtrace.c: ditto.
* vm_dump.c: ditto.
* vm_eval.c: ditto.
* vm_insnhelper.c: ditto.
* vm_method.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@51126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2015-07-03 07:24:50 -04:00
|
|
|
return mnew_internal(me, super_class, data->recv, data->me->called_id, rb_obj_class(method), FALSE, FALSE);
|
2014-07-26 12:22:41 -04:00
|
|
|
}
|
|
|
|
|
2007-02-02 10:21:41 -05:00
|
|
|
/*
|
2013-05-31 03:04:33 -04:00
|
|
|
* call-seq:
|
2010-05-17 17:07:33 -04:00
|
|
|
* local_jump_error.exit_value -> obj
|
2007-02-02 10:21:41 -05:00
|
|
|
*
|
|
|
|
* Returns the exit value associated with this +LocalJumpError+.
|
|
|
|
*/
|
|
|
|
static VALUE
|
|
|
|
localjump_xvalue(VALUE exc)
|
|
|
|
{
|
|
|
|
return rb_iv_get(exc, "@exit_value");
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-17 17:07:33 -04:00
|
|
|
* local_jump_error.reason -> symbol
|
2007-02-02 10:21:41 -05:00
|
|
|
*
|
|
|
|
* The reason this block was terminated:
|
|
|
|
* :break, :redo, :retry, :next, :return, or :noreason.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
localjump_reason(VALUE exc)
|
|
|
|
{
|
|
|
|
return rb_iv_get(exc, "@reason");
|
|
|
|
}
|
|
|
|
|
2015-05-21 04:45:57 -04:00
|
|
|
rb_cref_t *rb_vm_cref_new_toplevel(void); /* vm.c */
|
|
|
|
|
2016-07-28 15:13:26 -04:00
|
|
|
static const rb_env_t *
|
|
|
|
env_clone(const rb_env_t *env, const rb_cref_t *cref)
|
2015-05-21 04:45:57 -04:00
|
|
|
{
|
2016-07-28 15:13:26 -04:00
|
|
|
VALUE *new_ep;
|
|
|
|
VALUE *new_body;
|
|
|
|
const rb_env_t *new_env;
|
|
|
|
|
|
|
|
VM_ASSERT(env->ep > env->env);
|
|
|
|
VM_ASSERT(VM_ENV_ESCAPED_P(env->ep));
|
2015-05-21 04:45:57 -04:00
|
|
|
|
|
|
|
if (cref == NULL) {
|
|
|
|
cref = rb_vm_cref_new_toplevel();
|
|
|
|
}
|
|
|
|
|
2016-07-28 15:13:26 -04:00
|
|
|
new_body = ALLOC_N(VALUE, env->env_size);
|
|
|
|
MEMCPY(new_body, env->env, VALUE, env->env_size);
|
|
|
|
new_ep = &new_body[env->ep - env->env];
|
|
|
|
new_env = vm_env_new(new_ep, new_body, env->env_size, env->iseq);
|
|
|
|
RB_OBJ_WRITE(new_env, &new_ep[VM_ENV_DATA_INDEX_ME_CREF], (VALUE)cref);
|
|
|
|
VM_ASSERT(VM_ENV_ESCAPED_P(new_ep));
|
|
|
|
return new_env;
|
2015-05-21 04:45:57 -04:00
|
|
|
}
|
|
|
|
|
2007-12-20 03:20:02 -05:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-17 17:07:33 -04:00
|
|
|
* prc.binding -> binding
|
2009-02-22 09:23:33 -05:00
|
|
|
*
|
2007-12-20 03:20:02 -05:00
|
|
|
* Returns the binding associated with <i>prc</i>. Note that
|
|
|
|
* <code>Kernel#eval</code> accepts either a <code>Proc</code> or a
|
|
|
|
* <code>Binding</code> object as its second parameter.
|
2009-02-22 09:23:33 -05:00
|
|
|
*
|
2007-12-20 03:20:02 -05:00
|
|
|
* def fred(param)
|
|
|
|
* proc {}
|
|
|
|
* end
|
2009-02-22 09:23:33 -05:00
|
|
|
*
|
2007-12-20 03:20:02 -05:00
|
|
|
* b = fred(99)
|
|
|
|
* eval("param", b.binding) #=> 99
|
|
|
|
*/
|
|
|
|
static VALUE
|
|
|
|
proc_binding(VALUE self)
|
|
|
|
{
|
2016-07-28 15:13:26 -04:00
|
|
|
VALUE bindval, binding_self = Qundef;
|
2007-12-20 03:20:02 -05:00
|
|
|
rb_binding_t *bind;
|
2016-07-28 07:02:30 -04:00
|
|
|
const rb_proc_t *proc;
|
|
|
|
const rb_iseq_t *iseq = NULL;
|
|
|
|
const struct rb_block *block;
|
2016-07-28 15:13:26 -04:00
|
|
|
const rb_env_t *env = NULL;
|
2007-12-20 03:20:02 -05:00
|
|
|
|
|
|
|
GetProcPtr(self, proc);
|
2016-07-28 07:02:30 -04:00
|
|
|
block = &proc->block;
|
|
|
|
|
|
|
|
again:
|
|
|
|
switch (vm_block_type(block)) {
|
|
|
|
case block_type_iseq:
|
|
|
|
iseq = block->as.captured.code.iseq;
|
|
|
|
binding_self = block->as.captured.self;
|
2016-07-28 15:13:26 -04:00
|
|
|
env = VM_ENV_ENVVAL_PTR(block->as.captured.ep);
|
2016-07-28 07:02:30 -04:00
|
|
|
break;
|
|
|
|
case block_type_proc:
|
|
|
|
GetProcPtr(block->as.proc, proc);
|
|
|
|
block = &proc->block;
|
|
|
|
goto again;
|
|
|
|
case block_type_symbol:
|
|
|
|
goto error;
|
|
|
|
case block_type_ifunc:
|
|
|
|
{
|
|
|
|
const struct vm_ifunc *ifunc = block->as.captured.code.ifunc;
|
|
|
|
if (IS_METHOD_PROC_IFUNC(ifunc)) {
|
|
|
|
VALUE method = (VALUE)ifunc->data;
|
|
|
|
binding_self = method_receiver(method);
|
2016-07-28 15:13:26 -04:00
|
|
|
iseq = rb_method_iseq(method);
|
|
|
|
env = VM_ENV_ENVVAL_PTR(block->as.captured.ep);
|
|
|
|
env = env_clone(env, method_cref(method));
|
2016-07-28 07:02:30 -04:00
|
|
|
/* set empty iseq */
|
2016-07-28 15:13:26 -04:00
|
|
|
RB_OBJ_WRITE(env, &env->iseq, rb_iseq_new(NULL, rb_str_new2("<empty iseq>"), rb_str_new2("<empty_iseq>"), Qnil, 0, ISEQ_TYPE_TOP));
|
2016-07-28 07:02:30 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
error:
|
|
|
|
rb_raise(rb_eArgError, "Can't create Binding from C level Proc");
|
|
|
|
return Qnil;
|
|
|
|
}
|
2015-05-21 07:52:21 -04:00
|
|
|
}
|
2007-12-20 03:20:02 -05:00
|
|
|
}
|
|
|
|
|
2014-10-18 07:46:31 -04:00
|
|
|
bindval = rb_binding_alloc(rb_cBinding);
|
2009-09-21 04:12:12 -04:00
|
|
|
GetBindingPtr(bindval, bind);
|
2016-07-28 07:02:30 -04:00
|
|
|
|
|
|
|
bind->block.as.captured.self = binding_self;
|
|
|
|
bind->block.as.captured.code.iseq = env->iseq;
|
|
|
|
bind->block.as.captured.ep = env->ep;
|
2015-05-21 04:45:57 -04:00
|
|
|
|
|
|
|
if (iseq) {
|
2015-12-08 08:58:50 -05:00
|
|
|
rb_iseq_check(iseq);
|
2015-07-21 18:52:59 -04:00
|
|
|
bind->path = iseq->body->location.path;
|
|
|
|
bind->first_lineno = FIX2INT(rb_iseq_first_lineno(iseq));
|
2010-05-16 04:18:49 -04:00
|
|
|
}
|
|
|
|
else {
|
2012-06-03 22:49:37 -04:00
|
|
|
bind->path = Qnil;
|
|
|
|
bind->first_lineno = 0;
|
2010-05-16 04:18:49 -04:00
|
|
|
}
|
2015-05-21 04:45:57 -04:00
|
|
|
|
2007-12-20 03:20:02 -05:00
|
|
|
return bindval;
|
|
|
|
}
|
2007-02-02 10:21:41 -05:00
|
|
|
|
2008-06-10 12:33:51 -04:00
|
|
|
static VALUE curry(VALUE dummy, VALUE args, int argc, VALUE *argv, VALUE passed_proc);
|
2008-02-13 07:51:31 -05:00
|
|
|
|
|
|
|
static VALUE
|
|
|
|
make_curry_proc(VALUE proc, VALUE passed, VALUE arity)
|
|
|
|
{
|
2008-10-09 01:47:04 -04:00
|
|
|
VALUE args = rb_ary_new3(3, proc, passed, arity);
|
2009-07-05 20:31:55 -04:00
|
|
|
rb_proc_t *procp;
|
|
|
|
int is_lambda;
|
|
|
|
|
|
|
|
GetProcPtr(proc, procp);
|
|
|
|
is_lambda = procp->is_lambda;
|
2008-02-13 07:51:31 -05:00
|
|
|
rb_ary_freeze(passed);
|
|
|
|
rb_ary_freeze(args);
|
2009-07-05 20:31:55 -04:00
|
|
|
proc = rb_proc_new(curry, args);
|
|
|
|
GetProcPtr(proc, procp);
|
|
|
|
procp->is_lambda = is_lambda;
|
|
|
|
return proc;
|
2008-02-13 07:51:31 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
2008-06-10 12:33:51 -04:00
|
|
|
curry(VALUE dummy, VALUE args, int argc, VALUE *argv, VALUE passed_proc)
|
2008-02-13 07:51:31 -05:00
|
|
|
{
|
|
|
|
VALUE proc, passed, arity;
|
2013-05-13 05:56:22 -04:00
|
|
|
proc = RARRAY_AREF(args, 0);
|
|
|
|
passed = RARRAY_AREF(args, 1);
|
|
|
|
arity = RARRAY_AREF(args, 2);
|
2008-02-13 07:51:31 -05:00
|
|
|
|
|
|
|
passed = rb_ary_plus(passed, rb_ary_new4(argc, argv));
|
|
|
|
rb_ary_freeze(passed);
|
2008-06-10 12:33:51 -04:00
|
|
|
|
2009-07-05 20:31:55 -04:00
|
|
|
if (RARRAY_LEN(passed) < FIX2INT(arity)) {
|
2008-06-10 12:33:51 -04:00
|
|
|
if (!NIL_P(passed_proc)) {
|
2008-06-09 11:52:51 -04:00
|
|
|
rb_warn("given block not used");
|
|
|
|
}
|
2008-02-13 07:51:31 -05:00
|
|
|
arity = make_curry_proc(proc, passed, arity);
|
|
|
|
return arity;
|
|
|
|
}
|
2008-06-10 12:33:51 -04:00
|
|
|
else {
|
* include/ruby/ruby.h: rename RARRAY_RAWPTR() to RARRAY_CONST_PTR().
RARRAY_RAWPTR(ary) returns (const VALUE *) type pointer and
usecase of this macro is not acquire raw pointer, but acquire
read-only pointer. So we rename to better name.
RSTRUCT_RAWPTR() is also renamed to RSTRUCT_CONST_PTR()
(I expect that nobody use it).
* array.c, compile.c, cont.c, enumerator.c, gc.c, proc.c, random.c,
string.c, struct.c, thread.c, vm_eval.c, vm_insnhelper.c:
catch up this change.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@43043 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2013-09-25 04:24:34 -04:00
|
|
|
return rb_proc_call_with_block(proc, check_argc(RARRAY_LEN(passed)), RARRAY_CONST_PTR(passed), passed_proc);
|
2008-06-10 12:33:51 -04:00
|
|
|
}
|
2008-02-13 07:51:31 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-17 17:07:33 -04:00
|
|
|
* prc.curry -> a_proc
|
|
|
|
* prc.curry(arity) -> a_proc
|
2008-02-13 07:51:31 -05:00
|
|
|
*
|
|
|
|
* Returns a curried proc. If the optional <i>arity</i> argument is given,
|
|
|
|
* it determines the number of arguments.
|
|
|
|
* A curried proc receives some arguments. If a sufficient number of
|
|
|
|
* arguments are supplied, it passes the supplied arguments to the original
|
|
|
|
* proc and returns the result. Otherwise, returns another curried proc that
|
|
|
|
* takes the rest of arguments.
|
|
|
|
*
|
|
|
|
* b = proc {|x, y, z| (x||0) + (y||0) + (z||0) }
|
|
|
|
* p b.curry[1][2][3] #=> 6
|
|
|
|
* p b.curry[1, 2][3, 4] #=> 6
|
|
|
|
* p b.curry(5)[1][2][3][4][5] #=> 6
|
|
|
|
* p b.curry(5)[1, 2][3, 4][5] #=> 6
|
|
|
|
* p b.curry(1)[1] #=> 1
|
|
|
|
*
|
|
|
|
* b = proc {|x, y, z, *w| (x||0) + (y||0) + (z||0) + w.inject(0, &:+) }
|
|
|
|
* p b.curry[1][2][3] #=> 6
|
|
|
|
* p b.curry[1, 2][3, 4] #=> 10
|
|
|
|
* p b.curry(5)[1][2][3][4][5] #=> 15
|
|
|
|
* p b.curry(5)[1, 2][3, 4][5] #=> 15
|
|
|
|
* p b.curry(1)[1] #=> 1
|
|
|
|
*
|
|
|
|
* b = lambda {|x, y, z| (x||0) + (y||0) + (z||0) }
|
|
|
|
* p b.curry[1][2][3] #=> 6
|
2015-10-24 20:39:29 -04:00
|
|
|
* p b.curry[1, 2][3, 4] #=> wrong number of arguments (given 4, expected 3)
|
|
|
|
* p b.curry(5) #=> wrong number of arguments (given 5, expected 3)
|
|
|
|
* p b.curry(1) #=> wrong number of arguments (given 1, expected 3)
|
2008-02-13 07:51:31 -05:00
|
|
|
*
|
|
|
|
* b = lambda {|x, y, z, *w| (x||0) + (y||0) + (z||0) + w.inject(0, &:+) }
|
|
|
|
* p b.curry[1][2][3] #=> 6
|
|
|
|
* p b.curry[1, 2][3, 4] #=> 10
|
|
|
|
* p b.curry(5)[1][2][3][4][5] #=> 15
|
|
|
|
* p b.curry(5)[1, 2][3, 4][5] #=> 15
|
2015-10-24 20:39:29 -04:00
|
|
|
* p b.curry(1) #=> wrong number of arguments (given 1, expected 3)
|
2008-02-13 07:51:31 -05:00
|
|
|
*
|
|
|
|
* b = proc { :foo }
|
|
|
|
* p b.curry[] #=> :foo
|
|
|
|
*/
|
|
|
|
static VALUE
|
2014-06-19 00:42:16 -04:00
|
|
|
proc_curry(int argc, const VALUE *argv, VALUE self)
|
2008-02-13 07:51:31 -05:00
|
|
|
{
|
2013-02-01 17:46:32 -05:00
|
|
|
int sarity, max_arity, min_arity = rb_proc_min_max_arity(self, &max_arity);
|
|
|
|
VALUE arity;
|
2008-02-13 07:51:31 -05:00
|
|
|
|
|
|
|
rb_scan_args(argc, argv, "01", &arity);
|
|
|
|
if (NIL_P(arity)) {
|
2013-02-01 17:46:32 -05:00
|
|
|
arity = INT2FIX(min_arity);
|
2008-02-13 07:51:31 -05:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
sarity = FIX2INT(arity);
|
2013-02-01 17:46:32 -05:00
|
|
|
if (rb_proc_lambda_p(self)) {
|
|
|
|
rb_check_arity(sarity, min_arity, max_arity);
|
2008-02-13 07:51:31 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return make_curry_proc(self, rb_ary_new(), arity);
|
|
|
|
}
|
|
|
|
|
2014-06-19 00:42:16 -04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* meth.curry -> proc
|
|
|
|
* meth.curry(arity) -> proc
|
|
|
|
*
|
|
|
|
* Returns a curried proc based on the method. When the proc is called with a number of
|
|
|
|
* arguments that is lower than the method's arity, then another curried proc is returned.
|
|
|
|
* Only when enough arguments have been supplied to satisfy the method signature, will the
|
|
|
|
* method actually be called.
|
|
|
|
*
|
|
|
|
* The optional <i>arity</i> argument should be supplied when currying methods with
|
|
|
|
* variable arguments to determine how many arguments are needed before the method is
|
|
|
|
* called.
|
|
|
|
*
|
|
|
|
* def foo(a,b,c)
|
|
|
|
* [a, b, c]
|
|
|
|
* end
|
|
|
|
*
|
|
|
|
* proc = self.method(:foo).curry
|
|
|
|
* proc2 = proc.call(1, 2) #=> #<Proc>
|
|
|
|
* proc2.call(3) #=> [1,2,3]
|
|
|
|
*
|
|
|
|
* def vararg(*args)
|
|
|
|
* args
|
|
|
|
* end
|
|
|
|
*
|
|
|
|
* proc = self.method(:vararg).curry(4)
|
|
|
|
* proc2 = proc.call(:x) #=> #<Proc>
|
|
|
|
* proc3 = proc2.call(:y, :z) #=> #<Proc>
|
|
|
|
* proc3.call(:a) #=> [:x, :y, :z, :a]
|
|
|
|
*/
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
rb_method_curry(int argc, const VALUE *argv, VALUE self)
|
|
|
|
{
|
2015-05-20 22:00:31 -04:00
|
|
|
VALUE proc = method_to_proc(self);
|
2014-06-19 00:42:16 -04:00
|
|
|
return proc_curry(argc, argv, proc);
|
|
|
|
}
|
|
|
|
|
2010-05-08 00:50:09 -04:00
|
|
|
/*
|
|
|
|
* Document-class: LocalJumpError
|
|
|
|
*
|
|
|
|
* Raised when Ruby can't yield as requested.
|
|
|
|
*
|
|
|
|
* A typical scenario is attempting to yield when no block is given:
|
|
|
|
*
|
|
|
|
* def call_block
|
|
|
|
* yield 42
|
|
|
|
* end
|
|
|
|
* call_block
|
|
|
|
*
|
|
|
|
* <em>raises the exception:</em>
|
|
|
|
*
|
|
|
|
* LocalJumpError: no block given (yield)
|
|
|
|
*
|
|
|
|
* A more subtle example:
|
|
|
|
*
|
|
|
|
* def get_me_a_return
|
|
|
|
* Proc.new { return 42 }
|
|
|
|
* end
|
|
|
|
* get_me_a_return.call
|
|
|
|
*
|
|
|
|
* <em>raises the exception:</em>
|
|
|
|
*
|
|
|
|
* LocalJumpError: unexpected return
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Document-class: SystemStackError
|
|
|
|
*
|
|
|
|
* Raised in case of a stack overflow.
|
|
|
|
*
|
|
|
|
* def me_myself_and_i
|
|
|
|
* me_myself_and_i
|
|
|
|
* end
|
|
|
|
* me_myself_and_i
|
|
|
|
*
|
|
|
|
* <em>raises the exception:</em>
|
|
|
|
*
|
|
|
|
* SystemStackError: stack level too deep
|
|
|
|
*/
|
|
|
|
|
2007-02-02 10:21:41 -05:00
|
|
|
/*
|
|
|
|
* <code>Proc</code> objects are blocks of code that have been bound to
|
|
|
|
* a set of local variables. Once bound, the code may be called in
|
|
|
|
* different contexts and still access those variables.
|
2009-02-22 09:23:33 -05:00
|
|
|
*
|
2007-02-02 10:21:41 -05:00
|
|
|
* def gen_times(factor)
|
|
|
|
* return Proc.new {|n| n*factor }
|
|
|
|
* end
|
2009-02-22 09:23:33 -05:00
|
|
|
*
|
2007-02-02 10:21:41 -05:00
|
|
|
* times3 = gen_times(3)
|
|
|
|
* times5 = gen_times(5)
|
2009-02-22 09:23:33 -05:00
|
|
|
*
|
2007-02-02 10:21:41 -05:00
|
|
|
* times3.call(12) #=> 36
|
|
|
|
* times5.call(5) #=> 25
|
|
|
|
* times3.call(times5.call(4)) #=> 60
|
2009-02-22 09:23:33 -05:00
|
|
|
*
|
2007-02-02 10:21:41 -05:00
|
|
|
*/
|
|
|
|
|
|
|
|
void
|
2007-02-05 13:31:08 -05:00
|
|
|
Init_Proc(void)
|
2007-02-02 10:21:41 -05:00
|
|
|
{
|
|
|
|
/* Proc */
|
|
|
|
rb_cProc = rb_define_class("Proc", rb_cObject);
|
|
|
|
rb_undef_alloc_func(rb_cProc);
|
2007-12-05 02:18:52 -05:00
|
|
|
rb_define_singleton_method(rb_cProc, "new", rb_proc_s_new, -1);
|
2009-07-15 10:59:41 -04:00
|
|
|
|
|
|
|
rb_add_method(rb_cProc, rb_intern("call"), VM_METHOD_TYPE_OPTIMIZED,
|
2015-10-06 06:24:34 -04:00
|
|
|
(void *)OPTIMIZED_METHOD_TYPE_CALL, METHOD_VISI_PUBLIC);
|
2009-07-15 10:59:41 -04:00
|
|
|
rb_add_method(rb_cProc, rb_intern("[]"), VM_METHOD_TYPE_OPTIMIZED,
|
2015-10-06 06:24:34 -04:00
|
|
|
(void *)OPTIMIZED_METHOD_TYPE_CALL, METHOD_VISI_PUBLIC);
|
2009-07-15 10:59:41 -04:00
|
|
|
rb_add_method(rb_cProc, rb_intern("==="), VM_METHOD_TYPE_OPTIMIZED,
|
2015-10-06 06:24:34 -04:00
|
|
|
(void *)OPTIMIZED_METHOD_TYPE_CALL, METHOD_VISI_PUBLIC);
|
2009-07-15 10:59:41 -04:00
|
|
|
rb_add_method(rb_cProc, rb_intern("yield"), VM_METHOD_TYPE_OPTIMIZED,
|
2015-10-06 06:24:34 -04:00
|
|
|
(void *)OPTIMIZED_METHOD_TYPE_CALL, METHOD_VISI_PUBLIC);
|
2015-10-05 17:34:24 -04:00
|
|
|
|
2016-05-15 17:11:33 -04:00
|
|
|
#if 0 /* for RDoc */
|
|
|
|
rb_define_method(rb_cProc, "call", proc_call, -1);
|
|
|
|
rb_define_method(rb_cProc, "[]", proc_call, -1);
|
|
|
|
rb_define_method(rb_cProc, "===", proc_call, -1);
|
|
|
|
rb_define_method(rb_cProc, "yield", proc_call, -1);
|
|
|
|
#endif
|
|
|
|
|
2007-02-02 10:21:41 -05:00
|
|
|
rb_define_method(rb_cProc, "to_proc", proc_to_proc, 0);
|
|
|
|
rb_define_method(rb_cProc, "arity", proc_arity, 0);
|
|
|
|
rb_define_method(rb_cProc, "clone", proc_clone, 0);
|
|
|
|
rb_define_method(rb_cProc, "dup", proc_dup, 0);
|
|
|
|
rb_define_method(rb_cProc, "hash", proc_hash, 0);
|
|
|
|
rb_define_method(rb_cProc, "to_s", proc_to_s, 0);
|
2012-08-15 07:50:01 -04:00
|
|
|
rb_define_alias(rb_cProc, "inspect", "to_s");
|
2009-10-24 12:53:11 -04:00
|
|
|
rb_define_method(rb_cProc, "lambda?", rb_proc_lambda_p, 0);
|
2007-12-20 03:20:02 -05:00
|
|
|
rb_define_method(rb_cProc, "binding", proc_binding, 0);
|
2008-02-13 07:51:31 -05:00
|
|
|
rb_define_method(rb_cProc, "curry", proc_curry, -1);
|
2008-09-26 09:47:01 -04:00
|
|
|
rb_define_method(rb_cProc, "source_location", rb_proc_location, 0);
|
2008-11-27 23:19:37 -05:00
|
|
|
rb_define_method(rb_cProc, "parameters", rb_proc_parameters, 0);
|
2007-02-02 10:21:41 -05:00
|
|
|
|
2007-02-05 13:31:08 -05:00
|
|
|
/* Exceptions */
|
2007-02-02 10:21:41 -05:00
|
|
|
rb_eLocalJumpError = rb_define_class("LocalJumpError", rb_eStandardError);
|
|
|
|
rb_define_method(rb_eLocalJumpError, "exit_value", localjump_xvalue, 0);
|
|
|
|
rb_define_method(rb_eLocalJumpError, "reason", localjump_reason, 0);
|
|
|
|
|
|
|
|
rb_eSysStackError = rb_define_class("SystemStackError", rb_eException);
|
2014-09-11 06:53:48 -04:00
|
|
|
rb_vm_register_special_exception(ruby_error_sysstack, rb_eSysStackError, "stack level too deep");
|
2007-02-02 10:21:41 -05:00
|
|
|
|
|
|
|
/* utility functions */
|
|
|
|
rb_define_global_function("proc", rb_block_proc, 0);
|
2013-04-29 23:30:21 -04:00
|
|
|
rb_define_global_function("lambda", rb_block_lambda, 0);
|
2007-02-02 10:21:41 -05:00
|
|
|
|
|
|
|
/* Method */
|
|
|
|
rb_cMethod = rb_define_class("Method", rb_cObject);
|
|
|
|
rb_undef_alloc_func(rb_cMethod);
|
|
|
|
rb_undef_method(CLASS_OF(rb_cMethod), "new");
|
|
|
|
rb_define_method(rb_cMethod, "==", method_eq, 1);
|
|
|
|
rb_define_method(rb_cMethod, "eql?", method_eq, 1);
|
|
|
|
rb_define_method(rb_cMethod, "hash", method_hash, 0);
|
|
|
|
rb_define_method(rb_cMethod, "clone", method_clone, 0);
|
|
|
|
rb_define_method(rb_cMethod, "call", rb_method_call, -1);
|
2014-06-19 00:42:16 -04:00
|
|
|
rb_define_method(rb_cMethod, "curry", rb_method_curry, -1);
|
2007-02-02 10:21:41 -05:00
|
|
|
rb_define_method(rb_cMethod, "[]", rb_method_call, -1);
|
|
|
|
rb_define_method(rb_cMethod, "arity", method_arity_m, 0);
|
|
|
|
rb_define_method(rb_cMethod, "inspect", method_inspect, 0);
|
|
|
|
rb_define_method(rb_cMethod, "to_s", method_inspect, 0);
|
2015-05-20 22:00:31 -04:00
|
|
|
rb_define_method(rb_cMethod, "to_proc", method_to_proc, 0);
|
2007-02-02 10:21:41 -05:00
|
|
|
rb_define_method(rb_cMethod, "receiver", method_receiver, 0);
|
|
|
|
rb_define_method(rb_cMethod, "name", method_name, 0);
|
2013-02-13 04:12:04 -05:00
|
|
|
rb_define_method(rb_cMethod, "original_name", method_original_name, 0);
|
2007-02-02 10:21:41 -05:00
|
|
|
rb_define_method(rb_cMethod, "owner", method_owner, 0);
|
|
|
|
rb_define_method(rb_cMethod, "unbind", method_unbind, 0);
|
2008-09-26 09:47:01 -04:00
|
|
|
rb_define_method(rb_cMethod, "source_location", rb_method_location, 0);
|
2008-11-27 23:19:37 -05:00
|
|
|
rb_define_method(rb_cMethod, "parameters", rb_method_parameters, 0);
|
2014-07-26 12:22:41 -04:00
|
|
|
rb_define_method(rb_cMethod, "super_method", method_super_method, 0);
|
2007-02-02 10:21:41 -05:00
|
|
|
rb_define_method(rb_mKernel, "method", rb_obj_method, 1);
|
2007-12-17 18:01:50 -05:00
|
|
|
rb_define_method(rb_mKernel, "public_method", rb_obj_public_method, 1);
|
2013-05-13 01:52:03 -04:00
|
|
|
rb_define_method(rb_mKernel, "singleton_method", rb_obj_singleton_method, 1);
|
2007-02-02 10:21:41 -05:00
|
|
|
|
|
|
|
/* UnboundMethod */
|
|
|
|
rb_cUnboundMethod = rb_define_class("UnboundMethod", rb_cObject);
|
|
|
|
rb_undef_alloc_func(rb_cUnboundMethod);
|
|
|
|
rb_undef_method(CLASS_OF(rb_cUnboundMethod), "new");
|
|
|
|
rb_define_method(rb_cUnboundMethod, "==", method_eq, 1);
|
|
|
|
rb_define_method(rb_cUnboundMethod, "eql?", method_eq, 1);
|
|
|
|
rb_define_method(rb_cUnboundMethod, "hash", method_hash, 0);
|
|
|
|
rb_define_method(rb_cUnboundMethod, "clone", method_clone, 0);
|
|
|
|
rb_define_method(rb_cUnboundMethod, "arity", method_arity_m, 0);
|
|
|
|
rb_define_method(rb_cUnboundMethod, "inspect", method_inspect, 0);
|
|
|
|
rb_define_method(rb_cUnboundMethod, "to_s", method_inspect, 0);
|
|
|
|
rb_define_method(rb_cUnboundMethod, "name", method_name, 0);
|
2013-02-13 04:12:04 -05:00
|
|
|
rb_define_method(rb_cUnboundMethod, "original_name", method_original_name, 0);
|
2007-02-02 10:21:41 -05:00
|
|
|
rb_define_method(rb_cUnboundMethod, "owner", method_owner, 0);
|
|
|
|
rb_define_method(rb_cUnboundMethod, "bind", umethod_bind, 1);
|
2008-09-26 09:47:01 -04:00
|
|
|
rb_define_method(rb_cUnboundMethod, "source_location", rb_method_location, 0);
|
2008-11-27 23:19:37 -05:00
|
|
|
rb_define_method(rb_cUnboundMethod, "parameters", rb_method_parameters, 0);
|
2014-07-26 12:22:41 -04:00
|
|
|
rb_define_method(rb_cUnboundMethod, "super_method", method_super_method, 0);
|
2007-02-02 10:21:41 -05:00
|
|
|
|
|
|
|
/* Module#*_method */
|
2007-12-17 18:01:50 -05:00
|
|
|
rb_define_method(rb_cModule, "instance_method", rb_mod_instance_method, 1);
|
|
|
|
rb_define_method(rb_cModule, "public_instance_method", rb_mod_public_instance_method, 1);
|
2007-06-02 03:47:20 -04:00
|
|
|
rb_define_private_method(rb_cModule, "define_method", rb_mod_define_method, -1);
|
2007-08-30 01:06:52 -04:00
|
|
|
|
|
|
|
/* Kernel */
|
|
|
|
rb_define_method(rb_mKernel, "define_singleton_method", rb_obj_define_method, -1);
|
2012-11-01 19:24:33 -04:00
|
|
|
|
2013-01-07 07:42:48 -05:00
|
|
|
rb_define_private_method(rb_singleton_class(rb_vm_top_self()),
|
|
|
|
"define_method", top_define_method, -1);
|
2007-02-02 10:21:41 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Objects of class <code>Binding</code> encapsulate the execution
|
|
|
|
* context at some particular place in the code and retain this context
|
|
|
|
* for future use. The variables, methods, value of <code>self</code>,
|
|
|
|
* and possibly an iterator block that can be accessed in this context
|
|
|
|
* are all retained. Binding objects can be created using
|
|
|
|
* <code>Kernel#binding</code>, and are made available to the callback
|
|
|
|
* of <code>Kernel#set_trace_func</code>.
|
2009-02-22 09:23:33 -05:00
|
|
|
*
|
2007-02-02 10:21:41 -05:00
|
|
|
* These binding objects can be passed as the second argument of the
|
|
|
|
* <code>Kernel#eval</code> method, establishing an environment for the
|
|
|
|
* evaluation.
|
2009-02-22 09:23:33 -05:00
|
|
|
*
|
2007-02-02 10:21:41 -05:00
|
|
|
* class Demo
|
|
|
|
* def initialize(n)
|
|
|
|
* @secret = n
|
|
|
|
* end
|
2011-03-07 03:44:45 -05:00
|
|
|
* def get_binding
|
2007-02-02 10:21:41 -05:00
|
|
|
* return binding()
|
|
|
|
* end
|
|
|
|
* end
|
2009-02-22 09:23:33 -05:00
|
|
|
*
|
2007-02-02 10:21:41 -05:00
|
|
|
* k1 = Demo.new(99)
|
2011-03-07 03:44:45 -05:00
|
|
|
* b1 = k1.get_binding
|
2007-02-02 10:21:41 -05:00
|
|
|
* k2 = Demo.new(-3)
|
2011-03-07 03:44:45 -05:00
|
|
|
* b2 = k2.get_binding
|
2009-02-22 09:23:33 -05:00
|
|
|
*
|
2007-02-02 10:21:41 -05:00
|
|
|
* eval("@secret", b1) #=> 99
|
|
|
|
* eval("@secret", b2) #=> -3
|
|
|
|
* eval("@secret") #=> nil
|
2009-02-22 09:23:33 -05:00
|
|
|
*
|
2007-02-02 10:21:41 -05:00
|
|
|
* Binding objects have no class-specific methods.
|
2009-02-22 09:23:33 -05:00
|
|
|
*
|
2007-02-02 10:21:41 -05:00
|
|
|
*/
|
|
|
|
|
|
|
|
void
|
2007-02-05 13:31:08 -05:00
|
|
|
Init_Binding(void)
|
2007-02-02 10:21:41 -05:00
|
|
|
{
|
2007-02-05 13:31:08 -05:00
|
|
|
rb_cBinding = rb_define_class("Binding", rb_cObject);
|
|
|
|
rb_undef_alloc_func(rb_cBinding);
|
|
|
|
rb_undef_method(CLASS_OF(rb_cBinding), "new");
|
|
|
|
rb_define_method(rb_cBinding, "clone", binding_clone, 0);
|
|
|
|
rb_define_method(rb_cBinding, "dup", binding_dup, 0);
|
2007-02-05 13:50:35 -05:00
|
|
|
rb_define_method(rb_cBinding, "eval", bind_eval, -1);
|
2013-12-24 11:03:12 -05:00
|
|
|
rb_define_method(rb_cBinding, "local_variables", bind_local_variables, 0);
|
2013-08-09 05:51:00 -04:00
|
|
|
rb_define_method(rb_cBinding, "local_variable_get", bind_local_variable_get, 1);
|
|
|
|
rb_define_method(rb_cBinding, "local_variable_set", bind_local_variable_set, 2);
|
|
|
|
rb_define_method(rb_cBinding, "local_variable_defined?", bind_local_variable_defined_p, 1);
|
2014-07-01 13:24:02 -04:00
|
|
|
rb_define_method(rb_cBinding, "receiver", bind_receiver, 0);
|
2007-02-05 13:31:08 -05:00
|
|
|
rb_define_global_function("binding", rb_f_binding, 0);
|
2007-02-02 10:21:41 -05:00
|
|
|
}
|