Index: ext/dl/handle.c =================================================================== --- ext/dl/handle.c (revision 22884) +++ ext/dl/handle.c (working copy) @@ -130,39 +130,43 @@ rb_dlhandle_to_i(VALUE self) } +static VALUE dlhandle_sym(void *handle, const char *symbol); + VALUE rb_dlhandle_sym(VALUE self, VALUE sym) { - void (*func)(); struct dl_handle *dlhandle; - void *handle; - const char *name; -#if defined(HAVE_DLERROR) - const char *err; -# define CHECK_DLERROR if( err = dlerror() ){ func = 0; } -#else -# define CHECK_DLERROR -#endif rb_secure(2); + Data_Get_Struct(self, struct dl_handle, dlhandle); + if( ! dlhandle->open ){ + rb_raise(rb_eDLError, "closed handle"); + } + return dlhandle_sym(dlhandle->ptr, StringValuePtr(sym)); +} - if( sym == Qnil ){ -#if defined(RTLD_NEXT) - name = RTLD_NEXT; +VALUE +rb_dlhandle_s_sym(VALUE self, VALUE sym) +{ +#ifdef RTLD_NEXT + void *handle = RTLD_NEXT; #else - name = NULL; + void *handle = NULL; #endif - } - else{ - name = StringValuePtr(sym); - } + rb_secure(2); + return dlhandle_sym(handle, StringValuePtr(sym)); +} - Data_Get_Struct(self, struct dl_handle, dlhandle); - if( ! dlhandle->open ){ - rb_raise(rb_eDLError, "closed handle"); - } - handle = dlhandle->ptr; +static VALUE +dlhandle_sym(void *handle, const char *name) +{ +#if defined(HAVE_DLERROR) + const char *err; +# define CHECK_DLERROR if( err = dlerror() ){ func = 0; } +#else +# define CHECK_DLERROR +#endif + void (*func)() = dlsym(handle, name); - func = dlsym(handle, name); CHECK_DLERROR; #if defined(FUNC_STDCALL) @@ -219,4 +223,6 @@ Init_dlhandle(void) rb_cDLHandle = rb_define_class_under(rb_mDL, "Handle", rb_cObject); rb_define_alloc_func(rb_cDLHandle, rb_dlhandle_s_allocate); + rb_define_singleton_method(rb_cDLHandle, "sym", rb_dlhandle_s_sym, 1); + rb_define_singleton_method(rb_cDLHandle, "[]", rb_dlhandle_s_sym, 1); rb_define_method(rb_cDLHandle, "initialize", rb_dlhandle_initialize, -1); rb_define_method(rb_cDLHandle, "to_i", rb_dlhandle_to_i, 0);