ruby: Use a regular C array to pass the arguments through rb_rescue.
[libguestfs.git] / generator / generator_ruby.ml
index a2a112a..9ac35d7 100644 (file)
@@ -55,6 +55,8 @@ static VALUE c_guestfs;                       /* guestfs_h handle */
 static VALUE e_Error;                  /* used for all errors */
 
 static void ruby_event_callback_wrapper (guestfs_h *g, void *data, uint64_t event, int event_handle, int flags, const char *buf, size_t buf_len, const uint64_t *array, size_t array_len);
+static VALUE ruby_event_callback_wrapper_wrapper (VALUE argv);
+static VALUE ruby_event_callback_handle_exception (VALUE not_used, VALUE exn);
 static VALUE **get_all_event_callbacks (guestfs_h *g, size_t *len_rtn);
 
 static void
@@ -213,6 +215,7 @@ ruby_event_callback_wrapper (guestfs_h *g,
 {
   size_t i;
   VALUE eventv, event_handlev, bufv, arrayv;
+  VALUE argv[5];
 
   eventv = ULL2NUM (event);
   event_handlev = INT2NUM (event_handle);
@@ -223,12 +226,47 @@ ruby_event_callback_wrapper (guestfs_h *g,
   for (i = 0; i < array_len; ++i)
     rb_ary_push (arrayv, ULL2NUM (array[i]));
 
-  /* XXX If the Ruby callback raises any sort of exception then
-   * it causes the process to segfault.  I don't understand how
-   * to catch exceptions here.
+  /* This is a crap limitation of rb_rescue.
+   * http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/~poffice/mail/ruby-talk/65698
    */
-  rb_funcall (*(VALUE *) data, rb_intern (\"call\"), 4,
+  argv[0] = * (VALUE *) data; /* function */
+  argv[1] = eventv;
+  argv[2] = event_handlev;
+  argv[3] = bufv;
+  argv[4] = arrayv;
+
+  rb_rescue (ruby_event_callback_wrapper_wrapper, (VALUE) argv,
+             ruby_event_callback_handle_exception, Qnil);
+}
+
+static VALUE
+ruby_event_callback_wrapper_wrapper (VALUE argvv)
+{
+  VALUE *argv = (VALUE *) argvv;
+  VALUE fn, eventv, event_handlev, bufv, arrayv;
+
+  fn = argv[0];
+  eventv = argv[1];
+  event_handlev = argv[2];
+  bufv = argv[3];
+  arrayv = argv[4];
+
+  rb_funcall (fn, rb_intern (\"call\"), 4,
               eventv, event_handlev, bufv, arrayv);
+
+  return Qnil;
+}
+
+static VALUE
+ruby_event_callback_handle_exception (VALUE not_used, VALUE exn)
+{
+  /* Callbacks aren't supposed to throw exceptions.  The best we
+   * can do is to print the error.
+   */
+  fprintf (stderr, \"libguestfs: exception in callback: %%s\\n\",
+           StringValueCStr (exn));
+
+  return Qnil;
 }
 
 static VALUE **
@@ -351,11 +389,7 @@ get_all_event_callbacks (guestfs_h *g, size_t *len_rtn)
         function
         | Pathname n | Device n | Dev_or_Path n | String n | Key n
         | FileIn n | FileOut n ->
-            pr "  Check_Type (%sv, T_STRING);\n" n;
             pr "  const char *%s = StringValueCStr (%sv);\n" n n;
-            pr "  if (!%s)\n" n;
-            pr "    rb_raise (rb_eTypeError, \"expected string for parameter %%s of %%s\",\n";
-            pr "              \"%s\", \"%s\");\n" n name
         | BufferIn n ->
             pr "  Check_Type (%sv, T_STRING);\n" n;
             pr "  const char *%s = RSTRING (%sv)->ptr;\n" n n;
@@ -371,7 +405,7 @@ get_all_event_callbacks (guestfs_h *g, size_t *len_rtn)
             pr "  {\n";
             pr "    size_t i, len;\n";
             pr "    len = RARRAY_LEN (%sv);\n" n;
-            pr "    %s = guestfs_safe_malloc (g, sizeof (char *) * (len+1));\n"
+            pr "    %s = ALLOC_N (char *, len+1);\n"
               n;
             pr "    for (i = 0; i < len; ++i) {\n";
             pr "      VALUE v = rb_ary_entry (%sv, i);\n" n;
@@ -411,7 +445,6 @@ get_all_event_callbacks (guestfs_h *g, size_t *len_rtn)
              | Int64 n ->
                  pr "    optargs_s.%s = NUM2LL (v);\n" n;
              | String _ ->
-                 pr "    Check_Type (v, T_STRING);\n";
                  pr "    optargs_s.%s = StringValueCStr (v);\n" n
              | _ -> assert false
             );