1 /* Interface to Perl from OCaml.
2 * Copyright (C) 2003 Merjis Ltd.
3 * $Id: perl_c.c,v 1.23 2005-04-14 13:05:12 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))
62 #define Val_he(he) (Val_voidptr ((he)))
63 #define He_val(hev) (Voidptr_val (HE, (hev)))
68 char *file = __FILE__;
69 EXTERN_C void boot_DynaLoader (pTHX_ CV* cv);
71 newXS("DynaLoader::boot_DynaLoader", boot_DynaLoader, file);
75 perl4caml_init (value unit)
79 static char *argv[] = { "", "-w", "-e", "0", NULL };
81 PERL_SYS_INIT (&argc, &argv);
82 my_perl = perl_alloc ();
83 perl_construct (my_perl);
84 PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
85 perl_parse (my_perl, xs_init, argc, argv, (char **) NULL);
86 /*perl_run (my_perl);*/
88 CAMLreturn (Val_unit);
92 perl4caml_int_of_sv (value svv)
95 SV *sv = Sv_val (svv);
96 CAMLreturn (Val_int (SvIV (sv)));
100 perl4caml_sv_of_int (value iv)
103 CAMLreturn (Val_sv (newSViv (Int_val (iv))));
107 perl4caml_float_of_sv (value svv)
110 SV *sv = Sv_val (svv);
112 f = caml_copy_double (SvNV (sv));
117 perl4caml_sv_of_float (value fv)
120 CAMLreturn (Val_sv (newSViv (Double_val (fv))));
124 perl4caml_string_of_sv (value svv)
127 SV *sv = Sv_val (svv);
131 str = SvPV (sv, len);
132 strv = caml_alloc_string (len);
133 memcpy (String_val (strv), str, len);
138 perl4caml_sv_of_string (value strv)
141 CAMLreturn (Val_sv (newSVpv (String_val (strv), caml_string_length (strv))));
145 perl4caml_sv_is_true (value svv)
148 SV *sv = Sv_val (svv);
149 CAMLreturn (SvTRUE (sv) ? Val_true : Val_false);
153 perl4caml_sv_undef (value unit)
156 /*CAMLreturn (Val_sv (newSV (0)));*/
157 CAMLreturn (Val_sv (&PL_sv_undef));
161 perl4caml_sv_is_undef (value svv)
164 SV *sv = Sv_val (svv);
165 CAMLreturn (!SvPOK (sv) && !SvIOK (sv) && SvTYPE (sv) == SVt_NULL
166 ? Val_true : Val_false);
170 perl4caml_sv_yes (value unit)
173 CAMLreturn (Val_sv (&PL_sv_yes));
177 perl4caml_sv_no (value unit)
180 CAMLreturn (Val_sv (&PL_sv_no));
188 case SVt_IV: return 1;
189 case SVt_NV: return 2;
190 case SVt_PV: return 3;
191 case SVt_RV: return 4;
192 case SVt_PVAV: return 5;
193 case SVt_PVHV: return 6;
194 case SVt_PVCV: return 7;
195 case SVt_PVGV: return 8;
196 case SVt_PVMG: return 9;
202 perl4caml_sv_type (value svv)
205 SV *sv = Sv_val (svv);
207 CAMLreturn (Val_int (sv_type (sv)));
211 perl4caml_address_of_sv (value svv)
214 SV *sv = Sv_val (svv);
215 CAMLreturn (caml_copy_nativeint ((long) sv));
219 perl4caml_address_of_av (value avv)
222 AV *av = Av_val (avv);
223 CAMLreturn (caml_copy_nativeint ((long) av));
227 perl4caml_address_of_hv (value hvv)
230 HV *hv = Hv_val (hvv);
231 CAMLreturn (caml_copy_nativeint ((long) hv));
235 perl4caml_scalarref (value svv)
239 SV *sv = Sv_val (svv);
240 rsvv = Val_sv (newRV_inc (sv));
245 perl4caml_arrayref (value avv)
249 AV *av = Av_val (avv);
250 rsvv = Val_sv (newRV_inc ((SV *) av));
255 perl4caml_hashref (value hvv)
259 HV *hv = Hv_val (hvv);
260 rsvv = Val_sv (newRV_inc ((SV *) hv));
265 perl4caml_reftype (value svv)
268 SV *sv = Sv_val (svv);
271 caml_invalid_argument ("reftype: SV is not a reference");
273 CAMLreturn (Val_int (sv_type (SvRV (sv))));
277 perl4caml_deref (value svv)
281 SV *sv = Sv_val (svv);
284 caml_invalid_argument ("deref: SV is not a reference");
285 switch (SvTYPE (SvRV (sv))) {
293 caml_invalid_argument ("deref: SV is not a reference to a scalar");
296 /* Increment the reference count because we're creating another
297 * value pointing at the referenced SV.
299 sv = SvREFCNT_inc (sv);
305 perl4caml_deref_array (value svv)
309 SV *sv = Sv_val (svv);
312 caml_invalid_argument ("deref_array: SV is not a reference");
313 switch (SvTYPE (SvRV (sv))) {
317 caml_invalid_argument ("deref_array: SV is not a reference to an array");
320 /* Increment the reference count because we're creating another
321 * value pointing at the referenced AV.
323 sv = SvREFCNT_inc (sv);
324 ravv = Val_av ((AV *) sv);
329 perl4caml_deref_hash (value svv)
333 SV *sv = Sv_val (svv);
336 caml_invalid_argument ("deref_hash: SV is not a reference");
337 switch (SvTYPE (SvRV (sv))) {
341 caml_invalid_argument ("deref_hash: SV is not a reference to a hash");
344 /* Increment the reference count because we're creating another
345 * value pointing at the referenced HV.
347 sv = SvREFCNT_inc (sv);
348 rhvv = Val_hv ((HV *) sv);
353 perl4caml_av_empty (value unit)
357 CAMLreturn (Val_av (av));
360 /* We don't know in advance how long the list will be, which makes this
364 perl4caml_av_of_sv_list (value svlistv)
366 CAMLparam1 (svlistv);
368 SV *sv, **svlist = 0;
369 int alloc = 0, size = 0;
372 for (; svlistv != Val_int (0); svlistv = Field (svlistv, 1))
374 svv = Field (svlistv, 0);
377 alloc = alloc == 0 ? 1 : alloc * 2;
378 svlist = realloc (svlist, alloc * sizeof (SV *));
383 av = av_make (size, svlist);
385 if (alloc > 0) free (svlist); /* Free memory allocated to SV list. */
387 CAMLreturn (Val_av (av));
390 /* XXX av_map would be faster if we also had sv_list_of_av. */
393 perl4caml_av_push (value avv, value svv)
395 CAMLparam2 (avv, svv);
396 AV *av = Av_val (avv);
397 SV *sv = Sv_val (svv);
399 CAMLreturn (Val_unit);
403 perl4caml_av_pop (value avv)
406 AV *av = Av_val (avv);
407 SV *sv = av_pop (av);
408 /* Increment the reference count because we're creating another
409 * value pointing at the referenced AV.
411 sv = SvREFCNT_inc (sv);
412 CAMLreturn (Val_sv (sv));
416 perl4caml_av_unshift (value avv, value svv)
418 CAMLparam2 (avv, svv);
419 AV *av = Av_val (avv);
420 SV *sv = Sv_val (svv);
423 if (av_store (av, 0, sv) == 0)
425 CAMLreturn (Val_unit);
429 perl4caml_av_shift (value avv)
432 AV *av = Av_val (avv);
433 SV *sv = av_shift (av);
434 /* Increment the reference count because we're creating another
435 * value pointing at the referenced AV.
437 sv = SvREFCNT_inc (sv);
438 CAMLreturn (Val_sv (sv));
442 perl4caml_av_length (value avv)
445 AV *av = Av_val (avv);
446 CAMLreturn (Val_int (av_len (av) + 1));
450 perl4caml_av_set (value avv, value i, value svv)
452 CAMLparam3 (avv, i, svv);
453 AV *av = Av_val (avv);
454 SV *sv = Sv_val (svv);
456 if (av_store (av, Int_val (i), sv) == 0)
458 CAMLreturn (Val_unit);
462 perl4caml_av_get (value avv, value i)
465 AV *av = Av_val (avv);
466 SV **svp = av_fetch (av, Int_val (i), 0);
467 if (svp == 0) caml_invalid_argument ("av_get: index out of bounds");
468 /* Increment the reference count because we're creating another
469 * value pointing at the referenced AV.
471 *svp = SvREFCNT_inc (*svp);
472 CAMLreturn (Val_sv (*svp));
476 perl4caml_av_clear (value avv)
479 AV *av = Av_val (avv);
481 CAMLreturn (Val_unit);
485 perl4caml_av_undef (value avv)
488 AV *av = Av_val (avv);
490 CAMLreturn (Val_unit);
494 perl4caml_av_extend (value avv, value i)
497 AV *av = Av_val (avv);
498 av_extend (av, Int_val (i));
499 CAMLreturn (Val_unit);
503 perl4caml_hv_empty (value unit)
507 CAMLreturn (Val_hv (hv));
511 perl4caml_hv_set (value hvv, value key, value svv)
513 CAMLparam3 (hvv, key, svv);
514 HV *hv = Hv_val (hvv);
515 SV *sv = Sv_val (svv);
517 if (hv_store (hv, String_val (key), caml_string_length (key), sv, 0) == 0)
519 CAMLreturn (Val_unit);
523 perl4caml_hv_get (value hvv, value key)
525 CAMLparam2 (hvv, key);
526 HV *hv = Hv_val (hvv);
527 SV **svp = hv_fetch (hv, String_val (key), caml_string_length (key), 0);
528 if (svp == 0) caml_raise_not_found ();
529 /* Increment the reference count because we're creating another
530 * value pointing at the referenced SV.
533 CAMLreturn (Val_sv (*svp));
537 perl4caml_hv_exists (value hvv, value key)
539 CAMLparam2 (hvv, key);
540 HV *hv = Hv_val (hvv);
541 bool r = hv_exists (hv, String_val (key), caml_string_length (key));
542 CAMLreturn (r ? Val_true : Val_false);
546 perl4caml_hv_delete (value hvv, value key)
548 CAMLparam2 (hvv, key);
549 HV *hv = Hv_val (hvv);
550 hv_delete (hv, String_val (key), caml_string_length (key), G_DISCARD);
551 CAMLreturn (Val_unit);
555 perl4caml_hv_clear (value hvv)
558 HV *hv = Hv_val (hvv);
560 CAMLreturn (Val_unit);
564 perl4caml_hv_undef (value hvv)
567 HV *hv = Hv_val (hvv);
569 CAMLreturn (Val_unit);
573 perl4caml_hv_iterinit (value hvv)
576 HV *hv = Hv_val (hvv);
577 int i = hv_iterinit (hv);
578 CAMLreturn (caml_copy_int32 (i));
582 perl4caml_hv_iternext (value hvv)
586 HV *hv = Hv_val (hvv);
587 HE *he = hv_iternext (hv);
588 if (he == NULL) caml_raise_not_found ();
594 perl4caml_hv_iterkey (value hev)
598 HE *he = He_val (hev);
600 char *str = hv_iterkey (he, &len);
601 strv = caml_alloc_string (len);
602 memcpy (String_val (strv), str, len);
607 perl4caml_hv_iterval (value hvv, value hev)
609 CAMLparam2 (hvv, hev);
611 HV *hv = Hv_val (hvv);
612 HE *he = He_val (hev);
613 SV *sv = hv_iterval (hv, he);
620 perl4caml_hv_iternextsv (value hvv)
623 CAMLlocal3 (strv, svv, rv);
624 HV *hv = Hv_val (hvv);
626 SV *sv = hv_iternextsv (hv, &str, &len);
627 if (sv == NULL) caml_raise_not_found ();
630 strv = caml_alloc_string (len);
631 memcpy (String_val (strv), str, len);
632 /* Construct a tuple (strv, svv). */
633 rv = caml_alloc_tuple (2);
634 Field (rv, 0) = strv;
640 perl4caml_get_sv (value optcreate, value name)
642 CAMLparam2 (optcreate, name);
646 create = unoption (optcreate, Val_false);
647 sv = get_sv (String_val (name), create == Val_true ? TRUE : FALSE);
648 if (sv == NULL) caml_raise_not_found ();
650 /* Increment the reference count because we're creating another
651 * value pointing at the referenced SV.
655 CAMLreturn (Val_sv (sv));
659 perl4caml_get_av (value optcreate, value name)
661 CAMLparam2 (optcreate, name);
665 create = unoption (optcreate, Val_false);
666 av = get_av (String_val (name), create == Val_true ? TRUE : FALSE);
667 if (av == NULL) caml_raise_not_found ();
669 /* Increment the reference count because we're creating another
670 * value pointing at the AV.
674 CAMLreturn (Val_av (av));
678 perl4caml_get_hv (value optcreate, value name)
680 CAMLparam2 (optcreate, name);
684 create = unoption (optcreate, Val_false);
685 hv = get_hv (String_val (name), create == Val_true ? TRUE : FALSE);
686 if (hv == NULL) caml_raise_not_found ();
688 /* Increment the reference count because we're creating another
689 * value pointing at the HV.
693 CAMLreturn (Val_hv (hv));
697 check_perl_failure ()
699 SV *errsv = get_sv ("@", TRUE);
701 if (SvTRUE (errsv)) /* Equivalent of $@ in Perl. */
705 const char *err = SvPV (errsv, n_a);
707 errv = caml_copy_string (err);
709 caml_raise_with_arg (*caml_named_value ("perl4caml_perl_failure"), errv);
714 perl4caml_call (value optsv, value optfnname, value arglist)
716 CAMLparam3 (optsv, optfnname, arglist);
720 CAMLlocal3 (errv, svv, fnname);
725 /* Push the parameter list. */
728 /* Iteration over the linked list. */
729 for (; arglist != Val_int (0); arglist = Field (arglist, 1))
731 svv = Field (arglist, 0);
733 XPUSHs (sv_2mortal (newSVsv (sv)));
738 if (optsv != Val_int (0))
740 svv = unoption (optsv, Val_false);
742 count = call_sv (sv, G_EVAL|G_SCALAR);
744 else if (optfnname != Val_int (0))
746 fnname = unoption (optfnname, Val_false);
747 count = call_pv (String_val (fnname), G_EVAL|G_SCALAR);
752 "Perl.call: must supply either 'sv' or 'fn' parameters.");
758 assert (count == 1); /* Pretty sure it should never be anything else. */
760 /* Pop return value off the stack. Note that the return value on the
761 * stack is mortal, so we need to take a copy.
768 check_perl_failure ();
775 perl4caml_call_array (value optsv, value optfnname, value arglist)
777 CAMLparam3 (optsv, optfnname, arglist);
781 CAMLlocal5 (errv, svv, fnname, list, cons);
786 /* Push the parameter list. */
789 /* Iteration over the linked list. */
790 for (; arglist != Val_int (0); arglist = Field (arglist, 1))
792 svv = Field (arglist, 0);
794 XPUSHs (sv_2mortal (newSVsv (sv)));
799 if (optsv != Val_int (0))
801 svv = unoption (optsv, Val_false);
803 count = call_sv (sv, G_EVAL|G_ARRAY);
805 else if (optfnname != Val_int (0))
807 fnname = unoption (optfnname, Val_false);
808 count = call_pv (String_val (fnname), G_EVAL|G_ARRAY);
813 "Perl.call_array: must supply either 'sv' or 'fn' parameters.");
819 /* Pop all the return values off the stack into a list. Values on the
820 * stack are mortal, so we must copy them.
823 for (i = 0; i < count; ++i) {
826 cons = caml_alloc (2, 0);
827 Field (cons, 1) = list;
830 Field (cons, 0) = Val_sv (sv);
833 /* Restore the stack. */
838 check_perl_failure ();
844 perl4caml_call_void (value optsv, value optfnname, value arglist)
846 CAMLparam3 (optsv, optfnname, arglist);
850 CAMLlocal3 (errv, svv, fnname);
855 /* Push the parameter list. */
858 /* Iteration over the linked list. */
859 for (; arglist != Val_int (0); arglist = Field (arglist, 1))
861 svv = Field (arglist, 0);
863 XPUSHs (sv_2mortal (newSVsv (sv)));
868 if (optsv != Val_int (0))
870 svv = unoption (optsv, Val_false);
872 count = call_sv (sv, G_EVAL|G_VOID);
874 else if (optfnname != Val_int (0))
876 fnname = unoption (optfnname, Val_false);
877 count = call_pv (String_val (fnname), G_EVAL|G_VOID|G_DISCARD);
882 "Perl.call_void: must supply either 'sv' or 'fn' parameters.");
890 /* Restore the stack. */
895 check_perl_failure ();
897 CAMLreturn (Val_unit);
901 perl4caml_eval (value expr)
906 CAMLlocal2 (errv, svv);
908 sv = eval_pv (String_val (expr), G_SCALAR);
910 check_perl_failure ();
917 perl4caml_call_method (value ref, value name, value arglist)
919 CAMLparam3 (ref, name, arglist);
923 CAMLlocal2 (errv, svv);
928 /* Push the parameter list. */
932 XPUSHs (sv_2mortal (newSVsv (sv)));
934 /* Iteration over the linked list. */
935 for (; arglist != Val_int (0); arglist = Field (arglist, 1))
937 svv = Field (arglist, 0);
939 XPUSHs (sv_2mortal (newSVsv (sv)));
944 count = call_method (String_val (name), G_EVAL|G_SCALAR);
948 assert (count == 1); /* Pretty sure it should never be anything else. */
950 /* Pop return value off the stack. Note that the return value on the
951 * stack is mortal, so we need to take a copy.
958 check_perl_failure ();
965 perl4caml_call_method_array (value ref, value name, value arglist)
967 CAMLparam3 (ref, name, arglist);
971 CAMLlocal4 (errv, svv, list, cons);
976 /* Push the parameter list. */
980 XPUSHs (sv_2mortal (newSVsv (sv)));
982 /* Iteration over the linked list. */
983 for (; arglist != Val_int (0); arglist = Field (arglist, 1))
985 svv = Field (arglist, 0);
987 XPUSHs (sv_2mortal (newSVsv (sv)));
992 count = call_method (String_val (name), G_EVAL|G_ARRAY);
996 /* Pop all return values off the stack. Note that the return values on the
997 * stack are mortal, so we need to take a copy.
1000 for (i = 0; i < count; ++i) {
1003 cons = caml_alloc (2, 0);
1004 Field (cons, 1) = list;
1006 sv = newSVsv (POPs);
1007 Field (cons, 0) = Val_sv (sv);
1010 /* Restore the stack. */
1015 check_perl_failure ();
1021 perl4caml_call_method_void (value ref, value name, value arglist)
1023 CAMLparam3 (ref, name, arglist);
1027 CAMLlocal2 (errv, svv);
1032 /* Push the parameter list. */
1036 XPUSHs (sv_2mortal (newSVsv (sv)));
1038 /* Iteration over the linked list. */
1039 for (; arglist != Val_int (0); arglist = Field (arglist, 1))
1041 svv = Field (arglist, 0);
1043 XPUSHs (sv_2mortal (newSVsv (sv)));
1048 count = call_method (String_val (name), G_EVAL|G_VOID|G_DISCARD);
1052 assert (count == 0);
1054 /* Restore the stack. */
1059 check_perl_failure ();
1061 CAMLreturn (Val_unit);
1065 perl4caml_call_class_method (value classname, value name, value arglist)
1067 CAMLparam3 (classname, name, arglist);
1071 CAMLlocal2 (errv, svv);
1076 /* Push the parameter list. */
1079 XPUSHs (sv_2mortal (newSVpv (String_val (classname), 0)));
1081 /* Iteration over the linked list. */
1082 for (; arglist != Val_int (0); arglist = Field (arglist, 1))
1084 svv = Field (arglist, 0);
1086 XPUSHs (sv_2mortal (newSVsv (sv)));
1091 count = call_method (String_val (name), G_EVAL|G_SCALAR);
1095 assert (count == 1); /* Pretty sure it should never be anything else. */
1097 /* Pop return value off the stack. Note that the return value on the
1098 * stack is mortal, so we need to take a copy.
1100 sv = newSVsv (POPs);
1105 check_perl_failure ();
1112 perl4caml_call_class_method_array (value classname, value name, value arglist)
1114 CAMLparam3 (classname, name, arglist);
1118 CAMLlocal4 (errv, svv, list, cons);
1123 /* Push the parameter list. */
1126 XPUSHs (sv_2mortal (newSVpv (String_val (classname), 0)));
1128 /* Iteration over the linked list. */
1129 for (; arglist != Val_int (0); arglist = Field (arglist, 1))
1131 svv = Field (arglist, 0);
1133 XPUSHs (sv_2mortal (newSVsv (sv)));
1138 count = call_method (String_val (name), G_EVAL|G_ARRAY);
1142 /* Pop all return values off the stack. Note that the return values on the
1143 * stack are mortal, so we need to take a copy.
1146 for (i = 0; i < count; ++i) {
1147 cons = caml_alloc (2, 0);
1148 Field (cons, 1) = list;
1150 Field (cons, 0) = Val_sv (newSVsv (POPs));
1153 /* Restore the stack. */
1158 check_perl_failure ();
1164 perl4caml_call_class_method_void (value classname, value name, value arglist)
1166 CAMLparam3 (classname, name, arglist);
1170 CAMLlocal2 (errv, svv);
1175 /* Push the parameter list. */
1178 XPUSHs (sv_2mortal (newSVpv (String_val (classname), 0)));
1180 /* Iteration over the linked list. */
1181 for (; arglist != Val_int (0); arglist = Field (arglist, 1))
1183 svv = Field (arglist, 0);
1185 XPUSHs (sv_2mortal (newSVsv (sv)));
1190 count = call_method (String_val (name), G_EVAL|G_VOID|G_DISCARD);
1194 assert (count == 0);
1196 /* Restore the stack. */
1201 check_perl_failure ();
1203 CAMLreturn (Val_unit);
1207 Val_voidptr (void *ptr)
1211 rv = caml_alloc (1, Abstract_tag);
1212 Field(rv, 0) = (value) ptr;
1216 #if PERL4CAML_REFCOUNTING_EXPERIMENTAL
1219 xv_finalize (value v)
1221 /*fprintf (stderr, "about to decrement %p\n", Xv_val (v));*/
1222 SvREFCNT_dec ((SV *) Xv_val (v));
1225 static struct custom_operations xv_custom_operations = {
1226 "xv_custom_operations",
1228 custom_compare_default,
1229 custom_hash_default,
1230 custom_serialize_default,
1231 custom_deserialize_default
1239 rv = caml_alloc_custom (&xv_custom_operations, sizeof (void *), 0, 1);
1244 #endif /* PERL4CAML_REFCOUNTING_EXPERIMENTAL */
1247 unoption (value option, value deflt)
1249 if (option == Val_int (0)) /* "None" */
1251 else /* "Some 'a" */
1252 return Field (option, 0);