define_finalizer(p1, p2 = v2)
public
Adds aProc as a finalizer, to be called
after obj was destroyed.
Show source
static VALUE
define_final(int argc, VALUE *argv, VALUE os)
{
rb_objspace_t *objspace = &rb_objspace;
VALUE obj, block, table;
st_data_t data;
rb_scan_args(argc, argv, "11", &obj, &block);
rb_check_frozen(obj);
if (argc == 1) {
block = rb_block_proc();
}
else if (!rb_respond_to(block, rb_intern("call"))) {
rb_raise(rb_eArgError, "wrong type argument %s (should be callable)",
rb_obj_classname(block));
}
if (!FL_ABLE(obj)) {
rb_raise(rb_eArgError, "cannot define finalizer for %s",
rb_obj_classname(obj));
}
RBASIC(obj)->flags |= FL_FINALIZE;
block = rb_ary_new3(2, INT2FIX(rb_safe_level()), block);
OBJ_FREEZE(block);
if (st_lookup(finalizer_table, obj, &data)) {
table = (VALUE)data;
rb_ary_push(table, block);
}
else {
table = rb_ary_new3(1, block);
RBASIC(table)->klass = 0;
st_add_direct(finalizer_table, obj, table);
}
return block;
}