1 /* Interface to Perl from OCaml.
2 * Copyright (C) 2003 Merjis Ltd.
3 * $Id: perl_c.c,v 1.21 2005-01-29 12:22:49 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 /* XXX This won't work if the string contains NUL. */
133 strv = caml_copy_string (str);
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));
184 perl4caml_sv_type (value svv)
187 SV *sv = Sv_val (svv);
191 case SVt_IV: CAMLreturn (Val_int (1));
192 case SVt_NV: CAMLreturn (Val_int (2));
193 case SVt_PV: CAMLreturn (Val_int (3));
194 case SVt_RV: CAMLreturn (Val_int (4));
195 case SVt_PVAV: CAMLreturn (Val_int (5));
196 case SVt_PVHV: CAMLreturn (Val_int (6));
197 case SVt_PVCV: CAMLreturn (Val_int (7));
198 case SVt_PVGV: CAMLreturn (Val_int (8));
199 case SVt_PVMG: CAMLreturn (Val_int (9));
200 default: CAMLreturn (Val_int (0));
205 perl4caml_address_of_sv (value svv)
208 SV *sv = Sv_val (svv);
209 CAMLreturn (caml_copy_nativeint ((long) sv));
213 perl4caml_address_of_av (value avv)
216 AV *av = Av_val (avv);
217 CAMLreturn (caml_copy_nativeint ((long) av));
221 perl4caml_address_of_hv (value hvv)
224 HV *hv = Hv_val (hvv);
225 CAMLreturn (caml_copy_nativeint ((long) hv));
229 perl4caml_scalarref (value svv)
233 SV *sv = Sv_val (svv);
234 rsvv = Val_sv (newRV_inc (sv));
239 perl4caml_arrayref (value avv)
243 AV *av = Av_val (avv);
244 rsvv = Val_sv (newRV_inc ((SV *) av));
249 perl4caml_hashref (value hvv)
253 HV *hv = Hv_val (hvv);
254 rsvv = Val_sv (newRV_inc ((SV *) hv));
259 perl4caml_deref (value svv)
263 SV *sv = Sv_val (svv);
266 caml_invalid_argument ("deref: SV is not a reference");
267 switch (SvTYPE (SvRV (sv))) {
275 caml_invalid_argument ("deref: SV is not a reference to a scalar");
278 /* Increment the reference count because we're creating another
279 * value pointing at the referenced SV.
281 sv = SvREFCNT_inc (sv);
287 perl4caml_deref_array (value svv)
291 SV *sv = Sv_val (svv);
294 caml_invalid_argument ("deref_array: SV is not a reference");
295 switch (SvTYPE (SvRV (sv))) {
299 caml_invalid_argument ("deref_array: SV is not a reference to an array");
302 /* Increment the reference count because we're creating another
303 * value pointing at the referenced AV.
305 sv = SvREFCNT_inc (sv);
306 ravv = Val_av ((AV *) sv);
311 perl4caml_deref_hash (value svv)
315 SV *sv = Sv_val (svv);
318 caml_invalid_argument ("deref_hash: SV is not a reference");
319 switch (SvTYPE (SvRV (sv))) {
323 caml_invalid_argument ("deref_hash: SV is not a reference to a hash");
326 /* Increment the reference count because we're creating another
327 * value pointing at the referenced HV.
329 sv = SvREFCNT_inc (sv);
330 rhvv = Val_hv ((HV *) sv);
335 perl4caml_av_empty (value unit)
339 CAMLreturn (Val_av (av));
342 /* We don't know in advance how long the list will be, which makes this
346 perl4caml_av_of_sv_list (value svlistv)
348 CAMLparam1 (svlistv);
350 SV *sv, **svlist = 0;
351 int alloc = 0, size = 0;
354 for (; svlistv != Val_int (0); svlistv = Field (svlistv, 1))
356 svv = Field (svlistv, 0);
359 alloc = alloc == 0 ? 1 : alloc * 2;
360 svlist = realloc (svlist, alloc * sizeof (SV *));
365 av = av_make (size, svlist);
367 if (alloc > 0) free (svlist); /* Free memory allocated to SV list. */
369 CAMLreturn (Val_av (av));
372 /* XXX av_map would be faster if we also had sv_list_of_av. */
375 perl4caml_av_push (value avv, value svv)
377 CAMLparam2 (avv, svv);
378 AV *av = Av_val (avv);
379 SV *sv = Sv_val (svv);
381 CAMLreturn (Val_unit);
385 perl4caml_av_pop (value avv)
388 AV *av = Av_val (avv);
389 SV *sv = av_pop (av);
390 /* Increment the reference count because we're creating another
391 * value pointing at the referenced AV.
393 sv = SvREFCNT_inc (sv);
394 CAMLreturn (Val_sv (sv));
398 perl4caml_av_unshift (value avv, value svv)
400 CAMLparam2 (avv, svv);
401 AV *av = Av_val (avv);
402 SV *sv = Sv_val (svv);
405 if (av_store (av, 0, sv) == 0)
407 CAMLreturn (Val_unit);
411 perl4caml_av_shift (value avv)
414 AV *av = Av_val (avv);
415 SV *sv = av_shift (av);
416 /* Increment the reference count because we're creating another
417 * value pointing at the referenced AV.
419 sv = SvREFCNT_inc (sv);
420 CAMLreturn (Val_sv (sv));
424 perl4caml_av_length (value avv)
427 AV *av = Av_val (avv);
428 CAMLreturn (Val_int (av_len (av) + 1));
432 perl4caml_av_set (value avv, value i, value svv)
434 CAMLparam3 (avv, i, svv);
435 AV *av = Av_val (avv);
436 SV *sv = Sv_val (svv);
438 if (av_store (av, Int_val (i), sv) == 0)
440 CAMLreturn (Val_unit);
444 perl4caml_av_get (value avv, value i)
447 AV *av = Av_val (avv);
448 SV **svp = av_fetch (av, Int_val (i), 0);
449 if (svp == 0) caml_invalid_argument ("av_get: index out of bounds");
450 /* Increment the reference count because we're creating another
451 * value pointing at the referenced AV.
453 *svp = SvREFCNT_inc (*svp);
454 CAMLreturn (Val_sv (*svp));
458 perl4caml_av_clear (value avv)
461 AV *av = Av_val (avv);
463 CAMLreturn (Val_unit);
467 perl4caml_av_undef (value avv)
470 AV *av = Av_val (avv);
472 CAMLreturn (Val_unit);
476 perl4caml_av_extend (value avv, value i)
479 AV *av = Av_val (avv);
480 av_extend (av, Int_val (i));
481 CAMLreturn (Val_unit);
485 perl4caml_hv_empty (value unit)
489 CAMLreturn (Val_hv (hv));
493 perl4caml_hv_set (value hvv, value key, value svv)
495 CAMLparam3 (hvv, key, svv);
496 HV *hv = Hv_val (hvv);
497 SV *sv = Sv_val (svv);
499 if (hv_store (hv, String_val (key), caml_string_length (key), sv, 0) == 0)
501 CAMLreturn (Val_unit);
505 perl4caml_hv_get (value hvv, value key)
507 CAMLparam2 (hvv, key);
508 HV *hv = Hv_val (hvv);
509 SV **svp = hv_fetch (hv, String_val (key), caml_string_length (key), 0);
510 if (svp == 0) caml_raise_not_found ();
511 /* Increment the reference count because we're creating another
512 * value pointing at the referenced SV.
515 CAMLreturn (Val_sv (*svp));
519 perl4caml_hv_exists (value hvv, value key)
521 CAMLparam2 (hvv, key);
522 HV *hv = Hv_val (hvv);
523 bool r = hv_exists (hv, String_val (key), caml_string_length (key));
524 CAMLreturn (r ? Val_true : Val_false);
528 perl4caml_hv_delete (value hvv, value key)
530 CAMLparam2 (hvv, key);
531 HV *hv = Hv_val (hvv);
532 hv_delete (hv, String_val (key), caml_string_length (key), G_DISCARD);
533 CAMLreturn (Val_unit);
537 perl4caml_hv_clear (value hvv)
540 HV *hv = Hv_val (hvv);
542 CAMLreturn (Val_unit);
546 perl4caml_hv_undef (value hvv)
549 HV *hv = Hv_val (hvv);
551 CAMLreturn (Val_unit);
555 perl4caml_hv_iterinit (value hvv)
558 HV *hv = Hv_val (hvv);
559 int i = hv_iterinit (hv);
560 CAMLreturn (caml_copy_int32 (i));
564 perl4caml_hv_iternext (value hvv)
568 HV *hv = Hv_val (hvv);
569 HE *he = hv_iternext (hv);
570 if (he == NULL) caml_raise_not_found ();
576 perl4caml_hv_iterkey (value hev)
580 HE *he = He_val (hev);
582 char *str = hv_iterkey (he, &len);
583 strv = caml_alloc_string (len);
584 memcpy (String_val (strv), str, len);
589 perl4caml_hv_iterval (value hvv, value hev)
591 CAMLparam2 (hvv, hev);
593 HV *hv = Hv_val (hvv);
594 HE *he = He_val (hev);
595 SV *sv = hv_iterval (hv, he);
602 perl4caml_hv_iternextsv (value hvv)
605 CAMLlocal3 (strv, svv, rv);
606 HV *hv = Hv_val (hvv);
608 SV *sv = hv_iternextsv (hv, &str, &len);
609 if (sv == NULL) caml_raise_not_found ();
612 strv = caml_alloc_string (len);
613 memcpy (String_val (strv), str, len);
614 /* Construct a tuple (strv, svv). */
615 rv = caml_alloc_tuple (2);
616 Field (rv, 0) = strv;
622 perl4caml_get_sv (value optcreate, value name)
624 CAMLparam2 (optcreate, name);
628 create = unoption (optcreate, Val_false);
629 sv = get_sv (String_val (name), create == Val_true ? TRUE : FALSE);
630 if (sv == NULL) caml_raise_not_found ();
632 /* Increment the reference count because we're creating another
633 * value pointing at the referenced SV.
637 CAMLreturn (Val_sv (sv));
641 perl4caml_get_av (value optcreate, value name)
643 CAMLparam2 (optcreate, name);
647 create = unoption (optcreate, Val_false);
648 av = get_av (String_val (name), create == Val_true ? TRUE : FALSE);
649 if (av == NULL) caml_raise_not_found ();
651 /* Increment the reference count because we're creating another
652 * value pointing at the AV.
656 CAMLreturn (Val_av (av));
660 perl4caml_get_hv (value optcreate, value name)
662 CAMLparam2 (optcreate, name);
666 create = unoption (optcreate, Val_false);
667 hv = get_hv (String_val (name), create == Val_true ? TRUE : FALSE);
668 if (hv == NULL) caml_raise_not_found ();
670 /* Increment the reference count because we're creating another
671 * value pointing at the HV.
675 CAMLreturn (Val_hv (hv));
679 check_perl_failure ()
681 SV *errsv = get_sv ("@", TRUE);
683 if (SvTRUE (errsv)) /* Equivalent of $@ in Perl. */
687 const char *err = SvPV (errsv, n_a);
689 errv = caml_copy_string (err);
691 caml_raise_with_arg (*caml_named_value ("perl4caml_perl_failure"), errv);
696 perl4caml_call (value optsv, value optfnname, value arglist)
698 CAMLparam3 (optsv, optfnname, arglist);
702 CAMLlocal3 (errv, svv, fnname);
707 /* Push the parameter list. */
710 /* Iteration over the linked list. */
711 for (; arglist != Val_int (0); arglist = Field (arglist, 1))
713 svv = Field (arglist, 0);
715 XPUSHs (sv_2mortal (newSVsv (sv)));
720 if (optsv != Val_int (0))
722 svv = unoption (optsv, Val_false);
724 count = call_sv (sv, G_EVAL|G_SCALAR);
726 else if (optfnname != Val_int (0))
728 fnname = unoption (optfnname, Val_false);
729 count = call_pv (String_val (fnname), G_EVAL|G_SCALAR);
734 "Perl.call: must supply either 'sv' or 'fn' parameters.");
740 assert (count == 1); /* Pretty sure it should never be anything else. */
742 /* Pop return value off the stack. Note that the return value on the
743 * stack is mortal, so we need to take a copy.
750 check_perl_failure ();
757 perl4caml_call_array (value optsv, value optfnname, value arglist)
759 CAMLparam3 (optsv, optfnname, arglist);
763 CAMLlocal5 (errv, svv, fnname, list, cons);
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_ARRAY);
787 else if (optfnname != Val_int (0))
789 fnname = unoption (optfnname, Val_false);
790 count = call_pv (String_val (fnname), G_EVAL|G_ARRAY);
795 "Perl.call_array: must supply either 'sv' or 'fn' parameters.");
801 /* Pop all the return values off the stack into a list. Values on the
802 * stack are mortal, so we must copy them.
805 for (i = 0; i < count; ++i) {
808 cons = caml_alloc (2, 0);
809 Field (cons, 1) = list;
812 Field (cons, 0) = Val_sv (sv);
815 /* Restore the stack. */
820 check_perl_failure ();
826 perl4caml_call_void (value optsv, value optfnname, value arglist)
828 CAMLparam3 (optsv, optfnname, arglist);
832 CAMLlocal3 (errv, svv, fnname);
837 /* Push the parameter list. */
840 /* Iteration over the linked list. */
841 for (; arglist != Val_int (0); arglist = Field (arglist, 1))
843 svv = Field (arglist, 0);
845 XPUSHs (sv_2mortal (newSVsv (sv)));
850 if (optsv != Val_int (0))
852 svv = unoption (optsv, Val_false);
854 count = call_sv (sv, G_EVAL|G_VOID);
856 else if (optfnname != Val_int (0))
858 fnname = unoption (optfnname, Val_false);
859 count = call_pv (String_val (fnname), G_EVAL|G_VOID|G_DISCARD);
864 "Perl.call_void: must supply either 'sv' or 'fn' parameters.");
872 /* Restore the stack. */
877 check_perl_failure ();
879 CAMLreturn (Val_unit);
883 perl4caml_eval (value expr)
888 CAMLlocal2 (errv, svv);
890 sv = eval_pv (String_val (expr), G_SCALAR);
892 check_perl_failure ();
899 perl4caml_call_method (value ref, value name, value arglist)
901 CAMLparam3 (ref, name, arglist);
905 CAMLlocal2 (errv, svv);
910 /* Push the parameter list. */
914 XPUSHs (sv_2mortal (newSVsv (sv)));
916 /* Iteration over the linked list. */
917 for (; arglist != Val_int (0); arglist = Field (arglist, 1))
919 svv = Field (arglist, 0);
921 XPUSHs (sv_2mortal (newSVsv (sv)));
926 count = call_method (String_val (name), G_EVAL|G_SCALAR);
930 assert (count == 1); /* Pretty sure it should never be anything else. */
932 /* Pop return value off the stack. Note that the return value on the
933 * stack is mortal, so we need to take a copy.
940 check_perl_failure ();
947 perl4caml_call_method_array (value ref, value name, value arglist)
949 CAMLparam3 (ref, name, arglist);
953 CAMLlocal4 (errv, svv, list, cons);
958 /* Push the parameter list. */
962 XPUSHs (sv_2mortal (newSVsv (sv)));
964 /* Iteration over the linked list. */
965 for (; arglist != Val_int (0); arglist = Field (arglist, 1))
967 svv = Field (arglist, 0);
969 XPUSHs (sv_2mortal (newSVsv (sv)));
974 count = call_method (String_val (name), G_EVAL|G_ARRAY);
978 /* Pop all return values off the stack. Note that the return values on the
979 * stack are mortal, so we need to take a copy.
982 for (i = 0; i < count; ++i) {
985 cons = caml_alloc (2, 0);
986 Field (cons, 1) = list;
989 Field (cons, 0) = Val_sv (sv);
992 /* Restore the stack. */
997 check_perl_failure ();
1003 perl4caml_call_method_void (value ref, value name, value arglist)
1005 CAMLparam3 (ref, name, arglist);
1009 CAMLlocal2 (errv, svv);
1014 /* Push the parameter list. */
1018 XPUSHs (sv_2mortal (newSVsv (sv)));
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 perl4caml_call_class_method (value classname, value name, value arglist)
1049 CAMLparam3 (classname, name, arglist);
1053 CAMLlocal2 (errv, svv);
1058 /* Push the parameter list. */
1061 XPUSHs (sv_2mortal (newSVpv (String_val (classname), 0)));
1063 /* Iteration over the linked list. */
1064 for (; arglist != Val_int (0); arglist = Field (arglist, 1))
1066 svv = Field (arglist, 0);
1068 XPUSHs (sv_2mortal (newSVsv (sv)));
1073 count = call_method (String_val (name), G_EVAL|G_SCALAR);
1077 assert (count == 1); /* Pretty sure it should never be anything else. */
1079 /* Pop return value off the stack. Note that the return value on the
1080 * stack is mortal, so we need to take a copy.
1082 sv = newSVsv (POPs);
1087 check_perl_failure ();
1094 perl4caml_call_class_method_array (value classname, value name, value arglist)
1096 CAMLparam3 (classname, name, arglist);
1100 CAMLlocal4 (errv, svv, list, cons);
1105 /* Push the parameter list. */
1108 XPUSHs (sv_2mortal (newSVpv (String_val (classname), 0)));
1110 /* Iteration over the linked list. */
1111 for (; arglist != Val_int (0); arglist = Field (arglist, 1))
1113 svv = Field (arglist, 0);
1115 XPUSHs (sv_2mortal (newSVsv (sv)));
1120 count = call_method (String_val (name), G_EVAL|G_ARRAY);
1124 /* Pop all return values off the stack. Note that the return values on the
1125 * stack are mortal, so we need to take a copy.
1128 for (i = 0; i < count; ++i) {
1129 cons = caml_alloc (2, 0);
1130 Field (cons, 1) = list;
1132 Field (cons, 0) = Val_sv (newSVsv (POPs));
1135 /* Restore the stack. */
1140 check_perl_failure ();
1146 perl4caml_call_class_method_void (value classname, value name, value arglist)
1148 CAMLparam3 (classname, name, arglist);
1152 CAMLlocal2 (errv, svv);
1157 /* Push the parameter list. */
1160 XPUSHs (sv_2mortal (newSVpv (String_val (classname), 0)));
1162 /* Iteration over the linked list. */
1163 for (; arglist != Val_int (0); arglist = Field (arglist, 1))
1165 svv = Field (arglist, 0);
1167 XPUSHs (sv_2mortal (newSVsv (sv)));
1172 count = call_method (String_val (name), G_EVAL|G_VOID|G_DISCARD);
1176 assert (count == 0);
1178 /* Restore the stack. */
1183 check_perl_failure ();
1185 CAMLreturn (Val_unit);
1189 Val_voidptr (void *ptr)
1193 rv = caml_alloc (1, Abstract_tag);
1194 Field(rv, 0) = (value) ptr;
1198 #if PERL4CAML_REFCOUNTING_EXPERIMENTAL
1201 xv_finalize (value v)
1203 /*fprintf (stderr, "about to decrement %p\n", Xv_val (v));*/
1204 SvREFCNT_dec ((SV *) Xv_val (v));
1207 static struct custom_operations xv_custom_operations = {
1208 "xv_custom_operations",
1210 custom_compare_default,
1211 custom_hash_default,
1212 custom_serialize_default,
1213 custom_deserialize_default
1221 rv = caml_alloc_custom (&xv_custom_operations, sizeof (void *), 0, 1);
1226 #endif /* PERL4CAML_REFCOUNTING_EXPERIMENTAL */
1229 unoption (value option, value deflt)
1231 if (option == Val_int (0)) /* "None" */
1233 else /* "Some 'a" */
1234 return Field (option, 0);