Added correct handling of undef.
[perl4caml.git] / perl_c.c
index 3214e34..4241534 100644 (file)
--- a/perl_c.c
+++ b/perl_c.c
@@ -1,6 +1,6 @@
 /* Interface to Perl from OCaml.
  * Copyright (C) 2003 Merjis Ltd.
- * $Id: perl_c.c,v 1.14 2004-02-03 12:38:57 rich Exp $
+ * $Id: perl_c.c,v 1.17 2004-11-25 22:16:17 rich Exp $
  */
 
 #include <stdio.h>
@@ -158,7 +158,17 @@ CAMLprim value
 perl4caml_sv_undef (value unit)
 {
   CAMLparam1 (unit);
-  CAMLreturn (Val_sv (newSV (0)));
+  /*CAMLreturn (Val_sv (newSV (0)));*/
+  CAMLreturn (Val_sv (&PL_sv_undef));
+}
+
+CAMLprim value
+perl4caml_sv_is_undef (value svv)
+{
+  CAMLparam1 (svv);
+  SV *sv = Sv_val (svv);
+  CAMLreturn (!SvPOK (sv) && !SvIOK (sv) && SvTYPE (sv) == SVt_NULL
+             ? Val_true : Val_false);
 }
 
 CAMLprim value
@@ -233,7 +243,7 @@ perl4caml_deref (value svv)
   CAMLlocal1 (rsvv);
   SV *sv = Sv_val (svv);
 
-  if (SvTYPE (sv) != SVt_RV)
+  if (!SvROK (sv))
     invalid_argument ("deref: SV is not a reference");
   switch (SvTYPE (SvRV (sv))) {
   case SVt_IV:
@@ -256,7 +266,7 @@ perl4caml_deref_array (value svv)
   CAMLlocal1 (ravv);
   SV *sv = Sv_val (svv);
 
-  if (SvTYPE (sv) != SVt_RV)
+  if (!SvROK (sv))
     invalid_argument ("deref_array: SV is not a reference");
   switch (SvTYPE (SvRV (sv))) {
   case SVt_PVAV:
@@ -275,13 +285,13 @@ perl4caml_deref_hash (value svv)
   CAMLlocal1 (rhvv);
   SV *sv = Sv_val (svv);
 
-  if (SvTYPE (sv) != SVt_RV)
-    invalid_argument ("deref_array: SV is not a reference");
+  if (!SvROK (sv))
+    invalid_argument ("deref_hash: SV is not a reference");
   switch (SvTYPE (SvRV (sv))) {
   case SVt_PVHV:
     break;
   default:
-    invalid_argument ("deref_array: SV is not a reference to a hash");
+    invalid_argument ("deref_hash: SV is not a reference to a hash");
   }
   rhvv = Val_hv ((HV *) SvRV (sv));
   CAMLreturn (rhvv);