From 5e86bfad5637289608ed35b439bb5247f96e8453 Mon Sep 17 00:00:00 2001 From: rich Date: Sun, 26 Oct 2003 11:22:38 +0000 Subject: [PATCH] Added support for references. Fixed support for floats. --- perl.ml | 10 +++++++--- perl.mli | 19 ++++++++++++++++--- perl_c.c | 32 +++++++++++++++++++++++++++++++- 3 files changed, 54 insertions(+), 7 deletions(-) diff --git a/perl.ml b/perl.ml index 95ad3e5..5812a61 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.8 2003-10-18 12:36:09 rich Exp $ + * $Id: perl.ml,v 1.9 2003-10-26 11:22:38 rich Exp $ *) type t @@ -36,8 +36,8 @@ external set_context : t -> unit external int_of_sv : sv -> int = "perl4caml_int_of_sv" external sv_of_int : int -> sv = "perl4caml_sv_of_int" -external float_of_sv : sv -> int = "perl4caml_float_of_sv" -external sv_of_float : int -> sv = "perl4caml_sv_of_float" +external float_of_sv : sv -> float = "perl4caml_float_of_sv" +external sv_of_float : float -> sv = "perl4caml_sv_of_float" 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" @@ -77,6 +77,10 @@ let string_of_sv_t = function | SVt_PVGV -> "SVt_PVGV" | SVt_PVMG -> "SVt_PVMG" +external scalarref : sv -> sv = "perl4caml_scalarref" +external arrayref : av -> sv = "perl4caml_arrayref" +external hashref : hv -> sv = "perl4caml_hashref" + external deref : sv -> sv = "perl4caml_deref" external deref_array : sv -> av = "perl4caml_deref_array" external deref_hash : sv -> hv = "perl4caml_deref_hash" diff --git a/perl.mli b/perl.mli index ef8dea0..831d69b 100644 --- a/perl.mli +++ b/perl.mli @@ -2,7 +2,7 @@ * * Copyright (C) 2003 Merjis Ltd. * - * $Id: perl.mli,v 1.9 2003-10-18 12:36:09 rich Exp $ + * $Id: perl.mli,v 1.10 2003-10-26 11:22:38 rich Exp $ *) type t @@ -88,9 +88,9 @@ val int_of_sv : sv -> int *) val sv_of_int : int -> sv (** Convert an [int] into a Perl [SV]. *) -val float_of_sv : sv -> int +val float_of_sv : sv -> float (** Convert a Perl [SV] into a float. *) -val sv_of_float : int -> sv +val sv_of_float : float -> sv (** Convert a [float] into a Perl [SV]. *) val string_of_sv : sv -> string (** Convert a Perl [SV] into a string. *) @@ -136,6 +136,19 @@ val sv_type : sv -> sv_t val string_of_sv_t : sv_t -> string (** Return a printable string for an [sv_t] ([SV] type). *) +val scalarref : sv -> sv +(** Given a scalar, this returns a reference to the scalar. Note that + * because references are [SV]s, this returns [sv]. + *) +val arrayref : av -> sv +(** Given an array, this returns a reference to the array. Note that + * because references are [SV]s, this returns [sv]. + *) +val hashref : hv -> sv +(** Given a hash, this returns a reference to the hash. Note that + * because references are [SV]s, this returns [sv]. + *) + val deref : sv -> sv (** The input is a reference to a scalar. This returns the underlying * scalar [SV]. If the input is not a reference to a scalar, throws diff --git a/perl_c.c b/perl_c.c index 9ad8d68..897e174 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.8 2003-10-18 12:36:09 rich Exp $ + * $Id: perl_c.c,v 1.9 2003-10-26 11:22:38 rich Exp $ */ #include @@ -241,6 +241,36 @@ perl4caml_sv_type (value svv) } CAMLprim value +perl4caml_scalarref (value svv) +{ + CAMLparam1 (svv); + CAMLlocal1 (rsvv); + SV *sv = Sv_val (svv); + rsvv = Val_sv (newRV_inc (sv)); + CAMLreturn (rsvv); +} + +CAMLprim value +perl4caml_arrayref (value avv) +{ + CAMLparam1 (avv); + CAMLlocal1 (rsvv); + AV *av = Av_val (avv); + rsvv = Val_sv (newRV_inc ((SV *) av)); + CAMLreturn (rsvv); +} + +CAMLprim value +perl4caml_hashref (value hvv) +{ + CAMLparam1 (hvv); + CAMLlocal1 (rsvv); + HV *hv = Hv_val (hvv); + rsvv = Val_sv (newRV_inc ((SV *) hv)); + CAMLreturn (rsvv); +} + +CAMLprim value perl4caml_deref (value svv) { CAMLparam1 (svv); -- 1.8.3.1