From 7f82a6f31e40e596a43939ab18d685a8f0b78d7b Mon Sep 17 00:00:00 2001 From: rich Date: Thu, 25 Nov 2004 22:16:17 +0000 Subject: [PATCH] Added correct handling of undef. --- examples/test.ml | 7 ++++++- perl.ml | 5 ++--- perl_c.c | 14 ++++++++++++-- 3 files changed, 20 insertions(+), 6 deletions(-) diff --git a/examples/test.ml b/examples/test.ml index 05d6042..490401e 100644 --- a/examples/test.ml +++ b/examples/test.ml @@ -1,6 +1,6 @@ (* Simple test of the API. * Copyright (C) 2003 Merjis Ltd. - * $Id: test.ml,v 1.6 2003-12-11 17:41:52 rich Exp $ + * $Id: test.ml,v 1.7 2004-11-25 22:16:17 rich Exp $ *) open Printf @@ -48,5 +48,10 @@ let () = let sv = Perl.call_method obj "get_foo" [] in printf "TestClass.foo is %d\n" (Perl.int_of_sv sv); flush stdout; + (* Create an undef value and test it. *) + let undef = Perl.sv_undef () in + printf "sv_is_undef (undef) = %s\n" + (string_of_bool (Perl.sv_is_undef undef)); + (* Perform a full collection - good way to find GC/allocation bugs. *) Gc.full_major () diff --git a/perl.ml b/perl.ml index a193446..cdbfa7c 100644 --- a/perl.ml +++ b/perl.ml @@ -1,6 +1,6 @@ (* Interface to Perl from OCaml. * Copyright (C) 2003 Merjis Ltd. - * $Id: perl.ml,v 1.11 2003-12-11 17:41:52 rich Exp $ + * $Id: perl.ml,v 1.12 2004-11-25 22:16:17 rich Exp $ *) type sv @@ -26,6 +26,7 @@ external string_of_sv : sv -> string = "perl4caml_string_of_sv" external sv_of_string : string -> sv = "perl4caml_sv_of_string" external sv_is_true : sv -> bool = "perl4caml_sv_is_true" external sv_undef : unit -> sv = "perl4caml_sv_undef" +external sv_is_undef : sv -> bool = "perl4caml_sv_is_undef" external sv_yes : unit -> sv = "perl4caml_sv_yes" external sv_no : unit -> sv = "perl4caml_sv_no" @@ -47,8 +48,6 @@ type sv_t = SVt_NULL | SVt_PVMG external sv_type : sv -> sv_t = "perl4caml_sv_type" -let sv_is_undef sv = - SVt_NULL = sv_type sv let string_of_sv_t = function SVt_NULL -> "SVt_NULL" diff --git a/perl_c.c b/perl_c.c index f11a2b5..4241534 100644 --- 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.16 2004-11-03 14:15:18 rich Exp $ + * $Id: perl_c.c,v 1.17 2004-11-25 22:16:17 rich Exp $ */ #include @@ -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 -- 1.8.3.1