2000-05-01 05:42:38 -04:00
|
|
|
/**********************************************************************
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
class.c -
|
|
|
|
|
|
|
|
$Author$
|
|
|
|
$Date$
|
|
|
|
created at: Tue Aug 10 15:05:44 JST 1993
|
|
|
|
|
2000-01-04 23:41:21 -05:00
|
|
|
Copyright (C) 1993-2000 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
|
|
|
|
|
|
|
#include "ruby.h"
|
|
|
|
#include "node.h"
|
|
|
|
#include "st.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;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_class_new(super)
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE super;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
1998-01-16 07:19:22 -05:00
|
|
|
NEWOBJ(klass, struct RClass);
|
1999-01-19 23:59:39 -05:00
|
|
|
OBJSETUP(klass, rb_cClass, T_CLASS);
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1998-01-16 07:19:22 -05:00
|
|
|
klass->super = super;
|
|
|
|
klass->iv_tbl = 0;
|
|
|
|
klass->m_tbl = 0; /* safe GC */
|
1999-01-19 23:59:39 -05:00
|
|
|
klass->m_tbl = st_init_numtable();
|
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
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_singleton_class_new(super)
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE super;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
1999-01-19 23:59:39 -05:00
|
|
|
VALUE klass = rb_class_new(super);
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1998-01-16 07:19:22 -05:00
|
|
|
FL_SET(klass, FL_SINGLETON);
|
|
|
|
return klass;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
clone_method(mid, body, tbl)
|
|
|
|
ID mid;
|
|
|
|
NODE *body;
|
|
|
|
st_table *tbl;
|
|
|
|
{
|
|
|
|
st_insert(tbl, mid, NEW_METHOD(body->nd_body, body->nd_noex));
|
|
|
|
return ST_CONTINUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_singleton_class_clone(klass)
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE klass;
|
1998-01-16 07:13:05 -05: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 */
|
|
|
|
NEWOBJ(clone, struct RClass);
|
1998-01-16 07:19:22 -05:00
|
|
|
CLONESETUP(clone, klass);
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1998-01-16 07:19:22 -05:00
|
|
|
clone->super = RCLASS(klass)->super;
|
1998-01-16 07:13:05 -05:00
|
|
|
clone->iv_tbl = 0;
|
|
|
|
clone->m_tbl = 0;
|
1999-01-19 23:59:39 -05:00
|
|
|
clone->m_tbl = st_init_numtable();
|
1998-01-16 07:19:22 -05:00
|
|
|
st_foreach(RCLASS(klass)->m_tbl, clone_method, clone->m_tbl);
|
1998-01-16 07:13:05 -05:00
|
|
|
FL_SET(clone, FL_SINGLETON);
|
|
|
|
return (VALUE)clone;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1998-01-16 07:19:22 -05:00
|
|
|
void
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_singleton_class_attached(klass, obj)
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE klass, obj;
|
|
|
|
{
|
|
|
|
if (FL_TEST(klass, FL_SINGLETON))
|
|
|
|
rb_iv_set(klass, "__attached__", obj);
|
|
|
|
}
|
|
|
|
|
1998-01-16 07:13:05 -05:00
|
|
|
VALUE
|
|
|
|
rb_define_class_id(id, super)
|
|
|
|
ID id;
|
1998-01-16 07:19:22 -05:00
|
|
|
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);
|
1998-01-16 07:19:22 -05:00
|
|
|
rb_name_class(klass, id);
|
1998-01-16 07:13:05 -05:00
|
|
|
/* make metaclass */
|
1999-01-19 23:59:39 -05:00
|
|
|
RBASIC(klass)->klass = rb_singleton_class_new(RBASIC(super)->klass);
|
|
|
|
rb_singleton_class_attached(RBASIC(klass)->klass, klass);
|
1998-01-16 07:19:22 -05:00
|
|
|
rb_funcall(super, rb_intern("inherited"), 1, 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
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
|
|
|
rb_define_class(name, super)
|
1999-08-13 01:45:20 -04:00
|
|
|
const char *name;
|
1998-01-16 07:13:05 -05:00
|
|
|
VALUE super;
|
|
|
|
{
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE klass;
|
1998-01-16 07:13:05 -05:00
|
|
|
ID id;
|
|
|
|
|
|
|
|
id = rb_intern(name);
|
1998-01-16 07:19:22 -05:00
|
|
|
klass = rb_define_class_id(id, super);
|
1999-01-19 23:59:39 -05:00
|
|
|
|
1998-01-16 07:19:22 -05:00
|
|
|
st_add_direct(rb_class_tbl, id, 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
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_define_class_under(outer, name, super)
|
|
|
|
VALUE outer;
|
1999-08-13 01:45:20 -04:00
|
|
|
const char *name;
|
1998-01-16 07:13:05 -05:00
|
|
|
VALUE super;
|
|
|
|
{
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE klass;
|
1998-01-16 07:13:05 -05:00
|
|
|
ID id;
|
|
|
|
|
|
|
|
id = rb_intern(name);
|
1998-01-16 07:19:22 -05:00
|
|
|
klass = rb_define_class_id(id, super);
|
1999-12-14 01:50:43 -05:00
|
|
|
rb_const_set(outer, id, klass);
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_set_class_path(klass, outer, name);
|
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
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_module_new()
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
|
|
|
NEWOBJ(mdl, struct RClass);
|
1999-01-19 23:59:39 -05:00
|
|
|
OBJSETUP(mdl, rb_cModule, T_MODULE);
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
mdl->super = 0;
|
|
|
|
mdl->iv_tbl = 0;
|
|
|
|
mdl->m_tbl = 0;
|
1999-01-19 23:59:39 -05:00
|
|
|
mdl->m_tbl = st_init_numtable();
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
return (VALUE)mdl;
|
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
|
|
|
rb_define_module_id(id)
|
|
|
|
ID id;
|
|
|
|
{
|
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
|
|
|
|
rb_define_module(name)
|
1999-08-13 01:45:20 -04:00
|
|
|
const char *name;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
|
|
|
VALUE module;
|
|
|
|
ID id;
|
|
|
|
|
|
|
|
id = rb_intern(name);
|
|
|
|
module = rb_define_module_id(id);
|
|
|
|
st_add_direct(rb_class_tbl, id, module);
|
|
|
|
|
|
|
|
return module;
|
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_define_module_under(outer, name)
|
|
|
|
VALUE outer;
|
1999-08-13 01:45:20 -04:00
|
|
|
const char *name;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
|
|
|
VALUE module;
|
|
|
|
ID id;
|
|
|
|
|
|
|
|
id = rb_intern(name);
|
|
|
|
module = rb_define_module_id(id);
|
1999-12-14 01:50:43 -05:00
|
|
|
rb_const_set(outer, id, module);
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_set_class_path(module, outer, name);
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
return module;
|
|
|
|
}
|
|
|
|
|
1998-01-16 07:19:22 -05:00
|
|
|
static VALUE
|
1998-01-16 07:13:05 -05:00
|
|
|
include_class_new(module, super)
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE module, super;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
1998-01-16 07:19:22 -05:00
|
|
|
NEWOBJ(klass, struct RClass);
|
1999-01-19 23:59:39 -05:00
|
|
|
OBJSETUP(klass, rb_cClass, T_ICLASS);
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-08-13 01:45:20 -04:00
|
|
|
if (!RCLASS(module)->iv_tbl) {
|
|
|
|
RCLASS(module)->iv_tbl = st_init_numtable();
|
|
|
|
}
|
1998-01-16 07:19:22 -05:00
|
|
|
klass->iv_tbl = RCLASS(module)->iv_tbl;
|
1999-08-13 01:45:20 -04:00
|
|
|
klass->m_tbl = RCLASS(module)->m_tbl;
|
1998-01-16 07:19:22 -05:00
|
|
|
klass->super = 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
|
|
|
}
|
|
|
|
|
1998-01-16 07:19:22 -05:00
|
|
|
return (VALUE)klass;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
1998-01-16 07:19:22 -05:00
|
|
|
rb_include_module(klass, module)
|
|
|
|
VALUE klass, module;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE p;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
if (NIL_P(module)) return;
|
1999-01-19 23:59:39 -05:00
|
|
|
if (klass == module) return;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
switch (TYPE(module)) {
|
|
|
|
case T_MODULE:
|
|
|
|
case T_CLASS:
|
1999-01-19 23:59:39 -05:00
|
|
|
case T_ICLASS:
|
1998-01-16 07:13:05 -05:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
Check_Type(module, T_MODULE);
|
|
|
|
}
|
|
|
|
|
|
|
|
while (module) {
|
|
|
|
/* ignore if the module included already in superclasses */
|
1998-01-16 07:19:22 -05:00
|
|
|
for (p = RCLASS(klass)->super; p; p = RCLASS(p)->super) {
|
|
|
|
if (BUILTIN_TYPE(p) == T_ICLASS &&
|
1999-01-19 23:59:39 -05:00
|
|
|
RCLASS(p)->m_tbl == RCLASS(module)->m_tbl) {
|
|
|
|
if (RCLASS(module)->super) {
|
|
|
|
rb_include_module(p, RCLASS(module)->super);
|
|
|
|
}
|
1998-01-16 07:13:05 -05:00
|
|
|
return;
|
1999-01-19 23:59:39 -05:00
|
|
|
}
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
1998-01-16 07:19:22 -05:00
|
|
|
RCLASS(klass)->super =
|
|
|
|
include_class_new(module, RCLASS(klass)->super);
|
|
|
|
klass = RCLASS(klass)->super;
|
|
|
|
module = RCLASS(module)->super;
|
|
|
|
}
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_clear_cache();
|
1998-01-16 07:19:22 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_mod_included_modules(mod)
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE mod;
|
|
|
|
{
|
1999-01-19 23:59:39 -05:00
|
|
|
VALUE ary = rb_ary_new();
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE p;
|
|
|
|
|
|
|
|
for (p = RCLASS(mod)->super; p; p = RCLASS(p)->super) {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_mod_ancestors(mod)
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE mod;
|
|
|
|
{
|
1999-01-19 23:59:39 -05:00
|
|
|
VALUE ary = rb_ary_new();
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE p;
|
|
|
|
|
|
|
|
for (p = mod; p; p = RCLASS(p)->super) {
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
ins_methods_i(key, body, ary)
|
|
|
|
ID key;
|
|
|
|
NODE *body;
|
|
|
|
VALUE ary;
|
|
|
|
{
|
1999-01-19 23:59:39 -05:00
|
|
|
if ((body->nd_noex&(NOEX_PRIVATE|NOEX_PROTECTED)) == 0) {
|
|
|
|
VALUE name = rb_str_new2(rb_id2name(key));
|
1998-01-16 07:19:22 -05:00
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
if (!rb_ary_includes(ary, name)) {
|
1998-01-16 07:19:22 -05:00
|
|
|
if (!body->nd_body) {
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_ary_push(ary, Qnil);
|
1998-01-16 07:19:22 -05:00
|
|
|
}
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_ary_push(ary, name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (body->nd_body && nd_type(body->nd_body) == NODE_ZSUPER) {
|
|
|
|
rb_ary_push(ary, Qnil);
|
|
|
|
rb_ary_push(ary, rb_str_new2(rb_id2name(key)));
|
|
|
|
}
|
|
|
|
return ST_CONTINUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
ins_methods_prot_i(key, body, ary)
|
|
|
|
ID key;
|
|
|
|
NODE *body;
|
|
|
|
VALUE ary;
|
|
|
|
{
|
|
|
|
if (!body->nd_body) {
|
|
|
|
rb_ary_push(ary, Qnil);
|
|
|
|
rb_ary_push(ary, rb_str_new2(rb_id2name(key)));
|
|
|
|
}
|
|
|
|
else if (body->nd_noex & NOEX_PROTECTED) {
|
|
|
|
VALUE name = rb_str_new2(rb_id2name(key));
|
|
|
|
|
|
|
|
if (!rb_ary_includes(ary, name)) {
|
|
|
|
rb_ary_push(ary, name);
|
1998-01-16 07:19:22 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (nd_type(body->nd_body) == NODE_ZSUPER) {
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_ary_push(ary, Qnil);
|
|
|
|
rb_ary_push(ary, rb_str_new2(rb_id2name(key)));
|
1998-01-16 07:19:22 -05:00
|
|
|
}
|
|
|
|
return ST_CONTINUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
ins_methods_priv_i(key, body, ary)
|
|
|
|
ID key;
|
|
|
|
NODE *body;
|
|
|
|
VALUE ary;
|
|
|
|
{
|
|
|
|
if (!body->nd_body) {
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_ary_push(ary, Qnil);
|
|
|
|
rb_ary_push(ary, rb_str_new2(rb_id2name(key)));
|
1998-01-16 07:19:22 -05:00
|
|
|
}
|
1999-01-19 23:59:39 -05:00
|
|
|
else if (body->nd_noex & NOEX_PRIVATE) {
|
|
|
|
VALUE name = rb_str_new2(rb_id2name(key));
|
1998-01-16 07:19:22 -05:00
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
if (!rb_ary_includes(ary, name)) {
|
|
|
|
rb_ary_push(ary, name);
|
1998-01-16 07:19:22 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (nd_type(body->nd_body) == NODE_ZSUPER) {
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_ary_push(ary, Qnil);
|
|
|
|
rb_ary_push(ary, rb_str_new2(rb_id2name(key)));
|
1998-01-16 07:19:22 -05:00
|
|
|
}
|
|
|
|
return ST_CONTINUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
method_list(mod, option, func)
|
|
|
|
VALUE mod;
|
|
|
|
int option;
|
|
|
|
int (*func)();
|
|
|
|
{
|
|
|
|
VALUE ary;
|
|
|
|
VALUE klass;
|
|
|
|
VALUE *p, *q, *pend;
|
|
|
|
|
1999-12-14 01:50:43 -05:00
|
|
|
if (!OBJ_TAINTED(mod) && rb_safe_level() >= 4)
|
1999-08-13 01:45:20 -04:00
|
|
|
rb_raise(rb_eSecurityError, "Insecure: can't get metainfo");
|
1999-01-19 23:59:39 -05:00
|
|
|
ary = rb_ary_new();
|
1998-01-16 07:19:22 -05:00
|
|
|
for (klass = mod; klass; klass = RCLASS(klass)->super) {
|
|
|
|
st_foreach(RCLASS(klass)->m_tbl, func, ary);
|
|
|
|
if (!option) break;
|
|
|
|
}
|
|
|
|
p = q = RARRAY(ary)->ptr; pend = p + RARRAY(ary)->len;
|
|
|
|
while (p < pend) {
|
|
|
|
if (*p == Qnil) {
|
|
|
|
p+=2;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
*q++ = *p++;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
1998-01-16 07:19:22 -05:00
|
|
|
RARRAY(ary)->len = q - RARRAY(ary)->ptr;
|
|
|
|
return ary;
|
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_class_instance_methods(argc, argv, mod)
|
1998-01-16 07:19:22 -05:00
|
|
|
int argc;
|
|
|
|
VALUE *argv;
|
|
|
|
VALUE mod;
|
|
|
|
{
|
|
|
|
VALUE option;
|
|
|
|
|
|
|
|
rb_scan_args(argc, argv, "01", &option);
|
|
|
|
return method_list(mod, RTEST(option), ins_methods_i);
|
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_class_protected_instance_methods(argc, argv, mod)
|
|
|
|
int argc;
|
|
|
|
VALUE *argv;
|
|
|
|
VALUE mod;
|
|
|
|
{
|
|
|
|
VALUE option;
|
|
|
|
|
|
|
|
rb_scan_args(argc, argv, "01", &option);
|
|
|
|
return method_list(mod, RTEST(option), ins_methods_prot_i);
|
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
|
|
|
rb_class_private_instance_methods(argc, argv, mod)
|
1998-01-16 07:19:22 -05:00
|
|
|
int argc;
|
|
|
|
VALUE *argv;
|
|
|
|
VALUE mod;
|
|
|
|
{
|
|
|
|
VALUE option;
|
|
|
|
|
|
|
|
rb_scan_args(argc, argv, "01", &option);
|
|
|
|
return method_list(mod, RTEST(option), ins_methods_priv_i);
|
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_obj_singleton_methods(obj)
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE obj;
|
|
|
|
{
|
|
|
|
VALUE ary;
|
|
|
|
VALUE klass;
|
|
|
|
VALUE *p, *q, *pend;
|
|
|
|
|
1999-12-14 01:50:43 -05:00
|
|
|
if (rb_safe_level() >= 4 && !OBJ_TAINTED(obj))
|
1999-08-13 01:45:20 -04:00
|
|
|
rb_raise(rb_eSecurityError, "Insecure: can't get metainfo");
|
1999-01-19 23:59:39 -05:00
|
|
|
ary = rb_ary_new();
|
1998-01-16 07:19:22 -05:00
|
|
|
klass = CLASS_OF(obj);
|
|
|
|
while (klass && FL_TEST(klass, FL_SINGLETON)) {
|
|
|
|
st_foreach(RCLASS(klass)->m_tbl, ins_methods_i, ary);
|
|
|
|
klass = RCLASS(klass)->super;
|
|
|
|
}
|
|
|
|
p = q = RARRAY(ary)->ptr; pend = p + RARRAY(ary)->len;
|
|
|
|
while (p < pend) {
|
|
|
|
if (*p == Qnil) {
|
|
|
|
p+=2;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
*q++ = *p++;
|
|
|
|
}
|
|
|
|
RARRAY(ary)->len = q - RARRAY(ary)->ptr;
|
|
|
|
|
|
|
|
return ary;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
1998-01-16 07:19:22 -05:00
|
|
|
rb_define_method_id(klass, name, func, argc)
|
|
|
|
VALUE klass;
|
1998-01-16 07:13:05 -05:00
|
|
|
ID name;
|
|
|
|
VALUE (*func)();
|
|
|
|
int argc;
|
|
|
|
{
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_add_method(klass, name, NEW_CFUNC(func,argc), NOEX_PUBLIC|NOEX_CFUNC);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
1998-01-16 07:19:22 -05:00
|
|
|
rb_define_method(klass, name, func, argc)
|
|
|
|
VALUE klass;
|
1999-08-13 01:45:20 -04:00
|
|
|
const char *name;
|
1998-01-16 07:13:05 -05:00
|
|
|
VALUE (*func)();
|
|
|
|
int argc;
|
|
|
|
{
|
1999-01-19 23:59:39 -05:00
|
|
|
ID id = rb_intern(name);
|
|
|
|
|
|
|
|
rb_add_method(klass, id, NEW_CFUNC(func, argc),
|
|
|
|
((name[0] == 'i' && id == rb_intern("initialize"))?
|
|
|
|
NOEX_PRIVATE:NOEX_PUBLIC)|NOEX_CFUNC);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_define_protected_method(klass, name, func, argc)
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE klass;
|
1999-08-13 01:45:20 -04:00
|
|
|
const char *name;
|
1999-01-19 23:59:39 -05:00
|
|
|
VALUE (*func)();
|
|
|
|
int argc;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_add_method(klass, rb_intern(name), NEW_CFUNC(func, argc),
|
|
|
|
NOEX_PROTECTED|NOEX_CFUNC);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
1998-01-16 07:19:22 -05:00
|
|
|
rb_define_private_method(klass, name, func, argc)
|
|
|
|
VALUE klass;
|
1999-08-13 01:45:20 -04:00
|
|
|
const char *name;
|
1998-01-16 07:13:05 -05:00
|
|
|
VALUE (*func)();
|
|
|
|
int argc;
|
|
|
|
{
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_add_method(klass, rb_intern(name), NEW_CFUNC(func, argc),
|
|
|
|
NOEX_PRIVATE|NOEX_CFUNC);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
rb_undef_method(klass, name)
|
|
|
|
VALUE klass;
|
1999-08-13 01:45:20 -04:00
|
|
|
const char *name;
|
1999-01-19 23:59:39 -05:00
|
|
|
{
|
|
|
|
rb_add_method(klass, rb_intern(name), 0, NOEX_UNDEF);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
2000-05-01 05:42:38 -04:00
|
|
|
#define SPECIAL_SINGLETON(x,c) if (obj == (x)) {\
|
|
|
|
if (!FL_TEST(c, FL_SINGLETON)) {\
|
|
|
|
c = rb_singleton_class_new(c);\
|
|
|
|
rb_singleton_class_attached(c,obj);\
|
|
|
|
}\
|
|
|
|
return c;\
|
|
|
|
}
|
|
|
|
|
1998-01-16 07:13:05 -05:00
|
|
|
VALUE
|
|
|
|
rb_singleton_class(obj)
|
1998-01-16 07:19:22 -05:00
|
|
|
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);
|
|
|
|
rb_bug("unknown immediate %d", obj);
|
|
|
|
}
|
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
if (FL_TEST(RBASIC(obj)->klass, FL_SINGLETON)) {
|
1999-12-14 01:50:43 -05:00
|
|
|
klass = RBASIC(obj)->klass;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
klass = rb_singleton_class_new(RBASIC(obj)->klass);
|
|
|
|
RBASIC(obj)->klass = klass;
|
|
|
|
rb_singleton_class_attached(klass, obj);
|
|
|
|
}
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
return klass;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
rb_define_singleton_method(obj, name, func, argc)
|
|
|
|
VALUE obj;
|
1999-08-13 01:45:20 -04:00
|
|
|
const char *name;
|
1998-01-16 07:13:05 -05:00
|
|
|
VALUE (*func)();
|
|
|
|
int argc;
|
|
|
|
{
|
|
|
|
rb_define_method(rb_singleton_class(obj), name, func, argc);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
rb_define_module_function(module, name, func, argc)
|
|
|
|
VALUE module;
|
1999-08-13 01:45:20 -04:00
|
|
|
const char *name;
|
1998-01-16 07:13:05 -05:00
|
|
|
VALUE (*func)();
|
|
|
|
int argc;
|
|
|
|
{
|
|
|
|
rb_define_private_method(module, name, func, argc);
|
|
|
|
rb_define_singleton_method(module, name, func, argc);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
rb_define_global_function(name, func, argc)
|
1999-08-13 01:45:20 -04:00
|
|
|
const char *name;
|
1998-01-16 07:13:05 -05:00
|
|
|
VALUE (*func)();
|
|
|
|
int argc;
|
|
|
|
{
|
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
|
|
|
}
|
|
|
|
|
|
|
|
void
|
1998-01-16 07:19:22 -05:00
|
|
|
rb_define_alias(klass, name1, name2)
|
|
|
|
VALUE klass;
|
1999-08-13 01:45:20 -04:00
|
|
|
const char *name1, *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
|
|
|
}
|
|
|
|
|
|
|
|
void
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_define_attr(klass, name, read, write)
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE klass;
|
1999-08-13 01:45:20 -04:00
|
|
|
const char *name;
|
1998-01-16 07:19:22 -05:00
|
|
|
int read, write;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_attr(klass, rb_intern(name), read, write, Qfalse);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
#ifdef HAVE_STDARG_PROTOTYPES
|
|
|
|
#include <stdarg.h>
|
|
|
|
#define va_init_list(a,b) va_start(a,b)
|
|
|
|
#else
|
1998-01-16 07:13:05 -05:00
|
|
|
#include <varargs.h>
|
1999-01-19 23:59:39 -05:00
|
|
|
#define va_init_list(a,b) va_start(a)
|
|
|
|
#endif
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
int
|
1999-01-19 23:59:39 -05:00
|
|
|
#ifdef HAVE_STDARG_PROTOTYPES
|
1999-08-13 01:45:20 -04:00
|
|
|
rb_scan_args(int argc, VALUE *argv, const char *fmt, ...)
|
1999-01-19 23:59:39 -05:00
|
|
|
#else
|
1998-01-16 07:13:05 -05:00
|
|
|
rb_scan_args(argc, argv, fmt, va_alist)
|
|
|
|
int argc;
|
|
|
|
VALUE *argv;
|
1999-08-13 01:45:20 -04:00
|
|
|
const char *fmt;
|
1998-01-16 07:13:05 -05:00
|
|
|
va_dcl
|
1999-01-19 23:59:39 -05:00
|
|
|
#endif
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
2000-02-18 01:59:36 -05:00
|
|
|
int n, i = 0;
|
1999-08-13 01:45:20 -04:00
|
|
|
const char *p = fmt;
|
1998-01-16 07:13:05 -05:00
|
|
|
VALUE *var;
|
|
|
|
va_list vargs;
|
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
va_init_list(vargs, fmt);
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2000-02-24 22:51:23 -05:00
|
|
|
if (*p == '*') goto rest_arg;
|
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
if (ISDIGIT(*p)) {
|
1998-01-16 07:13:05 -05:00
|
|
|
n = *p - '0';
|
|
|
|
if (n > argc)
|
1999-08-13 01:45:20 -04:00
|
|
|
rb_raise(rb_eArgError, "wrong # of arguments (%d for %d)", argc, n);
|
1998-01-16 07:13:05 -05:00
|
|
|
for (i=0; i<n; i++) {
|
|
|
|
var = va_arg(vargs, VALUE*);
|
1999-08-13 01:45:20 -04:00
|
|
|
if (var) *var = argv[i];
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
if (ISDIGIT(*p)) {
|
1998-01-16 07:13:05 -05:00
|
|
|
n = i + *p - '0';
|
|
|
|
for (; i<n; i++) {
|
|
|
|
var = va_arg(vargs, VALUE*);
|
|
|
|
if (argc > i) {
|
1999-08-13 01:45:20 -04:00
|
|
|
if (var) *var = argv[i];
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
else {
|
1999-08-13 01:45:20 -04:00
|
|
|
if (var) *var = Qnil;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(*p == '*') {
|
2000-02-24 22:51:23 -05:00
|
|
|
rest_arg:
|
1998-01-16 07:13:05 -05:00
|
|
|
var = va_arg(vargs, VALUE*);
|
|
|
|
if (argc > i) {
|
1999-08-13 01:45:20 -04:00
|
|
|
if (var) *var = rb_ary_new4(argc-i, argv+i);
|
2000-02-18 01:59:36 -05:00
|
|
|
i = argc;
|
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
|
|
|
}
|
2000-02-18 01:59:36 -05:00
|
|
|
p++;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
2000-02-18 01:59:36 -05:00
|
|
|
|
|
|
|
if (*p == '&') {
|
|
|
|
var = va_arg(vargs, VALUE*);
|
|
|
|
if (rb_iterator_p()) {
|
|
|
|
*var = rb_f_lambda();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
*var = Qnil;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
2000-02-18 01:59:36 -05:00
|
|
|
p++;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
2000-02-18 01:59:36 -05:00
|
|
|
va_end(vargs);
|
|
|
|
|
|
|
|
if (*p != '\0') {
|
1998-01-16 07:13:05 -05:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
2000-02-18 01:59:36 -05:00
|
|
|
if (argc > i) {
|
|
|
|
rb_raise(rb_eArgError, "wrong # of arguments(%d for %d)", argc, i);
|
|
|
|
}
|
|
|
|
|
1998-01-16 07:13:05 -05:00
|
|
|
return argc;
|
|
|
|
|
|
|
|
error:
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_fatal("bad scan arg format: %s", fmt);
|
1998-01-16 07:13:05 -05:00
|
|
|
return 0;
|
|
|
|
}
|