2000-05-01 05:42:38 -04:00
|
|
|
/**********************************************************************
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
class.c -
|
|
|
|
|
|
|
|
$Author$
|
|
|
|
created at: Tue Aug 10 15:05:44 JST 1993
|
|
|
|
|
* encoding.c: provide basic features for M17N.
* parse.y: encoding aware parsing.
* parse.y (pragma_encoding): encoding specification pragma.
* parse.y (rb_intern3): encoding specified symbols.
* string.c (rb_str_length): length based on characters.
for older behavior, bytesize method added.
* string.c (rb_str_index_m): index based on characters. rindex as
well.
* string.c (succ_char): encoding aware succeeding string.
* string.c (rb_str_reverse): reverse based on characters.
* string.c (rb_str_inspect): encoding aware string description.
* string.c (rb_str_upcase_bang): encoding aware case conversion.
downcase, capitalize, swapcase as well.
* string.c (rb_str_tr_bang): tr based on characters. delete,
squeeze, tr_s, count as well.
* string.c (rb_str_split_m): split based on characters.
* string.c (rb_str_each_line): encoding aware each_line.
* string.c (rb_str_each_char): added. iteration based on
characters.
* string.c (rb_str_strip_bang): encoding aware whitespace
stripping. lstrip, rstrip as well.
* string.c (rb_str_justify): encoding aware justifying (ljust,
rjust, center).
* string.c (str_encoding): get encoding attribute from a string.
* re.c (rb_reg_initialize): encoding aware regular expression
* sprintf.c (rb_str_format): formatting (i.e. length count) based
on characters.
* io.c (rb_io_getc): getc to return one-character string.
for older behavior, getbyte method added.
* ext/stringio/stringio.c (strio_getc): ditto.
* io.c (rb_io_ungetc): allow pushing arbitrary string at the
current reading point.
* ext/stringio/stringio.c (strio_ungetc): ditto.
* ext/strscan/strscan.c: encoding support.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@13261 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-08-24 23:29:39 -04:00
|
|
|
Copyright (C) 1993-2007 Yukihiro Matsumoto
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2000-05-01 05:42:38 -04:00
|
|
|
**********************************************************************/
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2009-06-29 12:39:28 -04:00
|
|
|
/*!
|
|
|
|
* \defgroup class Classes and their hierarchy.
|
|
|
|
* \par Terminology
|
|
|
|
* - class: same as in Ruby.
|
|
|
|
* - singleton class: class for a particular object
|
|
|
|
* - eigenclass: = singleton class
|
|
|
|
* - metaclass: class of a class. metaclass is a kind of singleton class.
|
|
|
|
* - metametaclass: class of a metaclass.
|
|
|
|
* - meta^(n)-class: class of a meta^(n-1)-class.
|
|
|
|
* - attached object: A singleton class knows its unique instance.
|
|
|
|
* The instance is called the attached object for the singleton class.
|
|
|
|
* \{
|
|
|
|
*/
|
|
|
|
|
2007-06-09 23:06:15 -04:00
|
|
|
#include "ruby/ruby.h"
|
|
|
|
#include "ruby/st.h"
|
2009-07-15 10:59:41 -04:00
|
|
|
#include "method.h"
|
2010-10-26 13:27:32 -04:00
|
|
|
#include "constant.h"
|
2009-07-15 10:59:41 -04:00
|
|
|
#include "vm_core.h"
|
2011-05-18 09:41:54 -04:00
|
|
|
#include "internal.h"
|
1999-01-19 23:59:39 -05:00
|
|
|
#include <ctype.h>
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
extern st_table *rb_class_tbl;
|
2009-08-31 01:55:57 -04:00
|
|
|
static ID id_attached;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2009-06-29 12:39:28 -04:00
|
|
|
/**
|
|
|
|
* Allocates a struct RClass for a new class.
|
|
|
|
*
|
|
|
|
* \param flags initial value for basic.flags of the returned class.
|
|
|
|
* \param klass the class of the returned class.
|
|
|
|
* \return an uninitialized Class object.
|
|
|
|
* \pre \p klass must refer \c Class class or an ancestor of Class.
|
|
|
|
* \pre \code (flags | T_CLASS) != 0 \endcode
|
|
|
|
* \post the returned class can safely be \c #initialize 'd.
|
|
|
|
*
|
|
|
|
* \note this function is not Class#allocate.
|
|
|
|
*/
|
2007-09-28 02:21:46 -04:00
|
|
|
static VALUE
|
|
|
|
class_alloc(VALUE flags, VALUE klass)
|
|
|
|
{
|
|
|
|
rb_classext_t *ext = ALLOC(rb_classext_t);
|
|
|
|
NEWOBJ(obj, struct RClass);
|
|
|
|
OBJSETUP(obj, klass, flags);
|
|
|
|
obj->ptr = ext;
|
|
|
|
RCLASS_IV_TBL(obj) = 0;
|
2010-10-26 13:27:21 -04:00
|
|
|
RCLASS_CONST_TBL(obj) = 0;
|
2007-09-28 02:21:46 -04:00
|
|
|
RCLASS_M_TBL(obj) = 0;
|
|
|
|
RCLASS_SUPER(obj) = 0;
|
|
|
|
RCLASS_IV_INDEX_TBL(obj) = 0;
|
|
|
|
return (VALUE)obj;
|
|
|
|
}
|
|
|
|
|
2009-06-29 12:39:28 -04:00
|
|
|
|
|
|
|
/*!
|
|
|
|
* A utility function that wraps class_alloc.
|
|
|
|
*
|
|
|
|
* allocates a class and initializes safely.
|
|
|
|
* \param super a class from which the new class derives.
|
|
|
|
* \return a class object.
|
|
|
|
* \pre \a super must be a class.
|
|
|
|
* \post the metaclass of the new class is Class.
|
|
|
|
*/
|
1998-01-16 07:13:05 -05:00
|
|
|
VALUE
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 06:44:21 -04:00
|
|
|
rb_class_boot(VALUE super)
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
2007-09-28 02:21:46 -04:00
|
|
|
VALUE klass = class_alloc(T_CLASS, rb_cClass);
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2007-09-28 02:21:46 -04:00
|
|
|
RCLASS_SUPER(klass) = super;
|
|
|
|
RCLASS_M_TBL(klass) = st_init_numtable();
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2001-08-14 04:13:31 -04:00
|
|
|
OBJ_INFECT(klass, super);
|
1998-01-16 07:19:22 -05:00
|
|
|
return (VALUE)klass;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
2009-06-29 12:39:28 -04:00
|
|
|
|
|
|
|
/*!
|
|
|
|
* Ensures a class can be derived from super.
|
|
|
|
*
|
|
|
|
* \param super a reference to an object.
|
|
|
|
* \exception TypeError if \a super is not a Class or \a super is a singleton class.
|
|
|
|
*/
|
2004-11-15 23:55:14 -05:00
|
|
|
void
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 06:44:21 -04:00
|
|
|
rb_check_inheritable(VALUE super)
|
2004-11-15 23:55:14 -05:00
|
|
|
{
|
|
|
|
if (TYPE(super) != T_CLASS) {
|
|
|
|
rb_raise(rb_eTypeError, "superclass must be a Class (%s given)",
|
|
|
|
rb_obj_classname(super));
|
|
|
|
}
|
|
|
|
if (RBASIC(super)->flags & FL_SINGLETON) {
|
|
|
|
rb_raise(rb_eTypeError, "can't make subclass of singleton class");
|
|
|
|
}
|
2009-10-23 19:37:14 -04:00
|
|
|
if (super == rb_cClass) {
|
|
|
|
rb_raise(rb_eTypeError, "can't make subclass of Class");
|
|
|
|
}
|
2004-11-15 23:55:14 -05:00
|
|
|
}
|
|
|
|
|
2009-06-29 12:39:28 -04:00
|
|
|
|
|
|
|
/*!
|
|
|
|
* Creates a new class.
|
|
|
|
* \param super a class from which the new class derives.
|
|
|
|
* \exception TypeError \a super is not inheritable.
|
|
|
|
* \exception TypeError \a super is the Class class.
|
|
|
|
*/
|
2001-07-18 01:56:05 -04:00
|
|
|
VALUE
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 06:44:21 -04:00
|
|
|
rb_class_new(VALUE super)
|
2001-07-18 01:56:05 -04:00
|
|
|
{
|
|
|
|
Check_Type(super, T_CLASS);
|
2004-11-15 23:55:14 -05:00
|
|
|
rb_check_inheritable(super);
|
2001-07-18 01:56:05 -04:00
|
|
|
return rb_class_boot(super);
|
|
|
|
}
|
|
|
|
|
2011-05-31 12:16:06 -04:00
|
|
|
static void
|
|
|
|
rb_mod_clone_method(VALUE klass, ID mid, const rb_method_entry_t *me)
|
2009-07-15 10:59:41 -04:00
|
|
|
{
|
2011-01-26 08:59:20 -05:00
|
|
|
VALUE newiseqval;
|
2009-08-27 22:45:41 -04:00
|
|
|
if (me->def && me->def->type == VM_METHOD_TYPE_ISEQ) {
|
|
|
|
rb_iseq_t *iseq;
|
2011-05-31 12:16:06 -04:00
|
|
|
newiseqval = rb_iseq_clone(me->def->body.iseq->self, klass);
|
2009-08-27 22:45:41 -04:00
|
|
|
GetISeqPtr(newiseqval, iseq);
|
2011-05-31 12:16:06 -04:00
|
|
|
rb_add_method(klass, mid, VM_METHOD_TYPE_ISEQ, iseq, me->flag);
|
2011-01-26 08:59:20 -05:00
|
|
|
RB_GC_GUARD(newiseqval);
|
2009-08-27 22:45:41 -04:00
|
|
|
}
|
|
|
|
else {
|
2011-05-31 12:16:06 -04:00
|
|
|
rb_method_entry_set(klass, mid, me, me->flag);
|
2006-12-31 10:02:22 -05:00
|
|
|
}
|
2011-05-31 12:16:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
clone_method_i(st_data_t key, st_data_t value, st_data_t data)
|
|
|
|
{
|
|
|
|
rb_mod_clone_method((VALUE)data, (ID)key, (const rb_method_entry_t *)value);
|
1998-01-16 07:13:05 -05:00
|
|
|
return ST_CONTINUE;
|
|
|
|
}
|
|
|
|
|
2010-10-26 13:27:32 -04:00
|
|
|
static int
|
|
|
|
clone_const(ID key, const rb_const_entry_t *ce, st_table *tbl)
|
|
|
|
{
|
|
|
|
rb_const_entry_t *nce = ALLOC(rb_const_entry_t);
|
|
|
|
*nce = *ce;
|
|
|
|
st_insert(tbl, key, (st_data_t)nce);
|
2010-10-26 23:06:27 -04:00
|
|
|
return ST_CONTINUE;
|
2010-10-26 13:27:32 -04:00
|
|
|
}
|
|
|
|
|
2011-06-03 23:49:50 -04:00
|
|
|
static int
|
|
|
|
clone_const_i(st_data_t key, st_data_t value, st_data_t data)
|
|
|
|
{
|
|
|
|
return clone_const((ID)key, (const rb_const_entry_t *)value, (st_table *)data);
|
|
|
|
}
|
|
|
|
|
2004-01-18 09:16:47 -05:00
|
|
|
/* :nodoc: */
|
2001-05-02 00:22:21 -04:00
|
|
|
VALUE
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 06:44:21 -04:00
|
|
|
rb_mod_init_copy(VALUE clone, VALUE orig)
|
2001-05-02 00:22:21 -04:00
|
|
|
{
|
2003-08-06 17:50:06 -04:00
|
|
|
rb_obj_init_copy(clone, orig);
|
2003-12-22 07:15:34 -05:00
|
|
|
if (!FL_TEST(CLASS_OF(clone), FL_SINGLETON)) {
|
|
|
|
RBASIC(clone)->klass = rb_singleton_class_clone(orig);
|
2010-06-27 13:47:21 -04:00
|
|
|
rb_singleton_class_attached(RBASIC(clone)->klass, (VALUE)clone);
|
2003-12-22 07:15:34 -05:00
|
|
|
}
|
2007-09-28 02:21:46 -04:00
|
|
|
RCLASS_SUPER(clone) = RCLASS_SUPER(orig);
|
|
|
|
if (RCLASS_IV_TBL(orig)) {
|
2010-10-13 18:22:18 -04:00
|
|
|
st_data_t id;
|
2001-10-22 02:48:18 -04:00
|
|
|
|
2009-11-14 07:56:16 -05:00
|
|
|
if (RCLASS_IV_TBL(clone)) {
|
|
|
|
st_free_table(RCLASS_IV_TBL(clone));
|
|
|
|
}
|
2007-09-28 02:21:46 -04:00
|
|
|
RCLASS_IV_TBL(clone) = st_copy(RCLASS_IV_TBL(orig));
|
2008-06-09 05:25:32 -04:00
|
|
|
CONST_ID(id, "__classpath__");
|
2010-10-13 18:22:18 -04:00
|
|
|
st_delete(RCLASS_IV_TBL(clone), &id, 0);
|
2008-06-09 05:25:32 -04:00
|
|
|
CONST_ID(id, "__classid__");
|
2010-10-13 18:22:18 -04:00
|
|
|
st_delete(RCLASS_IV_TBL(clone), &id, 0);
|
2001-05-02 00:22:21 -04:00
|
|
|
}
|
2010-10-26 13:27:21 -04:00
|
|
|
if (RCLASS_CONST_TBL(orig)) {
|
|
|
|
if (RCLASS_CONST_TBL(clone)) {
|
2010-10-26 13:27:32 -04:00
|
|
|
rb_free_const_table(RCLASS_CONST_TBL(clone));
|
2010-10-26 13:27:21 -04:00
|
|
|
}
|
2010-10-26 13:27:32 -04:00
|
|
|
RCLASS_CONST_TBL(clone) = st_init_numtable();
|
2011-06-03 23:49:50 -04:00
|
|
|
st_foreach(RCLASS_CONST_TBL(orig), clone_const_i, (st_data_t)RCLASS_CONST_TBL(clone));
|
2010-10-26 13:27:21 -04:00
|
|
|
}
|
2007-09-28 02:21:46 -04:00
|
|
|
if (RCLASS_M_TBL(orig)) {
|
2009-11-14 07:56:16 -05:00
|
|
|
if (RCLASS_M_TBL(clone)) {
|
|
|
|
rb_free_m_table(RCLASS_M_TBL(clone));
|
|
|
|
}
|
2011-05-31 12:16:06 -04:00
|
|
|
RCLASS_M_TBL(clone) = st_init_numtable();
|
|
|
|
st_foreach(RCLASS_M_TBL(orig), clone_method_i, (st_data_t)clone);
|
2001-05-02 00:22:21 -04:00
|
|
|
}
|
|
|
|
|
2003-08-06 17:50:06 -04:00
|
|
|
return clone;
|
2001-05-02 00:22:21 -04:00
|
|
|
}
|
|
|
|
|
2004-01-18 09:16:47 -05:00
|
|
|
/* :nodoc: */
|
2001-05-02 00:22:21 -04:00
|
|
|
VALUE
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 06:44:21 -04:00
|
|
|
rb_class_init_copy(VALUE clone, VALUE orig)
|
2001-05-02 00:22:21 -04:00
|
|
|
{
|
2010-02-06 10:26:34 -05:00
|
|
|
if (orig == rb_cBasicObject) {
|
|
|
|
rb_raise(rb_eTypeError, "can't copy the root class");
|
|
|
|
}
|
2010-01-12 21:57:48 -05:00
|
|
|
if (RCLASS_SUPER(clone) != 0 || clone == rb_cBasicObject) {
|
2003-08-06 17:50:06 -04:00
|
|
|
rb_raise(rb_eTypeError, "already initialized class");
|
|
|
|
}
|
2005-05-16 09:28:59 -04:00
|
|
|
if (FL_TEST(orig, FL_SINGLETON)) {
|
|
|
|
rb_raise(rb_eTypeError, "can't copy singleton class");
|
|
|
|
}
|
2003-08-06 17:50:06 -04:00
|
|
|
return rb_mod_init_copy(clone, orig);
|
2001-05-02 00:22:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 06:44:21 -04:00
|
|
|
rb_singleton_class_clone(VALUE obj)
|
2001-05-02 00:22:21 -04:00
|
|
|
{
|
2002-09-03 01:20:14 -04:00
|
|
|
VALUE klass = RBASIC(obj)->klass;
|
2001-05-02 00:22:21 -04:00
|
|
|
|
1998-01-16 07:19:22 -05:00
|
|
|
if (!FL_TEST(klass, FL_SINGLETON))
|
|
|
|
return klass;
|
1998-01-16 07:13:05 -05:00
|
|
|
else {
|
|
|
|
/* copy singleton(unnamed) class */
|
2010-05-28 07:13:42 -04:00
|
|
|
VALUE clone = class_alloc((RBASIC(klass)->flags & ~(FL_MARK)), 0);
|
2002-09-03 01:20:14 -04:00
|
|
|
|
|
|
|
if (BUILTIN_TYPE(obj) == T_CLASS) {
|
2011-05-31 12:16:06 -04:00
|
|
|
RBASIC(clone)->klass = clone;
|
2002-09-03 01:20:14 -04:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
RBASIC(clone)->klass = rb_singleton_class_clone(klass);
|
|
|
|
}
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2007-09-28 02:21:46 -04:00
|
|
|
RCLASS_SUPER(clone) = RCLASS_SUPER(klass);
|
|
|
|
if (RCLASS_IV_TBL(klass)) {
|
|
|
|
RCLASS_IV_TBL(clone) = st_copy(RCLASS_IV_TBL(klass));
|
2001-03-28 03:43:25 -05:00
|
|
|
}
|
2010-10-26 13:27:21 -04:00
|
|
|
if (RCLASS_CONST_TBL(klass)) {
|
2010-10-26 13:27:32 -04:00
|
|
|
RCLASS_CONST_TBL(clone) = st_init_numtable();
|
2011-06-03 23:49:50 -04:00
|
|
|
st_foreach(RCLASS_CONST_TBL(klass), clone_const_i, (st_data_t)RCLASS_CONST_TBL(clone));
|
2010-10-26 13:27:21 -04:00
|
|
|
}
|
2007-09-28 02:21:46 -04:00
|
|
|
RCLASS_M_TBL(clone) = st_init_numtable();
|
2011-05-31 12:16:06 -04:00
|
|
|
st_foreach(RCLASS_M_TBL(klass), clone_method_i, (st_data_t)clone);
|
|
|
|
rb_singleton_class_attached(RBASIC(clone)->klass, clone);
|
1998-01-16 07:13:05 -05:00
|
|
|
FL_SET(clone, FL_SINGLETON);
|
2011-05-31 12:16:06 -04:00
|
|
|
return clone;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-08-31 01:55:57 -04:00
|
|
|
/*!
|
|
|
|
* Attach a object to a singleton class.
|
|
|
|
* @pre \a klass is the singleton class of \a obj.
|
|
|
|
*/
|
1998-01-16 07:19:22 -05:00
|
|
|
void
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 06:44:21 -04:00
|
|
|
rb_singleton_class_attached(VALUE klass, VALUE obj)
|
1998-01-16 07:19:22 -05:00
|
|
|
{
|
2001-05-02 00:22:21 -04:00
|
|
|
if (FL_TEST(klass, FL_SINGLETON)) {
|
2007-09-28 02:21:46 -04:00
|
|
|
if (!RCLASS_IV_TBL(klass)) {
|
|
|
|
RCLASS_IV_TBL(klass) = st_init_numtable();
|
2001-05-02 00:22:21 -04:00
|
|
|
}
|
2009-08-31 01:55:57 -04:00
|
|
|
st_insert(RCLASS_IV_TBL(klass), id_attached, obj);
|
2001-05-02 00:22:21 -04:00
|
|
|
}
|
1998-01-16 07:19:22 -05:00
|
|
|
}
|
|
|
|
|
* test/ruby/test_metaclass.rb: new test case for metaclass hierarchy.
* class.c (make_metametaclass): new function. extracted from
rb_make_metaclass.
* class.c (rb_make_metaclass): uses make_metametaclass when called for a
metaclass.
* class.c (rb_singleton_class): creates a meta^(n+2)-class in
addition to a meta^(n+1)-class when called for a meta^(n)-class.
This is because the returned meta^(n+1) class must acts as an instance of
Class, metaclass of Class, ..., meta^(n+1)-class of Class,
Module, metaclass of Module, ..., meta^(n+1)-class of Module,
Object, metaclass of Object, ..., meta^(n+2)-class of Object,
BasicObject, metaclass of BasicObject, ..., meta^(n+2)-class of
and BasicObject even when Class, Module, Object or BasicObject has
not have its meta^(i)-class yet.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@20747 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-12-15 01:23:43 -05:00
|
|
|
|
2009-08-31 01:55:57 -04:00
|
|
|
|
2010-05-29 14:51:39 -04:00
|
|
|
#define METACLASS_OF(k) RBASIC(k)->klass
|
2009-08-31 01:55:57 -04:00
|
|
|
|
2009-06-29 12:39:28 -04:00
|
|
|
/*!
|
2009-08-31 01:55:57 -04:00
|
|
|
* whether k is a meta^(n)-class of Class class
|
|
|
|
* @retval 1 if \a k is a meta^(n)-class of Class class (n >= 0)
|
|
|
|
* @retval 0 otherwise
|
2009-06-29 12:39:28 -04:00
|
|
|
*/
|
2010-12-03 21:18:42 -05:00
|
|
|
#define META_CLASS_OF_CLASS_CLASS_P(k) (METACLASS_OF(k) == (k))
|
2009-08-31 01:55:57 -04:00
|
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* ensures \a klass belongs to its own eigenclass.
|
|
|
|
* @return the eigenclass of \a klass
|
|
|
|
* @post \a klass belongs to the returned eigenclass.
|
|
|
|
* i.e. the attached object of the eigenclass is \a klass.
|
|
|
|
* @note this macro creates a new eigenclass if necessary.
|
|
|
|
*/
|
|
|
|
#define ENSURE_EIGENCLASS(klass) \
|
2010-12-03 21:18:42 -05:00
|
|
|
(rb_ivar_get(METACLASS_OF(klass), id_attached) == (klass) ? METACLASS_OF(klass) : make_metaclass(klass))
|
2009-08-31 01:55:57 -04:00
|
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* Creates a metaclass of \a klass
|
|
|
|
* \param klass a class
|
|
|
|
* \return created metaclass for the class
|
|
|
|
* \pre \a klass is a Class object
|
|
|
|
* \pre \a klass has no singleton class.
|
|
|
|
* \post the class of \a klass is the returned class.
|
|
|
|
* \post the returned class is meta^(n+1)-class when \a klass is a meta^(n)-klass for n >= 0
|
|
|
|
*/
|
|
|
|
static inline VALUE
|
|
|
|
make_metaclass(VALUE klass)
|
* test/ruby/test_metaclass.rb: new test case for metaclass hierarchy.
* class.c (make_metametaclass): new function. extracted from
rb_make_metaclass.
* class.c (rb_make_metaclass): uses make_metametaclass when called for a
metaclass.
* class.c (rb_singleton_class): creates a meta^(n+2)-class in
addition to a meta^(n+1)-class when called for a meta^(n)-class.
This is because the returned meta^(n+1) class must acts as an instance of
Class, metaclass of Class, ..., meta^(n+1)-class of Class,
Module, metaclass of Module, ..., meta^(n+1)-class of Module,
Object, metaclass of Object, ..., meta^(n+2)-class of Object,
BasicObject, metaclass of BasicObject, ..., meta^(n+2)-class of
and BasicObject even when Class, Module, Object or BasicObject has
not have its meta^(i)-class yet.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@20747 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-12-15 01:23:43 -05:00
|
|
|
{
|
2009-08-31 01:55:57 -04:00
|
|
|
VALUE super;
|
|
|
|
VALUE metaclass = rb_class_boot(Qundef);
|
* test/ruby/test_metaclass.rb: new test case for metaclass hierarchy.
* class.c (make_metametaclass): new function. extracted from
rb_make_metaclass.
* class.c (rb_make_metaclass): uses make_metametaclass when called for a
metaclass.
* class.c (rb_singleton_class): creates a meta^(n+2)-class in
addition to a meta^(n+1)-class when called for a meta^(n)-class.
This is because the returned meta^(n+1) class must acts as an instance of
Class, metaclass of Class, ..., meta^(n+1)-class of Class,
Module, metaclass of Module, ..., meta^(n+1)-class of Module,
Object, metaclass of Object, ..., meta^(n+2)-class of Object,
BasicObject, metaclass of BasicObject, ..., meta^(n+2)-class of
and BasicObject even when Class, Module, Object or BasicObject has
not have its meta^(i)-class yet.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@20747 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-12-15 01:23:43 -05:00
|
|
|
|
2009-08-31 01:55:57 -04:00
|
|
|
FL_SET(metaclass, FL_SINGLETON);
|
|
|
|
rb_singleton_class_attached(metaclass, klass);
|
|
|
|
|
|
|
|
if (META_CLASS_OF_CLASS_CLASS_P(klass)) {
|
|
|
|
METACLASS_OF(klass) = METACLASS_OF(metaclass) = metaclass;
|
* test/ruby/test_metaclass.rb: new test case for metaclass hierarchy.
* class.c (make_metametaclass): new function. extracted from
rb_make_metaclass.
* class.c (rb_make_metaclass): uses make_metametaclass when called for a
metaclass.
* class.c (rb_singleton_class): creates a meta^(n+2)-class in
addition to a meta^(n+1)-class when called for a meta^(n)-class.
This is because the returned meta^(n+1) class must acts as an instance of
Class, metaclass of Class, ..., meta^(n+1)-class of Class,
Module, metaclass of Module, ..., meta^(n+1)-class of Module,
Object, metaclass of Object, ..., meta^(n+2)-class of Object,
BasicObject, metaclass of BasicObject, ..., meta^(n+2)-class of
and BasicObject even when Class, Module, Object or BasicObject has
not have its meta^(i)-class yet.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@20747 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-12-15 01:23:43 -05:00
|
|
|
}
|
|
|
|
else {
|
2009-08-31 01:55:57 -04:00
|
|
|
VALUE tmp = METACLASS_OF(klass); /* for a meta^(n)-class klass, tmp is meta^(n)-class of Class class */
|
|
|
|
METACLASS_OF(klass) = metaclass;
|
|
|
|
METACLASS_OF(metaclass) = ENSURE_EIGENCLASS(tmp);
|
* test/ruby/test_metaclass.rb: new test case for metaclass hierarchy.
* class.c (make_metametaclass): new function. extracted from
rb_make_metaclass.
* class.c (rb_make_metaclass): uses make_metametaclass when called for a
metaclass.
* class.c (rb_singleton_class): creates a meta^(n+2)-class in
addition to a meta^(n+1)-class when called for a meta^(n)-class.
This is because the returned meta^(n+1) class must acts as an instance of
Class, metaclass of Class, ..., meta^(n+1)-class of Class,
Module, metaclass of Module, ..., meta^(n+1)-class of Module,
Object, metaclass of Object, ..., meta^(n+2)-class of Object,
BasicObject, metaclass of BasicObject, ..., meta^(n+2)-class of
and BasicObject even when Class, Module, Object or BasicObject has
not have its meta^(i)-class yet.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@20747 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-12-15 01:23:43 -05:00
|
|
|
}
|
|
|
|
|
2009-08-31 01:55:57 -04:00
|
|
|
super = RCLASS_SUPER(klass);
|
2010-12-05 03:56:42 -05:00
|
|
|
while (RB_TYPE_P(super, T_ICLASS)) super = RCLASS_SUPER(super);
|
2009-08-31 01:55:57 -04:00
|
|
|
RCLASS_SUPER(metaclass) = super ? ENSURE_EIGENCLASS(super) : rb_cClass;
|
|
|
|
|
|
|
|
OBJ_INFECT(metaclass, RCLASS_SUPER(metaclass));
|
|
|
|
|
|
|
|
return metaclass;
|
|
|
|
}
|
* test/ruby/test_metaclass.rb: new test case for metaclass hierarchy.
* class.c (make_metametaclass): new function. extracted from
rb_make_metaclass.
* class.c (rb_make_metaclass): uses make_metametaclass when called for a
metaclass.
* class.c (rb_singleton_class): creates a meta^(n+2)-class in
addition to a meta^(n+1)-class when called for a meta^(n)-class.
This is because the returned meta^(n+1) class must acts as an instance of
Class, metaclass of Class, ..., meta^(n+1)-class of Class,
Module, metaclass of Module, ..., meta^(n+1)-class of Module,
Object, metaclass of Object, ..., meta^(n+2)-class of Object,
BasicObject, metaclass of BasicObject, ..., meta^(n+2)-class of
and BasicObject even when Class, Module, Object or BasicObject has
not have its meta^(i)-class yet.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@20747 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-12-15 01:23:43 -05:00
|
|
|
|
2009-08-31 01:55:57 -04:00
|
|
|
/*!
|
|
|
|
* Creates a singleton class for \a obj.
|
|
|
|
* \pre \a obj must not a immediate nor a special const.
|
|
|
|
* \pre \a obj must not a Class object.
|
|
|
|
* \pre \a obj has no singleton class.
|
|
|
|
*/
|
|
|
|
static inline VALUE
|
|
|
|
make_singleton_class(VALUE obj)
|
|
|
|
{
|
2009-08-31 03:21:54 -04:00
|
|
|
VALUE orig_class = RBASIC(obj)->klass;
|
|
|
|
VALUE klass = rb_class_boot(orig_class);
|
2009-08-31 01:55:57 -04:00
|
|
|
|
|
|
|
FL_SET(klass, FL_SINGLETON);
|
|
|
|
RBASIC(obj)->klass = klass;
|
|
|
|
rb_singleton_class_attached(klass, obj);
|
|
|
|
|
2009-08-31 03:21:54 -04:00
|
|
|
METACLASS_OF(klass) = METACLASS_OF(rb_class_real(orig_class));
|
2009-08-31 01:55:57 -04:00
|
|
|
return klass;
|
|
|
|
}
|
* test/ruby/test_metaclass.rb: new test case for metaclass hierarchy.
* class.c (make_metametaclass): new function. extracted from
rb_make_metaclass.
* class.c (rb_make_metaclass): uses make_metametaclass when called for a
metaclass.
* class.c (rb_singleton_class): creates a meta^(n+2)-class in
addition to a meta^(n+1)-class when called for a meta^(n)-class.
This is because the returned meta^(n+1) class must acts as an instance of
Class, metaclass of Class, ..., meta^(n+1)-class of Class,
Module, metaclass of Module, ..., meta^(n+1)-class of Module,
Object, metaclass of Object, ..., meta^(n+2)-class of Object,
BasicObject, metaclass of BasicObject, ..., meta^(n+2)-class of
and BasicObject even when Class, Module, Object or BasicObject has
not have its meta^(i)-class yet.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@20747 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-12-15 01:23:43 -05:00
|
|
|
|
2009-08-31 01:55:57 -04:00
|
|
|
|
|
|
|
static VALUE
|
|
|
|
boot_defclass(const char *name, VALUE super)
|
|
|
|
{
|
|
|
|
extern st_table *rb_class_tbl;
|
|
|
|
VALUE obj = rb_class_boot(super);
|
|
|
|
ID id = rb_intern(name);
|
|
|
|
|
|
|
|
rb_name_class(obj, id);
|
|
|
|
st_add_direct(rb_class_tbl, id, obj);
|
|
|
|
rb_const_set((rb_cObject ? rb_cObject : obj), id, obj);
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Init_class_hierarchy(void)
|
|
|
|
{
|
|
|
|
id_attached = rb_intern("__attached__");
|
|
|
|
|
|
|
|
rb_cBasicObject = boot_defclass("BasicObject", 0);
|
|
|
|
rb_cObject = boot_defclass("Object", rb_cBasicObject);
|
|
|
|
rb_cModule = boot_defclass("Module", rb_cObject);
|
|
|
|
rb_cClass = boot_defclass("Class", rb_cModule);
|
|
|
|
|
2011-06-30 14:34:09 -04:00
|
|
|
rb_const_set(rb_cObject, rb_intern("BasicObject"), rb_cBasicObject);
|
2010-05-29 14:51:39 -04:00
|
|
|
RBASIC(rb_cClass)->klass
|
2009-08-31 01:55:57 -04:00
|
|
|
= RBASIC(rb_cModule)->klass
|
|
|
|
= RBASIC(rb_cObject)->klass
|
|
|
|
= RBASIC(rb_cBasicObject)->klass
|
|
|
|
= rb_cClass;
|
* test/ruby/test_metaclass.rb: new test case for metaclass hierarchy.
* class.c (make_metametaclass): new function. extracted from
rb_make_metaclass.
* class.c (rb_make_metaclass): uses make_metametaclass when called for a
metaclass.
* class.c (rb_singleton_class): creates a meta^(n+2)-class in
addition to a meta^(n+1)-class when called for a meta^(n)-class.
This is because the returned meta^(n+1) class must acts as an instance of
Class, metaclass of Class, ..., meta^(n+1)-class of Class,
Module, metaclass of Module, ..., meta^(n+1)-class of Module,
Object, metaclass of Object, ..., meta^(n+2)-class of Object,
BasicObject, metaclass of BasicObject, ..., meta^(n+2)-class of
and BasicObject even when Class, Module, Object or BasicObject has
not have its meta^(i)-class yet.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@20747 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-12-15 01:23:43 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-06-29 12:39:28 -04:00
|
|
|
/*!
|
|
|
|
* \internal
|
2009-08-31 01:55:57 -04:00
|
|
|
* Creates a new *singleton class* for an object.
|
2009-06-29 12:39:28 -04:00
|
|
|
*
|
2009-08-31 01:55:57 -04:00
|
|
|
* \pre \a obj has no singleton class.
|
|
|
|
* \note DO NOT USE the function in an extension libraries. Use \ref rb_singleton_class.
|
|
|
|
* \param obj An object.
|
|
|
|
* \param unused ignored.
|
|
|
|
* \return The singleton class of the object.
|
2009-06-29 12:39:28 -04:00
|
|
|
*/
|
2002-01-10 15:18:39 -05:00
|
|
|
VALUE
|
2009-08-31 01:55:57 -04:00
|
|
|
rb_make_metaclass(VALUE obj, VALUE unused)
|
2002-01-10 15:18:39 -05:00
|
|
|
{
|
2009-08-31 01:55:57 -04:00
|
|
|
if (BUILTIN_TYPE(obj) == T_CLASS) {
|
|
|
|
return make_metaclass(obj);
|
2002-09-25 03:03:05 -04:00
|
|
|
}
|
|
|
|
else {
|
2009-08-31 01:55:57 -04:00
|
|
|
return make_singleton_class(obj);
|
2002-09-03 01:20:14 -04:00
|
|
|
}
|
2002-01-10 15:18:39 -05:00
|
|
|
}
|
|
|
|
|
2009-08-29 09:39:44 -04:00
|
|
|
|
|
|
|
/*!
|
|
|
|
* Defines a new class.
|
|
|
|
* \param id ignored
|
|
|
|
* \param super A class from which the new class will derive. NULL means \c Object class.
|
|
|
|
* \return the created class
|
|
|
|
* \throw TypeError if super is not a \c Class object.
|
|
|
|
*
|
|
|
|
* \note the returned class will not be associated with \a id.
|
|
|
|
* You must explicitly set a class name if necessary.
|
|
|
|
*/
|
1998-01-16 07:13:05 -05:00
|
|
|
VALUE
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 06:44:21 -04:00
|
|
|
rb_define_class_id(ID id, VALUE super)
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE klass;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
if (!super) super = rb_cObject;
|
|
|
|
klass = rb_class_new(super);
|
2002-01-10 15:18:39 -05:00
|
|
|
rb_make_metaclass(klass, RBASIC(super)->klass);
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
return klass;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
2009-08-29 09:39:44 -04:00
|
|
|
|
|
|
|
/*!
|
|
|
|
* Calls Class#inherited.
|
|
|
|
* \param super A class which will be called #inherited.
|
|
|
|
* NULL means Object class.
|
|
|
|
* \param klass A Class object which derived from \a super
|
|
|
|
* \return the value \c Class#inherited's returns
|
|
|
|
* \pre Each of \a super and \a klass must be a \c Class object.
|
|
|
|
*/
|
2002-01-10 15:18:39 -05:00
|
|
|
VALUE
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 06:44:21 -04:00
|
|
|
rb_class_inherited(VALUE super, VALUE klass)
|
2002-01-10 15:18:39 -05:00
|
|
|
{
|
2008-06-09 05:25:32 -04:00
|
|
|
ID inherited;
|
2002-01-15 21:20:25 -05:00
|
|
|
if (!super) super = rb_cObject;
|
2008-06-09 05:25:32 -04:00
|
|
|
CONST_ID(inherited, "inherited");
|
|
|
|
return rb_funcall(super, inherited, 1, klass);
|
2002-01-10 15:18:39 -05:00
|
|
|
}
|
|
|
|
|
2009-08-29 09:39:44 -04:00
|
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* Defines a top-level class.
|
|
|
|
* \param name name of the class
|
2010-05-29 14:51:39 -04:00
|
|
|
* \param super a class from which the new class will derive.
|
2009-08-29 09:39:44 -04:00
|
|
|
* NULL means \c Object class.
|
|
|
|
* \return the created class
|
2010-05-29 14:51:39 -04:00
|
|
|
* \throw TypeError if the constant name \a name is already taken but
|
2009-08-29 09:39:44 -04:00
|
|
|
* the constant is not a \c Class.
|
|
|
|
* \throw NameError if the class is already defined but the class can not
|
|
|
|
* be reopened because its superclass is not \a super.
|
|
|
|
* \post top-level constant named \a name refers the returned class.
|
|
|
|
*
|
|
|
|
* \note if a class named \a name is already defined and its superclass is
|
|
|
|
* \a super, the function just returns the defined class.
|
|
|
|
*/
|
1998-01-16 07:13:05 -05:00
|
|
|
VALUE
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 06:44:21 -04:00
|
|
|
rb_define_class(const char *name, VALUE super)
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE klass;
|
1998-01-16 07:13:05 -05:00
|
|
|
ID id;
|
|
|
|
|
|
|
|
id = rb_intern(name);
|
2001-05-02 00:22:21 -04:00
|
|
|
if (rb_const_defined(rb_cObject, id)) {
|
|
|
|
klass = rb_const_get(rb_cObject, id);
|
2001-12-18 03:47:06 -05:00
|
|
|
if (TYPE(klass) != T_CLASS) {
|
|
|
|
rb_raise(rb_eTypeError, "%s is not a class", name);
|
|
|
|
}
|
2007-09-28 02:21:46 -04:00
|
|
|
if (rb_class_real(RCLASS_SUPER(klass)) != super) {
|
2010-01-09 14:54:38 -05:00
|
|
|
rb_raise(rb_eTypeError, "superclass mismatch for class %s", name);
|
2001-12-18 03:47:06 -05:00
|
|
|
}
|
|
|
|
return klass;
|
2001-05-02 00:22:21 -04:00
|
|
|
}
|
2002-02-17 09:44:14 -05:00
|
|
|
if (!super) {
|
|
|
|
rb_warn("no super class for `%s', Object assumed", name);
|
|
|
|
}
|
1998-01-16 07:19:22 -05:00
|
|
|
klass = rb_define_class_id(id, super);
|
|
|
|
st_add_direct(rb_class_tbl, id, klass);
|
2004-01-19 04:19:31 -05:00
|
|
|
rb_name_class(klass, id);
|
2003-06-30 21:36:25 -04:00
|
|
|
rb_const_set(rb_cObject, id, klass);
|
2003-01-07 02:36:40 -05:00
|
|
|
rb_class_inherited(super, klass);
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1998-01-16 07:19:22 -05:00
|
|
|
return klass;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
2009-08-29 09:39:44 -04:00
|
|
|
|
|
|
|
/*!
|
|
|
|
* Defines a class under the namespace of \a outer.
|
|
|
|
* \param outer a class which contains the new class.
|
|
|
|
* \param name name of the new class
|
2010-05-29 14:51:39 -04:00
|
|
|
* \param super a class from which the new class will derive.
|
2009-08-29 09:39:44 -04:00
|
|
|
* NULL means \c Object class.
|
|
|
|
* \return the created class
|
2010-05-29 14:51:39 -04:00
|
|
|
* \throw TypeError if the constant name \a name is already taken but
|
2009-08-29 09:39:44 -04:00
|
|
|
* the constant is not a \c Class.
|
|
|
|
* \throw NameError if the class is already defined but the class can not
|
|
|
|
* be reopened because its superclass is not \a super.
|
|
|
|
* \post top-level constant named \a name refers the returned class.
|
|
|
|
*
|
|
|
|
* \note if a class named \a name is already defined and its superclass is
|
|
|
|
* \a super, the function just returns the defined class.
|
|
|
|
*/
|
1998-01-16 07:13:05 -05:00
|
|
|
VALUE
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 06:44:21 -04:00
|
|
|
rb_define_class_under(VALUE outer, const char *name, VALUE super)
|
2009-08-12 02:32:21 -04:00
|
|
|
{
|
|
|
|
return rb_define_class_id_under(outer, rb_intern(name), super);
|
|
|
|
}
|
|
|
|
|
2009-08-29 09:39:44 -04:00
|
|
|
|
|
|
|
/*!
|
|
|
|
* Defines a class under the namespace of \a outer.
|
|
|
|
* \param outer a class which contains the new class.
|
|
|
|
* \param id name of the new class
|
2010-05-29 14:51:39 -04:00
|
|
|
* \param super a class from which the new class will derive.
|
2009-08-29 09:39:44 -04:00
|
|
|
* NULL means \c Object class.
|
|
|
|
* \return the created class
|
2010-05-29 14:51:39 -04:00
|
|
|
* \throw TypeError if the constant name \a name is already taken but
|
2009-08-29 09:39:44 -04:00
|
|
|
* the constant is not a \c Class.
|
|
|
|
* \throw NameError if the class is already defined but the class can not
|
|
|
|
* be reopened because its superclass is not \a super.
|
|
|
|
* \post top-level constant named \a name refers the returned class.
|
|
|
|
*
|
|
|
|
* \note if a class named \a name is already defined and its superclass is
|
|
|
|
* \a super, the function just returns the defined class.
|
|
|
|
*/
|
2009-08-12 02:32:21 -04:00
|
|
|
VALUE
|
|
|
|
rb_define_class_id_under(VALUE outer, ID id, VALUE super)
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE klass;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2001-05-02 00:22:21 -04:00
|
|
|
if (rb_const_defined_at(outer, id)) {
|
2003-06-20 03:11:44 -04:00
|
|
|
klass = rb_const_get_at(outer, id);
|
2001-12-18 03:47:06 -05:00
|
|
|
if (TYPE(klass) != T_CLASS) {
|
2009-08-12 02:32:21 -04:00
|
|
|
rb_raise(rb_eTypeError, "%s is not a class", rb_id2name(id));
|
2001-12-18 03:47:06 -05:00
|
|
|
}
|
2007-09-28 02:21:46 -04:00
|
|
|
if (rb_class_real(RCLASS_SUPER(klass)) != super) {
|
2009-08-12 02:32:21 -04:00
|
|
|
rb_name_error(id, "%s is already defined", rb_id2name(id));
|
2001-12-18 03:47:06 -05:00
|
|
|
}
|
|
|
|
return klass;
|
2001-05-02 00:22:21 -04:00
|
|
|
}
|
2002-02-17 09:44:14 -05:00
|
|
|
if (!super) {
|
|
|
|
rb_warn("no super class for `%s::%s', Object assumed",
|
2009-08-12 02:32:21 -04:00
|
|
|
rb_class2name(outer), rb_id2name(id));
|
2002-02-17 09:44:14 -05:00
|
|
|
}
|
1998-01-16 07:19:22 -05:00
|
|
|
klass = rb_define_class_id(id, super);
|
2009-08-12 02:32:21 -04:00
|
|
|
rb_set_class_path_string(klass, outer, rb_id2str(id));
|
2002-01-10 15:18:39 -05:00
|
|
|
rb_const_set(outer, id, klass);
|
2003-01-07 02:36:40 -05:00
|
|
|
rb_class_inherited(super, klass);
|
2010-10-16 21:40:22 -04:00
|
|
|
rb_gc_register_mark_object(klass);
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1998-01-16 07:19:22 -05:00
|
|
|
return klass;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 06:44:21 -04:00
|
|
|
rb_module_new(void)
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
2007-09-28 02:21:46 -04:00
|
|
|
VALUE mdl = class_alloc(T_MODULE, rb_cModule);
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2007-09-28 02:21:46 -04:00
|
|
|
RCLASS_M_TBL(mdl) = st_init_numtable();
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
return (VALUE)mdl;
|
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 06:44:21 -04:00
|
|
|
rb_define_module_id(ID id)
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
1999-01-19 23:59:39 -05:00
|
|
|
VALUE mdl;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
mdl = rb_module_new();
|
1998-01-16 07:13:05 -05:00
|
|
|
rb_name_class(mdl, id);
|
|
|
|
|
1998-01-16 07:19:22 -05:00
|
|
|
return mdl;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 06:44:21 -04:00
|
|
|
rb_define_module(const char *name)
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
|
|
|
VALUE module;
|
|
|
|
ID id;
|
|
|
|
|
|
|
|
id = rb_intern(name);
|
2001-05-02 00:22:21 -04:00
|
|
|
if (rb_const_defined(rb_cObject, id)) {
|
|
|
|
module = rb_const_get(rb_cObject, id);
|
|
|
|
if (TYPE(module) == T_MODULE)
|
|
|
|
return module;
|
2003-01-30 23:00:17 -05:00
|
|
|
rb_raise(rb_eTypeError, "%s is not a module", rb_obj_classname(module));
|
2001-05-02 00:22:21 -04:00
|
|
|
}
|
1998-01-16 07:13:05 -05:00
|
|
|
module = rb_define_module_id(id);
|
|
|
|
st_add_direct(rb_class_tbl, id, module);
|
2003-06-30 21:36:25 -04:00
|
|
|
rb_const_set(rb_cObject, id, module);
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
return module;
|
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 06:44:21 -04:00
|
|
|
rb_define_module_under(VALUE outer, const char *name)
|
2009-08-12 02:32:21 -04:00
|
|
|
{
|
|
|
|
return rb_define_module_id_under(outer, rb_intern(name));
|
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
2009-08-12 02:54:45 -04:00
|
|
|
rb_define_module_id_under(VALUE outer, ID id)
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
|
|
|
VALUE module;
|
|
|
|
|
2002-01-07 00:27:01 -05:00
|
|
|
if (rb_const_defined_at(outer, id)) {
|
2003-06-20 03:11:44 -04:00
|
|
|
module = rb_const_get_at(outer, id);
|
2001-05-02 00:22:21 -04:00
|
|
|
if (TYPE(module) == T_MODULE)
|
|
|
|
return module;
|
|
|
|
rb_raise(rb_eTypeError, "%s::%s is not a module",
|
2003-01-30 23:00:17 -05:00
|
|
|
rb_class2name(outer), rb_obj_classname(module));
|
2001-05-02 00:22:21 -04:00
|
|
|
}
|
1998-01-16 07:13:05 -05:00
|
|
|
module = rb_define_module_id(id);
|
1999-12-14 01:50:43 -05:00
|
|
|
rb_const_set(outer, id, module);
|
2009-08-12 02:54:45 -04:00
|
|
|
rb_set_class_path_string(module, outer, rb_id2str(id));
|
2010-10-16 21:40:22 -04:00
|
|
|
rb_gc_register_mark_object(module);
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
return module;
|
|
|
|
}
|
|
|
|
|
1998-01-16 07:19:22 -05:00
|
|
|
static VALUE
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 06:44:21 -04:00
|
|
|
include_class_new(VALUE module, VALUE super)
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
2007-09-28 02:21:46 -04:00
|
|
|
VALUE klass = class_alloc(T_ICLASS, rb_cClass);
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2002-07-11 04:22:18 -04:00
|
|
|
if (BUILTIN_TYPE(module) == T_ICLASS) {
|
|
|
|
module = RBASIC(module)->klass;
|
|
|
|
}
|
2007-09-28 02:21:46 -04:00
|
|
|
if (!RCLASS_IV_TBL(module)) {
|
|
|
|
RCLASS_IV_TBL(module) = st_init_numtable();
|
1999-08-13 01:45:20 -04:00
|
|
|
}
|
2010-10-26 13:27:21 -04:00
|
|
|
if (!RCLASS_CONST_TBL(module)) {
|
|
|
|
RCLASS_CONST_TBL(module) = st_init_numtable();
|
|
|
|
}
|
2007-09-28 02:21:46 -04:00
|
|
|
RCLASS_IV_TBL(klass) = RCLASS_IV_TBL(module);
|
2010-10-26 13:27:21 -04:00
|
|
|
RCLASS_CONST_TBL(klass) = RCLASS_CONST_TBL(module);
|
2007-09-28 02:21:46 -04:00
|
|
|
RCLASS_M_TBL(klass) = RCLASS_M_TBL(module);
|
|
|
|
RCLASS_SUPER(klass) = super;
|
1998-01-16 07:13:05 -05:00
|
|
|
if (TYPE(module) == T_ICLASS) {
|
1999-01-19 23:59:39 -05:00
|
|
|
RBASIC(klass)->klass = RBASIC(module)->klass;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
else {
|
1999-01-19 23:59:39 -05:00
|
|
|
RBASIC(klass)->klass = module;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
2001-08-14 04:13:31 -04:00
|
|
|
OBJ_INFECT(klass, module);
|
|
|
|
OBJ_INFECT(klass, super);
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1998-01-16 07:19:22 -05:00
|
|
|
return (VALUE)klass;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 06:44:21 -04:00
|
|
|
rb_include_module(VALUE klass, VALUE module)
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
2006-11-07 03:56:18 -05:00
|
|
|
VALUE p, c;
|
2001-03-28 03:43:25 -05:00
|
|
|
int changed = 0;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2001-05-02 00:22:21 -04:00
|
|
|
rb_frozen_class_p(klass);
|
* object.c (rb_obj_untrusted): new method Object#untrusted?.
(rb_obj_untrust): new method Object#untrust.
(rb_obj_trust): new method Object#trust.
* array.c, debug.c, time.c, include/ruby/ruby.h, re.c, variable.c,
string.c, io.c, dir.c, vm_method.c, struct.c, class.c, hash.c,
ruby.c, marshal.c: fixes for Object#untrusted?.
* test/ruby/test_module.rb, test/ruby/test_array.rb,
test/ruby/test_object.rb, test/ruby/test_string.rb,
test/ruby/test_marshal.rb, test/ruby/test_hash.rb: added tests for
Object#untrusted?.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@18568 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-08-13 03:25:05 -04:00
|
|
|
if (!OBJ_UNTRUSTED(klass)) {
|
2001-05-02 00:22:21 -04:00
|
|
|
rb_secure(4);
|
|
|
|
}
|
2009-02-22 09:23:33 -05:00
|
|
|
|
2002-12-19 04:20:20 -05:00
|
|
|
if (TYPE(module) != T_MODULE) {
|
1998-01-16 07:13:05 -05:00
|
|
|
Check_Type(module, T_MODULE);
|
|
|
|
}
|
|
|
|
|
2001-08-14 04:13:31 -04:00
|
|
|
OBJ_INFECT(klass, module);
|
2001-09-08 10:17:53 -04:00
|
|
|
c = klass;
|
1998-01-16 07:13:05 -05:00
|
|
|
while (module) {
|
2009-07-18 04:05:32 -04:00
|
|
|
int superclass_seen = FALSE;
|
2006-11-07 03:56:18 -05:00
|
|
|
|
2007-09-28 02:21:46 -04:00
|
|
|
if (RCLASS_M_TBL(klass) == RCLASS_M_TBL(module))
|
2002-01-25 03:22:11 -05:00
|
|
|
rb_raise(rb_eArgError, "cyclic include detected");
|
2009-03-13 01:55:13 -04:00
|
|
|
/* ignore if the module included already in superclasses */
|
|
|
|
for (p = RCLASS_SUPER(klass); p; p = RCLASS_SUPER(p)) {
|
|
|
|
switch (BUILTIN_TYPE(p)) {
|
|
|
|
case T_ICLASS:
|
|
|
|
if (RCLASS_M_TBL(p) == RCLASS_M_TBL(module)) {
|
|
|
|
if (!superclass_seen) {
|
|
|
|
c = p; /* move insertion point */
|
|
|
|
}
|
|
|
|
goto skip;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case T_CLASS:
|
2009-07-18 04:05:32 -04:00
|
|
|
superclass_seen = TRUE;
|
2009-03-13 01:55:13 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
c = RCLASS_SUPER(c) = include_class_new(module, RCLASS_SUPER(c));
|
2010-07-31 20:46:18 -04:00
|
|
|
if (RMODULE_M_TBL(module) && RMODULE_M_TBL(module)->num_entries)
|
|
|
|
changed = 1;
|
2006-11-07 03:56:18 -05:00
|
|
|
skip:
|
2007-09-28 02:21:46 -04:00
|
|
|
module = RCLASS_SUPER(module);
|
1998-01-16 07:19:22 -05:00
|
|
|
}
|
2001-03-28 03:43:25 -05:00
|
|
|
if (changed) rb_clear_cache();
|
1998-01-16 07:19:22 -05:00
|
|
|
}
|
|
|
|
|
2011-05-31 12:16:06 -04:00
|
|
|
struct mixing_arg {
|
|
|
|
st_table *mtbl;
|
|
|
|
ID id;
|
|
|
|
st_table *aliasing;
|
|
|
|
VALUE klass;
|
|
|
|
};
|
|
|
|
|
|
|
|
static int
|
|
|
|
check_mix_const_i(st_data_t key, st_data_t value, st_data_t arg)
|
|
|
|
{
|
|
|
|
struct mixing_arg *argp = (struct mixing_arg *)arg;
|
|
|
|
ID id = (ID)key;
|
|
|
|
st_table *aliasing = argp->aliasing;
|
|
|
|
st_data_t alias;
|
|
|
|
|
|
|
|
if (!rb_is_const_id(id)) return ST_CONTINUE;
|
|
|
|
if (aliasing && st_lookup(aliasing, ID2SYM(id), &alias)) {
|
|
|
|
id = rb_to_id(alias);
|
|
|
|
}
|
|
|
|
if (st_lookup(argp->mtbl, id, NULL)) {
|
|
|
|
argp->id = id;
|
|
|
|
return ST_STOP;
|
|
|
|
}
|
|
|
|
return ST_CONTINUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
do_mix_const_i(st_data_t key, st_data_t value, st_data_t arg)
|
|
|
|
{
|
|
|
|
struct mixing_arg *argp = (struct mixing_arg *)arg;
|
|
|
|
ID id = (ID)key;
|
|
|
|
st_table *aliasing = argp->aliasing;
|
|
|
|
st_data_t old, alias;
|
|
|
|
|
|
|
|
if (!rb_is_const_id(id)) return ST_CONTINUE;
|
|
|
|
if (aliasing && st_lookup(aliasing, ID2SYM(id), &alias)) {
|
|
|
|
id = rb_to_id(alias);
|
|
|
|
}
|
|
|
|
if (st_lookup(argp->mtbl, id, &old)) {
|
|
|
|
argp->id = id;
|
|
|
|
return ST_STOP;
|
|
|
|
}
|
|
|
|
st_insert(argp->mtbl, id, value);
|
|
|
|
return ST_CONTINUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
check_mix_method_i(st_data_t key, st_data_t value, st_data_t arg)
|
|
|
|
{
|
|
|
|
struct mixing_arg *argp = (struct mixing_arg *)arg;
|
|
|
|
ID id = (ID)key;
|
|
|
|
st_table *aliasing = argp->aliasing;
|
|
|
|
st_data_t alias;
|
|
|
|
|
|
|
|
if (aliasing && st_lookup(aliasing, ID2SYM(id), &alias)) {
|
2011-06-03 22:30:59 -04:00
|
|
|
if (NIL_P(alias)) return ST_CONTINUE;
|
2011-05-31 12:16:06 -04:00
|
|
|
id = rb_to_id(alias);
|
|
|
|
}
|
|
|
|
if (st_lookup(argp->mtbl, id, NULL)) {
|
|
|
|
argp->id = id;
|
|
|
|
return ST_STOP;
|
|
|
|
}
|
|
|
|
return ST_CONTINUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
do_mix_method_i(st_data_t key, st_data_t value, st_data_t arg)
|
|
|
|
{
|
|
|
|
struct mixing_arg *argp = (struct mixing_arg *)arg;
|
|
|
|
ID id = (ID)key;
|
|
|
|
st_table *aliasing = argp->aliasing;
|
|
|
|
st_data_t old, alias;
|
|
|
|
|
|
|
|
if (aliasing && st_lookup(aliasing, ID2SYM(id), &alias)) {
|
2011-06-03 22:30:59 -04:00
|
|
|
if (NIL_P(alias)) return ST_CONTINUE;
|
2011-05-31 12:16:06 -04:00
|
|
|
id = rb_to_id(alias);
|
|
|
|
}
|
|
|
|
if (st_lookup(argp->mtbl, id, &old)) {
|
|
|
|
argp->id = id;
|
|
|
|
return ST_STOP;
|
|
|
|
}
|
|
|
|
rb_mod_clone_method(argp->klass, id, (rb_method_entry_t *)value);
|
|
|
|
return ST_CONTINUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
rb_mix_module(VALUE klass, VALUE module, st_table *constants, st_table *methods)
|
|
|
|
{
|
|
|
|
st_table *mtbl_from;
|
|
|
|
struct mixing_arg methodarg, constarg;
|
|
|
|
|
|
|
|
rb_frozen_class_p(klass);
|
|
|
|
if (!OBJ_UNTRUSTED(klass)) {
|
|
|
|
rb_secure(4);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (TYPE(module) != T_MODULE) {
|
|
|
|
Check_Type(module, T_MODULE);
|
|
|
|
}
|
|
|
|
|
|
|
|
OBJ_INFECT(klass, module);
|
|
|
|
|
|
|
|
mtbl_from = RMODULE_M_TBL(module);
|
|
|
|
methodarg.mtbl = RMODULE_M_TBL(klass);
|
|
|
|
methodarg.id = 0;
|
|
|
|
methodarg.aliasing = methods;
|
|
|
|
methodarg.klass = klass;
|
|
|
|
constarg.mtbl = RMODULE_IV_TBL(klass);
|
|
|
|
constarg.id = 0;
|
|
|
|
constarg.aliasing = constants;
|
|
|
|
|
|
|
|
st_foreach(mtbl_from, check_mix_method_i, (st_data_t)&methodarg);
|
|
|
|
if (methodarg.id) {
|
|
|
|
rb_raise(rb_eArgError, "method would conflict - %s", rb_id2name(methodarg.id));
|
|
|
|
}
|
|
|
|
st_foreach(mtbl_from, check_mix_const_i, (st_data_t)&constarg);
|
|
|
|
if (constarg.id) {
|
|
|
|
rb_raise(rb_eArgError, "constant would conflict - %s", rb_id2name(constarg.id));
|
|
|
|
}
|
|
|
|
st_foreach(mtbl_from, do_mix_method_i, (st_data_t)&methodarg);
|
|
|
|
if (methodarg.id) {
|
|
|
|
rb_raise(rb_eArgError, "method would conflict - %s", rb_id2name(methodarg.id));
|
|
|
|
}
|
|
|
|
st_foreach(mtbl_from, do_mix_const_i, (st_data_t)&constarg);
|
|
|
|
if (constarg.id) {
|
|
|
|
rb_raise(rb_eArgError, "constant would conflict - %s", rb_id2name(constarg.id));
|
|
|
|
}
|
|
|
|
rb_vm_inc_const_missing_count();
|
|
|
|
}
|
|
|
|
|
2003-12-28 01:33:07 -05:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* mod.included_modules -> array
|
2009-02-22 09:23:33 -05:00
|
|
|
*
|
2003-12-28 01:33:07 -05:00
|
|
|
* Returns the list of modules included in <i>mod</i>.
|
2009-02-22 09:23:33 -05:00
|
|
|
*
|
2003-12-28 01:33:07 -05:00
|
|
|
* module Mixin
|
|
|
|
* end
|
2009-02-22 09:23:33 -05:00
|
|
|
*
|
2003-12-28 01:33:07 -05:00
|
|
|
* module Outer
|
|
|
|
* include Mixin
|
|
|
|
* end
|
2009-02-22 09:23:33 -05:00
|
|
|
*
|
2003-12-28 01:33:07 -05:00
|
|
|
* Mixin.included_modules #=> []
|
|
|
|
* Outer.included_modules #=> [Mixin]
|
|
|
|
*/
|
|
|
|
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 06:44:21 -04:00
|
|
|
rb_mod_included_modules(VALUE mod)
|
1998-01-16 07:19:22 -05:00
|
|
|
{
|
1999-01-19 23:59:39 -05:00
|
|
|
VALUE ary = rb_ary_new();
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE p;
|
|
|
|
|
2007-09-28 02:21:46 -04:00
|
|
|
for (p = RCLASS_SUPER(mod); p; p = RCLASS_SUPER(p)) {
|
1998-01-16 07:19:22 -05:00
|
|
|
if (BUILTIN_TYPE(p) == T_ICLASS) {
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_ary_push(ary, RBASIC(p)->klass);
|
1998-01-16 07:19:22 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return ary;
|
|
|
|
}
|
|
|
|
|
2003-12-28 01:33:07 -05:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-17 17:07:33 -04:00
|
|
|
* mod.include?(module) -> true or false
|
2009-02-22 09:23:33 -05:00
|
|
|
*
|
2003-12-28 01:33:07 -05:00
|
|
|
* Returns <code>true</code> if <i>module</i> is included in
|
|
|
|
* <i>mod</i> or one of <i>mod</i>'s ancestors.
|
2009-02-22 09:23:33 -05:00
|
|
|
*
|
2003-12-28 01:33:07 -05:00
|
|
|
* module A
|
|
|
|
* end
|
|
|
|
* class B
|
|
|
|
* include A
|
|
|
|
* end
|
|
|
|
* class C < B
|
|
|
|
* end
|
|
|
|
* B.include?(A) #=> true
|
|
|
|
* C.include?(A) #=> true
|
|
|
|
* A.include?(A) #=> false
|
|
|
|
*/
|
|
|
|
|
2001-07-24 05:07:33 -04:00
|
|
|
VALUE
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 06:44:21 -04:00
|
|
|
rb_mod_include_p(VALUE mod, VALUE mod2)
|
2001-07-24 05:07:33 -04:00
|
|
|
{
|
|
|
|
VALUE p;
|
|
|
|
|
|
|
|
Check_Type(mod2, T_MODULE);
|
2007-09-28 02:21:46 -04:00
|
|
|
for (p = RCLASS_SUPER(mod); p; p = RCLASS_SUPER(p)) {
|
2001-07-24 05:07:33 -04:00
|
|
|
if (BUILTIN_TYPE(p) == T_ICLASS) {
|
|
|
|
if (RBASIC(p)->klass == mod2) return Qtrue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Qfalse;
|
|
|
|
}
|
|
|
|
|
2003-12-28 01:33:07 -05:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* mod.ancestors -> array
|
2009-02-22 09:23:33 -05:00
|
|
|
*
|
2003-12-28 01:33:07 -05:00
|
|
|
* Returns a list of modules included in <i>mod</i> (including
|
|
|
|
* <i>mod</i> itself).
|
2009-02-22 09:23:33 -05:00
|
|
|
*
|
2003-12-28 01:33:07 -05:00
|
|
|
* module Mod
|
|
|
|
* include Math
|
|
|
|
* include Comparable
|
|
|
|
* end
|
2009-02-22 09:23:33 -05:00
|
|
|
*
|
2003-12-28 01:33:07 -05:00
|
|
|
* Mod.ancestors #=> [Mod, Comparable, Math]
|
|
|
|
* Math.ancestors #=> [Math]
|
|
|
|
*/
|
|
|
|
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 06:44:21 -04:00
|
|
|
rb_mod_ancestors(VALUE mod)
|
1998-01-16 07:19:22 -05:00
|
|
|
{
|
2003-08-06 17:50:06 -04:00
|
|
|
VALUE p, ary = rb_ary_new();
|
1998-01-16 07:19:22 -05:00
|
|
|
|
2007-09-28 02:21:46 -04:00
|
|
|
for (p = mod; p; p = RCLASS_SUPER(p)) {
|
1999-01-19 23:59:39 -05:00
|
|
|
if (FL_TEST(p, FL_SINGLETON))
|
|
|
|
continue;
|
1998-01-16 07:19:22 -05:00
|
|
|
if (BUILTIN_TYPE(p) == T_ICLASS) {
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_ary_push(ary, RBASIC(p)->klass);
|
1998-01-16 07:19:22 -05:00
|
|
|
}
|
|
|
|
else {
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_ary_push(ary, p);
|
1998-01-16 07:19:22 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return ary;
|
|
|
|
}
|
|
|
|
|
2003-05-02 04:24:43 -04:00
|
|
|
#define VISI(x) ((x)&NOEX_MASK)
|
|
|
|
#define VISI_CHECK(x,f) (VISI(x) == (f))
|
2002-10-30 03:04:32 -05:00
|
|
|
|
1998-01-16 07:19:22 -05:00
|
|
|
static int
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 06:44:21 -04:00
|
|
|
ins_methods_push(ID name, long type, VALUE ary, long visi)
|
2003-05-02 04:24:43 -04:00
|
|
|
{
|
|
|
|
if (type == -1) return ST_CONTINUE;
|
2006-12-31 10:02:22 -05:00
|
|
|
|
2003-05-02 04:24:43 -04:00
|
|
|
switch (visi) {
|
|
|
|
case NOEX_PRIVATE:
|
|
|
|
case NOEX_PROTECTED:
|
|
|
|
case NOEX_PUBLIC:
|
|
|
|
visi = (type == visi);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
visi = (type != NOEX_PRIVATE);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (visi) {
|
2006-09-02 11:05:27 -04:00
|
|
|
rb_ary_push(ary, ID2SYM(name));
|
1999-01-19 23:59:39 -05:00
|
|
|
}
|
|
|
|
return ST_CONTINUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2011-06-03 23:49:50 -04:00
|
|
|
ins_methods_i(st_data_t name, st_data_t type, st_data_t ary)
|
1999-01-19 23:59:39 -05:00
|
|
|
{
|
2011-06-03 23:49:50 -04:00
|
|
|
return ins_methods_push((ID)name, (long)type, (VALUE)ary, -1); /* everything but private */
|
2003-05-02 04:24:43 -04:00
|
|
|
}
|
1999-01-19 23:59:39 -05:00
|
|
|
|
2003-05-02 04:24:43 -04:00
|
|
|
static int
|
2011-06-03 23:49:50 -04:00
|
|
|
ins_methods_prot_i(st_data_t name, st_data_t type, st_data_t ary)
|
2003-05-02 04:24:43 -04:00
|
|
|
{
|
2011-06-03 23:49:50 -04:00
|
|
|
return ins_methods_push((ID)name, (long)type, (VALUE)ary, NOEX_PROTECTED);
|
1998-01-16 07:19:22 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2011-06-03 23:49:50 -04:00
|
|
|
ins_methods_priv_i(st_data_t name, st_data_t type, st_data_t ary)
|
1998-01-16 07:19:22 -05:00
|
|
|
{
|
2011-06-03 23:49:50 -04:00
|
|
|
return ins_methods_push((ID)name, (long)type, (VALUE)ary, NOEX_PRIVATE);
|
2003-05-02 04:24:43 -04:00
|
|
|
}
|
2002-10-30 03:04:32 -05:00
|
|
|
|
2003-05-02 04:24:43 -04:00
|
|
|
static int
|
2011-06-03 23:49:50 -04:00
|
|
|
ins_methods_pub_i(st_data_t name, st_data_t type, st_data_t ary)
|
2003-05-02 04:24:43 -04:00
|
|
|
{
|
2011-06-03 23:49:50 -04:00
|
|
|
return ins_methods_push((ID)name, (long)type, (VALUE)ary, NOEX_PUBLIC);
|
2002-10-30 03:04:32 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2011-06-03 23:49:50 -04:00
|
|
|
method_entry_i(st_data_t key, st_data_t value, st_data_t data)
|
2002-10-30 03:04:32 -05:00
|
|
|
{
|
2011-06-03 23:49:50 -04:00
|
|
|
const rb_method_entry_t *me = (const rb_method_entry_t *)value;
|
|
|
|
st_table *list = (st_table *)data;
|
2003-05-02 04:24:43 -04:00
|
|
|
long type;
|
1998-01-16 07:19:22 -05:00
|
|
|
|
2011-06-03 23:49:50 -04:00
|
|
|
if ((ID)key == ID_ALLOCATOR) {
|
2006-12-31 10:02:22 -05:00
|
|
|
return ST_CONTINUE;
|
|
|
|
}
|
2009-02-22 09:23:33 -05:00
|
|
|
|
2003-05-02 04:24:43 -04:00
|
|
|
if (!st_lookup(list, key, 0)) {
|
2009-08-27 22:45:41 -04:00
|
|
|
if (UNDEFINED_METHOD_ENTRY_P(me)) {
|
2006-12-31 10:02:22 -05:00
|
|
|
type = -1; /* none */
|
|
|
|
}
|
|
|
|
else {
|
2009-07-15 10:59:41 -04:00
|
|
|
type = VISI(me->flag);
|
2006-12-31 10:02:22 -05:00
|
|
|
}
|
2003-05-02 04:24:43 -04:00
|
|
|
st_add_direct(list, key, type);
|
1998-01-16 07:19:22 -05:00
|
|
|
}
|
|
|
|
return ST_CONTINUE;
|
|
|
|
}
|
|
|
|
|
2003-06-25 03:12:10 -04:00
|
|
|
static VALUE
|
2011-06-03 23:49:50 -04:00
|
|
|
class_instance_method_list(int argc, VALUE *argv, VALUE mod, int obj, int (*func) (st_data_t, st_data_t, st_data_t))
|
1998-01-16 07:19:22 -05:00
|
|
|
{
|
2003-06-26 08:34:51 -04:00
|
|
|
VALUE ary;
|
|
|
|
int recur;
|
|
|
|
st_table *list;
|
1998-01-16 07:19:22 -05:00
|
|
|
|
2003-05-02 12:11:48 -04:00
|
|
|
if (argc == 0) {
|
2009-07-18 04:05:32 -04:00
|
|
|
recur = TRUE;
|
2003-05-02 12:11:48 -04:00
|
|
|
}
|
2003-06-26 08:34:51 -04:00
|
|
|
else {
|
|
|
|
VALUE r;
|
|
|
|
rb_scan_args(argc, argv, "01", &r);
|
|
|
|
recur = RTEST(r);
|
|
|
|
}
|
|
|
|
|
|
|
|
list = st_init_numtable();
|
2007-09-28 02:21:46 -04:00
|
|
|
for (; mod; mod = RCLASS_SUPER(mod)) {
|
2011-06-03 23:49:50 -04:00
|
|
|
st_foreach(RCLASS_M_TBL(mod), method_entry_i, (st_data_t)list);
|
2003-08-04 04:31:24 -04:00
|
|
|
if (BUILTIN_TYPE(mod) == T_ICLASS) continue;
|
2010-06-17 12:58:25 -04:00
|
|
|
if (obj && FL_TEST(mod, FL_SINGLETON)) continue;
|
2003-06-26 08:34:51 -04:00
|
|
|
if (!recur) break;
|
|
|
|
}
|
|
|
|
ary = rb_ary_new();
|
|
|
|
st_foreach(list, func, ary);
|
|
|
|
st_free_table(list);
|
|
|
|
|
|
|
|
return ary;
|
1998-01-16 07:19:22 -05:00
|
|
|
}
|
|
|
|
|
2003-12-28 01:33:07 -05:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-17 17:07:33 -04:00
|
|
|
* mod.instance_methods(include_super=true) -> array
|
2009-02-22 09:23:33 -05:00
|
|
|
*
|
2010-06-22 00:52:03 -04:00
|
|
|
* Returns an array containing the names of the public and protected instance
|
|
|
|
* methods in the receiver. For a module, these are the public and protected methods;
|
2008-10-23 04:30:52 -04:00
|
|
|
* for a class, they are the instance (not singleton) methods. With no
|
2003-12-28 01:33:07 -05:00
|
|
|
* argument, or with an argument that is <code>false</code>, the
|
|
|
|
* instance methods in <i>mod</i> are returned, otherwise the methods
|
|
|
|
* in <i>mod</i> and <i>mod</i>'s superclasses are returned.
|
2009-02-22 09:23:33 -05:00
|
|
|
*
|
2003-12-28 01:33:07 -05:00
|
|
|
* module A
|
|
|
|
* def method1() end
|
|
|
|
* end
|
|
|
|
* class B
|
|
|
|
* def method2() end
|
|
|
|
* end
|
|
|
|
* class C < B
|
|
|
|
* def method3() end
|
|
|
|
* end
|
2009-02-22 09:23:33 -05:00
|
|
|
*
|
2008-03-08 20:04:46 -05:00
|
|
|
* A.instance_methods #=> [:method1]
|
|
|
|
* B.instance_methods(false) #=> [:method2]
|
|
|
|
* C.instance_methods(false) #=> [:method3]
|
2003-12-28 01:33:07 -05:00
|
|
|
* C.instance_methods(true).length #=> 43
|
|
|
|
*/
|
|
|
|
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 06:44:21 -04:00
|
|
|
rb_class_instance_methods(int argc, VALUE *argv, VALUE mod)
|
1999-01-19 23:59:39 -05:00
|
|
|
{
|
2010-06-17 12:58:25 -04:00
|
|
|
return class_instance_method_list(argc, argv, mod, 0, ins_methods_i);
|
2003-06-25 03:12:10 -04:00
|
|
|
}
|
1999-01-19 23:59:39 -05:00
|
|
|
|
2003-12-28 01:33:07 -05:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-17 17:07:33 -04:00
|
|
|
* mod.protected_instance_methods(include_super=true) -> array
|
2009-02-22 09:23:33 -05:00
|
|
|
*
|
2003-12-28 01:33:07 -05:00
|
|
|
* Returns a list of the protected instance methods defined in
|
|
|
|
* <i>mod</i>. If the optional parameter is not <code>false</code>, the
|
|
|
|
* methods of any ancestors are included.
|
|
|
|
*/
|
|
|
|
|
2003-06-25 03:12:10 -04:00
|
|
|
VALUE
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 06:44:21 -04:00
|
|
|
rb_class_protected_instance_methods(int argc, VALUE *argv, VALUE mod)
|
2003-06-25 03:12:10 -04:00
|
|
|
{
|
2010-06-17 12:58:25 -04:00
|
|
|
return class_instance_method_list(argc, argv, mod, 0, ins_methods_prot_i);
|
1999-01-19 23:59:39 -05:00
|
|
|
}
|
|
|
|
|
2003-12-28 01:33:07 -05:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-17 17:07:33 -04:00
|
|
|
* mod.private_instance_methods(include_super=true) -> array
|
2009-02-22 09:23:33 -05:00
|
|
|
*
|
2003-12-28 01:33:07 -05:00
|
|
|
* Returns a list of the private instance methods defined in
|
|
|
|
* <i>mod</i>. If the optional parameter is not <code>false</code>, the
|
|
|
|
* methods of any ancestors are included.
|
2009-02-22 09:23:33 -05:00
|
|
|
*
|
2003-12-28 01:33:07 -05:00
|
|
|
* module Mod
|
|
|
|
* def method1() end
|
|
|
|
* private :method1
|
|
|
|
* def method2() end
|
|
|
|
* end
|
2008-03-08 20:04:46 -05:00
|
|
|
* Mod.instance_methods #=> [:method2]
|
|
|
|
* Mod.private_instance_methods #=> [:method1]
|
2003-12-28 01:33:07 -05:00
|
|
|
*/
|
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
VALUE
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 06:44:21 -04:00
|
|
|
rb_class_private_instance_methods(int argc, VALUE *argv, VALUE mod)
|
1998-01-16 07:19:22 -05:00
|
|
|
{
|
2010-06-17 12:58:25 -04:00
|
|
|
return class_instance_method_list(argc, argv, mod, 0, ins_methods_priv_i);
|
1998-01-16 07:19:22 -05:00
|
|
|
}
|
|
|
|
|
2003-12-28 01:33:07 -05:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-17 17:07:33 -04:00
|
|
|
* mod.public_instance_methods(include_super=true) -> array
|
2009-02-22 09:23:33 -05:00
|
|
|
*
|
2003-12-28 01:33:07 -05:00
|
|
|
* Returns a list of the public instance methods defined in <i>mod</i>.
|
|
|
|
* If the optional parameter is not <code>false</code>, the methods of
|
|
|
|
* any ancestors are included.
|
|
|
|
*/
|
|
|
|
|
2002-10-30 03:04:32 -05:00
|
|
|
VALUE
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 06:44:21 -04:00
|
|
|
rb_class_public_instance_methods(int argc, VALUE *argv, VALUE mod)
|
2002-10-30 03:04:32 -05:00
|
|
|
{
|
2010-06-17 12:58:25 -04:00
|
|
|
return class_instance_method_list(argc, argv, mod, 0, ins_methods_pub_i);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* obj.methods -> array
|
|
|
|
*
|
2010-06-22 00:52:03 -04:00
|
|
|
* Returns a list of the names of public and protected methods of
|
2010-06-17 12:58:25 -04:00
|
|
|
* <i>obj</i>. This will include all the methods accessible in
|
|
|
|
* <i>obj</i>'s ancestors.
|
|
|
|
*
|
|
|
|
* class Klass
|
2011-03-07 03:44:45 -05:00
|
|
|
* def klass_method()
|
2010-06-17 12:58:25 -04:00
|
|
|
* end
|
|
|
|
* end
|
|
|
|
* k = Klass.new
|
2011-03-07 03:44:45 -05:00
|
|
|
* k.methods[0..9] #=> [:klass_method, :nil?, :===,
|
|
|
|
* # :==~, :!, :eql?
|
|
|
|
* # :hash, :<=>, :class, :singleton_class]
|
|
|
|
* k.methods.length #=> 57
|
2010-06-17 12:58:25 -04:00
|
|
|
*/
|
|
|
|
|
|
|
|
VALUE
|
|
|
|
rb_obj_methods(int argc, VALUE *argv, VALUE obj)
|
|
|
|
{
|
|
|
|
retry:
|
|
|
|
if (argc == 0) {
|
|
|
|
VALUE args[1];
|
|
|
|
|
|
|
|
args[0] = Qtrue;
|
|
|
|
return class_instance_method_list(argc, argv, CLASS_OF(obj), 1, ins_methods_i);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
VALUE recur;
|
|
|
|
|
|
|
|
rb_scan_args(argc, argv, "1", &recur);
|
|
|
|
if (RTEST(recur)) {
|
|
|
|
argc = 0;
|
|
|
|
goto retry;
|
|
|
|
}
|
|
|
|
return rb_obj_singleton_methods(argc, argv, obj);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* obj.protected_methods(all=true) -> array
|
|
|
|
*
|
|
|
|
* Returns the list of protected methods accessible to <i>obj</i>. If
|
|
|
|
* the <i>all</i> parameter is set to <code>false</code>, only those methods
|
|
|
|
* in the receiver will be listed.
|
|
|
|
*/
|
|
|
|
|
|
|
|
VALUE
|
|
|
|
rb_obj_protected_methods(int argc, VALUE *argv, VALUE obj)
|
|
|
|
{
|
|
|
|
return class_instance_method_list(argc, argv, CLASS_OF(obj), 1, ins_methods_prot_i);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* obj.private_methods(all=true) -> array
|
|
|
|
*
|
|
|
|
* Returns the list of private methods accessible to <i>obj</i>. If
|
|
|
|
* the <i>all</i> parameter is set to <code>false</code>, only those methods
|
|
|
|
* in the receiver will be listed.
|
|
|
|
*/
|
|
|
|
|
|
|
|
VALUE
|
|
|
|
rb_obj_private_methods(int argc, VALUE *argv, VALUE obj)
|
|
|
|
{
|
|
|
|
return class_instance_method_list(argc, argv, CLASS_OF(obj), 1, ins_methods_priv_i);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* obj.public_methods(all=true) -> array
|
|
|
|
*
|
|
|
|
* Returns the list of public methods accessible to <i>obj</i>. If
|
|
|
|
* the <i>all</i> parameter is set to <code>false</code>, only those methods
|
|
|
|
* in the receiver will be listed.
|
|
|
|
*/
|
|
|
|
|
|
|
|
VALUE
|
|
|
|
rb_obj_public_methods(int argc, VALUE *argv, VALUE obj)
|
|
|
|
{
|
|
|
|
return class_instance_method_list(argc, argv, CLASS_OF(obj), 1, ins_methods_pub_i);
|
2002-10-30 03:04:32 -05:00
|
|
|
}
|
|
|
|
|
2003-12-28 01:33:07 -05:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-17 17:07:33 -04:00
|
|
|
* obj.singleton_methods(all=true) -> array
|
2009-02-22 09:23:33 -05:00
|
|
|
*
|
2003-12-28 01:33:07 -05:00
|
|
|
* Returns an array of the names of singleton methods for <i>obj</i>.
|
|
|
|
* If the optional <i>all</i> parameter is true, the list will include
|
|
|
|
* methods in modules included in <i>obj</i>.
|
2010-06-22 00:52:03 -04:00
|
|
|
* Only public and protected singleton methods are returned.
|
2009-02-22 09:23:33 -05:00
|
|
|
*
|
2003-12-28 01:33:07 -05:00
|
|
|
* module Other
|
|
|
|
* def three() end
|
|
|
|
* end
|
2009-02-22 09:23:33 -05:00
|
|
|
*
|
2003-12-28 01:33:07 -05:00
|
|
|
* class Single
|
|
|
|
* def Single.four() end
|
|
|
|
* end
|
2009-02-22 09:23:33 -05:00
|
|
|
*
|
2003-12-28 01:33:07 -05:00
|
|
|
* a = Single.new
|
2009-02-22 09:23:33 -05:00
|
|
|
*
|
2003-12-28 01:33:07 -05:00
|
|
|
* def a.one()
|
2004-05-11 22:51:54 -04:00
|
|
|
* end
|
2009-02-22 09:23:33 -05:00
|
|
|
*
|
2003-12-28 01:33:07 -05:00
|
|
|
* class << a
|
|
|
|
* include Other
|
|
|
|
* def two()
|
2004-05-11 22:51:54 -04:00
|
|
|
* end
|
2003-12-28 01:33:07 -05:00
|
|
|
* end
|
2009-02-22 09:23:33 -05:00
|
|
|
*
|
2008-03-08 20:04:46 -05:00
|
|
|
* Single.singleton_methods #=> [:four]
|
|
|
|
* a.singleton_methods(false) #=> [:two, :one]
|
|
|
|
* a.singleton_methods #=> [:two, :one, :three]
|
2003-12-28 01:33:07 -05:00
|
|
|
*/
|
|
|
|
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 06:44:21 -04:00
|
|
|
rb_obj_singleton_methods(int argc, VALUE *argv, VALUE obj)
|
1998-01-16 07:19:22 -05:00
|
|
|
{
|
2003-05-06 02:51:31 -04:00
|
|
|
VALUE recur, ary, klass;
|
2003-05-02 04:24:43 -04:00
|
|
|
st_table *list;
|
1998-01-16 07:19:22 -05:00
|
|
|
|
2003-05-02 12:11:48 -04:00
|
|
|
if (argc == 0) {
|
2003-05-04 12:03:24 -04:00
|
|
|
recur = Qtrue;
|
2003-05-02 12:11:48 -04:00
|
|
|
}
|
2008-03-05 00:22:17 -05:00
|
|
|
else {
|
|
|
|
rb_scan_args(argc, argv, "01", &recur);
|
|
|
|
}
|
1998-01-16 07:19:22 -05:00
|
|
|
klass = CLASS_OF(obj);
|
2003-05-02 04:24:43 -04:00
|
|
|
list = st_init_numtable();
|
2003-08-01 16:16:53 -04:00
|
|
|
if (klass && FL_TEST(klass, FL_SINGLETON)) {
|
2011-06-03 23:49:50 -04:00
|
|
|
st_foreach(RCLASS_M_TBL(klass), method_entry_i, (st_data_t)list);
|
2007-09-28 02:21:46 -04:00
|
|
|
klass = RCLASS_SUPER(klass);
|
1998-01-16 07:19:22 -05:00
|
|
|
}
|
2003-05-06 02:51:31 -04:00
|
|
|
if (RTEST(recur)) {
|
2003-08-01 16:16:53 -04:00
|
|
|
while (klass && (FL_TEST(klass, FL_SINGLETON) || TYPE(klass) == T_ICLASS)) {
|
2011-06-03 23:49:50 -04:00
|
|
|
st_foreach(RCLASS_M_TBL(klass), method_entry_i, (st_data_t)list);
|
2007-09-28 02:21:46 -04:00
|
|
|
klass = RCLASS_SUPER(klass);
|
2001-05-30 05:12:34 -04:00
|
|
|
}
|
|
|
|
}
|
2003-05-02 04:24:43 -04:00
|
|
|
ary = rb_ary_new();
|
|
|
|
st_foreach(list, ins_methods_i, ary);
|
|
|
|
st_free_table(list);
|
1998-01-16 07:19:22 -05:00
|
|
|
|
|
|
|
return ary;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
2009-08-29 09:39:44 -04:00
|
|
|
/*!
|
|
|
|
* \}
|
|
|
|
*/
|
|
|
|
/*!
|
|
|
|
* \defgroup defmethod Defining methods
|
|
|
|
* There are some APIs to define a method from C.
|
|
|
|
* These API takes a C function as a method body.
|
|
|
|
*
|
|
|
|
* \par Method body functions
|
2010-05-29 14:51:39 -04:00
|
|
|
* Method body functions must return a VALUE and
|
2009-08-29 09:39:44 -04:00
|
|
|
* can be one of the following form:
|
|
|
|
* <dl>
|
|
|
|
* <dt>Fixed number of parameters</dt>
|
|
|
|
* <dd>
|
2010-05-29 14:51:39 -04:00
|
|
|
* This form is a normal C function, excepting it takes
|
2009-08-29 09:39:44 -04:00
|
|
|
* a receiver object as the first argument.
|
|
|
|
*
|
|
|
|
* \code
|
|
|
|
* static VALUE my_method(VALUE self, VALUE x, VALUE y);
|
|
|
|
* \endcode
|
|
|
|
* </dd>
|
|
|
|
* <dt>argc and argv style</dt>
|
|
|
|
* <dd>
|
|
|
|
* This form takes three parameters: \a argc, \a argv and \a self.
|
|
|
|
* \a self is the receiver. \a argc is the number of arguments.
|
|
|
|
* \a argv is a pointer to an array of the arguments.
|
|
|
|
*
|
|
|
|
* \code
|
|
|
|
* static VALUE my_method(int argc, VALUE *argv, VALUE self);
|
|
|
|
* \endcode
|
|
|
|
* </dd>
|
|
|
|
* <dt>Ruby array style</dt>
|
|
|
|
* <dd>
|
|
|
|
* This form takes two parameters: self and args.
|
2010-05-29 14:51:39 -04:00
|
|
|
* \a self is the receiver. \a args is an Array object which
|
2009-11-03 12:46:28 -05:00
|
|
|
* contains the arguments.
|
2009-08-29 09:39:44 -04:00
|
|
|
*
|
|
|
|
* \code
|
|
|
|
* static VALUE my_method(VALUE self, VALUE args);
|
|
|
|
* \endcode
|
|
|
|
* </dd>
|
|
|
|
*
|
|
|
|
* \par Number of parameters
|
|
|
|
* Method defining APIs takes the number of parameters which the
|
|
|
|
* method will takes. This number is called \a argc.
|
|
|
|
* \a argc can be:
|
|
|
|
* <dl>
|
|
|
|
* <dt>zero or positive number</dt>
|
|
|
|
* <dd>This means the method body function takes a fixed number of parameters</dd>
|
|
|
|
* <dt>-1</dt>
|
|
|
|
* <dd>This means the method body function is "argc and argv" style.</dd>
|
|
|
|
* <dt>-2</dt>
|
|
|
|
* <dd>This means the method body function is "self and args" style.</dd>
|
|
|
|
* </dl>
|
|
|
|
* \{
|
|
|
|
*/
|
|
|
|
|
1998-01-16 07:13:05 -05:00
|
|
|
void
|
2009-07-15 10:59:41 -04:00
|
|
|
rb_define_method_id(VALUE klass, ID mid, VALUE (*func)(ANYARGS), int argc)
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
2009-07-15 10:59:41 -04:00
|
|
|
rb_add_method_cfunc(klass, mid, func, argc, NOEX_PUBLIC);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2005-10-19 22:56:22 -04:00
|
|
|
rb_define_method(VALUE klass, const char *name, VALUE (*func)(ANYARGS), int argc)
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
2009-07-15 10:59:41 -04:00
|
|
|
rb_add_method_cfunc(klass, rb_intern(name), func, argc, NOEX_PUBLIC);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2005-10-19 22:56:22 -04:00
|
|
|
rb_define_protected_method(VALUE klass, const char *name, VALUE (*func)(ANYARGS), int argc)
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
2009-07-15 10:59:41 -04:00
|
|
|
rb_add_method_cfunc(klass, rb_intern(name), func, argc, NOEX_PROTECTED);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2005-10-19 22:56:22 -04:00
|
|
|
rb_define_private_method(VALUE klass, const char *name, VALUE (*func)(ANYARGS), int argc)
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
2009-07-15 10:59:41 -04:00
|
|
|
rb_add_method_cfunc(klass, rb_intern(name), func, argc, NOEX_PRIVATE);
|
1999-01-19 23:59:39 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 06:44:21 -04:00
|
|
|
rb_undef_method(VALUE klass, const char *name)
|
1999-01-19 23:59:39 -05:00
|
|
|
{
|
2009-07-15 10:59:41 -04:00
|
|
|
rb_add_method(klass, rb_intern(name), VM_METHOD_TYPE_UNDEF, 0, NOEX_UNDEF);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
2009-08-29 09:39:44 -04:00
|
|
|
/*!
|
|
|
|
* \}
|
|
|
|
*/
|
|
|
|
/*!
|
|
|
|
* \addtogroup class
|
|
|
|
* \{
|
|
|
|
*/
|
|
|
|
|
2002-04-25 09:57:01 -04:00
|
|
|
#define SPECIAL_SINGLETON(x,c) do {\
|
|
|
|
if (obj == (x)) {\
|
2010-12-03 21:18:42 -05:00
|
|
|
return (c);\
|
2002-04-25 09:57:01 -04:00
|
|
|
}\
|
|
|
|
} while (0)
|
2002-03-08 02:03:09 -05:00
|
|
|
|
2009-08-29 09:39:44 -04:00
|
|
|
|
|
|
|
/*!
|
2009-08-31 01:55:57 -04:00
|
|
|
* \internal
|
|
|
|
* Returns the singleton class of \a obj. Creates it if necessary.
|
2009-08-29 09:39:44 -04:00
|
|
|
*
|
2009-08-31 01:55:57 -04:00
|
|
|
* \note DO NOT expose the returned singleton class to
|
|
|
|
* outside of class.c.
|
2010-05-29 14:51:39 -04:00
|
|
|
* Use \ref rb_singleton_class instead for
|
2009-08-31 01:55:57 -04:00
|
|
|
* consistency of the metaclass hierarchy.
|
2009-08-29 09:39:44 -04:00
|
|
|
*/
|
2009-08-31 01:55:57 -04:00
|
|
|
static VALUE
|
|
|
|
singleton_class_of(VALUE obj)
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
1999-12-14 01:50:43 -05:00
|
|
|
VALUE klass;
|
2000-03-07 03:37:59 -05:00
|
|
|
|
2000-05-01 05:42:38 -04:00
|
|
|
if (FIXNUM_P(obj) || SYMBOL_P(obj)) {
|
1999-08-13 01:45:20 -04:00
|
|
|
rb_raise(rb_eTypeError, "can't define singleton");
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
2000-05-01 05:42:38 -04:00
|
|
|
if (rb_special_const_p(obj)) {
|
|
|
|
SPECIAL_SINGLETON(Qnil, rb_cNilClass);
|
|
|
|
SPECIAL_SINGLETON(Qfalse, rb_cFalseClass);
|
|
|
|
SPECIAL_SINGLETON(Qtrue, rb_cTrueClass);
|
2010-10-12 11:03:51 -04:00
|
|
|
rb_bug("unknown immediate %p", (void *)obj);
|
2000-05-01 05:42:38 -04:00
|
|
|
}
|
|
|
|
|
2002-03-08 02:03:09 -05:00
|
|
|
if (FL_TEST(RBASIC(obj)->klass, FL_SINGLETON) &&
|
2009-08-31 01:55:57 -04:00
|
|
|
rb_ivar_get(RBASIC(obj)->klass, id_attached) == obj) {
|
1999-12-14 01:50:43 -05:00
|
|
|
klass = RBASIC(obj)->klass;
|
|
|
|
}
|
|
|
|
else {
|
2002-01-10 15:18:39 -05:00
|
|
|
klass = rb_make_metaclass(obj, RBASIC(obj)->klass);
|
1999-12-14 01:50:43 -05:00
|
|
|
}
|
* test/ruby/test_metaclass.rb: new test case for metaclass hierarchy.
* class.c (make_metametaclass): new function. extracted from
rb_make_metaclass.
* class.c (rb_make_metaclass): uses make_metametaclass when called for a
metaclass.
* class.c (rb_singleton_class): creates a meta^(n+2)-class in
addition to a meta^(n+1)-class when called for a meta^(n)-class.
This is because the returned meta^(n+1) class must acts as an instance of
Class, metaclass of Class, ..., meta^(n+1)-class of Class,
Module, metaclass of Module, ..., meta^(n+1)-class of Module,
Object, metaclass of Object, ..., meta^(n+2)-class of Object,
BasicObject, metaclass of BasicObject, ..., meta^(n+2)-class of
and BasicObject even when Class, Module, Object or BasicObject has
not have its meta^(i)-class yet.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@20747 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-12-15 01:23:43 -05:00
|
|
|
|
1999-12-14 01:50:43 -05:00
|
|
|
if (OBJ_TAINTED(obj)) {
|
|
|
|
OBJ_TAINT(klass);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
1999-12-14 01:50:43 -05:00
|
|
|
else {
|
|
|
|
FL_UNSET(klass, FL_TAINT);
|
|
|
|
}
|
* object.c (rb_obj_untrusted): new method Object#untrusted?.
(rb_obj_untrust): new method Object#untrust.
(rb_obj_trust): new method Object#trust.
* array.c, debug.c, time.c, include/ruby/ruby.h, re.c, variable.c,
string.c, io.c, dir.c, vm_method.c, struct.c, class.c, hash.c,
ruby.c, marshal.c: fixes for Object#untrusted?.
* test/ruby/test_module.rb, test/ruby/test_array.rb,
test/ruby/test_object.rb, test/ruby/test_string.rb,
test/ruby/test_marshal.rb, test/ruby/test_hash.rb: added tests for
Object#untrusted?.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@18568 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-08-13 03:25:05 -04:00
|
|
|
if (OBJ_UNTRUSTED(obj)) {
|
|
|
|
OBJ_UNTRUST(klass);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
FL_UNSET(klass, FL_UNTRUSTED);
|
|
|
|
}
|
2000-06-22 04:29:58 -04:00
|
|
|
if (OBJ_FROZEN(obj)) OBJ_FREEZE(klass);
|
1999-12-14 01:50:43 -05:00
|
|
|
|
|
|
|
return klass;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
2009-08-31 01:55:57 -04:00
|
|
|
|
|
|
|
/*!
|
|
|
|
* Returns the singleton class of \a obj. Creates it if necessary.
|
|
|
|
*
|
|
|
|
* \param obj an arbitrary object.
|
|
|
|
* \throw TypeError if \a obj is a Fixnum or a Symbol.
|
|
|
|
* \return the singleton class.
|
|
|
|
*
|
|
|
|
* \post \a obj has its own singleton class.
|
2010-05-29 14:51:39 -04:00
|
|
|
* \post if \a obj is a class,
|
|
|
|
* the returned singleton class also has its own
|
2009-08-31 01:55:57 -04:00
|
|
|
* singleton class in order to keep consistency of the
|
|
|
|
* inheritance structure of metaclasses.
|
2010-05-29 14:51:39 -04:00
|
|
|
* \note a new singleton class will be created
|
2009-08-31 01:55:57 -04:00
|
|
|
* if \a obj does not have it.
|
|
|
|
* \note the singleton classes for nil, true and false are:
|
|
|
|
* NilClass, TrueClass and FalseClass.
|
|
|
|
*/
|
|
|
|
VALUE
|
|
|
|
rb_singleton_class(VALUE obj)
|
|
|
|
{
|
|
|
|
VALUE klass = singleton_class_of(obj);
|
|
|
|
|
|
|
|
/* ensures an exposed class belongs to its own eigenclass */
|
2010-07-27 03:13:43 -04:00
|
|
|
if (TYPE(obj) == T_CLASS) (void)ENSURE_EIGENCLASS(klass);
|
2009-08-31 01:55:57 -04:00
|
|
|
|
|
|
|
return klass;
|
|
|
|
}
|
|
|
|
|
2009-08-29 09:39:44 -04:00
|
|
|
/*!
|
|
|
|
* \}
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* \addtogroup defmethod
|
|
|
|
* \{
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* Defines a singleton method for \a obj.
|
|
|
|
* \param obj an arbitrary object
|
|
|
|
* \param name name of the singleton method
|
|
|
|
* \param func the method body
|
|
|
|
* \param argc the number of parameters, or -1 or -2. see \ref defmethod.
|
|
|
|
*/
|
1998-01-16 07:13:05 -05:00
|
|
|
void
|
2005-10-19 22:56:22 -04:00
|
|
|
rb_define_singleton_method(VALUE obj, const char *name, VALUE (*func)(ANYARGS), int argc)
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
2009-08-31 01:55:57 -04:00
|
|
|
rb_define_method(singleton_class_of(obj), name, func, argc);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
2009-08-29 09:39:44 -04:00
|
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* Defines a module function for \a module.
|
|
|
|
* \param module an module or a class.
|
|
|
|
* \param name name of the function
|
|
|
|
* \param func the method body
|
|
|
|
* \param argc the number of parameters, or -1 or -2. see \ref defmethod.
|
|
|
|
*/
|
1998-01-16 07:13:05 -05:00
|
|
|
void
|
2005-10-19 22:56:22 -04:00
|
|
|
rb_define_module_function(VALUE module, const char *name, VALUE (*func)(ANYARGS), int argc)
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
|
|
|
rb_define_private_method(module, name, func, argc);
|
|
|
|
rb_define_singleton_method(module, name, func, argc);
|
|
|
|
}
|
|
|
|
|
2009-08-29 09:39:44 -04:00
|
|
|
|
|
|
|
/*!
|
|
|
|
* Defines a global function
|
|
|
|
* \param name name of the function
|
|
|
|
* \param func the method body
|
|
|
|
* \param argc the number of parameters, or -1 or -2. see \ref defmethod.
|
|
|
|
*/
|
1998-01-16 07:13:05 -05:00
|
|
|
void
|
2005-10-19 22:56:22 -04:00
|
|
|
rb_define_global_function(const char *name, VALUE (*func)(ANYARGS), int argc)
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_define_module_function(rb_mKernel, name, func, argc);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
2009-08-29 09:39:44 -04:00
|
|
|
|
|
|
|
/*!
|
|
|
|
* Defines an alias of a method.
|
|
|
|
* \param klass the class which the original method belongs to
|
|
|
|
* \param name1 a new name for the method
|
|
|
|
* \param name2 the original name of the method
|
|
|
|
*/
|
1998-01-16 07:13:05 -05:00
|
|
|
void
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 06:44:21 -04:00
|
|
|
rb_define_alias(VALUE klass, const char *name1, const char *name2)
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
1998-01-16 07:19:22 -05:00
|
|
|
rb_alias(klass, rb_intern(name1), rb_intern(name2));
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
2009-08-29 09:39:44 -04:00
|
|
|
/*!
|
|
|
|
* Defines (a) public accessor method(s) for an attribute.
|
|
|
|
* \param klass the class which the attribute will belongs to
|
|
|
|
* \param name name of the attribute
|
|
|
|
* \param read a getter method for the attribute will be defined if \a read is non-zero.
|
|
|
|
* \param write a setter method for the attribute will be defined if \a write is non-zero.
|
|
|
|
*/
|
1998-01-16 07:13:05 -05:00
|
|
|
void
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 06:44:21 -04:00
|
|
|
rb_define_attr(VALUE klass, const char *name, int read, int write)
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
2009-07-18 04:05:32 -04:00
|
|
|
rb_attr(klass, rb_intern(name), read, write, FALSE);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
2009-10-21 16:17:27 -04:00
|
|
|
int
|
2009-10-22 08:12:24 -04:00
|
|
|
rb_obj_basic_to_s_p(VALUE obj)
|
2009-10-21 16:17:27 -04:00
|
|
|
{
|
|
|
|
const rb_method_entry_t *me = rb_method_entry(CLASS_OF(obj), rb_intern("to_s"));
|
|
|
|
if (me && me->def && me->def->type == VM_METHOD_TYPE_CFUNC &&
|
|
|
|
me->def->body.cfunc.func == rb_any_to_s)
|
|
|
|
return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
#include <stdarg.h>
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
int
|
2002-04-18 04:46:18 -04:00
|
|
|
rb_scan_args(int argc, const VALUE *argv, const char *fmt, ...)
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
2009-02-24 10:15:25 -05:00
|
|
|
int i;
|
|
|
|
const char *p = fmt;
|
1998-01-16 07:13:05 -05:00
|
|
|
VALUE *var;
|
|
|
|
va_list vargs;
|
2010-09-10 03:51:58 -04:00
|
|
|
int f_var = 0, f_hash = 0, f_block = 0;
|
2009-02-24 10:15:25 -05:00
|
|
|
int n_lead = 0, n_opt = 0, n_trail = 0, n_mand;
|
|
|
|
int argi = 0;
|
2010-09-10 03:51:58 -04:00
|
|
|
VALUE hash = Qnil;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2009-02-24 10:15:25 -05:00
|
|
|
if (ISDIGIT(*p)) {
|
|
|
|
n_lead = *p - '0';
|
|
|
|
p++;
|
|
|
|
if (ISDIGIT(*p)) {
|
|
|
|
n_opt = *p - '0';
|
|
|
|
p++;
|
2009-02-24 10:41:39 -05:00
|
|
|
if (ISDIGIT(*p)) {
|
|
|
|
n_trail = *p - '0';
|
|
|
|
p++;
|
|
|
|
goto block_arg;
|
|
|
|
}
|
2009-02-24 10:15:25 -05:00
|
|
|
}
|
2009-02-16 03:04:56 -05:00
|
|
|
}
|
|
|
|
if (*p == '*') {
|
2009-02-24 10:15:25 -05:00
|
|
|
f_var = 1;
|
|
|
|
p++;
|
|
|
|
if (ISDIGIT(*p)) {
|
|
|
|
n_trail = *p - '0';
|
|
|
|
p++;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
2009-02-24 10:15:25 -05:00
|
|
|
}
|
2009-02-24 10:41:39 -05:00
|
|
|
block_arg:
|
2010-09-10 03:51:58 -04:00
|
|
|
if (*p == ':') {
|
|
|
|
f_hash = 1;
|
|
|
|
p++;
|
|
|
|
}
|
2009-02-24 10:15:25 -05:00
|
|
|
if (*p == '&') {
|
|
|
|
f_block = 1;
|
1998-01-16 07:13:05 -05:00
|
|
|
p++;
|
|
|
|
}
|
2009-02-24 10:15:25 -05:00
|
|
|
if (*p != '\0') {
|
|
|
|
rb_fatal("bad scan arg format: %s", fmt);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
2009-02-24 10:15:25 -05:00
|
|
|
n_mand = n_lead + n_trail;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2009-02-24 10:15:25 -05:00
|
|
|
if (argc < n_mand)
|
|
|
|
goto argc_error;
|
|
|
|
|
|
|
|
va_start(vargs, fmt);
|
|
|
|
|
2010-09-10 03:51:58 -04:00
|
|
|
/* capture an option hash - phase 1: pop */
|
|
|
|
if (f_hash && n_mand < argc) {
|
|
|
|
VALUE last = argv[argc - 1];
|
|
|
|
|
|
|
|
if (NIL_P(last)) {
|
|
|
|
/* nil is taken as an empty option hash only if it is not
|
|
|
|
ambiguous; i.e. '*' is not specified and arguments are
|
|
|
|
given more than sufficient */
|
|
|
|
if (!f_var && n_mand + n_opt < argc)
|
|
|
|
argc--;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
hash = rb_check_convert_type(last, T_HASH, "Hash", "to_hash");
|
|
|
|
if (!NIL_P(hash))
|
|
|
|
argc--;
|
|
|
|
}
|
|
|
|
}
|
2009-02-24 10:15:25 -05:00
|
|
|
/* capture leading mandatory arguments */
|
|
|
|
for (i = n_lead; i-- > 0; ) {
|
|
|
|
var = va_arg(vargs, VALUE *);
|
|
|
|
if (var) *var = argv[argi];
|
|
|
|
argi++;
|
|
|
|
}
|
|
|
|
/* capture optional arguments */
|
|
|
|
for (i = n_opt; i-- > 0; ) {
|
|
|
|
var = va_arg(vargs, VALUE *);
|
|
|
|
if (argi < argc - n_trail) {
|
|
|
|
if (var) *var = argv[argi];
|
|
|
|
argi++;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (var) *var = Qnil;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
}
|
2009-02-24 10:15:25 -05:00
|
|
|
/* capture variable length arguments */
|
|
|
|
if (f_var) {
|
|
|
|
int n_var = argc - argi - n_trail;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2009-02-24 10:15:25 -05:00
|
|
|
var = va_arg(vargs, VALUE *);
|
|
|
|
if (0 < n_var) {
|
|
|
|
if (var) *var = rb_ary_new4(n_var, &argv[argi]);
|
|
|
|
argi += n_var;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
else {
|
1999-08-13 01:45:20 -04:00
|
|
|
if (var) *var = rb_ary_new();
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
}
|
2009-02-24 10:15:25 -05:00
|
|
|
/* capture trailing mandatory arguments */
|
|
|
|
for (i = n_trail; i-- > 0; ) {
|
|
|
|
var = va_arg(vargs, VALUE *);
|
|
|
|
if (var) *var = argv[argi];
|
|
|
|
argi++;
|
|
|
|
}
|
2010-09-10 03:51:58 -04:00
|
|
|
/* capture an option hash - phase 2: assignment */
|
|
|
|
if (f_hash) {
|
|
|
|
var = va_arg(vargs, VALUE *);
|
|
|
|
if (var) *var = hash;
|
|
|
|
}
|
2009-02-24 10:15:25 -05:00
|
|
|
/* capture iterator block */
|
|
|
|
if (f_block) {
|
|
|
|
var = va_arg(vargs, VALUE *);
|
2000-05-24 00:34:26 -04:00
|
|
|
if (rb_block_given_p()) {
|
2003-06-16 03:14:50 -04:00
|
|
|
*var = rb_block_proc();
|
2000-02-18 01:59:36 -05:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
*var = Qnil;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
}
|
2000-02-18 01:59:36 -05:00
|
|
|
va_end(vargs);
|
|
|
|
|
2009-02-24 10:15:25 -05:00
|
|
|
if (argi < argc)
|
|
|
|
goto argc_error;
|
2000-02-18 01:59:36 -05:00
|
|
|
|
1998-01-16 07:13:05 -05:00
|
|
|
return argc;
|
|
|
|
|
2009-02-24 10:15:25 -05:00
|
|
|
argc_error:
|
|
|
|
if (0 < n_opt)
|
|
|
|
rb_raise(rb_eArgError, "wrong number of arguments (%d for %d..%d%s)",
|
|
|
|
argc, n_mand, n_mand + n_opt, f_var ? "+" : "");
|
|
|
|
else
|
|
|
|
rb_raise(rb_eArgError, "wrong number of arguments (%d for %d%s)",
|
|
|
|
argc, n_mand, f_var ? "+" : "");
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
2009-06-29 12:39:28 -04:00
|
|
|
|
|
|
|
/*!
|
|
|
|
* \}
|
|
|
|
*/
|