1 /* Interface to Perl from OCaml.
2 * Copyright (C) 2003 Merjis Ltd.
3 * $Id: perl_c.c,v 1.14 2004-02-03 12:38:57 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)));
165 perl4caml_sv_yes (value unit)
168 CAMLreturn (Val_sv (&PL_sv_yes));
172 perl4caml_sv_no (value unit)
175 CAMLreturn (Val_sv (&PL_sv_no));
179 perl4caml_sv_type (value svv)
182 SV *sv = Sv_val (svv);
186 case SVt_IV: CAMLreturn (Val_int (1));
187 case SVt_NV: CAMLreturn (Val_int (2));
188 case SVt_PV: CAMLreturn (Val_int (3));
189 case SVt_RV: CAMLreturn (Val_int (4));
190 case SVt_PVAV: CAMLreturn (Val_int (5));
191 case SVt_PVHV: CAMLreturn (Val_int (6));
192 case SVt_PVCV: CAMLreturn (Val_int (7));
193 case SVt_PVGV: CAMLreturn (Val_int (8));
194 case SVt_PVMG: CAMLreturn (Val_int (9));
195 default: CAMLreturn (Val_int (0));
200 perl4caml_scalarref (value svv)
204 SV *sv = Sv_val (svv);
205 rsvv = Val_sv (newRV_inc (sv));
210 perl4caml_arrayref (value avv)
214 AV *av = Av_val (avv);
215 rsvv = Val_sv (newRV_inc ((SV *) av));
220 perl4caml_hashref (value hvv)
224 HV *hv = Hv_val (hvv);
225 rsvv = Val_sv (newRV_inc ((SV *) hv));
230 perl4caml_deref (value svv)
234 SV *sv = Sv_val (svv);
236 if (SvTYPE (sv) != SVt_RV)
237 invalid_argument ("deref: SV is not a reference");
238 switch (SvTYPE (SvRV (sv))) {
246 invalid_argument ("deref: SV is not a reference to a scalar");
248 rsvv = Val_sv (SvRV (sv));
253 perl4caml_deref_array (value svv)
257 SV *sv = Sv_val (svv);
259 if (SvTYPE (sv) != SVt_RV)
260 invalid_argument ("deref_array: SV is not a reference");
261 switch (SvTYPE (SvRV (sv))) {
265 invalid_argument ("deref_array: SV is not a reference to an array");
267 ravv = Val_av ((AV *) SvRV (sv));
272 perl4caml_deref_hash (value svv)
276 SV *sv = Sv_val (svv);
278 if (SvTYPE (sv) != SVt_RV)
279 invalid_argument ("deref_array: SV is not a reference");
280 switch (SvTYPE (SvRV (sv))) {
284 invalid_argument ("deref_array: SV is not a reference to a hash");
286 rhvv = Val_hv ((HV *) SvRV (sv));
291 perl4caml_av_empty (value unit)
295 CAMLreturn (Val_av (av));
298 /* We don't know in advance how long the list will be, which makes this
302 perl4caml_av_of_sv_list (value svlistv)
304 CAMLparam1 (svlistv);
306 SV *sv, **svlist = 0;
307 int alloc = 0, size = 0;
310 for (; svlistv != Val_int (0); svlistv = Field (svlistv, 1))
312 svv = Field (svlistv, 0);
315 alloc = alloc == 0 ? 1 : alloc * 2;
316 svlist = realloc (svlist, alloc * sizeof (SV *));
321 av = av_make (size, svlist);
323 if (alloc > 0) free (svlist); /* Free memory allocated to SV list. */
325 CAMLreturn (Val_av (av));
328 /* XXX av_map would be faster if we also had sv_list_of_av. */
331 perl4caml_av_push (value avv, value svv)
333 CAMLparam2 (avv, svv);
334 AV *av = Av_val (avv);
335 SV *sv = Sv_val (svv);
337 CAMLreturn (Val_unit);
341 perl4caml_av_pop (value avv)
344 AV *av = Av_val (avv);
345 SV *sv = av_pop (av);
346 CAMLreturn (Val_sv (sv));
350 perl4caml_av_unshift (value avv, value svv)
352 CAMLparam2 (avv, svv);
353 AV *av = Av_val (avv);
354 SV *sv = Sv_val (svv);
357 if (av_store (av, 0, sv) == 0)
359 CAMLreturn (Val_unit);
363 perl4caml_av_shift (value avv)
366 AV *av = Av_val (avv);
367 SV *sv = av_shift (av);
368 CAMLreturn (Val_sv (sv));
372 perl4caml_av_length (value avv)
375 AV *av = Av_val (avv);
376 CAMLreturn (Val_int (av_len (av) + 1));
380 perl4caml_av_set (value avv, value i, value svv)
382 CAMLparam3 (avv, i, svv);
383 AV *av = Av_val (avv);
384 SV *sv = Sv_val (svv);
386 if (av_store (av, Int_val (i), sv) == 0)
388 CAMLreturn (Val_unit);
392 perl4caml_av_get (value avv, value i)
395 AV *av = Av_val (avv);
396 SV **svp = av_fetch (av, Int_val (i), 0);
397 if (svp == 0) invalid_argument ("av_get: index out of bounds");
398 CAMLreturn (Val_sv (*svp));
402 perl4caml_av_clear (value avv)
405 AV *av = Av_val (avv);
407 CAMLreturn (Val_unit);
411 perl4caml_av_undef (value avv)
414 AV *av = Av_val (avv);
416 CAMLreturn (Val_unit);
420 perl4caml_av_extend (value avv, value i)
423 AV *av = Av_val (avv);
424 av_extend (av, Int_val (i));
425 CAMLreturn (Val_unit);
429 perl4caml_hv_empty (value unit)
433 CAMLreturn (Val_hv (hv));
437 perl4caml_hv_set (value hvv, value key, value svv)
439 CAMLparam3 (hvv, key, svv);
440 HV *hv = Hv_val (hvv);
441 SV *sv = Sv_val (svv);
443 if (hv_store (hv, String_val (key), string_length (key), sv, 0) == 0)
445 CAMLreturn (Val_unit);
449 perl4caml_hv_get (value hvv, value key)
451 CAMLparam2 (hvv, key);
452 HV *hv = Hv_val (hvv);
453 SV **svp = hv_fetch (hv, String_val (key), string_length (key), 0);
454 if (svp == 0) raise_not_found ();
455 CAMLreturn (Val_sv (*svp));
459 perl4caml_hv_exists (value hvv, value key)
461 CAMLparam2 (hvv, key);
462 HV *hv = Hv_val (hvv);
463 bool r = hv_exists (hv, String_val (key), string_length (key));
464 CAMLreturn (r ? Val_true : Val_false);
468 perl4caml_hv_delete (value hvv, value key)
470 CAMLparam2 (hvv, key);
471 HV *hv = Hv_val (hvv);
472 hv_delete (hv, String_val (key), string_length (key), G_DISCARD);
473 CAMLreturn (Val_unit);
477 perl4caml_hv_clear (value hvv)
480 HV *hv = Hv_val (hvv);
482 CAMLreturn (Val_unit);
486 perl4caml_hv_undef (value hvv)
489 HV *hv = Hv_val (hvv);
491 CAMLreturn (Val_unit);
495 perl4caml_get_sv (value optcreate, value name)
497 CAMLparam2 (optcreate, name);
501 create = unoption (optcreate, Val_false);
502 sv = get_sv (String_val (name), create == Val_true ? TRUE : FALSE);
503 if (sv == NULL) raise_not_found ();
505 CAMLreturn (Val_sv (sv));
509 perl4caml_get_av (value optcreate, value name)
511 CAMLparam2 (optcreate, name);
515 create = unoption (optcreate, Val_false);
516 av = get_av (String_val (name), create == Val_true ? TRUE : FALSE);
517 if (av == NULL) raise_not_found ();
519 CAMLreturn (Val_av (av));
523 perl4caml_get_hv (value optcreate, value name)
525 CAMLparam2 (optcreate, name);
529 create = unoption (optcreate, Val_false);
530 hv = get_hv (String_val (name), create == Val_true ? TRUE : FALSE);
531 if (hv == NULL) raise_not_found ();
533 CAMLreturn (Val_hv (hv));
537 check_perl_failure ()
539 SV *errsv = get_sv ("@", TRUE);
541 if (SvTRUE (errsv)) /* Equivalent of $@ in Perl. */
545 const char *err = SvPV (errsv, n_a);
547 errv = copy_string (err);
549 raise_with_arg (*caml_named_value ("perl4caml_perl_failure"), errv);
554 perl4caml_call (value optsv, value optfnname, value arglist)
556 CAMLparam3 (optsv, optfnname, arglist);
560 CAMLlocal3 (errv, svv, fnname);
565 /* Push the parameter list. */
568 /* Iteration over the linked list. */
569 for (; arglist != Val_int (0); arglist = Field (arglist, 1))
571 svv = Field (arglist, 0);
573 XPUSHs (sv_2mortal (newSVsv (sv)));
578 if (optsv != Val_int (0))
580 svv = unoption (optsv, Val_false);
582 count = call_sv (sv, G_EVAL|G_SCALAR);
584 else if (optfnname != Val_int (0))
586 fnname = unoption (optfnname, Val_false);
587 count = call_pv (String_val (fnname), G_EVAL|G_SCALAR);
592 "Perl.call: must supply either 'sv' or 'fn' parameters.");
598 assert (count == 1); /* Pretty sure it should never be anything else. */
600 /* Pop return value off the stack. Note that the return value on the
601 * stack is mortal, so we need to take a copy.
608 check_perl_failure ();
615 perl4caml_call_array (value optsv, value optfnname, value arglist)
617 CAMLparam3 (optsv, optfnname, arglist);
621 CAMLlocal5 (errv, svv, fnname, list, cons);
626 /* Push the parameter list. */
629 /* Iteration over the linked list. */
630 for (; arglist != Val_int (0); arglist = Field (arglist, 1))
632 svv = Field (arglist, 0);
634 XPUSHs (sv_2mortal (newSVsv (sv)));
639 if (optsv != Val_int (0))
641 svv = unoption (optsv, Val_false);
643 count = call_sv (sv, G_EVAL|G_ARRAY);
645 else if (optfnname != Val_int (0))
647 fnname = unoption (optfnname, Val_false);
648 count = call_pv (String_val (fnname), G_EVAL|G_ARRAY);
653 "Perl.call_array: must supply either 'sv' or 'fn' parameters.");
659 /* Pop all the return values off the stack into a list. Values on the
660 * stack are mortal, so we must copy them.
663 for (i = 0; i < count; ++i) {
667 Field (cons, 1) = list;
670 Field (cons, 0) = Val_sv (sv);
673 /* Restore the stack. */
678 check_perl_failure ();
684 perl4caml_call_void (value optsv, value optfnname, value arglist)
686 CAMLparam3 (optsv, optfnname, arglist);
690 CAMLlocal3 (errv, svv, fnname);
695 /* Push the parameter list. */
698 /* Iteration over the linked list. */
699 for (; arglist != Val_int (0); arglist = Field (arglist, 1))
701 svv = Field (arglist, 0);
703 XPUSHs (sv_2mortal (newSVsv (sv)));
708 if (optsv != Val_int (0))
710 svv = unoption (optsv, Val_false);
712 count = call_sv (sv, G_EVAL|G_VOID);
714 else if (optfnname != Val_int (0))
716 fnname = unoption (optfnname, Val_false);
717 count = call_pv (String_val (fnname), G_EVAL|G_VOID|G_DISCARD);
722 "Perl.call_void: must supply either 'sv' or 'fn' parameters.");
730 /* Restore the stack. */
735 check_perl_failure ();
737 CAMLreturn (Val_unit);
741 perl4caml_eval (value expr)
746 CAMLlocal2 (errv, svv);
748 sv = eval_pv (String_val (expr), G_SCALAR);
750 check_perl_failure ();
757 perl4caml_call_method (value ref, value name, value arglist)
759 CAMLparam3 (ref, name, arglist);
763 CAMLlocal2 (errv, svv);
768 /* Push the parameter list. */
772 XPUSHs (sv_2mortal (newSVsv (sv)));
774 /* Iteration over the linked list. */
775 for (; arglist != Val_int (0); arglist = Field (arglist, 1))
777 svv = Field (arglist, 0);
779 XPUSHs (sv_2mortal (newSVsv (sv)));
784 count = call_method (String_val (name), G_EVAL|G_SCALAR);
788 assert (count == 1); /* Pretty sure it should never be anything else. */
790 /* Pop return value off the stack. Note that the return value on the
791 * stack is mortal, so we need to take a copy.
798 check_perl_failure ();
805 perl4caml_call_method_array (value ref, value name, value arglist)
807 CAMLparam3 (ref, name, arglist);
811 CAMLlocal4 (errv, svv, list, cons);
816 /* Push the parameter list. */
820 XPUSHs (sv_2mortal (newSVsv (sv)));
822 /* Iteration over the linked list. */
823 for (; arglist != Val_int (0); arglist = Field (arglist, 1))
825 svv = Field (arglist, 0);
827 XPUSHs (sv_2mortal (newSVsv (sv)));
832 count = call_method (String_val (name), G_EVAL|G_ARRAY);
836 /* Pop all return values off the stack. Note that the return values on the
837 * stack are mortal, so we need to take a copy.
840 for (i = 0; i < count; ++i) {
844 Field (cons, 1) = list;
847 Field (cons, 0) = Val_sv (sv);
850 /* Restore the stack. */
855 check_perl_failure ();
861 perl4caml_call_method_void (value ref, value name, value arglist)
863 CAMLparam3 (ref, name, arglist);
867 CAMLlocal2 (errv, svv);
872 /* Push the parameter list. */
876 XPUSHs (sv_2mortal (newSVsv (sv)));
878 /* Iteration over the linked list. */
879 for (; arglist != Val_int (0); arglist = Field (arglist, 1))
881 svv = Field (arglist, 0);
883 XPUSHs (sv_2mortal (newSVsv (sv)));
888 count = call_method (String_val (name), G_EVAL|G_VOID|G_DISCARD);
894 /* Restore the stack. */
899 check_perl_failure ();
901 CAMLreturn (Val_unit);
905 perl4caml_call_class_method (value classname, value name, value arglist)
907 CAMLparam3 (classname, name, arglist);
911 CAMLlocal2 (errv, svv);
916 /* Push the parameter list. */
919 XPUSHs (sv_2mortal (newSVpv (String_val (classname), 0)));
921 /* Iteration over the linked list. */
922 for (; arglist != Val_int (0); arglist = Field (arglist, 1))
924 svv = Field (arglist, 0);
926 XPUSHs (sv_2mortal (newSVsv (sv)));
931 count = call_method (String_val (name), G_EVAL|G_SCALAR);
935 assert (count == 1); /* Pretty sure it should never be anything else. */
937 /* Pop return value off the stack. Note that the return value on the
938 * stack is mortal, so we need to take a copy.
945 check_perl_failure ();
952 perl4caml_call_class_method_array (value classname, value name, value arglist)
954 CAMLparam3 (classname, name, arglist);
958 CAMLlocal4 (errv, svv, list, cons);
963 /* Push the parameter list. */
966 XPUSHs (sv_2mortal (newSVpv (String_val (classname), 0)));
968 /* Iteration over the linked list. */
969 for (; arglist != Val_int (0); arglist = Field (arglist, 1))
971 svv = Field (arglist, 0);
973 XPUSHs (sv_2mortal (newSVsv (sv)));
978 count = call_method (String_val (name), G_EVAL|G_ARRAY);
982 /* Pop all return values off the stack. Note that the return values on the
983 * stack are mortal, so we need to take a copy.
986 for (i = 0; i < count; ++i) {
988 Field (cons, 1) = list;
990 Field (cons, 0) = Val_sv (newSVsv (POPs));
993 /* Restore the stack. */
998 check_perl_failure ();
1004 perl4caml_call_class_method_void (value classname, value name, value arglist)
1006 CAMLparam3 (classname, name, arglist);
1010 CAMLlocal2 (errv, svv);
1015 /* Push the parameter list. */
1018 XPUSHs (sv_2mortal (newSVpv (String_val (classname), 0)));
1020 /* Iteration over the linked list. */
1021 for (; arglist != Val_int (0); arglist = Field (arglist, 1))
1023 svv = Field (arglist, 0);
1025 XPUSHs (sv_2mortal (newSVsv (sv)));
1030 count = call_method (String_val (name), G_EVAL|G_VOID|G_DISCARD);
1034 assert (count == 0);
1036 /* Restore the stack. */
1041 check_perl_failure ();
1043 CAMLreturn (Val_unit);
1047 Val_voidptr (void *ptr)
1051 rv = alloc (1, Abstract_tag);
1052 Field(rv, 0) = (value) ptr;
1056 #if PERL4CAML_REFCOUNTING_EXPERIMENTAL
1059 xv_finalize (value v)
1061 SvREFCNT_dec ((SV *) Xv_val (v));
1064 static struct custom_operations xv_custom_operations = {
1065 "xv_custom_operations",
1067 custom_compare_default,
1068 custom_hash_default,
1069 custom_serialize_default,
1070 custom_deserialize_default
1078 rv = alloc_custom (&xv_custom_operations, sizeof (void *), 0, 1);
1083 #endif /* PERL4CAML_REFCOUNTING_EXPERIMENTAL */
1086 unoption (value option, value deflt)
1088 if (option == Val_int (0)) /* "None" */
1090 else /* "Some 'a" */
1091 return Field (option, 0);