From 3349e337fa0373abb936c3f8540e23613229178e Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Sun, 27 Feb 2011 07:42:38 +0000 Subject: add Mahoro#compile instance method Implement the existing singleton class method in terms of this so we won't leak memory on exceptions. --- mahoro.c | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/mahoro.c b/mahoro.c index e9e5e91..3971fdd 100644 --- a/mahoro.c +++ b/mahoro.c @@ -130,20 +130,27 @@ mahoro_check(argc, argv, self) } static VALUE -mahoro_compile(klass, path) - VALUE klass, path; +mahoro_compile(self, path) + VALUE self, path; { - magic_t cookie = magic_open(MAGIC_NONE); + magic_t cookie = (magic_t)DATA_PTR(self); if(magic_compile(cookie, StringValueCStr(path))) { rb_raise(eMahoroError, "failed compile: %s", magic_error(cookie)); } - magic_close(cookie); - return Qtrue; } +static VALUE +mahoro_s_compile(klass, path) + VALUE klass, path; +{ + VALUE m = rb_funcall(klass, rb_intern("new"), 0, 0); + + return mahoro_compile(m, path); +} + static VALUE mahoro_load(self, path) VALUE self, path; @@ -272,7 +279,8 @@ void Init_mahoro(void) rb_define_method(cMahoro, "buffer", mahoro_buffer, 1); rb_define_method(cMahoro, "flags=", mahoro_set_flags, 1); rb_define_method(cMahoro, "valid?", mahoro_check, -1); - rb_define_singleton_method(cMahoro, "compile", mahoro_compile, 1); + rb_define_singleton_method(cMahoro, "compile", mahoro_s_compile, 1); + rb_define_method(cMahoro, "compile", mahoro_compile, 1); rb_define_method(cMahoro, "load", mahoro_load, 1); } -- cgit v1.2.3-24-ge0c7