about summary refs log tree commit homepage
diff options
context:
space:
mode:
authorEric Wong <normalperson@yhbt.net>2011-02-27 07:42:38 +0000
committerEric Wong <normalperson@yhbt.net>2011-02-27 07:42:38 +0000
commit3349e337fa0373abb936c3f8540e23613229178e (patch)
tree9b44acf3b072663d7b38646b412d1d15fbcc0aa6
parent2a7697fea8f0a427e0cd37f08b49480c749c8f6a (diff)
downloadmahoro-3349e337fa0373abb936c3f8540e23613229178e.tar.gz
add Mahoro#compile instance method
Implement the existing singleton class method in terms
of this so we won't leak memory on exceptions.
-rw-r--r--mahoro.c20
1 files changed, 14 insertions, 6 deletions
diff --git a/mahoro.c b/mahoro.c
index e9e5e91..3971fdd 100644
--- a/mahoro.c
+++ b/mahoro.c
@@ -130,21 +130,28 @@ 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);
 }