00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 #include "eval_intern.h"
00013 #include "internal.h"
00014 #include "gc.h"
00015 #include "iseq.h"
00016
00017 struct METHOD {
00018 VALUE recv;
00019 VALUE rclass;
00020 ID id;
00021 rb_method_entry_t *me;
00022 struct unlinked_method_entry_list_entry *ume;
00023 };
00024
00025 VALUE rb_cUnboundMethod;
00026 VALUE rb_cMethod;
00027 VALUE rb_cBinding;
00028 VALUE rb_cProc;
00029
00030 static VALUE bmcall(VALUE, VALUE);
00031 static int method_arity(VALUE);
00032
00033
00034
00035 #define IS_METHOD_PROC_NODE(node) (nd_type(node) == NODE_IFUNC && (node)->nd_cfnc == bmcall)
00036
00037 static void
00038 proc_free(void *ptr)
00039 {
00040 RUBY_FREE_ENTER("proc");
00041 if (ptr) {
00042 ruby_xfree(ptr);
00043 }
00044 RUBY_FREE_LEAVE("proc");
00045 }
00046
00047 static void
00048 proc_mark(void *ptr)
00049 {
00050 rb_proc_t *proc;
00051 RUBY_MARK_ENTER("proc");
00052 if (ptr) {
00053 proc = ptr;
00054 RUBY_MARK_UNLESS_NULL(proc->envval);
00055 RUBY_MARK_UNLESS_NULL(proc->blockprocval);
00056 RUBY_MARK_UNLESS_NULL(proc->block.proc);
00057 RUBY_MARK_UNLESS_NULL(proc->block.self);
00058 if (proc->block.iseq && RUBY_VM_IFUNC_P(proc->block.iseq)) {
00059 RUBY_MARK_UNLESS_NULL((VALUE)(proc->block.iseq));
00060 }
00061 }
00062 RUBY_MARK_LEAVE("proc");
00063 }
00064
00065 static size_t
00066 proc_memsize(const void *ptr)
00067 {
00068 return ptr ? sizeof(rb_proc_t) : 0;
00069 }
00070
00071 static const rb_data_type_t proc_data_type = {
00072 "proc",
00073 {
00074 proc_mark,
00075 proc_free,
00076 proc_memsize,
00077 },
00078 };
00079
00080 VALUE
00081 rb_proc_alloc(VALUE klass)
00082 {
00083 rb_proc_t *proc;
00084 return TypedData_Make_Struct(klass, rb_proc_t, &proc_data_type, proc);
00085 }
00086
00087 VALUE
00088 rb_obj_is_proc(VALUE proc)
00089 {
00090 if (rb_typeddata_is_kind_of(proc, &proc_data_type)) {
00091 return Qtrue;
00092 }
00093 else {
00094 return Qfalse;
00095 }
00096 }
00097
00098
00099 static VALUE
00100 proc_dup(VALUE self)
00101 {
00102 VALUE procval = rb_proc_alloc(rb_cProc);
00103 rb_proc_t *src, *dst;
00104 GetProcPtr(self, src);
00105 GetProcPtr(procval, dst);
00106
00107 dst->block = src->block;
00108 dst->block.proc = procval;
00109 dst->blockprocval = src->blockprocval;
00110 dst->envval = src->envval;
00111 dst->safe_level = src->safe_level;
00112 dst->is_lambda = src->is_lambda;
00113
00114 return procval;
00115 }
00116
00117
00118 static VALUE
00119 proc_clone(VALUE self)
00120 {
00121 VALUE procval = proc_dup(self);
00122 CLONESETUP(procval, self);
00123 return procval;
00124 }
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227 VALUE
00228 rb_proc_lambda_p(VALUE procval)
00229 {
00230 rb_proc_t *proc;
00231 GetProcPtr(procval, proc);
00232
00233 return proc->is_lambda ? Qtrue : Qfalse;
00234 }
00235
00236
00237
00238 static void
00239 binding_free(void *ptr)
00240 {
00241 rb_binding_t *bind;
00242 RUBY_FREE_ENTER("binding");
00243 if (ptr) {
00244 bind = ptr;
00245 ruby_xfree(ptr);
00246 }
00247 RUBY_FREE_LEAVE("binding");
00248 }
00249
00250 static void
00251 binding_mark(void *ptr)
00252 {
00253 rb_binding_t *bind;
00254 RUBY_MARK_ENTER("binding");
00255 if (ptr) {
00256 bind = ptr;
00257 RUBY_MARK_UNLESS_NULL(bind->env);
00258 RUBY_MARK_UNLESS_NULL(bind->filename);
00259 }
00260 RUBY_MARK_LEAVE("binding");
00261 }
00262
00263 static size_t
00264 binding_memsize(const void *ptr)
00265 {
00266 return ptr ? sizeof(rb_binding_t) : 0;
00267 }
00268
00269 static const rb_data_type_t binding_data_type = {
00270 "binding",
00271 {
00272 binding_mark,
00273 binding_free,
00274 binding_memsize,
00275 },
00276 };
00277
00278 static VALUE
00279 binding_alloc(VALUE klass)
00280 {
00281 VALUE obj;
00282 rb_binding_t *bind;
00283 obj = TypedData_Make_Struct(klass, rb_binding_t, &binding_data_type, bind);
00284 return obj;
00285 }
00286
00287
00288 static VALUE
00289 binding_dup(VALUE self)
00290 {
00291 VALUE bindval = binding_alloc(rb_cBinding);
00292 rb_binding_t *src, *dst;
00293 GetBindingPtr(self, src);
00294 GetBindingPtr(bindval, dst);
00295 dst->env = src->env;
00296 dst->filename = src->filename;
00297 dst->line_no = src->line_no;
00298 return bindval;
00299 }
00300
00301
00302 static VALUE
00303 binding_clone(VALUE self)
00304 {
00305 VALUE bindval = binding_dup(self);
00306 CLONESETUP(bindval, self);
00307 return bindval;
00308 }
00309
00310 VALUE
00311 rb_binding_new(void)
00312 {
00313 rb_thread_t *th = GET_THREAD();
00314 rb_control_frame_t *cfp = rb_vm_get_ruby_level_next_cfp(th, th->cfp);
00315 VALUE bindval = binding_alloc(rb_cBinding);
00316 rb_binding_t *bind;
00317
00318 if (cfp == 0) {
00319 rb_raise(rb_eRuntimeError, "Can't create Binding Object on top of Fiber.");
00320 }
00321
00322 GetBindingPtr(bindval, bind);
00323 bind->env = rb_vm_make_env_object(th, cfp);
00324 bind->filename = cfp->iseq->filename;
00325 bind->line_no = rb_vm_get_sourceline(cfp);
00326 return bindval;
00327 }
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345 static VALUE
00346 rb_f_binding(VALUE self)
00347 {
00348 return rb_binding_new();
00349 }
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367 static VALUE
00368 bind_eval(int argc, VALUE *argv, VALUE bindval)
00369 {
00370 VALUE args[4];
00371
00372 rb_scan_args(argc, argv, "12", &args[0], &args[2], &args[3]);
00373 args[1] = bindval;
00374 return rb_f_eval(argc+1, args, Qnil );
00375 }
00376
00377 static VALUE
00378 proc_new(VALUE klass, int is_lambda)
00379 {
00380 VALUE procval = Qnil;
00381 rb_thread_t *th = GET_THREAD();
00382 rb_control_frame_t *cfp = th->cfp;
00383 rb_block_t *block;
00384
00385 if ((GC_GUARDED_PTR_REF(cfp->lfp[0])) != 0) {
00386
00387 block = GC_GUARDED_PTR_REF(cfp->lfp[0]);
00388 }
00389 else {
00390 cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp);
00391
00392 if ((GC_GUARDED_PTR_REF(cfp->lfp[0])) != 0) {
00393
00394 block = GC_GUARDED_PTR_REF(cfp->lfp[0]);
00395
00396 if (is_lambda) {
00397 rb_warn("tried to create Proc object without a block");
00398 }
00399 }
00400 else {
00401 rb_raise(rb_eArgError,
00402 "tried to create Proc object without a block");
00403 }
00404 }
00405
00406 procval = block->proc;
00407
00408 if (procval) {
00409 if (RBASIC(procval)->klass == klass) {
00410 return procval;
00411 }
00412 else {
00413 VALUE newprocval = proc_dup(procval);
00414 RBASIC(newprocval)->klass = klass;
00415 return newprocval;
00416 }
00417 }
00418
00419 procval = rb_vm_make_proc(th, block, klass);
00420 rb_vm_rewrite_dfp_in_errinfo(th, cfp);
00421
00422 if (is_lambda) {
00423 rb_proc_t *proc;
00424 GetProcPtr(procval, proc);
00425 proc->is_lambda = TRUE;
00426 }
00427 return procval;
00428 }
00429
00430
00431
00432
00433
00434
00435
00436
00437
00438
00439
00440
00441
00442
00443
00444
00445
00446
00447 static VALUE
00448 rb_proc_s_new(int argc, VALUE *argv, VALUE klass)
00449 {
00450 VALUE block = proc_new(klass, FALSE);
00451
00452 rb_obj_call_init(block, argc, argv);
00453 return block;
00454 }
00455
00456
00457
00458
00459
00460
00461
00462
00463 VALUE
00464 rb_block_proc(void)
00465 {
00466 return proc_new(rb_cProc, FALSE);
00467 }
00468
00469 VALUE
00470 rb_block_lambda(void)
00471 {
00472 return proc_new(rb_cProc, TRUE);
00473 }
00474
00475 VALUE
00476 rb_f_lambda(void)
00477 {
00478 rb_warn("rb_f_lambda() is deprecated; use rb_block_proc() instead");
00479 return rb_block_lambda();
00480 }
00481
00482
00483
00484
00485
00486
00487
00488
00489
00490 static VALUE
00491 proc_lambda(void)
00492 {
00493 return rb_block_lambda();
00494 }
00495
00496
00497
00498
00499
00500
00501
00502
00503
00504
00505
00506
00507
00508
00509
00510
00511
00512
00513
00514
00515
00516
00517
00518
00519
00520
00521
00522
00523
00524
00525
00526
00527
00528
00529
00530
00531
00532
00533
00534
00535
00536
00537
00538
00539
00540
00541
00542
00543 static VALUE
00544 proc_call(int argc, VALUE *argv, VALUE procval)
00545 {
00546 rb_proc_t *proc;
00547 rb_block_t *blockptr = 0;
00548 rb_iseq_t *iseq;
00549 VALUE passed_procval;
00550 GetProcPtr(procval, proc);
00551
00552 iseq = proc->block.iseq;
00553 if (BUILTIN_TYPE(iseq) == T_NODE || iseq->arg_block != -1) {
00554 if (rb_block_given_p()) {
00555 rb_proc_t *passed_proc;
00556 RB_GC_GUARD(passed_procval) = rb_block_proc();
00557 GetProcPtr(passed_procval, passed_proc);
00558 blockptr = &passed_proc->block;
00559 }
00560 }
00561
00562 return rb_vm_invoke_proc(GET_THREAD(), proc, proc->block.self,
00563 argc, argv, blockptr);
00564 }
00565
00566 #if SIZEOF_LONG > SIZEOF_INT
00567 static inline int
00568 check_argc(long argc)
00569 {
00570 if (argc > INT_MAX || argc < 0) {
00571 rb_raise(rb_eArgError, "too many arguments (%lu)",
00572 (unsigned long)argc);
00573 }
00574 return (int)argc;
00575 }
00576 #else
00577 #define check_argc(argc) (argc)
00578 #endif
00579
00580 VALUE
00581 rb_proc_call(VALUE self, VALUE args)
00582 {
00583 rb_proc_t *proc;
00584 GetProcPtr(self, proc);
00585 return rb_vm_invoke_proc(GET_THREAD(), proc, proc->block.self,
00586 check_argc(RARRAY_LEN(args)), RARRAY_PTR(args), 0);
00587 }
00588
00589 VALUE
00590 rb_proc_call_with_block(VALUE self, int argc, VALUE *argv, VALUE pass_procval)
00591 {
00592 rb_proc_t *proc;
00593 rb_block_t *block = 0;
00594 GetProcPtr(self, proc);
00595
00596 if (!NIL_P(pass_procval)) {
00597 rb_proc_t *pass_proc;
00598 GetProcPtr(pass_procval, pass_proc);
00599 block = &pass_proc->block;
00600 }
00601
00602 return rb_vm_invoke_proc(GET_THREAD(), proc, proc->block.self,
00603 argc, argv, block);
00604 }
00605
00606
00607
00608
00609
00610
00611
00612
00613
00614
00615
00616
00617
00618
00619
00620
00621
00622
00623
00624
00625
00626
00627 static VALUE
00628 proc_arity(VALUE self)
00629 {
00630 int arity = rb_proc_arity(self);
00631 return INT2FIX(arity);
00632 }
00633
00634 int
00635 rb_proc_arity(VALUE self)
00636 {
00637 rb_proc_t *proc;
00638 rb_iseq_t *iseq;
00639 GetProcPtr(self, proc);
00640 iseq = proc->block.iseq;
00641 if (iseq) {
00642 if (BUILTIN_TYPE(iseq) != T_NODE) {
00643 if (iseq->arg_rest < 0) {
00644 return iseq->argc;
00645 }
00646 else {
00647 return -(iseq->argc + 1 + iseq->arg_post_len);
00648 }
00649 }
00650 else {
00651 NODE *node = (NODE *)iseq;
00652 if (IS_METHOD_PROC_NODE(node)) {
00653
00654 return method_arity(node->nd_tval);
00655 }
00656 }
00657 }
00658 return -1;
00659 }
00660
00661 #define get_proc_iseq rb_proc_get_iseq
00662
00663 rb_iseq_t *
00664 rb_proc_get_iseq(VALUE self, int *is_proc)
00665 {
00666 rb_proc_t *proc;
00667 rb_iseq_t *iseq;
00668
00669 GetProcPtr(self, proc);
00670 iseq = proc->block.iseq;
00671 if (is_proc) *is_proc = !proc->is_lambda;
00672 if (!RUBY_VM_NORMAL_ISEQ_P(iseq)) {
00673 NODE *node = (NODE *)iseq;
00674 iseq = 0;
00675 if (IS_METHOD_PROC_NODE(node)) {
00676
00677 iseq = rb_method_get_iseq(node->nd_tval);
00678 if (is_proc) *is_proc = 0;
00679 }
00680 }
00681 return iseq;
00682 }
00683
00684 static VALUE
00685 iseq_location(rb_iseq_t *iseq)
00686 {
00687 VALUE loc[2];
00688
00689 if (!iseq) return Qnil;
00690 loc[0] = iseq->filename;
00691 if (iseq->insn_info_table) {
00692 loc[1] = INT2FIX(rb_iseq_first_lineno(iseq));
00693 }
00694 else {
00695 loc[1] = Qnil;
00696 }
00697 return rb_ary_new4(2, loc);
00698 }
00699
00700
00701
00702
00703
00704
00705
00706
00707
00708 VALUE
00709 rb_proc_location(VALUE self)
00710 {
00711 return iseq_location(get_proc_iseq(self, 0));
00712 }
00713
00714 static VALUE
00715 unnamed_parameters(int arity)
00716 {
00717 VALUE a, param = rb_ary_new2((arity < 0) ? -arity : arity);
00718 int n = (arity < 0) ? ~arity : arity;
00719 ID req, rest;
00720 CONST_ID(req, "req");
00721 a = rb_ary_new3(1, ID2SYM(req));
00722 OBJ_FREEZE(a);
00723 for (; n; --n) {
00724 rb_ary_push(param, a);
00725 }
00726 if (arity < 0) {
00727 CONST_ID(rest, "rest");
00728 rb_ary_store(param, ~arity, rb_ary_new3(1, ID2SYM(rest)));
00729 }
00730 return param;
00731 }
00732
00733
00734
00735
00736
00737
00738
00739
00740
00741
00742
00743 static VALUE
00744 rb_proc_parameters(VALUE self)
00745 {
00746 int is_proc;
00747 rb_iseq_t *iseq = get_proc_iseq(self, &is_proc);
00748 if (!iseq) {
00749 return unnamed_parameters(rb_proc_arity(self));
00750 }
00751 return rb_iseq_parameters(iseq, is_proc);
00752 }
00753
00754
00755
00756
00757
00758
00759
00760
00761
00762 static VALUE
00763 proc_eq(VALUE self, VALUE other)
00764 {
00765 if (self == other) {
00766 return Qtrue;
00767 }
00768 else {
00769 if (rb_obj_is_proc(other)) {
00770 rb_proc_t *p1, *p2;
00771 GetProcPtr(self, p1);
00772 GetProcPtr(other, p2);
00773 if (p1->envval == p2->envval &&
00774 p1->block.iseq->iseq_size == p2->block.iseq->iseq_size &&
00775 p1->block.iseq->local_size == p2->block.iseq->local_size &&
00776 MEMCMP(p1->block.iseq->iseq, p2->block.iseq->iseq, VALUE,
00777 p1->block.iseq->iseq_size) == 0) {
00778 return Qtrue;
00779 }
00780 }
00781 }
00782 return Qfalse;
00783 }
00784
00785
00786
00787
00788
00789
00790
00791
00792 static VALUE
00793 proc_hash(VALUE self)
00794 {
00795 st_index_t hash;
00796 rb_proc_t *proc;
00797 GetProcPtr(self, proc);
00798 hash = rb_hash_start((st_index_t)proc->block.iseq);
00799 hash = rb_hash_uint(hash, (st_index_t)proc->envval);
00800 hash = rb_hash_uint(hash, (st_index_t)proc->block.lfp >> 16);
00801 hash = rb_hash_end(hash);
00802 return LONG2FIX(hash);
00803 }
00804
00805
00806
00807
00808
00809
00810
00811
00812
00813 static VALUE
00814 proc_to_s(VALUE self)
00815 {
00816 VALUE str = 0;
00817 rb_proc_t *proc;
00818 const char *cname = rb_obj_classname(self);
00819 rb_iseq_t *iseq;
00820 const char *is_lambda;
00821
00822 GetProcPtr(self, proc);
00823 iseq = proc->block.iseq;
00824 is_lambda = proc->is_lambda ? " (lambda)" : "";
00825
00826 if (RUBY_VM_NORMAL_ISEQ_P(iseq)) {
00827 int line_no = 0;
00828
00829 if (iseq->insn_info_table) {
00830 line_no = rb_iseq_first_lineno(iseq);
00831 }
00832 str = rb_sprintf("#<%s:%p@%s:%d%s>", cname, (void *)self,
00833 RSTRING_PTR(iseq->filename),
00834 line_no, is_lambda);
00835 }
00836 else {
00837 str = rb_sprintf("#<%s:%p%s>", cname, (void *)proc->block.iseq,
00838 is_lambda);
00839 }
00840
00841 if (OBJ_TAINTED(self)) {
00842 OBJ_TAINT(str);
00843 }
00844 return str;
00845 }
00846
00847
00848
00849
00850
00851
00852
00853
00854
00855
00856 static VALUE
00857 proc_to_proc(VALUE self)
00858 {
00859 return self;
00860 }
00861
00862 static void
00863 bm_mark(void *ptr)
00864 {
00865 struct METHOD *data = ptr;
00866 rb_gc_mark(data->rclass);
00867 rb_gc_mark(data->recv);
00868 if (data->me) rb_mark_method_entry(data->me);
00869 }
00870
00871 static void
00872 bm_free(void *ptr)
00873 {
00874 struct METHOD *data = ptr;
00875 struct unlinked_method_entry_list_entry *ume = data->ume;
00876 ume->me = data->me;
00877 ume->next = GET_VM()->unlinked_method_entry_list;
00878 GET_VM()->unlinked_method_entry_list = ume;
00879 xfree(ptr);
00880 }
00881
00882 static size_t
00883 bm_memsize(const void *ptr)
00884 {
00885 return ptr ? sizeof(struct METHOD) : 0;
00886 }
00887
00888 static const rb_data_type_t method_data_type = {
00889 "method",
00890 {
00891 bm_mark,
00892 bm_free,
00893 bm_memsize,
00894 },
00895 };
00896
00897 VALUE
00898 rb_obj_is_method(VALUE m)
00899 {
00900 if (rb_typeddata_is_kind_of(m, &method_data_type)) {
00901 return Qtrue;
00902 }
00903 else {
00904 return Qfalse;
00905 }
00906 }
00907
00908 static VALUE
00909 mnew(VALUE klass, VALUE obj, ID id, VALUE mclass, int scope)
00910 {
00911 VALUE method;
00912 VALUE rclass = klass;
00913 ID rid = id;
00914 struct METHOD *data;
00915 rb_method_entry_t *me, meb;
00916 rb_method_definition_t *def = 0;
00917 rb_method_flag_t flag = NOEX_UNDEF;
00918
00919 again:
00920 me = rb_method_entry(klass, id);
00921 if (UNDEFINED_METHOD_ENTRY_P(me)) {
00922 ID rmiss = rb_intern("respond_to_missing?");
00923 VALUE sym = ID2SYM(id);
00924
00925 if (obj != Qundef && !rb_method_basic_definition_p(klass, rmiss)) {
00926 if (RTEST(rb_funcall(obj, rmiss, 2, sym, scope ? Qfalse : Qtrue))) {
00927 def = ALLOC(rb_method_definition_t);
00928 def->type = VM_METHOD_TYPE_MISSING;
00929 def->original_id = id;
00930 def->alias_count = 0;
00931
00932 meb.flag = 0;
00933 meb.mark = 0;
00934 meb.called_id = id;
00935 meb.klass = klass;
00936 meb.def = def;
00937 me = &meb;
00938 def = 0;
00939
00940 goto gen_method;
00941 }
00942 }
00943 rb_print_undef(klass, id, 0);
00944 }
00945 def = me->def;
00946 if (flag == NOEX_UNDEF) {
00947 flag = me->flag;
00948 if (scope && (flag & NOEX_MASK) != NOEX_PUBLIC) {
00949 const char *v = "";
00950 switch (flag & NOEX_MASK) {
00951 case NOEX_PRIVATE: v = "private"; break;
00952 case NOEX_PROTECTED: v = "protected"; break;
00953 }
00954 rb_name_error(id, "method `%s' for %s `%s' is %s",
00955 rb_id2name(id),
00956 (TYPE(klass) == T_MODULE) ? "module" : "class",
00957 rb_class2name(klass),
00958 v);
00959 }
00960 }
00961 if (def && def->type == VM_METHOD_TYPE_ZSUPER) {
00962 klass = RCLASS_SUPER(me->klass);
00963 id = def->original_id;
00964 goto again;
00965 }
00966
00967 klass = me->klass;
00968
00969 while (rclass != klass &&
00970 (FL_TEST(rclass, FL_SINGLETON) || TYPE(rclass) == T_ICLASS)) {
00971 rclass = RCLASS_SUPER(rclass);
00972 }
00973
00974 if (TYPE(klass) == T_ICLASS) {
00975 klass = RBASIC(klass)->klass;
00976 }
00977
00978 gen_method:
00979 method = TypedData_Make_Struct(mclass, struct METHOD, &method_data_type, data);
00980
00981 data->recv = obj;
00982 data->rclass = rclass;
00983 data->id = rid;
00984 data->me = ALLOC(rb_method_entry_t);
00985 *data->me = *me;
00986 data->me->def->alias_count++;
00987 data->ume = ALLOC(struct unlinked_method_entry_list_entry);
00988
00989 OBJ_INFECT(method, klass);
00990
00991 return method;
00992 }
00993
00994
00995
00996
00997
00998
00999
01000
01001
01002
01003
01004
01005
01006
01007
01008
01009
01010
01011
01012
01013
01014
01015
01016
01017
01018
01019
01020
01021
01022
01023
01024
01025
01026
01027 static VALUE
01028 method_eq(VALUE method, VALUE other)
01029 {
01030 struct METHOD *m1, *m2;
01031
01032 if (!rb_obj_is_method(other))
01033 return Qfalse;
01034 if (CLASS_OF(method) != CLASS_OF(other))
01035 return Qfalse;
01036
01037 Check_TypedStruct(method, &method_data_type);
01038 m1 = (struct METHOD *)DATA_PTR(method);
01039 m2 = (struct METHOD *)DATA_PTR(other);
01040
01041 if (!rb_method_entry_eq(m1->me, m2->me) ||
01042 m1->rclass != m2->rclass ||
01043 m1->recv != m2->recv) {
01044 return Qfalse;
01045 }
01046
01047 return Qtrue;
01048 }
01049
01050
01051
01052
01053
01054
01055
01056
01057 static VALUE
01058 method_hash(VALUE method)
01059 {
01060 struct METHOD *m;
01061 st_index_t hash;
01062
01063 TypedData_Get_Struct(method, struct METHOD, &method_data_type, m);
01064 hash = rb_hash_start((st_index_t)m->rclass);
01065 hash = rb_hash_uint(hash, (st_index_t)m->recv);
01066 hash = rb_hash_uint(hash, (st_index_t)m->me->def);
01067 hash = rb_hash_end(hash);
01068
01069 return INT2FIX(hash);
01070 }
01071
01072
01073
01074
01075
01076
01077
01078
01079
01080
01081 static VALUE
01082 method_unbind(VALUE obj)
01083 {
01084 VALUE method;
01085 struct METHOD *orig, *data;
01086
01087 TypedData_Get_Struct(obj, struct METHOD, &method_data_type, orig);
01088 method = TypedData_Make_Struct(rb_cUnboundMethod, struct METHOD,
01089 &method_data_type, data);
01090 data->recv = Qundef;
01091 data->id = orig->id;
01092 data->me = ALLOC(rb_method_entry_t);
01093 *data->me = *orig->me;
01094 if (orig->me->def) orig->me->def->alias_count++;
01095 data->rclass = orig->rclass;
01096 data->ume = ALLOC(struct unlinked_method_entry_list_entry);
01097 OBJ_INFECT(method, obj);
01098
01099 return method;
01100 }
01101
01102
01103
01104
01105
01106
01107
01108
01109 static VALUE
01110 method_receiver(VALUE obj)
01111 {
01112 struct METHOD *data;
01113
01114 TypedData_Get_Struct(obj, struct METHOD, &method_data_type, data);
01115 return data->recv;
01116 }
01117
01118
01119
01120
01121
01122
01123
01124
01125 static VALUE
01126 method_name(VALUE obj)
01127 {
01128 struct METHOD *data;
01129
01130 TypedData_Get_Struct(obj, struct METHOD, &method_data_type, data);
01131 return ID2SYM(data->id);
01132 }
01133
01134
01135
01136
01137
01138
01139
01140
01141 static VALUE
01142 method_owner(VALUE obj)
01143 {
01144 struct METHOD *data;
01145
01146 TypedData_Get_Struct(obj, struct METHOD, &method_data_type, data);
01147 return data->me->klass;
01148 }
01149
01150
01151
01152
01153
01154
01155
01156
01157
01158
01159
01160
01161
01162
01163
01164
01165
01166
01167
01168
01169
01170
01171
01172
01173
01174
01175
01176
01177
01178 VALUE
01179 rb_obj_method(VALUE obj, VALUE vid)
01180 {
01181 return mnew(CLASS_OF(obj), obj, rb_to_id(vid), rb_cMethod, FALSE);
01182 }
01183
01184
01185
01186
01187
01188
01189
01190
01191 VALUE
01192 rb_obj_public_method(VALUE obj, VALUE vid)
01193 {
01194 return mnew(CLASS_OF(obj), obj, rb_to_id(vid), rb_cMethod, TRUE);
01195 }
01196
01197
01198
01199
01200
01201
01202
01203
01204
01205
01206
01207
01208
01209
01210
01211
01212
01213
01214
01215
01216
01217
01218
01219
01220
01221
01222
01223
01224
01225
01226
01227
01228 static VALUE
01229 rb_mod_instance_method(VALUE mod, VALUE vid)
01230 {
01231 return mnew(mod, Qundef, rb_to_id(vid), rb_cUnboundMethod, FALSE);
01232 }
01233
01234
01235
01236
01237
01238
01239
01240
01241 static VALUE
01242 rb_mod_public_instance_method(VALUE mod, VALUE vid)
01243 {
01244 return mnew(mod, Qundef, rb_to_id(vid), rb_cUnboundMethod, TRUE);
01245 }
01246
01247
01248
01249
01250
01251
01252
01253
01254
01255
01256
01257
01258
01259
01260
01261
01262
01263
01264
01265
01266
01267
01268
01269
01270
01271
01272
01273
01274
01275
01276
01277
01278
01279
01280
01281
01282
01283
01284 static VALUE
01285 rb_mod_define_method(int argc, VALUE *argv, VALUE mod)
01286 {
01287 ID id;
01288 VALUE body;
01289 int noex = NOEX_PUBLIC;
01290
01291 if (argc == 1) {
01292 id = rb_to_id(argv[0]);
01293 body = rb_block_lambda();
01294 }
01295 else if (argc == 2) {
01296 id = rb_to_id(argv[0]);
01297 body = argv[1];
01298 if (!rb_obj_is_method(body) && !rb_obj_is_proc(body)) {
01299 rb_raise(rb_eTypeError,
01300 "wrong argument type %s (expected Proc/Method)",
01301 rb_obj_classname(body));
01302 }
01303 }
01304 else {
01305 rb_raise(rb_eArgError, "wrong number of arguments (%d for 1)", argc);
01306 }
01307
01308 if (rb_obj_is_method(body)) {
01309 struct METHOD *method = (struct METHOD *)DATA_PTR(body);
01310 VALUE rclass = method->rclass;
01311 if (rclass != mod && !RTEST(rb_class_inherited_p(mod, rclass))) {
01312 if (FL_TEST(rclass, FL_SINGLETON)) {
01313 rb_raise(rb_eTypeError,
01314 "can't bind singleton method to a different class");
01315 }
01316 else {
01317 rb_raise(rb_eTypeError,
01318 "bind argument must be a subclass of %s",
01319 rb_class2name(rclass));
01320 }
01321 }
01322 rb_method_entry_set(mod, id, method->me, noex);
01323 }
01324 else if (rb_obj_is_proc(body)) {
01325 rb_proc_t *proc;
01326 body = proc_dup(body);
01327 GetProcPtr(body, proc);
01328 if (BUILTIN_TYPE(proc->block.iseq) != T_NODE) {
01329 proc->block.iseq->defined_method_id = id;
01330 proc->block.iseq->klass = mod;
01331 proc->is_lambda = TRUE;
01332 proc->is_from_method = TRUE;
01333 }
01334 rb_add_method(mod, id, VM_METHOD_TYPE_BMETHOD, (void *)body, noex);
01335 }
01336 else {
01337
01338 rb_raise(rb_eTypeError, "wrong argument type (expected Proc/Method)");
01339 }
01340
01341 return body;
01342 }
01343
01344
01345
01346
01347
01348
01349
01350
01351
01352
01353
01354
01355
01356
01357
01358
01359
01360
01361
01362
01363
01364
01365
01366
01367
01368
01369
01370 static VALUE
01371 rb_obj_define_method(int argc, VALUE *argv, VALUE obj)
01372 {
01373 VALUE klass = rb_singleton_class(obj);
01374
01375 return rb_mod_define_method(argc, argv, klass);
01376 }
01377
01378
01379
01380
01381
01382
01383 static VALUE
01384 method_clone(VALUE self)
01385 {
01386 VALUE clone;
01387 struct METHOD *orig, *data;
01388
01389 TypedData_Get_Struct(self, struct METHOD, &method_data_type, orig);
01390 clone = TypedData_Make_Struct(CLASS_OF(self), struct METHOD, &method_data_type, data);
01391 CLONESETUP(clone, self);
01392 *data = *orig;
01393 data->me = ALLOC(rb_method_entry_t);
01394 *data->me = *orig->me;
01395 if (data->me->def) data->me->def->alias_count++;
01396 data->ume = ALLOC(struct unlinked_method_entry_list_entry);
01397
01398 return clone;
01399 }
01400
01401
01402
01403
01404
01405
01406
01407
01408
01409
01410
01411
01412
01413
01414 VALUE
01415 rb_method_call(int argc, VALUE *argv, VALUE method)
01416 {
01417 VALUE result = Qnil;
01418 struct METHOD *data;
01419 int state;
01420 volatile int safe = -1;
01421
01422 TypedData_Get_Struct(method, struct METHOD, &method_data_type, data);
01423 if (data->recv == Qundef) {
01424 rb_raise(rb_eTypeError, "can't call unbound method; bind first");
01425 }
01426 PUSH_TAG();
01427 if (OBJ_TAINTED(method)) {
01428 safe = rb_safe_level();
01429 if (rb_safe_level() < 4) {
01430 rb_set_safe_level_force(4);
01431 }
01432 }
01433 if ((state = EXEC_TAG()) == 0) {
01434 rb_thread_t *th = GET_THREAD();
01435
01436 PASS_PASSED_BLOCK_TH(th);
01437 result = rb_vm_call(th, data->recv, data->id, argc, argv, data->me);
01438 }
01439 POP_TAG();
01440 if (safe >= 0)
01441 rb_set_safe_level_force(safe);
01442 if (state)
01443 JUMP_TAG(state);
01444 return result;
01445 }
01446
01447
01448
01449
01450
01451
01452
01453
01454
01455
01456
01457
01458
01459
01460
01461
01462
01463
01464
01465
01466
01467
01468
01469
01470
01471
01472
01473
01474
01475
01476
01477
01478
01479
01480
01481
01482
01483
01484
01485
01486
01487
01488
01489
01490
01491
01492
01493
01494
01495
01496
01497
01498
01499
01500
01501
01502
01503
01504
01505
01506
01507
01508
01509
01510
01511
01512
01513
01514
01515
01516
01517
01518
01519
01520
01521
01522
01523
01524
01525
01526
01527
01528
01529
01530
01531
01532
01533
01534
01535
01536
01537
01538 static VALUE
01539 umethod_bind(VALUE method, VALUE recv)
01540 {
01541 struct METHOD *data, *bound;
01542
01543 TypedData_Get_Struct(method, struct METHOD, &method_data_type, data);
01544
01545 if (data->rclass != CLASS_OF(recv) && !rb_obj_is_kind_of(recv, data->rclass)) {
01546 if (FL_TEST(data->rclass, FL_SINGLETON)) {
01547 rb_raise(rb_eTypeError,
01548 "singleton method called for a different object");
01549 }
01550 else {
01551 rb_raise(rb_eTypeError, "bind argument must be an instance of %s",
01552 rb_class2name(data->rclass));
01553 }
01554 }
01555
01556 method = TypedData_Make_Struct(rb_cMethod, struct METHOD, &method_data_type, bound);
01557 *bound = *data;
01558 bound->me = ALLOC(rb_method_entry_t);
01559 *bound->me = *data->me;
01560 if (bound->me->def) bound->me->def->alias_count++;
01561 bound->recv = recv;
01562 bound->rclass = CLASS_OF(recv);
01563 data->ume = ALLOC(struct unlinked_method_entry_list_entry);
01564
01565 return method;
01566 }
01567
01568 int
01569 rb_method_entry_arity(const rb_method_entry_t *me)
01570 {
01571 const rb_method_definition_t *def = me->def;
01572 if (!def) return 0;
01573 switch (def->type) {
01574 case VM_METHOD_TYPE_CFUNC:
01575 if (def->body.cfunc.argc < 0)
01576 return -1;
01577 return check_argc(def->body.cfunc.argc);
01578 case VM_METHOD_TYPE_ZSUPER:
01579 return -1;
01580 case VM_METHOD_TYPE_ATTRSET:
01581 return 1;
01582 case VM_METHOD_TYPE_IVAR:
01583 return 0;
01584 case VM_METHOD_TYPE_BMETHOD:
01585 return rb_proc_arity(def->body.proc);
01586 case VM_METHOD_TYPE_ISEQ: {
01587 rb_iseq_t *iseq = def->body.iseq;
01588 if (iseq->arg_rest == -1 && iseq->arg_opts == 0) {
01589 return iseq->argc;
01590 }
01591 else {
01592 return -(iseq->argc + 1 + iseq->arg_post_len);
01593 }
01594 }
01595 case VM_METHOD_TYPE_UNDEF:
01596 case VM_METHOD_TYPE_NOTIMPLEMENTED:
01597 return 0;
01598 case VM_METHOD_TYPE_MISSING:
01599 return -1;
01600 case VM_METHOD_TYPE_OPTIMIZED: {
01601 switch (def->body.optimize_type) {
01602 case OPTIMIZED_METHOD_TYPE_SEND:
01603 return -1;
01604 default:
01605 break;
01606 }
01607 }
01608 }
01609 rb_bug("rb_method_entry_arity: invalid method entry type (%d)", def->type);
01610 }
01611
01612
01613
01614
01615
01616
01617
01618
01619
01620
01621
01622
01623
01624
01625
01626
01627
01628
01629
01630
01631
01632
01633
01634
01635
01636
01637
01638
01639
01640
01641
01642
01643
01644
01645 static VALUE
01646 method_arity_m(VALUE method)
01647 {
01648 int n = method_arity(method);
01649 return INT2FIX(n);
01650 }
01651
01652 static int
01653 method_arity(VALUE method)
01654 {
01655 struct METHOD *data;
01656
01657 TypedData_Get_Struct(method, struct METHOD, &method_data_type, data);
01658 return rb_method_entry_arity(data->me);
01659 }
01660
01661 int
01662 rb_mod_method_arity(VALUE mod, ID id)
01663 {
01664 rb_method_entry_t *me = rb_method_entry(mod, id);
01665 return rb_method_entry_arity(me);
01666 }
01667
01668 int
01669 rb_obj_method_arity(VALUE obj, ID id)
01670 {
01671 return rb_mod_method_arity(CLASS_OF(obj), id);
01672 }
01673
01674 static inline rb_method_definition_t *
01675 method_get_def(VALUE method)
01676 {
01677 struct METHOD *data;
01678
01679 TypedData_Get_Struct(method, struct METHOD, &method_data_type, data);
01680 return data->me->def;
01681 }
01682
01683 static rb_iseq_t *
01684 method_get_iseq(rb_method_definition_t *def)
01685 {
01686 switch (def->type) {
01687 case VM_METHOD_TYPE_BMETHOD:
01688 return get_proc_iseq(def->body.proc, 0);
01689 case VM_METHOD_TYPE_ISEQ:
01690 return def->body.iseq;
01691 default:
01692 return 0;
01693 }
01694 }
01695
01696 rb_iseq_t *
01697 rb_method_get_iseq(VALUE method)
01698 {
01699 return method_get_iseq(method_get_def(method));
01700 }
01701
01702
01703
01704
01705
01706
01707
01708
01709
01710 VALUE
01711 rb_method_location(VALUE method)
01712 {
01713 rb_method_definition_t *def = method_get_def(method);
01714 if (def->type == VM_METHOD_TYPE_ATTRSET || def->type == VM_METHOD_TYPE_IVAR) {
01715 if (!def->body.attr.location)
01716 return Qnil;
01717 return rb_ary_dup(def->body.attr.location);
01718 }
01719 return iseq_location(method_get_iseq(def));
01720 }
01721
01722
01723
01724
01725
01726
01727
01728
01729 static VALUE
01730 rb_method_parameters(VALUE method)
01731 {
01732 rb_iseq_t *iseq = rb_method_get_iseq(method);
01733 if (!iseq) {
01734 return unnamed_parameters(method_arity(method));
01735 }
01736 return rb_iseq_parameters(iseq, 0);
01737 }
01738
01739
01740
01741
01742
01743
01744
01745
01746
01747
01748
01749 static VALUE
01750 method_inspect(VALUE method)
01751 {
01752 struct METHOD *data;
01753 VALUE str;
01754 const char *s;
01755 const char *sharp = "#";
01756
01757 TypedData_Get_Struct(method, struct METHOD, &method_data_type, data);
01758 str = rb_str_buf_new2("#<");
01759 s = rb_obj_classname(method);
01760 rb_str_buf_cat2(str, s);
01761 rb_str_buf_cat2(str, ": ");
01762
01763 if (FL_TEST(data->me->klass, FL_SINGLETON)) {
01764 VALUE v = rb_iv_get(data->me->klass, "__attached__");
01765
01766 if (data->recv == Qundef) {
01767 rb_str_buf_append(str, rb_inspect(data->me->klass));
01768 }
01769 else if (data->recv == v) {
01770 rb_str_buf_append(str, rb_inspect(v));
01771 sharp = ".";
01772 }
01773 else {
01774 rb_str_buf_append(str, rb_inspect(data->recv));
01775 rb_str_buf_cat2(str, "(");
01776 rb_str_buf_append(str, rb_inspect(v));
01777 rb_str_buf_cat2(str, ")");
01778 sharp = ".";
01779 }
01780 }
01781 else {
01782 rb_str_buf_cat2(str, rb_class2name(data->rclass));
01783 if (data->rclass != data->me->klass) {
01784 rb_str_buf_cat2(str, "(");
01785 rb_str_buf_cat2(str, rb_class2name(data->me->klass));
01786 rb_str_buf_cat2(str, ")");
01787 }
01788 }
01789 rb_str_buf_cat2(str, sharp);
01790 rb_str_append(str, rb_id2str(data->me->def->original_id));
01791 if (data->me->def->type == VM_METHOD_TYPE_NOTIMPLEMENTED) {
01792 rb_str_buf_cat2(str, " (not-implemented)");
01793 }
01794 rb_str_buf_cat2(str, ">");
01795
01796 return str;
01797 }
01798
01799 static VALUE
01800 mproc(VALUE method)
01801 {
01802 return rb_funcall(Qnil, rb_intern("proc"), 0);
01803 }
01804
01805 static VALUE
01806 mlambda(VALUE method)
01807 {
01808 return rb_funcall(Qnil, rb_intern("lambda"), 0);
01809 }
01810
01811 static VALUE
01812 bmcall(VALUE args, VALUE method)
01813 {
01814 volatile VALUE a;
01815 VALUE ret;
01816 int argc;
01817
01818 if (CLASS_OF(args) != rb_cArray) {
01819 args = rb_ary_new3(1, args);
01820 argc = 1;
01821 }
01822 else {
01823 argc = check_argc(RARRAY_LEN(args));
01824 }
01825 ret = rb_method_call(argc, RARRAY_PTR(args), method);
01826 RB_GC_GUARD(a) = args;
01827 return ret;
01828 }
01829
01830 VALUE
01831 rb_proc_new(
01832 VALUE (*func)(ANYARGS),
01833 VALUE val)
01834 {
01835 VALUE procval = rb_iterate(mproc, 0, func, val);
01836 return procval;
01837 }
01838
01839
01840
01841
01842
01843
01844
01845
01846 static VALUE
01847 method_proc(VALUE method)
01848 {
01849 VALUE procval;
01850 rb_proc_t *proc;
01851
01852
01853
01854
01855
01856
01857
01858
01859
01860 procval = rb_iterate(mlambda, 0, bmcall, method);
01861 GetProcPtr(procval, proc);
01862 proc->is_from_method = 1;
01863 return procval;
01864 }
01865
01866
01867
01868
01869
01870
01871
01872 static VALUE
01873 localjump_xvalue(VALUE exc)
01874 {
01875 return rb_iv_get(exc, "@exit_value");
01876 }
01877
01878
01879
01880
01881
01882
01883
01884
01885
01886 static VALUE
01887 localjump_reason(VALUE exc)
01888 {
01889 return rb_iv_get(exc, "@reason");
01890 }
01891
01892
01893
01894
01895
01896
01897
01898
01899
01900
01901
01902
01903
01904
01905
01906
01907 static VALUE
01908 proc_binding(VALUE self)
01909 {
01910 rb_proc_t *proc;
01911 VALUE bindval;
01912 rb_binding_t *bind;
01913
01914 GetProcPtr(self, proc);
01915 if (TYPE(proc->block.iseq) == T_NODE) {
01916 if (!IS_METHOD_PROC_NODE((NODE *)proc->block.iseq)) {
01917 rb_raise(rb_eArgError, "Can't create Binding from C level Proc");
01918 }
01919 }
01920
01921 bindval = binding_alloc(rb_cBinding);
01922 GetBindingPtr(bindval, bind);
01923 bind->env = proc->envval;
01924 if (RUBY_VM_NORMAL_ISEQ_P(proc->block.iseq)) {
01925 bind->filename = proc->block.iseq->filename;
01926 bind->line_no = rb_iseq_first_lineno(proc->block.iseq);
01927 }
01928 else {
01929 bind->filename = Qnil;
01930 bind->line_no = 0;
01931 }
01932 return bindval;
01933 }
01934
01935 static VALUE curry(VALUE dummy, VALUE args, int argc, VALUE *argv, VALUE passed_proc);
01936
01937 static VALUE
01938 make_curry_proc(VALUE proc, VALUE passed, VALUE arity)
01939 {
01940 VALUE args = rb_ary_new3(3, proc, passed, arity);
01941 rb_proc_t *procp;
01942 int is_lambda;
01943
01944 GetProcPtr(proc, procp);
01945 is_lambda = procp->is_lambda;
01946 rb_ary_freeze(passed);
01947 rb_ary_freeze(args);
01948 proc = rb_proc_new(curry, args);
01949 GetProcPtr(proc, procp);
01950 procp->is_lambda = is_lambda;
01951 return proc;
01952 }
01953
01954 static VALUE
01955 curry(VALUE dummy, VALUE args, int argc, VALUE *argv, VALUE passed_proc)
01956 {
01957 VALUE proc, passed, arity;
01958 proc = RARRAY_PTR(args)[0];
01959 passed = RARRAY_PTR(args)[1];
01960 arity = RARRAY_PTR(args)[2];
01961
01962 passed = rb_ary_plus(passed, rb_ary_new4(argc, argv));
01963 rb_ary_freeze(passed);
01964
01965 if (RARRAY_LEN(passed) < FIX2INT(arity)) {
01966 if (!NIL_P(passed_proc)) {
01967 rb_warn("given block not used");
01968 }
01969 arity = make_curry_proc(proc, passed, arity);
01970 return arity;
01971 }
01972 else {
01973 return rb_proc_call_with_block(proc, check_argc(RARRAY_LEN(passed)),
01974 RARRAY_PTR(passed), passed_proc);
01975 }
01976 }
01977
01978
01979
01980
01981
01982
01983
01984
01985
01986
01987
01988
01989
01990
01991
01992
01993
01994
01995
01996
01997
01998
01999
02000
02001
02002
02003
02004
02005
02006
02007
02008
02009
02010
02011
02012
02013
02014
02015
02016
02017
02018
02019
02020 static VALUE
02021 proc_curry(int argc, VALUE *argv, VALUE self)
02022 {
02023 int sarity, marity = rb_proc_arity(self);
02024 VALUE arity, opt = Qfalse;
02025
02026 if (marity < 0) {
02027 marity = -marity - 1;
02028 opt = Qtrue;
02029 }
02030
02031 rb_scan_args(argc, argv, "01", &arity);
02032 if (NIL_P(arity)) {
02033 arity = INT2FIX(marity);
02034 }
02035 else {
02036 sarity = FIX2INT(arity);
02037 if (rb_proc_lambda_p(self) && (sarity < marity || (sarity > marity && !opt))) {
02038 rb_raise(rb_eArgError, "wrong number of arguments (%d for %d)", sarity, marity);
02039 }
02040 }
02041
02042 return make_curry_proc(self, rb_ary_new(), arity);
02043 }
02044
02045
02046
02047
02048
02049
02050
02051
02052
02053
02054
02055
02056
02057
02058
02059
02060
02061
02062
02063
02064
02065
02066
02067
02068
02069
02070
02071
02072
02073
02074
02075
02076
02077
02078
02079
02080
02081
02082
02083
02084
02085
02086
02087
02088
02089
02090
02091
02092
02093
02094
02095
02096
02097
02098
02099
02100
02101
02102
02103
02104
02105
02106 void
02107 Init_Proc(void)
02108 {
02109
02110 rb_cProc = rb_define_class("Proc", rb_cObject);
02111 rb_undef_alloc_func(rb_cProc);
02112 rb_define_singleton_method(rb_cProc, "new", rb_proc_s_new, -1);
02113
02114 #if 0
02115 rb_add_method(rb_cProc, rb_intern("call"), VM_METHOD_TYPE_OPTIMIZED,
02116 (void *)OPTIMIZED_METHOD_TYPE_CALL, 0);
02117 rb_add_method(rb_cProc, rb_intern("[]"), VM_METHOD_TYPE_OPTIMIZED,
02118 (void *)OPTIMIZED_METHOD_TYPE_CALL, 0);
02119 rb_add_method(rb_cProc, rb_intern("==="), VM_METHOD_TYPE_OPTIMIZED,
02120 (void *)OPTIMIZED_METHOD_TYPE_CALL, 0);
02121 rb_add_method(rb_cProc, rb_intern("yield"), VM_METHOD_TYPE_OPTIMIZED,
02122 (void *)OPTIMIZED_METHOD_TYPE_CALL, 0);
02123 #else
02124 rb_define_method(rb_cProc, "call", proc_call, -1);
02125 rb_define_method(rb_cProc, "[]", proc_call, -1);
02126 rb_define_method(rb_cProc, "===", proc_call, -1);
02127 rb_define_method(rb_cProc, "yield", proc_call, -1);
02128 #endif
02129 rb_define_method(rb_cProc, "to_proc", proc_to_proc, 0);
02130 rb_define_method(rb_cProc, "arity", proc_arity, 0);
02131 rb_define_method(rb_cProc, "clone", proc_clone, 0);
02132 rb_define_method(rb_cProc, "dup", proc_dup, 0);
02133 rb_define_method(rb_cProc, "==", proc_eq, 1);
02134 rb_define_method(rb_cProc, "eql?", proc_eq, 1);
02135 rb_define_method(rb_cProc, "hash", proc_hash, 0);
02136 rb_define_method(rb_cProc, "to_s", proc_to_s, 0);
02137 rb_define_method(rb_cProc, "lambda?", rb_proc_lambda_p, 0);
02138 rb_define_method(rb_cProc, "binding", proc_binding, 0);
02139 rb_define_method(rb_cProc, "curry", proc_curry, -1);
02140 rb_define_method(rb_cProc, "source_location", rb_proc_location, 0);
02141 rb_define_method(rb_cProc, "parameters", rb_proc_parameters, 0);
02142
02143
02144 rb_eLocalJumpError = rb_define_class("LocalJumpError", rb_eStandardError);
02145 rb_define_method(rb_eLocalJumpError, "exit_value", localjump_xvalue, 0);
02146 rb_define_method(rb_eLocalJumpError, "reason", localjump_reason, 0);
02147
02148 rb_eSysStackError = rb_define_class("SystemStackError", rb_eException);
02149 sysstack_error = rb_exc_new3(rb_eSysStackError,
02150 rb_obj_freeze(rb_str_new2("stack level too deep")));
02151 OBJ_TAINT(sysstack_error);
02152
02153
02154 rb_define_global_function("proc", rb_block_proc, 0);
02155 rb_define_global_function("lambda", proc_lambda, 0);
02156
02157
02158 rb_cMethod = rb_define_class("Method", rb_cObject);
02159 rb_undef_alloc_func(rb_cMethod);
02160 rb_undef_method(CLASS_OF(rb_cMethod), "new");
02161 rb_define_method(rb_cMethod, "==", method_eq, 1);
02162 rb_define_method(rb_cMethod, "eql?", method_eq, 1);
02163 rb_define_method(rb_cMethod, "hash", method_hash, 0);
02164 rb_define_method(rb_cMethod, "clone", method_clone, 0);
02165 rb_define_method(rb_cMethod, "call", rb_method_call, -1);
02166 rb_define_method(rb_cMethod, "[]", rb_method_call, -1);
02167 rb_define_method(rb_cMethod, "arity", method_arity_m, 0);
02168 rb_define_method(rb_cMethod, "inspect", method_inspect, 0);
02169 rb_define_method(rb_cMethod, "to_s", method_inspect, 0);
02170 rb_define_method(rb_cMethod, "to_proc", method_proc, 0);
02171 rb_define_method(rb_cMethod, "receiver", method_receiver, 0);
02172 rb_define_method(rb_cMethod, "name", method_name, 0);
02173 rb_define_method(rb_cMethod, "owner", method_owner, 0);
02174 rb_define_method(rb_cMethod, "unbind", method_unbind, 0);
02175 rb_define_method(rb_cMethod, "source_location", rb_method_location, 0);
02176 rb_define_method(rb_cMethod, "parameters", rb_method_parameters, 0);
02177 rb_define_method(rb_mKernel, "method", rb_obj_method, 1);
02178 rb_define_method(rb_mKernel, "public_method", rb_obj_public_method, 1);
02179
02180
02181 rb_cUnboundMethod = rb_define_class("UnboundMethod", rb_cObject);
02182 rb_undef_alloc_func(rb_cUnboundMethod);
02183 rb_undef_method(CLASS_OF(rb_cUnboundMethod), "new");
02184 rb_define_method(rb_cUnboundMethod, "==", method_eq, 1);
02185 rb_define_method(rb_cUnboundMethod, "eql?", method_eq, 1);
02186 rb_define_method(rb_cUnboundMethod, "hash", method_hash, 0);
02187 rb_define_method(rb_cUnboundMethod, "clone", method_clone, 0);
02188 rb_define_method(rb_cUnboundMethod, "arity", method_arity_m, 0);
02189 rb_define_method(rb_cUnboundMethod, "inspect", method_inspect, 0);
02190 rb_define_method(rb_cUnboundMethod, "to_s", method_inspect, 0);
02191 rb_define_method(rb_cUnboundMethod, "name", method_name, 0);
02192 rb_define_method(rb_cUnboundMethod, "owner", method_owner, 0);
02193 rb_define_method(rb_cUnboundMethod, "bind", umethod_bind, 1);
02194 rb_define_method(rb_cUnboundMethod, "source_location", rb_method_location, 0);
02195 rb_define_method(rb_cUnboundMethod, "parameters", rb_method_parameters, 0);
02196
02197
02198 rb_define_method(rb_cModule, "instance_method", rb_mod_instance_method, 1);
02199 rb_define_method(rb_cModule, "public_instance_method", rb_mod_public_instance_method, 1);
02200 rb_define_private_method(rb_cModule, "define_method", rb_mod_define_method, -1);
02201
02202
02203 rb_define_method(rb_mKernel, "define_singleton_method", rb_obj_define_method, -1);
02204 }
02205
02206
02207
02208
02209
02210
02211
02212
02213
02214
02215
02216
02217
02218
02219
02220
02221
02222
02223
02224
02225
02226
02227
02228
02229
02230
02231
02232
02233
02234
02235
02236
02237
02238
02239
02240
02241 void
02242 Init_Binding(void)
02243 {
02244 rb_cBinding = rb_define_class("Binding", rb_cObject);
02245 rb_undef_alloc_func(rb_cBinding);
02246 rb_undef_method(CLASS_OF(rb_cBinding), "new");
02247 rb_define_method(rb_cBinding, "clone", binding_clone, 0);
02248 rb_define_method(rb_cBinding, "dup", binding_dup, 0);
02249 rb_define_method(rb_cBinding, "eval", bind_eval, -1);
02250 rb_define_global_function("binding", rb_f_binding, 0);
02251 }
02252
02253