1 /* Interface to Perl from OCaml.
2 * Copyright (C) 2003 Merjis Ltd.
3 * $Id: perl_c.c,v 1.19 2004-12-11 15:40:09 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_scalarref (value svv)
207 SV *sv = Sv_val (svv);
208 rsvv = Val_sv (newRV_inc (sv));
213 perl4caml_arrayref (value avv)
217 AV *av = Av_val (avv);
218 rsvv = Val_sv (newRV_inc ((SV *) av));
223 perl4caml_hashref (value hvv)
227 HV *hv = Hv_val (hvv);
228 rsvv = Val_sv (newRV_inc ((SV *) hv));
233 perl4caml_deref (value svv)
237 SV *sv = Sv_val (svv);
240 caml_invalid_argument ("deref: SV is not a reference");
241 switch (SvTYPE (SvRV (sv))) {
249 caml_invalid_argument ("deref: SV is not a reference to a scalar");
251 rsvv = Val_sv (SvRV (sv));
256 perl4caml_deref_array (value svv)
260 SV *sv = Sv_val (svv);
263 caml_invalid_argument ("deref_array: SV is not a reference");
264 switch (SvTYPE (SvRV (sv))) {
268 caml_invalid_argument ("deref_array: SV is not a reference to an array");
270 ravv = Val_av ((AV *) SvRV (sv));
275 perl4caml_deref_hash (value svv)
279 SV *sv = Sv_val (svv);
282 caml_invalid_argument ("deref_hash: SV is not a reference");
283 switch (SvTYPE (SvRV (sv))) {
287 caml_invalid_argument ("deref_hash: SV is not a reference to a hash");
289 rhvv = Val_hv ((HV *) SvRV (sv));
294 perl4caml_av_empty (value unit)
298 CAMLreturn (Val_av (av));
301 /* We don't know in advance how long the list will be, which makes this
305 perl4caml_av_of_sv_list (value svlistv)
307 CAMLparam1 (svlistv);
309 SV *sv, **svlist = 0;
310 int alloc = 0, size = 0;
313 for (; svlistv != Val_int (0); svlistv = Field (svlistv, 1))
315 svv = Field (svlistv, 0);
318 alloc = alloc == 0 ? 1 : alloc * 2;
319 svlist = realloc (svlist, alloc * sizeof (SV *));
324 av = av_make (size, svlist);
326 if (alloc > 0) free (svlist); /* Free memory allocated to SV list. */
328 CAMLreturn (Val_av (av));
331 /* XXX av_map would be faster if we also had sv_list_of_av. */
334 perl4caml_av_push (value avv, value svv)
336 CAMLparam2 (avv, svv);
337 AV *av = Av_val (avv);
338 SV *sv = Sv_val (svv);
340 CAMLreturn (Val_unit);
344 perl4caml_av_pop (value avv)
347 AV *av = Av_val (avv);
348 SV *sv = av_pop (av);
349 CAMLreturn (Val_sv (sv));
353 perl4caml_av_unshift (value avv, value svv)
355 CAMLparam2 (avv, svv);
356 AV *av = Av_val (avv);
357 SV *sv = Sv_val (svv);
360 if (av_store (av, 0, sv) == 0)
362 CAMLreturn (Val_unit);
366 perl4caml_av_shift (value avv)
369 AV *av = Av_val (avv);
370 SV *sv = av_shift (av);
371 CAMLreturn (Val_sv (sv));
375 perl4caml_av_length (value avv)
378 AV *av = Av_val (avv);
379 CAMLreturn (Val_int (av_len (av) + 1));
383 perl4caml_av_set (value avv, value i, value svv)
385 CAMLparam3 (avv, i, svv);
386 AV *av = Av_val (avv);
387 SV *sv = Sv_val (svv);
389 if (av_store (av, Int_val (i), sv) == 0)
391 CAMLreturn (Val_unit);
395 perl4caml_av_get (value avv, value i)
398 AV *av = Av_val (avv);
399 SV **svp = av_fetch (av, Int_val (i), 0);
400 if (svp == 0) caml_invalid_argument ("av_get: index out of bounds");
401 CAMLreturn (Val_sv (*svp));
405 perl4caml_av_clear (value avv)
408 AV *av = Av_val (avv);
410 CAMLreturn (Val_unit);
414 perl4caml_av_undef (value avv)
417 AV *av = Av_val (avv);
419 CAMLreturn (Val_unit);
423 perl4caml_av_extend (value avv, value i)
426 AV *av = Av_val (avv);
427 av_extend (av, Int_val (i));
428 CAMLreturn (Val_unit);
432 perl4caml_hv_empty (value unit)
436 CAMLreturn (Val_hv (hv));
440 perl4caml_hv_set (value hvv, value key, value svv)
442 CAMLparam3 (hvv, key, svv);
443 HV *hv = Hv_val (hvv);
444 SV *sv = Sv_val (svv);
446 if (hv_store (hv, String_val (key), caml_string_length (key), sv, 0) == 0)
448 CAMLreturn (Val_unit);
452 perl4caml_hv_get (value hvv, value key)
454 CAMLparam2 (hvv, key);
455 HV *hv = Hv_val (hvv);
456 SV **svp = hv_fetch (hv, String_val (key), caml_string_length (key), 0);
457 if (svp == 0) caml_raise_not_found ();
458 CAMLreturn (Val_sv (*svp));
462 perl4caml_hv_exists (value hvv, value key)
464 CAMLparam2 (hvv, key);
465 HV *hv = Hv_val (hvv);
466 bool r = hv_exists (hv, String_val (key), caml_string_length (key));
467 CAMLreturn (r ? Val_true : Val_false);
471 perl4caml_hv_delete (value hvv, value key)
473 CAMLparam2 (hvv, key);
474 HV *hv = Hv_val (hvv);
475 hv_delete (hv, String_val (key), caml_string_length (key), G_DISCARD);
476 CAMLreturn (Val_unit);
480 perl4caml_hv_clear (value hvv)
483 HV *hv = Hv_val (hvv);
485 CAMLreturn (Val_unit);
489 perl4caml_hv_undef (value hvv)
492 HV *hv = Hv_val (hvv);
494 CAMLreturn (Val_unit);
498 perl4caml_get_sv (value optcreate, value name)
500 CAMLparam2 (optcreate, name);
504 create = unoption (optcreate, Val_false);
505 sv = get_sv (String_val (name), create == Val_true ? TRUE : FALSE);
506 if (sv == NULL) caml_raise_not_found ();
508 CAMLreturn (Val_sv (sv));
512 perl4caml_get_av (value optcreate, value name)
514 CAMLparam2 (optcreate, name);
518 create = unoption (optcreate, Val_false);
519 av = get_av (String_val (name), create == Val_true ? TRUE : FALSE);
520 if (av == NULL) caml_raise_not_found ();
522 CAMLreturn (Val_av (av));
526 perl4caml_get_hv (value optcreate, value name)
528 CAMLparam2 (optcreate, name);
532 create = unoption (optcreate, Val_false);
533 hv = get_hv (String_val (name), create == Val_true ? TRUE : FALSE);
534 if (hv == NULL) caml_raise_not_found ();
536 CAMLreturn (Val_hv (hv));
540 check_perl_failure ()
542 SV *errsv = get_sv ("@", TRUE);
544 if (SvTRUE (errsv)) /* Equivalent of $@ in Perl. */
548 const char *err = SvPV (errsv, n_a);
550 errv = caml_copy_string (err);
552 caml_raise_with_arg (*caml_named_value ("perl4caml_perl_failure"), errv);
557 perl4caml_call (value optsv, value optfnname, value arglist)
559 CAMLparam3 (optsv, optfnname, arglist);
563 CAMLlocal3 (errv, svv, fnname);
568 /* Push the parameter list. */
571 /* Iteration over the linked list. */
572 for (; arglist != Val_int (0); arglist = Field (arglist, 1))
574 svv = Field (arglist, 0);
576 XPUSHs (sv_2mortal (newSVsv (sv)));
581 if (optsv != Val_int (0))
583 svv = unoption (optsv, Val_false);
585 count = call_sv (sv, G_EVAL|G_SCALAR);
587 else if (optfnname != Val_int (0))
589 fnname = unoption (optfnname, Val_false);
590 count = call_pv (String_val (fnname), G_EVAL|G_SCALAR);
595 "Perl.call: must supply either 'sv' or 'fn' parameters.");
601 assert (count == 1); /* Pretty sure it should never be anything else. */
603 /* Pop return value off the stack. Note that the return value on the
604 * stack is mortal, so we need to take a copy.
611 check_perl_failure ();
618 perl4caml_call_array (value optsv, value optfnname, value arglist)
620 CAMLparam3 (optsv, optfnname, arglist);
624 CAMLlocal5 (errv, svv, fnname, list, cons);
629 /* Push the parameter list. */
632 /* Iteration over the linked list. */
633 for (; arglist != Val_int (0); arglist = Field (arglist, 1))
635 svv = Field (arglist, 0);
637 XPUSHs (sv_2mortal (newSVsv (sv)));
642 if (optsv != Val_int (0))
644 svv = unoption (optsv, Val_false);
646 count = call_sv (sv, G_EVAL|G_ARRAY);
648 else if (optfnname != Val_int (0))
650 fnname = unoption (optfnname, Val_false);
651 count = call_pv (String_val (fnname), G_EVAL|G_ARRAY);
656 "Perl.call_array: must supply either 'sv' or 'fn' parameters.");
662 /* Pop all the return values off the stack into a list. Values on the
663 * stack are mortal, so we must copy them.
666 for (i = 0; i < count; ++i) {
669 cons = caml_alloc (2, 0);
670 Field (cons, 1) = list;
673 Field (cons, 0) = Val_sv (sv);
676 /* Restore the stack. */
681 check_perl_failure ();
687 perl4caml_call_void (value optsv, value optfnname, value arglist)
689 CAMLparam3 (optsv, optfnname, arglist);
693 CAMLlocal3 (errv, svv, fnname);
698 /* Push the parameter list. */
701 /* Iteration over the linked list. */
702 for (; arglist != Val_int (0); arglist = Field (arglist, 1))
704 svv = Field (arglist, 0);
706 XPUSHs (sv_2mortal (newSVsv (sv)));
711 if (optsv != Val_int (0))
713 svv = unoption (optsv, Val_false);
715 count = call_sv (sv, G_EVAL|G_VOID);
717 else if (optfnname != Val_int (0))
719 fnname = unoption (optfnname, Val_false);
720 count = call_pv (String_val (fnname), G_EVAL|G_VOID|G_DISCARD);
725 "Perl.call_void: must supply either 'sv' or 'fn' parameters.");
733 /* Restore the stack. */
738 check_perl_failure ();
740 CAMLreturn (Val_unit);
744 perl4caml_eval (value expr)
749 CAMLlocal2 (errv, svv);
751 sv = eval_pv (String_val (expr), G_SCALAR);
753 check_perl_failure ();
760 perl4caml_call_method (value ref, value name, value arglist)
762 CAMLparam3 (ref, name, arglist);
766 CAMLlocal2 (errv, svv);
771 /* Push the parameter list. */
775 XPUSHs (sv_2mortal (newSVsv (sv)));
777 /* Iteration over the linked list. */
778 for (; arglist != Val_int (0); arglist = Field (arglist, 1))
780 svv = Field (arglist, 0);
782 XPUSHs (sv_2mortal (newSVsv (sv)));
787 count = call_method (String_val (name), G_EVAL|G_SCALAR);
791 assert (count == 1); /* Pretty sure it should never be anything else. */
793 /* Pop return value off the stack. Note that the return value on the
794 * stack is mortal, so we need to take a copy.
801 check_perl_failure ();
808 perl4caml_call_method_array (value ref, value name, value arglist)
810 CAMLparam3 (ref, name, arglist);
814 CAMLlocal4 (errv, svv, list, cons);
819 /* Push the parameter list. */
823 XPUSHs (sv_2mortal (newSVsv (sv)));
825 /* Iteration over the linked list. */
826 for (; arglist != Val_int (0); arglist = Field (arglist, 1))
828 svv = Field (arglist, 0);
830 XPUSHs (sv_2mortal (newSVsv (sv)));
835 count = call_method (String_val (name), G_EVAL|G_ARRAY);
839 /* Pop all return values off the stack. Note that the return values on the
840 * stack are mortal, so we need to take a copy.
843 for (i = 0; i < count; ++i) {
846 cons = caml_alloc (2, 0);
847 Field (cons, 1) = list;
850 Field (cons, 0) = Val_sv (sv);
853 /* Restore the stack. */
858 check_perl_failure ();
864 perl4caml_call_method_void (value ref, value name, value arglist)
866 CAMLparam3 (ref, name, arglist);
870 CAMLlocal2 (errv, svv);
875 /* Push the parameter list. */
879 XPUSHs (sv_2mortal (newSVsv (sv)));
881 /* Iteration over the linked list. */
882 for (; arglist != Val_int (0); arglist = Field (arglist, 1))
884 svv = Field (arglist, 0);
886 XPUSHs (sv_2mortal (newSVsv (sv)));
891 count = call_method (String_val (name), G_EVAL|G_VOID|G_DISCARD);
897 /* Restore the stack. */
902 check_perl_failure ();
904 CAMLreturn (Val_unit);
908 perl4caml_call_class_method (value classname, value name, value arglist)
910 CAMLparam3 (classname, name, arglist);
914 CAMLlocal2 (errv, svv);
919 /* Push the parameter list. */
922 XPUSHs (sv_2mortal (newSVpv (String_val (classname), 0)));
924 /* Iteration over the linked list. */
925 for (; arglist != Val_int (0); arglist = Field (arglist, 1))
927 svv = Field (arglist, 0);
929 XPUSHs (sv_2mortal (newSVsv (sv)));
934 count = call_method (String_val (name), G_EVAL|G_SCALAR);
938 assert (count == 1); /* Pretty sure it should never be anything else. */
940 /* Pop return value off the stack. Note that the return value on the
941 * stack is mortal, so we need to take a copy.
948 check_perl_failure ();
955 perl4caml_call_class_method_array (value classname, value name, value arglist)
957 CAMLparam3 (classname, name, arglist);
961 CAMLlocal4 (errv, svv, list, cons);
966 /* Push the parameter list. */
969 XPUSHs (sv_2mortal (newSVpv (String_val (classname), 0)));
971 /* Iteration over the linked list. */
972 for (; arglist != Val_int (0); arglist = Field (arglist, 1))
974 svv = Field (arglist, 0);
976 XPUSHs (sv_2mortal (newSVsv (sv)));
981 count = call_method (String_val (name), G_EVAL|G_ARRAY);
985 /* Pop all return values off the stack. Note that the return values on the
986 * stack are mortal, so we need to take a copy.
989 for (i = 0; i < count; ++i) {
990 cons = caml_alloc (2, 0);
991 Field (cons, 1) = list;
993 Field (cons, 0) = Val_sv (newSVsv (POPs));
996 /* Restore the stack. */
1001 check_perl_failure ();
1007 perl4caml_call_class_method_void (value classname, value name, value arglist)
1009 CAMLparam3 (classname, name, arglist);
1013 CAMLlocal2 (errv, svv);
1018 /* Push the parameter list. */
1021 XPUSHs (sv_2mortal (newSVpv (String_val (classname), 0)));
1023 /* Iteration over the linked list. */
1024 for (; arglist != Val_int (0); arglist = Field (arglist, 1))
1026 svv = Field (arglist, 0);
1028 XPUSHs (sv_2mortal (newSVsv (sv)));
1033 count = call_method (String_val (name), G_EVAL|G_VOID|G_DISCARD);
1037 assert (count == 0);
1039 /* Restore the stack. */
1044 check_perl_failure ();
1046 CAMLreturn (Val_unit);
1050 Val_voidptr (void *ptr)
1054 rv = caml_alloc (1, Abstract_tag);
1055 Field(rv, 0) = (value) ptr;
1059 #if PERL4CAML_REFCOUNTING_EXPERIMENTAL
1062 xv_finalize (value v)
1064 SvREFCNT_dec ((SV *) Xv_val (v));
1067 static struct custom_operations xv_custom_operations = {
1068 "xv_custom_operations",
1070 custom_compare_default,
1071 custom_hash_default,
1072 custom_serialize_default,
1073 custom_deserialize_default
1081 rv = caml_alloc_custom (&xv_custom_operations, sizeof (void *), 0, 1);
1086 #endif /* PERL4CAML_REFCOUNTING_EXPERIMENTAL */
1089 unoption (value option, value deflt)
1091 if (option == Val_int (0)) /* "None" */
1093 else /* "Some 'a" */
1094 return Field (option, 0);