1 /* Interface to Perl from OCaml.
2 * Copyright (C) 2003 Merjis Ltd.
3 * $Id: perl_c.c,v 1.17 2004-11-25 22:16:17 rich Exp $
12 #include <caml/alloc.h>
13 #include <caml/callback.h>
14 #include <caml/custom.h>
15 #include <caml/fail.h>
16 #include <caml/memory.h>
17 #include <caml/mlvalues.h>
19 /* XXX This was required to avoid an error on my machine when loading the Perl
20 * headers. Not clear why this is missing.
22 #define off64_t __off64_t
24 /* XXX This is required by Perl >= 5.8.2. */
31 /* Perl requires the interpreter to be called literally 'my_perl'! */
32 static PerlInterpreter *my_perl;
34 /* Get the concrete value from an optional field. */
35 static value unoption (value option, value deflt);
37 /* Wrap up an arbitrary void pointer in an opaque OCaml object. */
38 static value Val_voidptr (void *ptr);
40 /* Unwrap an arbitrary void pointer from an opaque OCaml object. */
41 #define Voidptr_val(type,rv) ((type *) Field ((rv), 0))
43 #if PERL4CAML_REFCOUNTING_EXPERIMENTAL
45 /* Unwrap a custom block. */
46 #define Xv_val(rv) (*((void **)Data_custom_val(rv)))
48 /* Wrap up an SV, AV or HV in a custom OCaml object which will decrement
49 * the reference count on finalization.
51 static value Val_xv (SV *sv);
55 #define Xv_val(rv) Voidptr_val (SV, (rv))
56 #define Val_xv(sv) Val_voidptr ((sv))
60 /* Hide Perl types in opaque OCaml objects. */
61 #define Val_perl(pl) (Val_voidptr ((pl)))
62 #define Perl_val(plv) (Voidptr_val (PerlInterpreter, (plv)))
63 #define Val_sv(sv) (Val_xv ((sv)))
64 #define Sv_val(svv) ((SV *) Xv_val (svv))
65 #define Val_av(av) (Val_xv ((SV *)(av)))
66 #define Av_val(avv) ((AV *) Xv_val (avv))
67 #define Val_hv(hv) (Val_xv ((SV *)(hv)))
68 #define Hv_val(hvv) ((HV *) Xv_val (hvv))
73 char *file = __FILE__;
74 EXTERN_C void boot_DynaLoader (pTHX_ CV* cv);
76 newXS("DynaLoader::boot_DynaLoader", boot_DynaLoader, file);
80 perl4caml_init (value unit)
84 static char *argv[] = { "", "-w", "-e", "0" };
86 PERL_SYS_INIT3 (NULL, NULL, NULL);
87 my_perl = perl_alloc ();
88 perl_construct (my_perl);
89 PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
90 perl_parse (my_perl, xs_init, argc, argv, NULL);
91 /*perl_run (my_perl);*/
93 CAMLreturn (Val_unit);
97 perl4caml_int_of_sv (value svv)
100 SV *sv = Sv_val (svv);
101 CAMLreturn (Val_int (SvIV (sv)));
105 perl4caml_sv_of_int (value iv)
108 CAMLreturn (Val_sv (newSViv (Int_val (iv))));
112 perl4caml_float_of_sv (value svv)
115 SV *sv = Sv_val (svv);
117 f = copy_double (SvNV (sv));
122 perl4caml_sv_of_float (value fv)
125 CAMLreturn (Val_sv (newSViv (Double_val (fv))));
129 perl4caml_string_of_sv (value svv)
132 SV *sv = Sv_val (svv);
136 str = SvPV (sv, len);
137 /* XXX This won't work if the string contains NUL. */
138 strv = copy_string (str);
143 perl4caml_sv_of_string (value strv)
146 CAMLreturn (Val_sv (newSVpv (String_val (strv), string_length (strv))));
150 perl4caml_sv_is_true (value svv)
153 SV *sv = Sv_val (svv);
154 CAMLreturn (SvTRUE (sv) ? Val_true : Val_false);
158 perl4caml_sv_undef (value unit)
161 /*CAMLreturn (Val_sv (newSV (0)));*/
162 CAMLreturn (Val_sv (&PL_sv_undef));
166 perl4caml_sv_is_undef (value svv)
169 SV *sv = Sv_val (svv);
170 CAMLreturn (!SvPOK (sv) && !SvIOK (sv) && SvTYPE (sv) == SVt_NULL
171 ? Val_true : Val_false);
175 perl4caml_sv_yes (value unit)
178 CAMLreturn (Val_sv (&PL_sv_yes));
182 perl4caml_sv_no (value unit)
185 CAMLreturn (Val_sv (&PL_sv_no));
189 perl4caml_sv_type (value svv)
192 SV *sv = Sv_val (svv);
196 case SVt_IV: CAMLreturn (Val_int (1));
197 case SVt_NV: CAMLreturn (Val_int (2));
198 case SVt_PV: CAMLreturn (Val_int (3));
199 case SVt_RV: CAMLreturn (Val_int (4));
200 case SVt_PVAV: CAMLreturn (Val_int (5));
201 case SVt_PVHV: CAMLreturn (Val_int (6));
202 case SVt_PVCV: CAMLreturn (Val_int (7));
203 case SVt_PVGV: CAMLreturn (Val_int (8));
204 case SVt_PVMG: CAMLreturn (Val_int (9));
205 default: CAMLreturn (Val_int (0));
210 perl4caml_scalarref (value svv)
214 SV *sv = Sv_val (svv);
215 rsvv = Val_sv (newRV_inc (sv));
220 perl4caml_arrayref (value avv)
224 AV *av = Av_val (avv);
225 rsvv = Val_sv (newRV_inc ((SV *) av));
230 perl4caml_hashref (value hvv)
234 HV *hv = Hv_val (hvv);
235 rsvv = Val_sv (newRV_inc ((SV *) hv));
240 perl4caml_deref (value svv)
244 SV *sv = Sv_val (svv);
247 invalid_argument ("deref: SV is not a reference");
248 switch (SvTYPE (SvRV (sv))) {
256 invalid_argument ("deref: SV is not a reference to a scalar");
258 rsvv = Val_sv (SvRV (sv));
263 perl4caml_deref_array (value svv)
267 SV *sv = Sv_val (svv);
270 invalid_argument ("deref_array: SV is not a reference");
271 switch (SvTYPE (SvRV (sv))) {
275 invalid_argument ("deref_array: SV is not a reference to an array");
277 ravv = Val_av ((AV *) SvRV (sv));
282 perl4caml_deref_hash (value svv)
286 SV *sv = Sv_val (svv);
289 invalid_argument ("deref_hash: SV is not a reference");
290 switch (SvTYPE (SvRV (sv))) {
294 invalid_argument ("deref_hash: SV is not a reference to a hash");
296 rhvv = Val_hv ((HV *) SvRV (sv));
301 perl4caml_av_empty (value unit)
305 CAMLreturn (Val_av (av));
308 /* We don't know in advance how long the list will be, which makes this
312 perl4caml_av_of_sv_list (value svlistv)
314 CAMLparam1 (svlistv);
316 SV *sv, **svlist = 0;
317 int alloc = 0, size = 0;
320 for (; svlistv != Val_int (0); svlistv = Field (svlistv, 1))
322 svv = Field (svlistv, 0);
325 alloc = alloc == 0 ? 1 : alloc * 2;
326 svlist = realloc (svlist, alloc * sizeof (SV *));
331 av = av_make (size, svlist);
333 if (alloc > 0) free (svlist); /* Free memory allocated to SV list. */
335 CAMLreturn (Val_av (av));
338 /* XXX av_map would be faster if we also had sv_list_of_av. */
341 perl4caml_av_push (value avv, value svv)
343 CAMLparam2 (avv, svv);
344 AV *av = Av_val (avv);
345 SV *sv = Sv_val (svv);
347 CAMLreturn (Val_unit);
351 perl4caml_av_pop (value avv)
354 AV *av = Av_val (avv);
355 SV *sv = av_pop (av);
356 CAMLreturn (Val_sv (sv));
360 perl4caml_av_unshift (value avv, value svv)
362 CAMLparam2 (avv, svv);
363 AV *av = Av_val (avv);
364 SV *sv = Sv_val (svv);
367 if (av_store (av, 0, sv) == 0)
369 CAMLreturn (Val_unit);
373 perl4caml_av_shift (value avv)
376 AV *av = Av_val (avv);
377 SV *sv = av_shift (av);
378 CAMLreturn (Val_sv (sv));
382 perl4caml_av_length (value avv)
385 AV *av = Av_val (avv);
386 CAMLreturn (Val_int (av_len (av) + 1));
390 perl4caml_av_set (value avv, value i, value svv)
392 CAMLparam3 (avv, i, svv);
393 AV *av = Av_val (avv);
394 SV *sv = Sv_val (svv);
396 if (av_store (av, Int_val (i), sv) == 0)
398 CAMLreturn (Val_unit);
402 perl4caml_av_get (value avv, value i)
405 AV *av = Av_val (avv);
406 SV **svp = av_fetch (av, Int_val (i), 0);
407 if (svp == 0) invalid_argument ("av_get: index out of bounds");
408 CAMLreturn (Val_sv (*svp));
412 perl4caml_av_clear (value avv)
415 AV *av = Av_val (avv);
417 CAMLreturn (Val_unit);
421 perl4caml_av_undef (value avv)
424 AV *av = Av_val (avv);
426 CAMLreturn (Val_unit);
430 perl4caml_av_extend (value avv, value i)
433 AV *av = Av_val (avv);
434 av_extend (av, Int_val (i));
435 CAMLreturn (Val_unit);
439 perl4caml_hv_empty (value unit)
443 CAMLreturn (Val_hv (hv));
447 perl4caml_hv_set (value hvv, value key, value svv)
449 CAMLparam3 (hvv, key, svv);
450 HV *hv = Hv_val (hvv);
451 SV *sv = Sv_val (svv);
453 if (hv_store (hv, String_val (key), string_length (key), sv, 0) == 0)
455 CAMLreturn (Val_unit);
459 perl4caml_hv_get (value hvv, value key)
461 CAMLparam2 (hvv, key);
462 HV *hv = Hv_val (hvv);
463 SV **svp = hv_fetch (hv, String_val (key), string_length (key), 0);
464 if (svp == 0) raise_not_found ();
465 CAMLreturn (Val_sv (*svp));
469 perl4caml_hv_exists (value hvv, value key)
471 CAMLparam2 (hvv, key);
472 HV *hv = Hv_val (hvv);
473 bool r = hv_exists (hv, String_val (key), string_length (key));
474 CAMLreturn (r ? Val_true : Val_false);
478 perl4caml_hv_delete (value hvv, value key)
480 CAMLparam2 (hvv, key);
481 HV *hv = Hv_val (hvv);
482 hv_delete (hv, String_val (key), string_length (key), G_DISCARD);
483 CAMLreturn (Val_unit);
487 perl4caml_hv_clear (value hvv)
490 HV *hv = Hv_val (hvv);
492 CAMLreturn (Val_unit);
496 perl4caml_hv_undef (value hvv)
499 HV *hv = Hv_val (hvv);
501 CAMLreturn (Val_unit);
505 perl4caml_get_sv (value optcreate, value name)
507 CAMLparam2 (optcreate, name);
511 create = unoption (optcreate, Val_false);
512 sv = get_sv (String_val (name), create == Val_true ? TRUE : FALSE);
513 if (sv == NULL) raise_not_found ();
515 CAMLreturn (Val_sv (sv));
519 perl4caml_get_av (value optcreate, value name)
521 CAMLparam2 (optcreate, name);
525 create = unoption (optcreate, Val_false);
526 av = get_av (String_val (name), create == Val_true ? TRUE : FALSE);
527 if (av == NULL) raise_not_found ();
529 CAMLreturn (Val_av (av));
533 perl4caml_get_hv (value optcreate, value name)
535 CAMLparam2 (optcreate, name);
539 create = unoption (optcreate, Val_false);
540 hv = get_hv (String_val (name), create == Val_true ? TRUE : FALSE);
541 if (hv == NULL) raise_not_found ();
543 CAMLreturn (Val_hv (hv));
547 check_perl_failure ()
549 SV *errsv = get_sv ("@", TRUE);
551 if (SvTRUE (errsv)) /* Equivalent of $@ in Perl. */
555 const char *err = SvPV (errsv, n_a);
557 errv = copy_string (err);
559 raise_with_arg (*caml_named_value ("perl4caml_perl_failure"), errv);
564 perl4caml_call (value optsv, value optfnname, value arglist)
566 CAMLparam3 (optsv, optfnname, arglist);
570 CAMLlocal3 (errv, svv, fnname);
575 /* Push the parameter list. */
578 /* Iteration over the linked list. */
579 for (; arglist != Val_int (0); arglist = Field (arglist, 1))
581 svv = Field (arglist, 0);
583 XPUSHs (sv_2mortal (newSVsv (sv)));
588 if (optsv != Val_int (0))
590 svv = unoption (optsv, Val_false);
592 count = call_sv (sv, G_EVAL|G_SCALAR);
594 else if (optfnname != Val_int (0))
596 fnname = unoption (optfnname, Val_false);
597 count = call_pv (String_val (fnname), G_EVAL|G_SCALAR);
602 "Perl.call: must supply either 'sv' or 'fn' parameters.");
608 assert (count == 1); /* Pretty sure it should never be anything else. */
610 /* Pop return value off the stack. Note that the return value on the
611 * stack is mortal, so we need to take a copy.
618 check_perl_failure ();
625 perl4caml_call_array (value optsv, value optfnname, value arglist)
627 CAMLparam3 (optsv, optfnname, arglist);
631 CAMLlocal5 (errv, svv, fnname, list, cons);
636 /* Push the parameter list. */
639 /* Iteration over the linked list. */
640 for (; arglist != Val_int (0); arglist = Field (arglist, 1))
642 svv = Field (arglist, 0);
644 XPUSHs (sv_2mortal (newSVsv (sv)));
649 if (optsv != Val_int (0))
651 svv = unoption (optsv, Val_false);
653 count = call_sv (sv, G_EVAL|G_ARRAY);
655 else if (optfnname != Val_int (0))
657 fnname = unoption (optfnname, Val_false);
658 count = call_pv (String_val (fnname), G_EVAL|G_ARRAY);
663 "Perl.call_array: must supply either 'sv' or 'fn' parameters.");
669 /* Pop all the return values off the stack into a list. Values on the
670 * stack are mortal, so we must copy them.
673 for (i = 0; i < count; ++i) {
677 Field (cons, 1) = list;
680 Field (cons, 0) = Val_sv (sv);
683 /* Restore the stack. */
688 check_perl_failure ();
694 perl4caml_call_void (value optsv, value optfnname, value arglist)
696 CAMLparam3 (optsv, optfnname, arglist);
700 CAMLlocal3 (errv, svv, fnname);
705 /* Push the parameter list. */
708 /* Iteration over the linked list. */
709 for (; arglist != Val_int (0); arglist = Field (arglist, 1))
711 svv = Field (arglist, 0);
713 XPUSHs (sv_2mortal (newSVsv (sv)));
718 if (optsv != Val_int (0))
720 svv = unoption (optsv, Val_false);
722 count = call_sv (sv, G_EVAL|G_VOID);
724 else if (optfnname != Val_int (0))
726 fnname = unoption (optfnname, Val_false);
727 count = call_pv (String_val (fnname), G_EVAL|G_VOID|G_DISCARD);
732 "Perl.call_void: must supply either 'sv' or 'fn' parameters.");
740 /* Restore the stack. */
745 check_perl_failure ();
747 CAMLreturn (Val_unit);
751 perl4caml_eval (value expr)
756 CAMLlocal2 (errv, svv);
758 sv = eval_pv (String_val (expr), G_SCALAR);
760 check_perl_failure ();
767 perl4caml_call_method (value ref, value name, value arglist)
769 CAMLparam3 (ref, name, arglist);
773 CAMLlocal2 (errv, svv);
778 /* Push the parameter list. */
782 XPUSHs (sv_2mortal (newSVsv (sv)));
784 /* Iteration over the linked list. */
785 for (; arglist != Val_int (0); arglist = Field (arglist, 1))
787 svv = Field (arglist, 0);
789 XPUSHs (sv_2mortal (newSVsv (sv)));
794 count = call_method (String_val (name), G_EVAL|G_SCALAR);
798 assert (count == 1); /* Pretty sure it should never be anything else. */
800 /* Pop return value off the stack. Note that the return value on the
801 * stack is mortal, so we need to take a copy.
808 check_perl_failure ();
815 perl4caml_call_method_array (value ref, value name, value arglist)
817 CAMLparam3 (ref, name, arglist);
821 CAMLlocal4 (errv, svv, list, cons);
826 /* Push the parameter list. */
830 XPUSHs (sv_2mortal (newSVsv (sv)));
832 /* Iteration over the linked list. */
833 for (; arglist != Val_int (0); arglist = Field (arglist, 1))
835 svv = Field (arglist, 0);
837 XPUSHs (sv_2mortal (newSVsv (sv)));
842 count = call_method (String_val (name), G_EVAL|G_ARRAY);
846 /* Pop all return values off the stack. Note that the return values on the
847 * stack are mortal, so we need to take a copy.
850 for (i = 0; i < count; ++i) {
854 Field (cons, 1) = list;
857 Field (cons, 0) = Val_sv (sv);
860 /* Restore the stack. */
865 check_perl_failure ();
871 perl4caml_call_method_void (value ref, value name, value arglist)
873 CAMLparam3 (ref, name, arglist);
877 CAMLlocal2 (errv, svv);
882 /* Push the parameter list. */
886 XPUSHs (sv_2mortal (newSVsv (sv)));
888 /* Iteration over the linked list. */
889 for (; arglist != Val_int (0); arglist = Field (arglist, 1))
891 svv = Field (arglist, 0);
893 XPUSHs (sv_2mortal (newSVsv (sv)));
898 count = call_method (String_val (name), G_EVAL|G_VOID|G_DISCARD);
904 /* Restore the stack. */
909 check_perl_failure ();
911 CAMLreturn (Val_unit);
915 perl4caml_call_class_method (value classname, value name, value arglist)
917 CAMLparam3 (classname, name, arglist);
921 CAMLlocal2 (errv, svv);
926 /* Push the parameter list. */
929 XPUSHs (sv_2mortal (newSVpv (String_val (classname), 0)));
931 /* Iteration over the linked list. */
932 for (; arglist != Val_int (0); arglist = Field (arglist, 1))
934 svv = Field (arglist, 0);
936 XPUSHs (sv_2mortal (newSVsv (sv)));
941 count = call_method (String_val (name), G_EVAL|G_SCALAR);
945 assert (count == 1); /* Pretty sure it should never be anything else. */
947 /* Pop return value off the stack. Note that the return value on the
948 * stack is mortal, so we need to take a copy.
955 check_perl_failure ();
962 perl4caml_call_class_method_array (value classname, value name, value arglist)
964 CAMLparam3 (classname, name, arglist);
968 CAMLlocal4 (errv, svv, list, cons);
973 /* Push the parameter list. */
976 XPUSHs (sv_2mortal (newSVpv (String_val (classname), 0)));
978 /* Iteration over the linked list. */
979 for (; arglist != Val_int (0); arglist = Field (arglist, 1))
981 svv = Field (arglist, 0);
983 XPUSHs (sv_2mortal (newSVsv (sv)));
988 count = call_method (String_val (name), G_EVAL|G_ARRAY);
992 /* Pop all return values off the stack. Note that the return values on the
993 * stack are mortal, so we need to take a copy.
996 for (i = 0; i < count; ++i) {
998 Field (cons, 1) = list;
1000 Field (cons, 0) = Val_sv (newSVsv (POPs));
1003 /* Restore the stack. */
1008 check_perl_failure ();
1014 perl4caml_call_class_method_void (value classname, value name, value arglist)
1016 CAMLparam3 (classname, name, arglist);
1020 CAMLlocal2 (errv, svv);
1025 /* Push the parameter list. */
1028 XPUSHs (sv_2mortal (newSVpv (String_val (classname), 0)));
1030 /* Iteration over the linked list. */
1031 for (; arglist != Val_int (0); arglist = Field (arglist, 1))
1033 svv = Field (arglist, 0);
1035 XPUSHs (sv_2mortal (newSVsv (sv)));
1040 count = call_method (String_val (name), G_EVAL|G_VOID|G_DISCARD);
1044 assert (count == 0);
1046 /* Restore the stack. */
1051 check_perl_failure ();
1053 CAMLreturn (Val_unit);
1057 Val_voidptr (void *ptr)
1061 rv = alloc (1, Abstract_tag);
1062 Field(rv, 0) = (value) ptr;
1066 #if PERL4CAML_REFCOUNTING_EXPERIMENTAL
1069 xv_finalize (value v)
1071 SvREFCNT_dec ((SV *) Xv_val (v));
1074 static struct custom_operations xv_custom_operations = {
1075 "xv_custom_operations",
1077 custom_compare_default,
1078 custom_hash_default,
1079 custom_serialize_default,
1080 custom_deserialize_default
1088 rv = alloc_custom (&xv_custom_operations, sizeof (void *), 0, 1);
1093 #endif /* PERL4CAML_REFCOUNTING_EXPERIMENTAL */
1096 unoption (value option, value deflt)
1098 if (option == Val_int (0)) /* "None" */
1100 else /* "Some 'a" */
1101 return Field (option, 0);