1 /* Interface to Perl from OCaml.
2 * Copyright (C) 2003 Merjis Ltd.
3 * $Id: perl_c.c,v 1.20 2005-01-28 23:09:32 rich Exp $
12 #define CAML_NAME_SPACE 1
14 #include <caml/alloc.h>
15 #include <caml/callback.h>
16 #include <caml/custom.h>
17 #include <caml/fail.h>
18 #include <caml/memory.h>
19 #include <caml/mlvalues.h>
24 /* Perl requires the interpreter to be called literally 'my_perl'! */
25 static PerlInterpreter *my_perl;
27 /* Get the concrete value from an optional field. */
28 static value unoption (value option, value deflt);
30 /* Wrap up an arbitrary void pointer in an opaque OCaml object. */
31 static value Val_voidptr (void *ptr);
33 /* Unwrap an arbitrary void pointer from an opaque OCaml object. */
34 #define Voidptr_val(type,rv) ((type *) Field ((rv), 0))
36 #if PERL4CAML_REFCOUNTING_EXPERIMENTAL
38 /* Unwrap a custom block. */
39 #define Xv_val(rv) (*((void **)Data_custom_val(rv)))
41 /* Wrap up an SV, AV or HV in a custom OCaml object which will decrement
42 * the reference count on finalization.
44 static value Val_xv (SV *sv);
48 #define Xv_val(rv) Voidptr_val (SV, (rv))
49 #define Val_xv(sv) Val_voidptr ((sv))
53 /* Hide Perl types in opaque OCaml objects. */
54 #define Val_perl(pl) (Val_voidptr ((pl)))
55 #define Perl_val(plv) (Voidptr_val (PerlInterpreter, (plv)))
56 #define Val_sv(sv) (Val_xv ((sv)))
57 #define Sv_val(svv) ((SV *) Xv_val (svv))
58 #define Val_av(av) (Val_xv ((SV *)(av)))
59 #define Av_val(avv) ((AV *) Xv_val (avv))
60 #define Val_hv(hv) (Val_xv ((SV *)(hv)))
61 #define Hv_val(hvv) ((HV *) Xv_val (hvv))
66 char *file = __FILE__;
67 EXTERN_C void boot_DynaLoader (pTHX_ CV* cv);
69 newXS("DynaLoader::boot_DynaLoader", boot_DynaLoader, file);
73 perl4caml_init (value unit)
77 static char *argv[] = { "", "-w", "-e", "0", NULL };
79 PERL_SYS_INIT (&argc, &argv);
80 my_perl = perl_alloc ();
81 perl_construct (my_perl);
82 PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
83 perl_parse (my_perl, xs_init, argc, argv, (char **) NULL);
84 /*perl_run (my_perl);*/
86 CAMLreturn (Val_unit);
90 perl4caml_int_of_sv (value svv)
93 SV *sv = Sv_val (svv);
94 CAMLreturn (Val_int (SvIV (sv)));
98 perl4caml_sv_of_int (value iv)
101 CAMLreturn (Val_sv (newSViv (Int_val (iv))));
105 perl4caml_float_of_sv (value svv)
108 SV *sv = Sv_val (svv);
110 f = caml_copy_double (SvNV (sv));
115 perl4caml_sv_of_float (value fv)
118 CAMLreturn (Val_sv (newSViv (Double_val (fv))));
122 perl4caml_string_of_sv (value svv)
125 SV *sv = Sv_val (svv);
129 str = SvPV (sv, len);
130 /* XXX This won't work if the string contains NUL. */
131 strv = caml_copy_string (str);
136 perl4caml_sv_of_string (value strv)
139 CAMLreturn (Val_sv (newSVpv (String_val (strv), caml_string_length (strv))));
143 perl4caml_sv_is_true (value svv)
146 SV *sv = Sv_val (svv);
147 CAMLreturn (SvTRUE (sv) ? Val_true : Val_false);
151 perl4caml_sv_undef (value unit)
154 /*CAMLreturn (Val_sv (newSV (0)));*/
155 CAMLreturn (Val_sv (&PL_sv_undef));
159 perl4caml_sv_is_undef (value svv)
162 SV *sv = Sv_val (svv);
163 CAMLreturn (!SvPOK (sv) && !SvIOK (sv) && SvTYPE (sv) == SVt_NULL
164 ? Val_true : Val_false);
168 perl4caml_sv_yes (value unit)
171 CAMLreturn (Val_sv (&PL_sv_yes));
175 perl4caml_sv_no (value unit)
178 CAMLreturn (Val_sv (&PL_sv_no));
182 perl4caml_sv_type (value svv)
185 SV *sv = Sv_val (svv);
189 case SVt_IV: CAMLreturn (Val_int (1));
190 case SVt_NV: CAMLreturn (Val_int (2));
191 case SVt_PV: CAMLreturn (Val_int (3));
192 case SVt_RV: CAMLreturn (Val_int (4));
193 case SVt_PVAV: CAMLreturn (Val_int (5));
194 case SVt_PVHV: CAMLreturn (Val_int (6));
195 case SVt_PVCV: CAMLreturn (Val_int (7));
196 case SVt_PVGV: CAMLreturn (Val_int (8));
197 case SVt_PVMG: CAMLreturn (Val_int (9));
198 default: CAMLreturn (Val_int (0));
203 perl4caml_address_of_sv (value svv)
206 SV *sv = Sv_val (svv);
207 CAMLreturn (caml_copy_nativeint ((long) sv));
211 perl4caml_address_of_av (value avv)
214 AV *av = Av_val (avv);
215 CAMLreturn (caml_copy_nativeint ((long) av));
219 perl4caml_address_of_hv (value hvv)
222 HV *hv = Hv_val (hvv);
223 CAMLreturn (caml_copy_nativeint ((long) hv));
227 perl4caml_scalarref (value svv)
231 SV *sv = Sv_val (svv);
232 rsvv = Val_sv (newRV_inc (sv));
237 perl4caml_arrayref (value avv)
241 AV *av = Av_val (avv);
242 rsvv = Val_sv (newRV_inc ((SV *) av));
247 perl4caml_hashref (value hvv)
251 HV *hv = Hv_val (hvv);
252 rsvv = Val_sv (newRV_inc ((SV *) hv));
257 perl4caml_deref (value svv)
261 SV *sv = Sv_val (svv);
264 caml_invalid_argument ("deref: SV is not a reference");
265 switch (SvTYPE (SvRV (sv))) {
273 caml_invalid_argument ("deref: SV is not a reference to a scalar");
276 /* Increment the reference count because we're creating another
277 * value pointing at the referenced SV.
279 sv = SvREFCNT_inc (sv);
285 perl4caml_deref_array (value svv)
289 SV *sv = Sv_val (svv);
292 caml_invalid_argument ("deref_array: SV is not a reference");
293 switch (SvTYPE (SvRV (sv))) {
297 caml_invalid_argument ("deref_array: SV is not a reference to an array");
300 /* Increment the reference count because we're creating another
301 * value pointing at the referenced AV.
303 sv = SvREFCNT_inc (sv);
304 ravv = Val_av ((AV *) sv);
309 perl4caml_deref_hash (value svv)
313 SV *sv = Sv_val (svv);
316 caml_invalid_argument ("deref_hash: SV is not a reference");
317 switch (SvTYPE (SvRV (sv))) {
321 caml_invalid_argument ("deref_hash: SV is not a reference to a hash");
324 /* Increment the reference count because we're creating another
325 * value pointing at the referenced HV.
327 sv = SvREFCNT_inc (sv);
328 rhvv = Val_hv ((HV *) sv);
333 perl4caml_av_empty (value unit)
337 CAMLreturn (Val_av (av));
340 /* We don't know in advance how long the list will be, which makes this
344 perl4caml_av_of_sv_list (value svlistv)
346 CAMLparam1 (svlistv);
348 SV *sv, **svlist = 0;
349 int alloc = 0, size = 0;
352 for (; svlistv != Val_int (0); svlistv = Field (svlistv, 1))
354 svv = Field (svlistv, 0);
357 alloc = alloc == 0 ? 1 : alloc * 2;
358 svlist = realloc (svlist, alloc * sizeof (SV *));
363 av = av_make (size, svlist);
365 if (alloc > 0) free (svlist); /* Free memory allocated to SV list. */
367 CAMLreturn (Val_av (av));
370 /* XXX av_map would be faster if we also had sv_list_of_av. */
373 perl4caml_av_push (value avv, value svv)
375 CAMLparam2 (avv, svv);
376 AV *av = Av_val (avv);
377 SV *sv = Sv_val (svv);
379 CAMLreturn (Val_unit);
383 perl4caml_av_pop (value avv)
386 AV *av = Av_val (avv);
387 SV *sv = av_pop (av);
388 /* Increment the reference count because we're creating another
389 * value pointing at the referenced AV.
391 sv = SvREFCNT_inc (sv);
392 CAMLreturn (Val_sv (sv));
396 perl4caml_av_unshift (value avv, value svv)
398 CAMLparam2 (avv, svv);
399 AV *av = Av_val (avv);
400 SV *sv = Sv_val (svv);
403 if (av_store (av, 0, sv) == 0)
405 CAMLreturn (Val_unit);
409 perl4caml_av_shift (value avv)
412 AV *av = Av_val (avv);
413 SV *sv = av_shift (av);
414 /* Increment the reference count because we're creating another
415 * value pointing at the referenced AV.
417 sv = SvREFCNT_inc (sv);
418 CAMLreturn (Val_sv (sv));
422 perl4caml_av_length (value avv)
425 AV *av = Av_val (avv);
426 CAMLreturn (Val_int (av_len (av) + 1));
430 perl4caml_av_set (value avv, value i, value svv)
432 CAMLparam3 (avv, i, svv);
433 AV *av = Av_val (avv);
434 SV *sv = Sv_val (svv);
436 if (av_store (av, Int_val (i), sv) == 0)
438 CAMLreturn (Val_unit);
442 perl4caml_av_get (value avv, value i)
445 AV *av = Av_val (avv);
446 SV **svp = av_fetch (av, Int_val (i), 0);
447 if (svp == 0) caml_invalid_argument ("av_get: index out of bounds");
448 /* Increment the reference count because we're creating another
449 * value pointing at the referenced AV.
451 *svp = SvREFCNT_inc (*svp);
452 CAMLreturn (Val_sv (*svp));
456 perl4caml_av_clear (value avv)
459 AV *av = Av_val (avv);
461 CAMLreturn (Val_unit);
465 perl4caml_av_undef (value avv)
468 AV *av = Av_val (avv);
470 CAMLreturn (Val_unit);
474 perl4caml_av_extend (value avv, value i)
477 AV *av = Av_val (avv);
478 av_extend (av, Int_val (i));
479 CAMLreturn (Val_unit);
483 perl4caml_hv_empty (value unit)
487 CAMLreturn (Val_hv (hv));
491 perl4caml_hv_set (value hvv, value key, value svv)
493 CAMLparam3 (hvv, key, svv);
494 HV *hv = Hv_val (hvv);
495 SV *sv = Sv_val (svv);
497 if (hv_store (hv, String_val (key), caml_string_length (key), sv, 0) == 0)
499 CAMLreturn (Val_unit);
503 perl4caml_hv_get (value hvv, value key)
505 CAMLparam2 (hvv, key);
506 HV *hv = Hv_val (hvv);
507 SV **svp = hv_fetch (hv, String_val (key), caml_string_length (key), 0);
508 if (svp == 0) caml_raise_not_found ();
509 /* Increment the reference count because we're creating another
510 * value pointing at the referenced SV.
513 CAMLreturn (Val_sv (*svp));
517 perl4caml_hv_exists (value hvv, value key)
519 CAMLparam2 (hvv, key);
520 HV *hv = Hv_val (hvv);
521 bool r = hv_exists (hv, String_val (key), caml_string_length (key));
522 CAMLreturn (r ? Val_true : Val_false);
526 perl4caml_hv_delete (value hvv, value key)
528 CAMLparam2 (hvv, key);
529 HV *hv = Hv_val (hvv);
530 hv_delete (hv, String_val (key), caml_string_length (key), G_DISCARD);
531 CAMLreturn (Val_unit);
535 perl4caml_hv_clear (value hvv)
538 HV *hv = Hv_val (hvv);
540 CAMLreturn (Val_unit);
544 perl4caml_hv_undef (value hvv)
547 HV *hv = Hv_val (hvv);
549 CAMLreturn (Val_unit);
553 perl4caml_get_sv (value optcreate, value name)
555 CAMLparam2 (optcreate, name);
559 create = unoption (optcreate, Val_false);
560 sv = get_sv (String_val (name), create == Val_true ? TRUE : FALSE);
561 if (sv == NULL) caml_raise_not_found ();
563 /* Increment the reference count because we're creating another
564 * value pointing at the referenced SV.
568 CAMLreturn (Val_sv (sv));
572 perl4caml_get_av (value optcreate, value name)
574 CAMLparam2 (optcreate, name);
578 create = unoption (optcreate, Val_false);
579 av = get_av (String_val (name), create == Val_true ? TRUE : FALSE);
580 if (av == NULL) caml_raise_not_found ();
582 /* Increment the reference count because we're creating another
583 * value pointing at the AV.
587 CAMLreturn (Val_av (av));
591 perl4caml_get_hv (value optcreate, value name)
593 CAMLparam2 (optcreate, name);
597 create = unoption (optcreate, Val_false);
598 hv = get_hv (String_val (name), create == Val_true ? TRUE : FALSE);
599 if (hv == NULL) caml_raise_not_found ();
601 /* Increment the reference count because we're creating another
602 * value pointing at the HV.
606 CAMLreturn (Val_hv (hv));
610 check_perl_failure ()
612 SV *errsv = get_sv ("@", TRUE);
614 if (SvTRUE (errsv)) /* Equivalent of $@ in Perl. */
618 const char *err = SvPV (errsv, n_a);
620 errv = caml_copy_string (err);
622 caml_raise_with_arg (*caml_named_value ("perl4caml_perl_failure"), errv);
627 perl4caml_call (value optsv, value optfnname, value arglist)
629 CAMLparam3 (optsv, optfnname, arglist);
633 CAMLlocal3 (errv, svv, fnname);
638 /* Push the parameter list. */
641 /* Iteration over the linked list. */
642 for (; arglist != Val_int (0); arglist = Field (arglist, 1))
644 svv = Field (arglist, 0);
646 XPUSHs (sv_2mortal (newSVsv (sv)));
651 if (optsv != Val_int (0))
653 svv = unoption (optsv, Val_false);
655 count = call_sv (sv, G_EVAL|G_SCALAR);
657 else if (optfnname != Val_int (0))
659 fnname = unoption (optfnname, Val_false);
660 count = call_pv (String_val (fnname), G_EVAL|G_SCALAR);
665 "Perl.call: must supply either 'sv' or 'fn' parameters.");
671 assert (count == 1); /* Pretty sure it should never be anything else. */
673 /* Pop return value off the stack. Note that the return value on the
674 * stack is mortal, so we need to take a copy.
681 check_perl_failure ();
688 perl4caml_call_array (value optsv, value optfnname, value arglist)
690 CAMLparam3 (optsv, optfnname, arglist);
694 CAMLlocal5 (errv, svv, fnname, list, cons);
699 /* Push the parameter list. */
702 /* Iteration over the linked list. */
703 for (; arglist != Val_int (0); arglist = Field (arglist, 1))
705 svv = Field (arglist, 0);
707 XPUSHs (sv_2mortal (newSVsv (sv)));
712 if (optsv != Val_int (0))
714 svv = unoption (optsv, Val_false);
716 count = call_sv (sv, G_EVAL|G_ARRAY);
718 else if (optfnname != Val_int (0))
720 fnname = unoption (optfnname, Val_false);
721 count = call_pv (String_val (fnname), G_EVAL|G_ARRAY);
726 "Perl.call_array: must supply either 'sv' or 'fn' parameters.");
732 /* Pop all the return values off the stack into a list. Values on the
733 * stack are mortal, so we must copy them.
736 for (i = 0; i < count; ++i) {
739 cons = caml_alloc (2, 0);
740 Field (cons, 1) = list;
743 Field (cons, 0) = Val_sv (sv);
746 /* Restore the stack. */
751 check_perl_failure ();
757 perl4caml_call_void (value optsv, value optfnname, value arglist)
759 CAMLparam3 (optsv, optfnname, arglist);
763 CAMLlocal3 (errv, svv, fnname);
768 /* Push the parameter list. */
771 /* Iteration over the linked list. */
772 for (; arglist != Val_int (0); arglist = Field (arglist, 1))
774 svv = Field (arglist, 0);
776 XPUSHs (sv_2mortal (newSVsv (sv)));
781 if (optsv != Val_int (0))
783 svv = unoption (optsv, Val_false);
785 count = call_sv (sv, G_EVAL|G_VOID);
787 else if (optfnname != Val_int (0))
789 fnname = unoption (optfnname, Val_false);
790 count = call_pv (String_val (fnname), G_EVAL|G_VOID|G_DISCARD);
795 "Perl.call_void: must supply either 'sv' or 'fn' parameters.");
803 /* Restore the stack. */
808 check_perl_failure ();
810 CAMLreturn (Val_unit);
814 perl4caml_eval (value expr)
819 CAMLlocal2 (errv, svv);
821 sv = eval_pv (String_val (expr), G_SCALAR);
823 check_perl_failure ();
830 perl4caml_call_method (value ref, value name, value arglist)
832 CAMLparam3 (ref, name, arglist);
836 CAMLlocal2 (errv, svv);
841 /* Push the parameter list. */
845 XPUSHs (sv_2mortal (newSVsv (sv)));
847 /* Iteration over the linked list. */
848 for (; arglist != Val_int (0); arglist = Field (arglist, 1))
850 svv = Field (arglist, 0);
852 XPUSHs (sv_2mortal (newSVsv (sv)));
857 count = call_method (String_val (name), G_EVAL|G_SCALAR);
861 assert (count == 1); /* Pretty sure it should never be anything else. */
863 /* Pop return value off the stack. Note that the return value on the
864 * stack is mortal, so we need to take a copy.
871 check_perl_failure ();
878 perl4caml_call_method_array (value ref, value name, value arglist)
880 CAMLparam3 (ref, name, arglist);
884 CAMLlocal4 (errv, svv, list, cons);
889 /* Push the parameter list. */
893 XPUSHs (sv_2mortal (newSVsv (sv)));
895 /* Iteration over the linked list. */
896 for (; arglist != Val_int (0); arglist = Field (arglist, 1))
898 svv = Field (arglist, 0);
900 XPUSHs (sv_2mortal (newSVsv (sv)));
905 count = call_method (String_val (name), G_EVAL|G_ARRAY);
909 /* Pop all return values off the stack. Note that the return values on the
910 * stack are mortal, so we need to take a copy.
913 for (i = 0; i < count; ++i) {
916 cons = caml_alloc (2, 0);
917 Field (cons, 1) = list;
920 Field (cons, 0) = Val_sv (sv);
923 /* Restore the stack. */
928 check_perl_failure ();
934 perl4caml_call_method_void (value ref, value name, value arglist)
936 CAMLparam3 (ref, name, arglist);
940 CAMLlocal2 (errv, svv);
945 /* Push the parameter list. */
949 XPUSHs (sv_2mortal (newSVsv (sv)));
951 /* Iteration over the linked list. */
952 for (; arglist != Val_int (0); arglist = Field (arglist, 1))
954 svv = Field (arglist, 0);
956 XPUSHs (sv_2mortal (newSVsv (sv)));
961 count = call_method (String_val (name), G_EVAL|G_VOID|G_DISCARD);
967 /* Restore the stack. */
972 check_perl_failure ();
974 CAMLreturn (Val_unit);
978 perl4caml_call_class_method (value classname, value name, value arglist)
980 CAMLparam3 (classname, name, arglist);
984 CAMLlocal2 (errv, svv);
989 /* Push the parameter list. */
992 XPUSHs (sv_2mortal (newSVpv (String_val (classname), 0)));
994 /* Iteration over the linked list. */
995 for (; arglist != Val_int (0); arglist = Field (arglist, 1))
997 svv = Field (arglist, 0);
999 XPUSHs (sv_2mortal (newSVsv (sv)));
1004 count = call_method (String_val (name), G_EVAL|G_SCALAR);
1008 assert (count == 1); /* Pretty sure it should never be anything else. */
1010 /* Pop return value off the stack. Note that the return value on the
1011 * stack is mortal, so we need to take a copy.
1013 sv = newSVsv (POPs);
1018 check_perl_failure ();
1025 perl4caml_call_class_method_array (value classname, value name, value arglist)
1027 CAMLparam3 (classname, name, arglist);
1031 CAMLlocal4 (errv, svv, list, cons);
1036 /* Push the parameter list. */
1039 XPUSHs (sv_2mortal (newSVpv (String_val (classname), 0)));
1041 /* Iteration over the linked list. */
1042 for (; arglist != Val_int (0); arglist = Field (arglist, 1))
1044 svv = Field (arglist, 0);
1046 XPUSHs (sv_2mortal (newSVsv (sv)));
1051 count = call_method (String_val (name), G_EVAL|G_ARRAY);
1055 /* Pop all return values off the stack. Note that the return values on the
1056 * stack are mortal, so we need to take a copy.
1059 for (i = 0; i < count; ++i) {
1060 cons = caml_alloc (2, 0);
1061 Field (cons, 1) = list;
1063 Field (cons, 0) = Val_sv (newSVsv (POPs));
1066 /* Restore the stack. */
1071 check_perl_failure ();
1077 perl4caml_call_class_method_void (value classname, value name, value arglist)
1079 CAMLparam3 (classname, name, arglist);
1083 CAMLlocal2 (errv, svv);
1088 /* Push the parameter list. */
1091 XPUSHs (sv_2mortal (newSVpv (String_val (classname), 0)));
1093 /* Iteration over the linked list. */
1094 for (; arglist != Val_int (0); arglist = Field (arglist, 1))
1096 svv = Field (arglist, 0);
1098 XPUSHs (sv_2mortal (newSVsv (sv)));
1103 count = call_method (String_val (name), G_EVAL|G_VOID|G_DISCARD);
1107 assert (count == 0);
1109 /* Restore the stack. */
1114 check_perl_failure ();
1116 CAMLreturn (Val_unit);
1120 Val_voidptr (void *ptr)
1124 rv = caml_alloc (1, Abstract_tag);
1125 Field(rv, 0) = (value) ptr;
1129 #if PERL4CAML_REFCOUNTING_EXPERIMENTAL
1132 xv_finalize (value v)
1134 /*fprintf (stderr, "about to decrement %p\n", Xv_val (v));*/
1135 SvREFCNT_dec ((SV *) Xv_val (v));
1138 static struct custom_operations xv_custom_operations = {
1139 "xv_custom_operations",
1141 custom_compare_default,
1142 custom_hash_default,
1143 custom_serialize_default,
1144 custom_deserialize_default
1152 rv = caml_alloc_custom (&xv_custom_operations, sizeof (void *), 0, 1);
1157 #endif /* PERL4CAML_REFCOUNTING_EXPERIMENTAL */
1160 unoption (value option, value deflt)
1162 if (option == Val_int (0)) /* "None" */
1164 else /* "Some 'a" */
1165 return Field (option, 0);