|
Ruby
2.0.0p451(2014-02-24revision45167)
|
00001 /* 00002 * This file is included by vm.c 00003 */ 00004 00005 #define CACHE_SIZE 0x800 00006 #define CACHE_MASK 0x7ff 00007 #define EXPR1(c,m) ((((c)>>3)^(m))&CACHE_MASK) 00008 00009 #define NOEX_NOREDEF 0 00010 #ifndef NOEX_NOREDEF 00011 #define NOEX_NOREDEF NOEX_RESPONDS 00012 #endif 00013 00014 static void rb_vm_check_redefinition_opt_method(const rb_method_entry_t *me, VALUE klass); 00015 00016 static ID object_id; 00017 static ID removed, singleton_removed, undefined, singleton_undefined; 00018 static ID added, singleton_added, attached; 00019 00020 struct cache_entry { /* method hash table. */ 00021 VALUE filled_version; /* filled state version */ 00022 ID mid; /* method's id */ 00023 VALUE klass; /* receiver's class */ 00024 rb_method_entry_t *me; 00025 VALUE defined_class; 00026 }; 00027 00028 static struct cache_entry cache[CACHE_SIZE]; 00029 #define ruby_running (GET_VM()->running) 00030 /* int ruby_running = 0; */ 00031 00032 static void 00033 vm_clear_global_method_cache(void) 00034 { 00035 struct cache_entry *ent, *end; 00036 00037 ent = cache; 00038 end = ent + CACHE_SIZE; 00039 while (ent < end) { 00040 ent->filled_version = 0; 00041 ent++; 00042 } 00043 } 00044 00045 void 00046 rb_clear_cache(void) 00047 { 00048 rb_vm_change_state(); 00049 } 00050 00051 static void 00052 rb_clear_cache_for_undef(VALUE klass, ID id) 00053 { 00054 rb_vm_change_state(); 00055 } 00056 00057 static void 00058 rb_clear_cache_by_id(ID id) 00059 { 00060 rb_vm_change_state(); 00061 } 00062 00063 void 00064 rb_clear_cache_by_class(VALUE klass) 00065 { 00066 rb_vm_change_state(); 00067 } 00068 00069 VALUE 00070 rb_f_notimplement(int argc, VALUE *argv, VALUE obj) 00071 { 00072 rb_notimplement(); 00073 00074 UNREACHABLE; 00075 } 00076 00077 static void 00078 rb_define_notimplement_method_id(VALUE mod, ID id, rb_method_flag_t noex) 00079 { 00080 rb_add_method(mod, id, VM_METHOD_TYPE_NOTIMPLEMENTED, 0, noex); 00081 } 00082 00083 void 00084 rb_add_method_cfunc(VALUE klass, ID mid, VALUE (*func)(ANYARGS), int argc, rb_method_flag_t noex) 00085 { 00086 if (argc < -2 || 15 < argc) rb_raise(rb_eArgError, "arity out of range: %d for -2..15", argc); 00087 if (func != rb_f_notimplement) { 00088 rb_method_cfunc_t opt; 00089 opt.func = func; 00090 opt.argc = argc; 00091 rb_add_method(klass, mid, VM_METHOD_TYPE_CFUNC, &opt, noex); 00092 } 00093 else { 00094 rb_define_notimplement_method_id(klass, mid, noex); 00095 } 00096 } 00097 00098 void 00099 rb_unlink_method_entry(rb_method_entry_t *me) 00100 { 00101 struct unlinked_method_entry_list_entry *ume = ALLOC(struct unlinked_method_entry_list_entry); 00102 ume->me = me; 00103 ume->next = GET_VM()->unlinked_method_entry_list; 00104 GET_VM()->unlinked_method_entry_list = ume; 00105 } 00106 00107 void 00108 rb_gc_mark_unlinked_live_method_entries(void *pvm) 00109 { 00110 rb_vm_t *vm = pvm; 00111 struct unlinked_method_entry_list_entry *ume = vm->unlinked_method_entry_list; 00112 00113 while (ume) { 00114 if (ume->me->mark) { 00115 rb_mark_method_entry(ume->me); 00116 } 00117 ume = ume->next; 00118 } 00119 } 00120 00121 void 00122 rb_sweep_method_entry(void *pvm) 00123 { 00124 rb_vm_t *vm = pvm; 00125 struct unlinked_method_entry_list_entry *ume = vm->unlinked_method_entry_list, *prev_ume = 0, *curr_ume; 00126 00127 while (ume) { 00128 if (ume->me->mark) { 00129 ume->me->mark = 0; 00130 prev_ume = ume; 00131 ume = ume->next; 00132 } 00133 else { 00134 rb_free_method_entry(ume->me); 00135 00136 if (prev_ume == 0) { 00137 vm->unlinked_method_entry_list = ume->next; 00138 } 00139 else { 00140 prev_ume->next = ume->next; 00141 } 00142 00143 curr_ume = ume; 00144 ume = ume->next; 00145 xfree(curr_ume); 00146 } 00147 } 00148 } 00149 00150 static void 00151 release_method_definition(rb_method_definition_t *def) 00152 { 00153 if (def == 0) 00154 return; 00155 if (def->alias_count == 0) { 00156 if (def->type == VM_METHOD_TYPE_REFINED && 00157 def->body.orig_me) { 00158 release_method_definition(def->body.orig_me->def); 00159 xfree(def->body.orig_me); 00160 } 00161 xfree(def); 00162 } 00163 else if (def->alias_count > 0) { 00164 def->alias_count--; 00165 } 00166 } 00167 00168 void 00169 rb_free_method_entry(rb_method_entry_t *me) 00170 { 00171 release_method_definition(me->def); 00172 xfree(me); 00173 } 00174 00175 static int rb_method_definition_eq(const rb_method_definition_t *d1, const rb_method_definition_t *d2); 00176 00177 static inline rb_method_entry_t * 00178 lookup_method_table(VALUE klass, ID id) 00179 { 00180 st_data_t body; 00181 st_table *m_tbl = RCLASS_M_TBL(klass); 00182 if (st_lookup(m_tbl, id, &body)) { 00183 return (rb_method_entry_t *) body; 00184 } 00185 else { 00186 return 0; 00187 } 00188 } 00189 00190 static void 00191 make_method_entry_refined(rb_method_entry_t *me) 00192 { 00193 rb_method_definition_t *new_def; 00194 00195 if (me->def && me->def->type == VM_METHOD_TYPE_REFINED) 00196 return; 00197 00198 new_def = ALLOC(rb_method_definition_t); 00199 new_def->type = VM_METHOD_TYPE_REFINED; 00200 new_def->original_id = me->called_id; 00201 new_def->alias_count = 0; 00202 new_def->body.orig_me = ALLOC(rb_method_entry_t); 00203 *new_def->body.orig_me = *me; 00204 rb_vm_check_redefinition_opt_method(me, me->klass); 00205 if (me->def) me->def->alias_count++; 00206 me->flag = NOEX_WITH_SAFE(NOEX_PUBLIC); 00207 me->def = new_def; 00208 } 00209 00210 void 00211 rb_add_refined_method_entry(VALUE refined_class, ID mid) 00212 { 00213 rb_method_entry_t *me = lookup_method_table(refined_class, mid); 00214 00215 if (me) { 00216 make_method_entry_refined(me); 00217 } 00218 else { 00219 rb_add_method(refined_class, mid, VM_METHOD_TYPE_REFINED, 0, 00220 NOEX_PUBLIC); 00221 } 00222 } 00223 00224 static rb_method_entry_t * 00225 rb_method_entry_make(VALUE klass, ID mid, rb_method_type_t type, 00226 rb_method_definition_t *def, rb_method_flag_t noex, 00227 VALUE defined_class) 00228 { 00229 rb_method_entry_t *me; 00230 #if NOEX_NOREDEF 00231 VALUE rklass; 00232 #endif 00233 st_table *mtbl; 00234 st_data_t data; 00235 int make_refined = 0; 00236 00237 if (NIL_P(klass)) { 00238 klass = rb_cObject; 00239 } 00240 if (rb_safe_level() >= 4 && 00241 (klass == rb_cObject || !OBJ_UNTRUSTED(klass))) { 00242 rb_raise(rb_eSecurityError, "Insecure: can't define method"); 00243 } 00244 if (!FL_TEST(klass, FL_SINGLETON) && 00245 type != VM_METHOD_TYPE_NOTIMPLEMENTED && 00246 type != VM_METHOD_TYPE_ZSUPER && 00247 (mid == idInitialize || mid == idInitialize_copy || 00248 mid == idInitialize_clone || mid == idInitialize_dup || 00249 mid == idRespond_to_missing)) { 00250 noex = NOEX_PRIVATE | noex; 00251 } 00252 00253 rb_check_frozen(klass); 00254 #if NOEX_NOREDEF 00255 rklass = klass; 00256 #endif 00257 if (FL_TEST(klass, RMODULE_IS_REFINEMENT)) { 00258 VALUE refined_class = 00259 rb_refinement_module_get_refined_class(klass); 00260 00261 rb_add_refined_method_entry(refined_class, mid); 00262 } 00263 if (type == VM_METHOD_TYPE_REFINED) { 00264 rb_method_entry_t *old_me = 00265 lookup_method_table(RCLASS_ORIGIN(klass), mid); 00266 if (old_me) rb_vm_check_redefinition_opt_method(old_me, klass); 00267 } 00268 else { 00269 klass = RCLASS_ORIGIN(klass); 00270 } 00271 mtbl = RCLASS_M_TBL(klass); 00272 00273 /* check re-definition */ 00274 if (st_lookup(mtbl, mid, &data)) { 00275 rb_method_entry_t *old_me = (rb_method_entry_t *)data; 00276 rb_method_definition_t *old_def = old_me->def; 00277 00278 if (rb_method_definition_eq(old_def, def)) return old_me; 00279 #if NOEX_NOREDEF 00280 if (old_me->flag & NOEX_NOREDEF) { 00281 rb_raise(rb_eTypeError, "cannot redefine %"PRIsVALUE"#%"PRIsVALUE, 00282 rb_class_name(rklass), rb_id2str(mid)); 00283 } 00284 #endif 00285 rb_vm_check_redefinition_opt_method(old_me, klass); 00286 if (old_def->type == VM_METHOD_TYPE_REFINED) 00287 make_refined = 1; 00288 00289 if (RTEST(ruby_verbose) && 00290 type != VM_METHOD_TYPE_UNDEF && 00291 old_def->alias_count == 0 && 00292 old_def->type != VM_METHOD_TYPE_UNDEF && 00293 old_def->type != VM_METHOD_TYPE_ZSUPER) { 00294 rb_iseq_t *iseq = 0; 00295 00296 rb_warning("method redefined; discarding old %s", rb_id2name(mid)); 00297 switch (old_def->type) { 00298 case VM_METHOD_TYPE_ISEQ: 00299 iseq = old_def->body.iseq; 00300 break; 00301 case VM_METHOD_TYPE_BMETHOD: 00302 iseq = rb_proc_get_iseq(old_def->body.proc, 0); 00303 break; 00304 default: 00305 break; 00306 } 00307 if (iseq && !NIL_P(iseq->location.path)) { 00308 int line = iseq->line_info_table ? rb_iseq_first_lineno(iseq) : 0; 00309 rb_compile_warning(RSTRING_PTR(iseq->location.path), line, 00310 "previous definition of %s was here", 00311 rb_id2name(old_def->original_id)); 00312 } 00313 } 00314 00315 rb_unlink_method_entry(old_me); 00316 } 00317 00318 me = ALLOC(rb_method_entry_t); 00319 00320 rb_clear_cache_by_id(mid); 00321 00322 me->flag = NOEX_WITH_SAFE(noex); 00323 me->mark = 0; 00324 me->called_id = mid; 00325 me->klass = defined_class; 00326 me->def = def; 00327 if (def) def->alias_count++; 00328 00329 /* check mid */ 00330 if (klass == rb_cObject && mid == idInitialize) { 00331 rb_warn("redefining Object#initialize may cause infinite loop"); 00332 } 00333 /* check mid */ 00334 if (mid == object_id || mid == id__send__) { 00335 if (type == VM_METHOD_TYPE_ISEQ) { 00336 rb_warn("redefining `%s' may cause serious problems", rb_id2name(mid)); 00337 } 00338 } 00339 00340 if (make_refined) { 00341 make_method_entry_refined(me); 00342 } 00343 00344 st_insert(mtbl, mid, (st_data_t) me); 00345 00346 return me; 00347 } 00348 00349 #define CALL_METHOD_HOOK(klass, hook, mid) do { \ 00350 const VALUE arg = ID2SYM(mid); \ 00351 VALUE recv_class = (klass); \ 00352 ID hook_id = (hook); \ 00353 if (FL_TEST((klass), FL_SINGLETON)) { \ 00354 recv_class = rb_ivar_get((klass), attached); \ 00355 hook_id = singleton_##hook; \ 00356 } \ 00357 rb_funcall2(recv_class, hook_id, 1, &arg); \ 00358 } while (0) 00359 00360 static void 00361 method_added(VALUE klass, ID mid) 00362 { 00363 if (ruby_running) { 00364 CALL_METHOD_HOOK(klass, added, mid); 00365 } 00366 } 00367 00368 static VALUE 00369 (*call_cfunc_invoker_func(int argc))(VALUE (*func)(ANYARGS), VALUE recv, int argc, const VALUE *) 00370 { 00371 switch (argc) { 00372 case -2: return &call_cfunc_m2; 00373 case -1: return &call_cfunc_m1; 00374 case 0: return &call_cfunc_0; 00375 case 1: return &call_cfunc_1; 00376 case 2: return &call_cfunc_2; 00377 case 3: return &call_cfunc_3; 00378 case 4: return &call_cfunc_4; 00379 case 5: return &call_cfunc_5; 00380 case 6: return &call_cfunc_6; 00381 case 7: return &call_cfunc_7; 00382 case 8: return &call_cfunc_8; 00383 case 9: return &call_cfunc_9; 00384 case 10: return &call_cfunc_10; 00385 case 11: return &call_cfunc_11; 00386 case 12: return &call_cfunc_12; 00387 case 13: return &call_cfunc_13; 00388 case 14: return &call_cfunc_14; 00389 case 15: return &call_cfunc_15; 00390 default: 00391 rb_bug("call_cfunc_func: unsupported length: %d", argc); 00392 } 00393 } 00394 00395 static void 00396 setup_method_cfunc_struct(rb_method_cfunc_t *cfunc, VALUE (*func)(), int argc) 00397 { 00398 cfunc->func = func; 00399 cfunc->argc = argc; 00400 cfunc->invoker = call_cfunc_invoker_func(argc); 00401 } 00402 00403 rb_method_entry_t * 00404 rb_add_method(VALUE klass, ID mid, rb_method_type_t type, void *opts, rb_method_flag_t noex) 00405 { 00406 rb_thread_t *th; 00407 rb_control_frame_t *cfp; 00408 int line; 00409 rb_method_entry_t *me = rb_method_entry_make(klass, mid, type, 0, noex, klass); 00410 rb_method_definition_t *def = ALLOC(rb_method_definition_t); 00411 if (me->def && me->def->type == VM_METHOD_TYPE_REFINED) { 00412 me->def->body.orig_me->def = def; 00413 } 00414 else { 00415 me->def = def; 00416 } 00417 def->type = type; 00418 def->original_id = mid; 00419 def->alias_count = 0; 00420 switch (type) { 00421 case VM_METHOD_TYPE_ISEQ: 00422 def->body.iseq = (rb_iseq_t *)opts; 00423 break; 00424 case VM_METHOD_TYPE_CFUNC: 00425 { 00426 rb_method_cfunc_t *cfunc = (rb_method_cfunc_t *)opts; 00427 setup_method_cfunc_struct(&def->body.cfunc, cfunc->func, cfunc->argc); 00428 } 00429 break; 00430 case VM_METHOD_TYPE_ATTRSET: 00431 case VM_METHOD_TYPE_IVAR: 00432 def->body.attr.id = (ID)opts; 00433 def->body.attr.location = Qfalse; 00434 th = GET_THREAD(); 00435 cfp = rb_vm_get_ruby_level_next_cfp(th, th->cfp); 00436 if (cfp && (line = rb_vm_get_sourceline(cfp))) { 00437 VALUE location = rb_ary_new3(2, cfp->iseq->location.path, INT2FIX(line)); 00438 def->body.attr.location = rb_ary_freeze(location); 00439 } 00440 break; 00441 case VM_METHOD_TYPE_BMETHOD: 00442 def->body.proc = (VALUE)opts; 00443 break; 00444 case VM_METHOD_TYPE_NOTIMPLEMENTED: 00445 setup_method_cfunc_struct(&def->body.cfunc, rb_f_notimplement, -1); 00446 break; 00447 case VM_METHOD_TYPE_OPTIMIZED: 00448 def->body.optimize_type = (enum method_optimized_type)opts; 00449 break; 00450 case VM_METHOD_TYPE_ZSUPER: 00451 case VM_METHOD_TYPE_UNDEF: 00452 break; 00453 case VM_METHOD_TYPE_REFINED: 00454 def->body.orig_me = (rb_method_entry_t *) opts; 00455 break; 00456 default: 00457 rb_bug("rb_add_method: unsupported method type (%d)\n", type); 00458 } 00459 if (type != VM_METHOD_TYPE_UNDEF && type != VM_METHOD_TYPE_REFINED) { 00460 method_added(klass, mid); 00461 } 00462 return me; 00463 } 00464 00465 static rb_method_entry_t * 00466 method_entry_set(VALUE klass, ID mid, const rb_method_entry_t *me, 00467 rb_method_flag_t noex, VALUE defined_class) 00468 { 00469 rb_method_type_t type = me->def ? me->def->type : VM_METHOD_TYPE_UNDEF; 00470 rb_method_entry_t *newme = rb_method_entry_make(klass, mid, type, me->def, noex, 00471 defined_class); 00472 method_added(klass, mid); 00473 return newme; 00474 } 00475 00476 rb_method_entry_t * 00477 rb_method_entry_set(VALUE klass, ID mid, const rb_method_entry_t *me, rb_method_flag_t noex) 00478 { 00479 return method_entry_set(klass, mid, me, noex, klass); 00480 } 00481 00482 #define UNDEF_ALLOC_FUNC ((rb_alloc_func_t)-1) 00483 00484 void 00485 rb_define_alloc_func(VALUE klass, VALUE (*func)(VALUE)) 00486 { 00487 Check_Type(klass, T_CLASS); 00488 RCLASS_EXT(klass)->allocator = func; 00489 } 00490 00491 void 00492 rb_undef_alloc_func(VALUE klass) 00493 { 00494 rb_define_alloc_func(klass, UNDEF_ALLOC_FUNC); 00495 } 00496 00497 rb_alloc_func_t 00498 rb_get_alloc_func(VALUE klass) 00499 { 00500 Check_Type(klass, T_CLASS); 00501 00502 for (; klass; klass = RCLASS_SUPER(klass)) { 00503 rb_alloc_func_t allocator = RCLASS_EXT(klass)->allocator; 00504 if (allocator == UNDEF_ALLOC_FUNC) break; 00505 if (allocator) return allocator; 00506 } 00507 return 0; 00508 } 00509 00510 static inline rb_method_entry_t* 00511 search_method(VALUE klass, ID id, VALUE *defined_class_ptr) 00512 { 00513 rb_method_entry_t *me; 00514 00515 for (me = 0; klass; klass = RCLASS_SUPER(klass)) { 00516 if ((me = lookup_method_table(klass, id)) != 0) break; 00517 } 00518 00519 if (defined_class_ptr) 00520 *defined_class_ptr = klass; 00521 return me; 00522 } 00523 00524 /* 00525 * search method entry without the method cache. 00526 * 00527 * if you need method entry with method cache (normal case), use 00528 * rb_method_entry() simply. 00529 */ 00530 rb_method_entry_t * 00531 rb_method_entry_get_without_cache(VALUE klass, ID id, 00532 VALUE *defined_class_ptr) 00533 { 00534 VALUE defined_class; 00535 rb_method_entry_t *me = search_method(klass, id, &defined_class); 00536 00537 if (me && RB_TYPE_P(me->klass, T_ICLASS)) 00538 defined_class = me->klass; 00539 00540 if (ruby_running) { 00541 struct cache_entry *ent; 00542 ent = cache + EXPR1(klass, id); 00543 ent->filled_version = GET_VM_STATE_VERSION(); 00544 ent->klass = klass; 00545 ent->defined_class = defined_class; 00546 00547 if (UNDEFINED_METHOD_ENTRY_P(me)) { 00548 ent->mid = id; 00549 ent->me = 0; 00550 me = 0; 00551 } 00552 else { 00553 ent->mid = id; 00554 ent->me = me; 00555 } 00556 } 00557 00558 if (defined_class_ptr) 00559 *defined_class_ptr = defined_class; 00560 return me; 00561 } 00562 00563 rb_method_entry_t * 00564 rb_method_entry(VALUE klass, ID id, VALUE *defined_class_ptr) 00565 { 00566 #if OPT_GLOBAL_METHOD_CACHE 00567 struct cache_entry *ent; 00568 00569 ent = cache + EXPR1(klass, id); 00570 if (ent->filled_version == GET_VM_STATE_VERSION() && 00571 ent->mid == id && ent->klass == klass) { 00572 if (defined_class_ptr) 00573 *defined_class_ptr = ent->defined_class; 00574 return ent->me; 00575 } 00576 #endif 00577 00578 return rb_method_entry_get_without_cache(klass, id, defined_class_ptr); 00579 } 00580 00581 static rb_method_entry_t * 00582 get_original_method_entry(VALUE refinements, 00583 const rb_method_entry_t *me, 00584 VALUE *defined_class_ptr) 00585 { 00586 if (me->def->body.orig_me) { 00587 return me->def->body.orig_me; 00588 } 00589 else { 00590 rb_method_entry_t *tmp_me; 00591 tmp_me = rb_method_entry(RCLASS_SUPER(me->klass), me->called_id, 00592 defined_class_ptr); 00593 return rb_resolve_refined_method(refinements, tmp_me, 00594 defined_class_ptr); 00595 } 00596 } 00597 00598 rb_method_entry_t * 00599 rb_resolve_refined_method(VALUE refinements, const rb_method_entry_t *me, 00600 VALUE *defined_class_ptr) 00601 { 00602 if (me && me->def->type == VM_METHOD_TYPE_REFINED) { 00603 VALUE refinement; 00604 rb_method_entry_t *tmp_me; 00605 00606 refinement = find_refinement(refinements, me->klass); 00607 if (NIL_P(refinement)) { 00608 return get_original_method_entry(refinements, me, 00609 defined_class_ptr); 00610 } 00611 tmp_me = rb_method_entry(refinement, me->called_id, 00612 defined_class_ptr); 00613 if (tmp_me && tmp_me->def->type != VM_METHOD_TYPE_REFINED) { 00614 return tmp_me; 00615 } 00616 else { 00617 return get_original_method_entry(refinements, me, 00618 defined_class_ptr); 00619 } 00620 } 00621 else { 00622 return (rb_method_entry_t *)me; 00623 } 00624 } 00625 00626 rb_method_entry_t * 00627 rb_method_entry_with_refinements(VALUE klass, ID id, 00628 VALUE *defined_class_ptr) 00629 { 00630 VALUE defined_class; 00631 rb_method_entry_t *me = rb_method_entry(klass, id, &defined_class); 00632 00633 if (me && me->def->type == VM_METHOD_TYPE_REFINED) { 00634 NODE *cref = rb_vm_cref(); 00635 VALUE refinements = cref ? cref->nd_refinements : Qnil; 00636 00637 me = rb_resolve_refined_method(refinements, me, &defined_class); 00638 } 00639 if (defined_class_ptr) 00640 *defined_class_ptr = defined_class; 00641 return me; 00642 } 00643 00644 rb_method_entry_t * 00645 rb_method_entry_without_refinements(VALUE klass, ID id, 00646 VALUE *defined_class_ptr) 00647 { 00648 VALUE defined_class; 00649 rb_method_entry_t *me = rb_method_entry(klass, id, &defined_class); 00650 00651 if (me && me->def->type == VM_METHOD_TYPE_REFINED) { 00652 me = rb_resolve_refined_method(Qnil, me, &defined_class); 00653 } 00654 if (defined_class_ptr) 00655 *defined_class_ptr = defined_class; 00656 if (UNDEFINED_METHOD_ENTRY_P(me)) { 00657 return 0; 00658 } 00659 else { 00660 return me; 00661 } 00662 } 00663 00664 static void 00665 remove_method(VALUE klass, ID mid) 00666 { 00667 st_data_t key, data; 00668 rb_method_entry_t *me = 0; 00669 VALUE self = klass; 00670 00671 klass = RCLASS_ORIGIN(klass); 00672 if (klass == rb_cObject) { 00673 rb_secure(4); 00674 } 00675 if (rb_safe_level() >= 4 && !OBJ_UNTRUSTED(klass)) { 00676 rb_raise(rb_eSecurityError, "Insecure: can't remove method"); 00677 } 00678 rb_check_frozen(klass); 00679 if (mid == object_id || mid == id__send__ || mid == idInitialize) { 00680 rb_warn("removing `%s' may cause serious problems", rb_id2name(mid)); 00681 } 00682 00683 if (!st_lookup(RCLASS_M_TBL(klass), mid, &data) || 00684 !(me = (rb_method_entry_t *)data) || 00685 (!me->def || me->def->type == VM_METHOD_TYPE_UNDEF)) { 00686 rb_name_error(mid, "method `%s' not defined in %s", 00687 rb_id2name(mid), rb_class2name(klass)); 00688 } 00689 key = (st_data_t)mid; 00690 st_delete(RCLASS_M_TBL(klass), &key, &data); 00691 00692 rb_vm_check_redefinition_opt_method(me, klass); 00693 rb_clear_cache_for_undef(klass, mid); 00694 rb_unlink_method_entry(me); 00695 00696 CALL_METHOD_HOOK(self, removed, mid); 00697 } 00698 00699 void 00700 rb_remove_method_id(VALUE klass, ID mid) 00701 { 00702 remove_method(klass, mid); 00703 } 00704 00705 void 00706 rb_remove_method(VALUE klass, const char *name) 00707 { 00708 remove_method(klass, rb_intern(name)); 00709 } 00710 00711 /* 00712 * call-seq: 00713 * remove_method(symbol) -> self 00714 * 00715 * Removes the method identified by _symbol_ from the current 00716 * class. For an example, see <code>Module.undef_method</code>. 00717 */ 00718 00719 static VALUE 00720 rb_mod_remove_method(int argc, VALUE *argv, VALUE mod) 00721 { 00722 int i; 00723 00724 for (i = 0; i < argc; i++) { 00725 VALUE v = argv[i]; 00726 ID id = rb_check_id(&v); 00727 if (!id) { 00728 rb_name_error_str(v, "method `%s' not defined in %s", 00729 RSTRING_PTR(v), rb_class2name(mod)); 00730 } 00731 remove_method(mod, id); 00732 } 00733 return mod; 00734 } 00735 00736 #undef rb_disable_super 00737 #undef rb_enable_super 00738 00739 void 00740 rb_disable_super(VALUE klass, const char *name) 00741 { 00742 /* obsolete - no use */ 00743 } 00744 00745 void 00746 rb_enable_super(VALUE klass, const char *name) 00747 { 00748 rb_warning("rb_enable_super() is obsolete"); 00749 } 00750 00751 static void 00752 rb_export_method(VALUE klass, ID name, rb_method_flag_t noex) 00753 { 00754 rb_method_entry_t *me; 00755 VALUE defined_class; 00756 00757 if (klass == rb_cObject) { 00758 rb_secure(4); 00759 } 00760 00761 me = search_method(klass, name, &defined_class); 00762 if (!me && RB_TYPE_P(klass, T_MODULE)) { 00763 me = search_method(rb_cObject, name, &defined_class); 00764 } 00765 00766 if (UNDEFINED_METHOD_ENTRY_P(me)) { 00767 rb_print_undef(klass, name, 0); 00768 } 00769 00770 if (me->flag != noex) { 00771 rb_vm_check_redefinition_opt_method(me, klass); 00772 00773 if (klass == defined_class || 00774 RCLASS_ORIGIN(klass) == defined_class) { 00775 me->flag = noex; 00776 if (me->def->type == VM_METHOD_TYPE_REFINED) { 00777 me->def->body.orig_me->flag = noex; 00778 } 00779 } 00780 else { 00781 rb_add_method(klass, name, VM_METHOD_TYPE_ZSUPER, 0, noex); 00782 } 00783 } 00784 } 00785 00786 int 00787 rb_method_boundp(VALUE klass, ID id, int ex) 00788 { 00789 rb_method_entry_t *me = 00790 rb_method_entry_without_refinements(klass, id, 0); 00791 00792 if (me != 0) { 00793 if ((ex & ~NOEX_RESPONDS) && 00794 ((me->flag & NOEX_PRIVATE) || 00795 ((ex & NOEX_RESPONDS) && (me->flag & NOEX_PROTECTED)))) { 00796 return 0; 00797 } 00798 if (!me->def) return 0; 00799 if (me->def->type == VM_METHOD_TYPE_NOTIMPLEMENTED) { 00800 if (ex & NOEX_RESPONDS) return 2; 00801 return 0; 00802 } 00803 return 1; 00804 } 00805 return 0; 00806 } 00807 00808 void 00809 rb_attr(VALUE klass, ID id, int read, int write, int ex) 00810 { 00811 const char *name; 00812 ID attriv; 00813 VALUE aname; 00814 rb_method_flag_t noex; 00815 00816 if (!ex) { 00817 noex = NOEX_PUBLIC; 00818 } 00819 else { 00820 if (SCOPE_TEST(NOEX_PRIVATE)) { 00821 noex = NOEX_PRIVATE; 00822 rb_warning((SCOPE_CHECK(NOEX_MODFUNC)) ? 00823 "attribute accessor as module_function" : 00824 "private attribute?"); 00825 } 00826 else if (SCOPE_TEST(NOEX_PROTECTED)) { 00827 noex = NOEX_PROTECTED; 00828 } 00829 else { 00830 noex = NOEX_PUBLIC; 00831 } 00832 } 00833 00834 if (!rb_is_local_id(id) && !rb_is_const_id(id)) { 00835 rb_name_error(id, "invalid attribute name `%s'", rb_id2name(id)); 00836 } 00837 name = rb_id2name(id); 00838 if (!name) { 00839 rb_raise(rb_eArgError, "argument needs to be symbol or string"); 00840 } 00841 aname = rb_sprintf("@%s", name); 00842 rb_enc_copy(aname, rb_id2str(id)); 00843 attriv = rb_intern_str(aname); 00844 if (read) { 00845 rb_add_method(klass, id, VM_METHOD_TYPE_IVAR, (void *)attriv, noex); 00846 } 00847 if (write) { 00848 rb_add_method(klass, rb_id_attrset(id), VM_METHOD_TYPE_ATTRSET, (void *)attriv, noex); 00849 } 00850 } 00851 00852 void 00853 rb_undef(VALUE klass, ID id) 00854 { 00855 rb_method_entry_t *me; 00856 00857 if (NIL_P(klass)) { 00858 rb_raise(rb_eTypeError, "no class to undef method"); 00859 } 00860 if (rb_vm_cbase() == rb_cObject && klass == rb_cObject) { 00861 rb_secure(4); 00862 } 00863 if (rb_safe_level() >= 4 && !OBJ_UNTRUSTED(klass)) { 00864 rb_raise(rb_eSecurityError, "Insecure: can't undef `%s'", rb_id2name(id)); 00865 } 00866 rb_frozen_class_p(klass); 00867 if (id == object_id || id == id__send__ || id == idInitialize) { 00868 rb_warn("undefining `%s' may cause serious problems", rb_id2name(id)); 00869 } 00870 00871 me = search_method(klass, id, 0); 00872 00873 if (UNDEFINED_METHOD_ENTRY_P(me) || 00874 (me->def->type == VM_METHOD_TYPE_REFINED && 00875 UNDEFINED_METHOD_ENTRY_P(me->def->body.orig_me))) { 00876 const char *s0 = " class"; 00877 VALUE c = klass; 00878 00879 if (FL_TEST(c, FL_SINGLETON)) { 00880 VALUE obj = rb_ivar_get(klass, attached); 00881 00882 if (RB_TYPE_P(obj, T_MODULE) || RB_TYPE_P(obj, T_CLASS)) { 00883 c = obj; 00884 s0 = ""; 00885 } 00886 } 00887 else if (RB_TYPE_P(c, T_MODULE)) { 00888 s0 = " module"; 00889 } 00890 rb_name_error(id, "undefined method `%"PRIsVALUE"' for%s `%"PRIsVALUE"'", 00891 QUOTE_ID(id), s0, rb_class_name(c)); 00892 } 00893 00894 rb_add_method(klass, id, VM_METHOD_TYPE_UNDEF, 0, NOEX_PUBLIC); 00895 00896 CALL_METHOD_HOOK(klass, undefined, id); 00897 } 00898 00899 /* 00900 * call-seq: 00901 * undef_method(symbol) -> self 00902 * 00903 * Prevents the current class from responding to calls to the named 00904 * method. Contrast this with <code>remove_method</code>, which deletes 00905 * the method from the particular class; Ruby will still search 00906 * superclasses and mixed-in modules for a possible receiver. 00907 * 00908 * class Parent 00909 * def hello 00910 * puts "In parent" 00911 * end 00912 * end 00913 * class Child < Parent 00914 * def hello 00915 * puts "In child" 00916 * end 00917 * end 00918 * 00919 * 00920 * c = Child.new 00921 * c.hello 00922 * 00923 * 00924 * class Child 00925 * remove_method :hello # remove from child, still in parent 00926 * end 00927 * c.hello 00928 * 00929 * 00930 * class Child 00931 * undef_method :hello # prevent any calls to 'hello' 00932 * end 00933 * c.hello 00934 * 00935 * <em>produces:</em> 00936 * 00937 * In child 00938 * In parent 00939 * prog.rb:23: undefined method `hello' for #<Child:0x401b3bb4> (NoMethodError) 00940 */ 00941 00942 static VALUE 00943 rb_mod_undef_method(int argc, VALUE *argv, VALUE mod) 00944 { 00945 int i; 00946 for (i = 0; i < argc; i++) { 00947 VALUE v = argv[i]; 00948 ID id = rb_check_id(&v); 00949 if (!id) { 00950 rb_method_name_error(mod, v); 00951 } 00952 rb_undef(mod, id); 00953 } 00954 return mod; 00955 } 00956 00957 /* 00958 * call-seq: 00959 * mod.method_defined?(symbol) -> true or false 00960 * 00961 * Returns +true+ if the named method is defined by 00962 * _mod_ (or its included modules and, if _mod_ is a class, 00963 * its ancestors). Public and protected methods are matched. 00964 * 00965 * module A 00966 * def method1() end 00967 * end 00968 * class B 00969 * def method2() end 00970 * end 00971 * class C < B 00972 * include A 00973 * def method3() end 00974 * end 00975 * 00976 * A.method_defined? :method1 #=> true 00977 * C.method_defined? "method1" #=> true 00978 * C.method_defined? "method2" #=> true 00979 * C.method_defined? "method3" #=> true 00980 * C.method_defined? "method4" #=> false 00981 */ 00982 00983 static VALUE 00984 rb_mod_method_defined(VALUE mod, VALUE mid) 00985 { 00986 ID id = rb_check_id(&mid); 00987 if (!id || !rb_method_boundp(mod, id, 1)) { 00988 return Qfalse; 00989 } 00990 return Qtrue; 00991 00992 } 00993 00994 #define VISI_CHECK(x,f) (((x)&NOEX_MASK) == (f)) 00995 00996 static VALUE 00997 check_definition(VALUE mod, VALUE mid, rb_method_flag_t noex) 00998 { 00999 const rb_method_entry_t *me; 01000 ID id = rb_check_id(&mid); 01001 if (!id) return Qfalse; 01002 me = rb_method_entry(mod, id, 0); 01003 if (me) { 01004 if (VISI_CHECK(me->flag, noex)) 01005 return Qtrue; 01006 } 01007 return Qfalse; 01008 } 01009 01010 /* 01011 * call-seq: 01012 * mod.public_method_defined?(symbol) -> true or false 01013 * 01014 * Returns +true+ if the named public method is defined by 01015 * _mod_ (or its included modules and, if _mod_ is a class, 01016 * its ancestors). 01017 * 01018 * module A 01019 * def method1() end 01020 * end 01021 * class B 01022 * protected 01023 * def method2() end 01024 * end 01025 * class C < B 01026 * include A 01027 * def method3() end 01028 * end 01029 * 01030 * A.method_defined? :method1 #=> true 01031 * C.public_method_defined? "method1" #=> true 01032 * C.public_method_defined? "method2" #=> false 01033 * C.method_defined? "method2" #=> true 01034 */ 01035 01036 static VALUE 01037 rb_mod_public_method_defined(VALUE mod, VALUE mid) 01038 { 01039 return check_definition(mod, mid, NOEX_PUBLIC); 01040 } 01041 01042 /* 01043 * call-seq: 01044 * mod.private_method_defined?(symbol) -> true or false 01045 * 01046 * Returns +true+ if the named private method is defined by 01047 * _ mod_ (or its included modules and, if _mod_ is a class, 01048 * its ancestors). 01049 * 01050 * module A 01051 * def method1() end 01052 * end 01053 * class B 01054 * private 01055 * def method2() end 01056 * end 01057 * class C < B 01058 * include A 01059 * def method3() end 01060 * end 01061 * 01062 * A.method_defined? :method1 #=> true 01063 * C.private_method_defined? "method1" #=> false 01064 * C.private_method_defined? "method2" #=> true 01065 * C.method_defined? "method2" #=> false 01066 */ 01067 01068 static VALUE 01069 rb_mod_private_method_defined(VALUE mod, VALUE mid) 01070 { 01071 return check_definition(mod, mid, NOEX_PRIVATE); 01072 } 01073 01074 /* 01075 * call-seq: 01076 * mod.protected_method_defined?(symbol) -> true or false 01077 * 01078 * Returns +true+ if the named protected method is defined 01079 * by _mod_ (or its included modules and, if _mod_ is a 01080 * class, its ancestors). 01081 * 01082 * module A 01083 * def method1() end 01084 * end 01085 * class B 01086 * protected 01087 * def method2() end 01088 * end 01089 * class C < B 01090 * include A 01091 * def method3() end 01092 * end 01093 * 01094 * A.method_defined? :method1 #=> true 01095 * C.protected_method_defined? "method1" #=> false 01096 * C.protected_method_defined? "method2" #=> true 01097 * C.method_defined? "method2" #=> true 01098 */ 01099 01100 static VALUE 01101 rb_mod_protected_method_defined(VALUE mod, VALUE mid) 01102 { 01103 return check_definition(mod, mid, NOEX_PROTECTED); 01104 } 01105 01106 int 01107 rb_method_entry_eq(const rb_method_entry_t *m1, const rb_method_entry_t *m2) 01108 { 01109 return rb_method_definition_eq(m1->def, m2->def); 01110 } 01111 01112 static int 01113 rb_method_definition_eq(const rb_method_definition_t *d1, const rb_method_definition_t *d2) 01114 { 01115 if (d1 && d1->type == VM_METHOD_TYPE_REFINED && d1->body.orig_me) 01116 d1 = d1->body.orig_me->def; 01117 if (d2 && d2->type == VM_METHOD_TYPE_REFINED && d2->body.orig_me) 01118 d2 = d2->body.orig_me->def; 01119 if (d1 == d2) return 1; 01120 if (!d1 || !d2) return 0; 01121 if (d1->type != d2->type) { 01122 return 0; 01123 } 01124 switch (d1->type) { 01125 case VM_METHOD_TYPE_ISEQ: 01126 return d1->body.iseq == d2->body.iseq; 01127 case VM_METHOD_TYPE_CFUNC: 01128 return 01129 d1->body.cfunc.func == d2->body.cfunc.func && 01130 d1->body.cfunc.argc == d2->body.cfunc.argc; 01131 case VM_METHOD_TYPE_ATTRSET: 01132 case VM_METHOD_TYPE_IVAR: 01133 return d1->body.attr.id == d2->body.attr.id; 01134 case VM_METHOD_TYPE_BMETHOD: 01135 return RTEST(rb_equal(d1->body.proc, d2->body.proc)); 01136 case VM_METHOD_TYPE_MISSING: 01137 return d1->original_id == d2->original_id; 01138 case VM_METHOD_TYPE_ZSUPER: 01139 case VM_METHOD_TYPE_NOTIMPLEMENTED: 01140 case VM_METHOD_TYPE_UNDEF: 01141 return 1; 01142 case VM_METHOD_TYPE_OPTIMIZED: 01143 return d1->body.optimize_type == d2->body.optimize_type; 01144 default: 01145 rb_bug("rb_method_entry_eq: unsupported method type (%d)\n", d1->type); 01146 return 0; 01147 } 01148 } 01149 01150 static st_index_t 01151 rb_hash_method_definition(st_index_t hash, const rb_method_definition_t *def) 01152 { 01153 again: 01154 hash = rb_hash_uint(hash, def->type); 01155 switch (def->type) { 01156 case VM_METHOD_TYPE_ISEQ: 01157 return rb_hash_uint(hash, (st_index_t)def->body.iseq); 01158 case VM_METHOD_TYPE_CFUNC: 01159 hash = rb_hash_uint(hash, (st_index_t)def->body.cfunc.func); 01160 return rb_hash_uint(hash, def->body.cfunc.argc); 01161 case VM_METHOD_TYPE_ATTRSET: 01162 case VM_METHOD_TYPE_IVAR: 01163 return rb_hash_uint(hash, def->body.attr.id); 01164 case VM_METHOD_TYPE_BMETHOD: 01165 return rb_hash_proc(hash, def->body.proc); 01166 case VM_METHOD_TYPE_MISSING: 01167 return rb_hash_uint(hash, def->original_id); 01168 case VM_METHOD_TYPE_ZSUPER: 01169 case VM_METHOD_TYPE_NOTIMPLEMENTED: 01170 case VM_METHOD_TYPE_UNDEF: 01171 return hash; 01172 case VM_METHOD_TYPE_OPTIMIZED: 01173 return rb_hash_uint(hash, def->body.optimize_type); 01174 case VM_METHOD_TYPE_REFINED: 01175 if (def->body.orig_me) { 01176 def = def->body.orig_me->def; 01177 goto again; 01178 } 01179 else { 01180 return hash; 01181 } 01182 default: 01183 rb_bug("rb_hash_method_definition: unsupported method type (%d)\n", def->type); 01184 } 01185 return hash; 01186 } 01187 01188 st_index_t 01189 rb_hash_method_entry(st_index_t hash, const rb_method_entry_t *me) 01190 { 01191 return rb_hash_method_definition(hash, me->def); 01192 } 01193 01194 void 01195 rb_alias(VALUE klass, ID name, ID def) 01196 { 01197 VALUE target_klass = klass; 01198 VALUE defined_class; 01199 rb_method_entry_t *orig_me; 01200 rb_method_flag_t flag = NOEX_UNDEF; 01201 01202 if (NIL_P(klass)) { 01203 rb_raise(rb_eTypeError, "no class to make alias"); 01204 } 01205 01206 rb_frozen_class_p(klass); 01207 if (klass == rb_cObject) { 01208 rb_secure(4); 01209 } 01210 01211 again: 01212 orig_me = search_method(klass, def, &defined_class); 01213 01214 if (UNDEFINED_METHOD_ENTRY_P(orig_me)) { 01215 if ((!RB_TYPE_P(klass, T_MODULE)) || 01216 (orig_me = search_method(rb_cObject, def, 0), 01217 UNDEFINED_METHOD_ENTRY_P(orig_me))) { 01218 rb_print_undef(klass, def, 0); 01219 } 01220 } 01221 if (orig_me->def->type == VM_METHOD_TYPE_ZSUPER) { 01222 klass = RCLASS_SUPER(klass); 01223 def = orig_me->def->original_id; 01224 flag = orig_me->flag; 01225 goto again; 01226 } 01227 if (RB_TYPE_P(defined_class, T_ICLASS)) { 01228 VALUE real_class = RBASIC(defined_class)->klass; 01229 if (real_class && RCLASS_ORIGIN(real_class) == defined_class) 01230 defined_class = real_class; 01231 } 01232 01233 if (flag == NOEX_UNDEF) flag = orig_me->flag; 01234 method_entry_set(target_klass, name, orig_me, flag, defined_class); 01235 } 01236 01237 /* 01238 * call-seq: 01239 * alias_method(new_name, old_name) -> self 01240 * 01241 * Makes <i>new_name</i> a new copy of the method <i>old_name</i>. This can 01242 * be used to retain access to methods that are overridden. 01243 * 01244 * module Mod 01245 * alias_method :orig_exit, :exit 01246 * def exit(code=0) 01247 * puts "Exiting with code #{code}" 01248 * orig_exit(code) 01249 * end 01250 * end 01251 * include Mod 01252 * exit(99) 01253 * 01254 * <em>produces:</em> 01255 * 01256 * Exiting with code 99 01257 */ 01258 01259 static VALUE 01260 rb_mod_alias_method(VALUE mod, VALUE newname, VALUE oldname) 01261 { 01262 ID oldid = rb_check_id(&oldname); 01263 if (!oldid) { 01264 rb_print_undef_str(mod, oldname); 01265 } 01266 rb_alias(mod, rb_to_id(newname), oldid); 01267 return mod; 01268 } 01269 01270 static void 01271 secure_visibility(VALUE self) 01272 { 01273 if (rb_safe_level() >= 4 && !OBJ_UNTRUSTED(self)) { 01274 rb_raise(rb_eSecurityError, 01275 "Insecure: can't change method visibility"); 01276 } 01277 } 01278 01279 static void 01280 set_method_visibility(VALUE self, int argc, VALUE *argv, rb_method_flag_t ex) 01281 { 01282 int i; 01283 secure_visibility(self); 01284 01285 if (argc == 0) { 01286 rb_warning("%s with no argument is just ignored", rb_id2name(rb_frame_callee())); 01287 } 01288 01289 for (i = 0; i < argc; i++) { 01290 VALUE v = argv[i]; 01291 ID id = rb_check_id(&v); 01292 if (!id) { 01293 rb_print_undef_str(self, v); 01294 } 01295 rb_export_method(self, id, ex); 01296 } 01297 rb_clear_cache_by_class(self); 01298 } 01299 01300 /* 01301 * call-seq: 01302 * public -> self 01303 * public(symbol, ...) -> self 01304 * 01305 * With no arguments, sets the default visibility for subsequently 01306 * defined methods to public. With arguments, sets the named methods to 01307 * have public visibility. 01308 */ 01309 01310 static VALUE 01311 rb_mod_public(int argc, VALUE *argv, VALUE module) 01312 { 01313 secure_visibility(module); 01314 if (argc == 0) { 01315 SCOPE_SET(NOEX_PUBLIC); 01316 } 01317 else { 01318 set_method_visibility(module, argc, argv, NOEX_PUBLIC); 01319 } 01320 return module; 01321 } 01322 01323 /* 01324 * call-seq: 01325 * protected -> self 01326 * protected(symbol, ...) -> self 01327 * 01328 * With no arguments, sets the default visibility for subsequently 01329 * defined methods to protected. With arguments, sets the named methods 01330 * to have protected visibility. 01331 */ 01332 01333 static VALUE 01334 rb_mod_protected(int argc, VALUE *argv, VALUE module) 01335 { 01336 secure_visibility(module); 01337 if (argc == 0) { 01338 SCOPE_SET(NOEX_PROTECTED); 01339 } 01340 else { 01341 set_method_visibility(module, argc, argv, NOEX_PROTECTED); 01342 } 01343 return module; 01344 } 01345 01346 /* 01347 * call-seq: 01348 * private -> self 01349 * private(symbol, ...) -> self 01350 * 01351 * With no arguments, sets the default visibility for subsequently 01352 * defined methods to private. With arguments, sets the named methods 01353 * to have private visibility. 01354 * 01355 * module Mod 01356 * def a() end 01357 * def b() end 01358 * private 01359 * def c() end 01360 * private :a 01361 * end 01362 * Mod.private_instance_methods #=> [:a, :c] 01363 */ 01364 01365 static VALUE 01366 rb_mod_private(int argc, VALUE *argv, VALUE module) 01367 { 01368 secure_visibility(module); 01369 if (argc == 0) { 01370 SCOPE_SET(NOEX_PRIVATE); 01371 } 01372 else { 01373 set_method_visibility(module, argc, argv, NOEX_PRIVATE); 01374 } 01375 return module; 01376 } 01377 01378 /* 01379 * call-seq: 01380 * mod.public_class_method(symbol, ...) -> mod 01381 * 01382 * Makes a list of existing class methods public. 01383 */ 01384 01385 static VALUE 01386 rb_mod_public_method(int argc, VALUE *argv, VALUE obj) 01387 { 01388 set_method_visibility(rb_singleton_class(obj), argc, argv, NOEX_PUBLIC); 01389 return obj; 01390 } 01391 01392 /* 01393 * call-seq: 01394 * mod.private_class_method(symbol, ...) -> mod 01395 * 01396 * Makes existing class methods private. Often used to hide the default 01397 * constructor <code>new</code>. 01398 * 01399 * class SimpleSingleton # Not thread safe 01400 * private_class_method :new 01401 * def SimpleSingleton.create(*args, &block) 01402 * @me = new(*args, &block) if ! @me 01403 * @me 01404 * end 01405 * end 01406 */ 01407 01408 static VALUE 01409 rb_mod_private_method(int argc, VALUE *argv, VALUE obj) 01410 { 01411 set_method_visibility(rb_singleton_class(obj), argc, argv, NOEX_PRIVATE); 01412 return obj; 01413 } 01414 01415 /* 01416 * call-seq: 01417 * public 01418 * public(symbol, ...) 01419 * 01420 * With no arguments, sets the default visibility for subsequently 01421 * defined methods to public. With arguments, sets the named methods to 01422 * have public visibility. 01423 */ 01424 01425 static VALUE 01426 top_public(int argc, VALUE *argv) 01427 { 01428 return rb_mod_public(argc, argv, rb_cObject); 01429 } 01430 01431 static VALUE 01432 top_private(int argc, VALUE *argv) 01433 { 01434 return rb_mod_private(argc, argv, rb_cObject); 01435 } 01436 01437 /* 01438 * call-seq: 01439 * module_function(symbol, ...) -> self 01440 * 01441 * Creates module functions for the named methods. These functions may 01442 * be called with the module as a receiver, and also become available 01443 * as instance methods to classes that mix in the module. Module 01444 * functions are copies of the original, and so may be changed 01445 * independently. The instance-method versions are made private. If 01446 * used with no arguments, subsequently defined methods become module 01447 * functions. 01448 * 01449 * module Mod 01450 * def one 01451 * "This is one" 01452 * end 01453 * module_function :one 01454 * end 01455 * class Cls 01456 * include Mod 01457 * def call_one 01458 * one 01459 * end 01460 * end 01461 * Mod.one #=> "This is one" 01462 * c = Cls.new 01463 * c.call_one #=> "This is one" 01464 * module Mod 01465 * def one 01466 * "This is the new one" 01467 * end 01468 * end 01469 * Mod.one #=> "This is one" 01470 * c.call_one #=> "This is the new one" 01471 */ 01472 01473 static VALUE 01474 rb_mod_modfunc(int argc, VALUE *argv, VALUE module) 01475 { 01476 int i; 01477 ID id; 01478 const rb_method_entry_t *me; 01479 01480 if (!RB_TYPE_P(module, T_MODULE)) { 01481 rb_raise(rb_eTypeError, "module_function must be called for modules"); 01482 } 01483 01484 secure_visibility(module); 01485 if (argc == 0) { 01486 SCOPE_SET(NOEX_MODFUNC); 01487 return module; 01488 } 01489 01490 set_method_visibility(module, argc, argv, NOEX_PRIVATE); 01491 01492 for (i = 0; i < argc; i++) { 01493 VALUE m = module; 01494 01495 id = rb_to_id(argv[i]); 01496 for (;;) { 01497 me = search_method(m, id, 0); 01498 if (me == 0) { 01499 me = search_method(rb_cObject, id, 0); 01500 } 01501 if (UNDEFINED_METHOD_ENTRY_P(me)) { 01502 rb_print_undef(module, id, 0); 01503 } 01504 if (me->def->type != VM_METHOD_TYPE_ZSUPER) { 01505 break; /* normal case: need not to follow 'super' link */ 01506 } 01507 m = RCLASS_SUPER(m); 01508 if (!m) 01509 break; 01510 } 01511 rb_method_entry_set(rb_singleton_class(module), id, me, NOEX_PUBLIC); 01512 } 01513 return module; 01514 } 01515 01516 int 01517 rb_method_basic_definition_p(VALUE klass, ID id) 01518 { 01519 const rb_method_entry_t *me = rb_method_entry(klass, id, 0); 01520 if (me && (me->flag & NOEX_BASIC)) 01521 return 1; 01522 return 0; 01523 } 01524 01525 static inline int 01526 basic_obj_respond_to(VALUE obj, ID id, int pub) 01527 { 01528 VALUE klass = CLASS_OF(obj); 01529 VALUE args[2]; 01530 01531 switch (rb_method_boundp(klass, id, pub|NOEX_RESPONDS)) { 01532 case 2: 01533 return FALSE; 01534 case 0: 01535 args[0] = ID2SYM(id); 01536 args[1] = pub ? Qfalse : Qtrue; 01537 return RTEST(rb_funcall2(obj, idRespond_to_missing, 2, args)); 01538 default: 01539 return TRUE; 01540 } 01541 } 01542 01543 int 01544 rb_obj_respond_to(VALUE obj, ID id, int priv) 01545 { 01546 VALUE klass = CLASS_OF(obj); 01547 01548 if (rb_method_basic_definition_p(klass, idRespond_to)) { 01549 return basic_obj_respond_to(obj, id, !RTEST(priv)); 01550 } 01551 else { 01552 int argc = 1; 01553 VALUE args[2]; 01554 args[0] = ID2SYM(id); 01555 args[1] = Qtrue; 01556 if (priv) { 01557 if (rb_obj_method_arity(obj, idRespond_to) != 1) { 01558 argc = 2; 01559 } 01560 else if (!NIL_P(ruby_verbose)) { 01561 VALUE klass = CLASS_OF(obj); 01562 VALUE location = rb_mod_method_location(klass, idRespond_to); 01563 rb_warn("%"PRIsVALUE"%c""respond_to?(:%"PRIsVALUE") is" 01564 " old fashion which takes only one parameter", 01565 (FL_TEST(klass, FL_SINGLETON) ? obj : klass), 01566 (FL_TEST(klass, FL_SINGLETON) ? '.' : '#'), 01567 QUOTE_ID(id)); 01568 if (!NIL_P(location)) { 01569 VALUE path = RARRAY_PTR(location)[0]; 01570 VALUE line = RARRAY_PTR(location)[1]; 01571 if (!NIL_P(path)) { 01572 rb_compile_warn(RSTRING_PTR(path), NUM2INT(line), 01573 "respond_to? is defined here"); 01574 } 01575 } 01576 } 01577 } 01578 return RTEST(rb_funcall2(obj, idRespond_to, argc, args)); 01579 } 01580 } 01581 01582 int 01583 rb_respond_to(VALUE obj, ID id) 01584 { 01585 return rb_obj_respond_to(obj, id, FALSE); 01586 } 01587 01588 01589 /* 01590 * call-seq: 01591 * obj.respond_to?(symbol, include_all=false) -> true or false 01592 * 01593 * Returns +true+ if _obj_ responds to the given method. Private and 01594 * protected methods are included in the search only if the optional 01595 * second parameter evaluates to +true+. 01596 * 01597 * If the method is not implemented, 01598 * as Process.fork on Windows, File.lchmod on GNU/Linux, etc., 01599 * false is returned. 01600 * 01601 * If the method is not defined, <code>respond_to_missing?</code> 01602 * method is called and the result is returned. 01603 */ 01604 01605 static VALUE 01606 obj_respond_to(int argc, VALUE *argv, VALUE obj) 01607 { 01608 VALUE mid, priv; 01609 ID id; 01610 01611 rb_scan_args(argc, argv, "11", &mid, &priv); 01612 if (!(id = rb_check_id(&mid))) { 01613 if (!rb_method_basic_definition_p(CLASS_OF(obj), idRespond_to_missing)) { 01614 VALUE args[2]; 01615 args[0] = ID2SYM(rb_to_id(mid)); 01616 args[1] = priv; 01617 return rb_funcall2(obj, idRespond_to_missing, 2, args); 01618 } 01619 return Qfalse; 01620 } 01621 if (basic_obj_respond_to(obj, id, !RTEST(priv))) 01622 return Qtrue; 01623 return Qfalse; 01624 } 01625 01626 /* 01627 * call-seq: 01628 * obj.respond_to_missing?(symbol, include_all) -> true or false 01629 * 01630 * DO NOT USE THIS DIRECTLY. 01631 * 01632 * Hook method to return whether the _obj_ can respond to _id_ method 01633 * or not. 01634 * 01635 * See #respond_to?. 01636 */ 01637 static VALUE 01638 obj_respond_to_missing(VALUE obj, VALUE mid, VALUE priv) 01639 { 01640 return Qfalse; 01641 } 01642 01643 void 01644 Init_eval_method(void) 01645 { 01646 #undef rb_intern 01647 #define rb_intern(str) rb_intern_const(str) 01648 01649 rb_define_method(rb_mKernel, "respond_to?", obj_respond_to, -1); 01650 rb_define_method(rb_mKernel, "respond_to_missing?", obj_respond_to_missing, 2); 01651 01652 rb_define_private_method(rb_cModule, "remove_method", rb_mod_remove_method, -1); 01653 rb_define_private_method(rb_cModule, "undef_method", rb_mod_undef_method, -1); 01654 rb_define_private_method(rb_cModule, "alias_method", rb_mod_alias_method, 2); 01655 rb_define_private_method(rb_cModule, "public", rb_mod_public, -1); 01656 rb_define_private_method(rb_cModule, "protected", rb_mod_protected, -1); 01657 rb_define_private_method(rb_cModule, "private", rb_mod_private, -1); 01658 rb_define_private_method(rb_cModule, "module_function", rb_mod_modfunc, -1); 01659 01660 rb_define_method(rb_cModule, "method_defined?", rb_mod_method_defined, 1); 01661 rb_define_method(rb_cModule, "public_method_defined?", rb_mod_public_method_defined, 1); 01662 rb_define_method(rb_cModule, "private_method_defined?", rb_mod_private_method_defined, 1); 01663 rb_define_method(rb_cModule, "protected_method_defined?", rb_mod_protected_method_defined, 1); 01664 rb_define_method(rb_cModule, "public_class_method", rb_mod_public_method, -1); 01665 rb_define_method(rb_cModule, "private_class_method", rb_mod_private_method, -1); 01666 01667 rb_define_private_method(rb_singleton_class(rb_vm_top_self()), 01668 "public", top_public, -1); 01669 rb_define_private_method(rb_singleton_class(rb_vm_top_self()), 01670 "private", top_private, -1); 01671 01672 object_id = rb_intern("object_id"); 01673 added = rb_intern("method_added"); 01674 singleton_added = rb_intern("singleton_method_added"); 01675 removed = rb_intern("method_removed"); 01676 singleton_removed = rb_intern("singleton_method_removed"); 01677 undefined = rb_intern("method_undefined"); 01678 singleton_undefined = rb_intern("singleton_method_undefined"); 01679 attached = rb_intern("__attached__"); 01680 01681 { 01682 #define REPLICATE_METHOD(klass, id, noex) \ 01683 rb_method_entry_set((klass), (id), \ 01684 rb_method_entry((klass), (id), 0), \ 01685 (rb_method_flag_t)(noex | NOEX_BASIC | NOEX_NOREDEF)) 01686 REPLICATE_METHOD(rb_eException, idMethodMissing, NOEX_PRIVATE); 01687 REPLICATE_METHOD(rb_eException, idRespond_to, NOEX_PUBLIC); 01688 REPLICATE_METHOD(rb_eException, idRespond_to_missing, NOEX_PUBLIC); 01689 } 01690 } 01691
1.7.6.1