1 /* Interface to Perl from OCaml.
2 * Copyright (C) 2003 Merjis Ltd.
3 * $Id: perl_c.c,v 1.18 2004-12-11 13:38:32 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 #ifndef PERL4CAML_NO_OFF64_T
20 /* XXX This was required to avoid an error on my machine when loading the Perl
21 * headers. Not clear why this is missing.
23 #define off64_t __off64_t
26 #ifndef PERL4CAML_NO_CRYPT_H
27 /* XXX This is required by Perl >= 5.8.2. */
35 /* Perl requires the interpreter to be called literally 'my_perl'! */
36 static PerlInterpreter *my_perl;
38 /* Get the concrete value from an optional field. */
39 static value unoption (value option, value deflt);
41 /* Wrap up an arbitrary void pointer in an opaque OCaml object. */
42 static value Val_voidptr (void *ptr);
44 /* Unwrap an arbitrary void pointer from an opaque OCaml object. */
45 #define Voidptr_val(type,rv) ((type *) Field ((rv), 0))
47 #if PERL4CAML_REFCOUNTING_EXPERIMENTAL
49 /* Unwrap a custom block. */
50 #define Xv_val(rv) (*((void **)Data_custom_val(rv)))
52 /* Wrap up an SV, AV or HV in a custom OCaml object which will decrement
53 * the reference count on finalization.
55 static value Val_xv (SV *sv);
59 #define Xv_val(rv) Voidptr_val (SV, (rv))
60 #define Val_xv(sv) Val_voidptr ((sv))
64 /* Hide Perl types in opaque OCaml objects. */
65 #define Val_perl(pl) (Val_voidptr ((pl)))
66 #define Perl_val(plv) (Voidptr_val (PerlInterpreter, (plv)))
67 #define Val_sv(sv) (Val_xv ((sv)))
68 #define Sv_val(svv) ((SV *) Xv_val (svv))
69 #define Val_av(av) (Val_xv ((SV *)(av)))
70 #define Av_val(avv) ((AV *) Xv_val (avv))
71 #define Val_hv(hv) (Val_xv ((SV *)(hv)))
72 #define Hv_val(hvv) ((HV *) Xv_val (hvv))
77 char *file = __FILE__;
78 EXTERN_C void boot_DynaLoader (pTHX_ CV* cv);
80 newXS("DynaLoader::boot_DynaLoader", boot_DynaLoader, file);
84 perl4caml_init (value unit)
88 static char *argv[] = { "", "-w", "-e", "0" };
90 PERL_SYS_INIT3 (NULL, NULL, NULL);
91 my_perl = perl_alloc ();
92 perl_construct (my_perl);
93 PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
94 perl_parse (my_perl, xs_init, argc, argv, NULL);
95 /*perl_run (my_perl);*/
97 CAMLreturn (Val_unit);
101 perl4caml_int_of_sv (value svv)
104 SV *sv = Sv_val (svv);
105 CAMLreturn (Val_int (SvIV (sv)));
109 perl4caml_sv_of_int (value iv)
112 CAMLreturn (Val_sv (newSViv (Int_val (iv))));
116 perl4caml_float_of_sv (value svv)
119 SV *sv = Sv_val (svv);
121 f = copy_double (SvNV (sv));
126 perl4caml_sv_of_float (value fv)
129 CAMLreturn (Val_sv (newSViv (Double_val (fv))));
133 perl4caml_string_of_sv (value svv)
136 SV *sv = Sv_val (svv);
140 str = SvPV (sv, len);
141 /* XXX This won't work if the string contains NUL. */
142 strv = copy_string (str);
147 perl4caml_sv_of_string (value strv)
150 CAMLreturn (Val_sv (newSVpv (String_val (strv), string_length (strv))));
154 perl4caml_sv_is_true (value svv)
157 SV *sv = Sv_val (svv);
158 CAMLreturn (SvTRUE (sv) ? Val_true : Val_false);
162 perl4caml_sv_undef (value unit)
165 /*CAMLreturn (Val_sv (newSV (0)));*/
166 CAMLreturn (Val_sv (&PL_sv_undef));
170 perl4caml_sv_is_undef (value svv)
173 SV *sv = Sv_val (svv);
174 CAMLreturn (!SvPOK (sv) && !SvIOK (sv) && SvTYPE (sv) == SVt_NULL
175 ? Val_true : Val_false);
179 perl4caml_sv_yes (value unit)
182 CAMLreturn (Val_sv (&PL_sv_yes));
186 perl4caml_sv_no (value unit)
189 CAMLreturn (Val_sv (&PL_sv_no));
193 perl4caml_sv_type (value svv)
196 SV *sv = Sv_val (svv);
200 case SVt_IV: CAMLreturn (Val_int (1));
201 case SVt_NV: CAMLreturn (Val_int (2));
202 case SVt_PV: CAMLreturn (Val_int (3));
203 case SVt_RV: CAMLreturn (Val_int (4));
204 case SVt_PVAV: CAMLreturn (Val_int (5));
205 case SVt_PVHV: CAMLreturn (Val_int (6));
206 case SVt_PVCV: CAMLreturn (Val_int (7));
207 case SVt_PVGV: CAMLreturn (Val_int (8));
208 case SVt_PVMG: CAMLreturn (Val_int (9));
209 default: CAMLreturn (Val_int (0));
214 perl4caml_scalarref (value svv)
218 SV *sv = Sv_val (svv);
219 rsvv = Val_sv (newRV_inc (sv));
224 perl4caml_arrayref (value avv)
228 AV *av = Av_val (avv);
229 rsvv = Val_sv (newRV_inc ((SV *) av));
234 perl4caml_hashref (value hvv)
238 HV *hv = Hv_val (hvv);
239 rsvv = Val_sv (newRV_inc ((SV *) hv));
244 perl4caml_deref (value svv)
248 SV *sv = Sv_val (svv);
251 invalid_argument ("deref: SV is not a reference");
252 switch (SvTYPE (SvRV (sv))) {
260 invalid_argument ("deref: SV is not a reference to a scalar");
262 rsvv = Val_sv (SvRV (sv));
267 perl4caml_deref_array (value svv)
271 SV *sv = Sv_val (svv);
274 invalid_argument ("deref_array: SV is not a reference");
275 switch (SvTYPE (SvRV (sv))) {
279 invalid_argument ("deref_array: SV is not a reference to an array");
281 ravv = Val_av ((AV *) SvRV (sv));
286 perl4caml_deref_hash (value svv)
290 SV *sv = Sv_val (svv);
293 invalid_argument ("deref_hash: SV is not a reference");
294 switch (SvTYPE (SvRV (sv))) {
298 invalid_argument ("deref_hash: SV is not a reference to a hash");
300 rhvv = Val_hv ((HV *) SvRV (sv));
305 perl4caml_av_empty (value unit)
309 CAMLreturn (Val_av (av));
312 /* We don't know in advance how long the list will be, which makes this
316 perl4caml_av_of_sv_list (value svlistv)
318 CAMLparam1 (svlistv);
320 SV *sv, **svlist = 0;
321 int alloc = 0, size = 0;
324 for (; svlistv != Val_int (0); svlistv = Field (svlistv, 1))
326 svv = Field (svlistv, 0);
329 alloc = alloc == 0 ? 1 : alloc * 2;
330 svlist = realloc (svlist, alloc * sizeof (SV *));
335 av = av_make (size, svlist);
337 if (alloc > 0) free (svlist); /* Free memory allocated to SV list. */
339 CAMLreturn (Val_av (av));
342 /* XXX av_map would be faster if we also had sv_list_of_av. */
345 perl4caml_av_push (value avv, value svv)
347 CAMLparam2 (avv, svv);
348 AV *av = Av_val (avv);
349 SV *sv = Sv_val (svv);
351 CAMLreturn (Val_unit);
355 perl4caml_av_pop (value avv)
358 AV *av = Av_val (avv);
359 SV *sv = av_pop (av);
360 CAMLreturn (Val_sv (sv));
364 perl4caml_av_unshift (value avv, value svv)
366 CAMLparam2 (avv, svv);
367 AV *av = Av_val (avv);
368 SV *sv = Sv_val (svv);
371 if (av_store (av, 0, sv) == 0)
373 CAMLreturn (Val_unit);
377 perl4caml_av_shift (value avv)
380 AV *av = Av_val (avv);
381 SV *sv = av_shift (av);
382 CAMLreturn (Val_sv (sv));
386 perl4caml_av_length (value avv)
389 AV *av = Av_val (avv);
390 CAMLreturn (Val_int (av_len (av) + 1));
394 perl4caml_av_set (value avv, value i, value svv)
396 CAMLparam3 (avv, i, svv);
397 AV *av = Av_val (avv);
398 SV *sv = Sv_val (svv);
400 if (av_store (av, Int_val (i), sv) == 0)
402 CAMLreturn (Val_unit);
406 perl4caml_av_get (value avv, value i)
409 AV *av = Av_val (avv);
410 SV **svp = av_fetch (av, Int_val (i), 0);
411 if (svp == 0) invalid_argument ("av_get: index out of bounds");
412 CAMLreturn (Val_sv (*svp));
416 perl4caml_av_clear (value avv)
419 AV *av = Av_val (avv);
421 CAMLreturn (Val_unit);
425 perl4caml_av_undef (value avv)
428 AV *av = Av_val (avv);
430 CAMLreturn (Val_unit);
434 perl4caml_av_extend (value avv, value i)
437 AV *av = Av_val (avv);
438 av_extend (av, Int_val (i));
439 CAMLreturn (Val_unit);
443 perl4caml_hv_empty (value unit)
447 CAMLreturn (Val_hv (hv));
451 perl4caml_hv_set (value hvv, value key, value svv)
453 CAMLparam3 (hvv, key, svv);
454 HV *hv = Hv_val (hvv);
455 SV *sv = Sv_val (svv);
457 if (hv_store (hv, String_val (key), string_length (key), sv, 0) == 0)
459 CAMLreturn (Val_unit);
463 perl4caml_hv_get (value hvv, value key)
465 CAMLparam2 (hvv, key);
466 HV *hv = Hv_val (hvv);
467 SV **svp = hv_fetch (hv, String_val (key), string_length (key), 0);
468 if (svp == 0) raise_not_found ();
469 CAMLreturn (Val_sv (*svp));
473 perl4caml_hv_exists (value hvv, value key)
475 CAMLparam2 (hvv, key);
476 HV *hv = Hv_val (hvv);
477 bool r = hv_exists (hv, String_val (key), string_length (key));
478 CAMLreturn (r ? Val_true : Val_false);
482 perl4caml_hv_delete (value hvv, value key)
484 CAMLparam2 (hvv, key);
485 HV *hv = Hv_val (hvv);
486 hv_delete (hv, String_val (key), string_length (key), G_DISCARD);
487 CAMLreturn (Val_unit);
491 perl4caml_hv_clear (value hvv)
494 HV *hv = Hv_val (hvv);
496 CAMLreturn (Val_unit);
500 perl4caml_hv_undef (value hvv)
503 HV *hv = Hv_val (hvv);
505 CAMLreturn (Val_unit);
509 perl4caml_get_sv (value optcreate, value name)
511 CAMLparam2 (optcreate, name);
515 create = unoption (optcreate, Val_false);
516 sv = get_sv (String_val (name), create == Val_true ? TRUE : FALSE);
517 if (sv == NULL) raise_not_found ();
519 CAMLreturn (Val_sv (sv));
523 perl4caml_get_av (value optcreate, value name)
525 CAMLparam2 (optcreate, name);
529 create = unoption (optcreate, Val_false);
530 av = get_av (String_val (name), create == Val_true ? TRUE : FALSE);
531 if (av == NULL) raise_not_found ();
533 CAMLreturn (Val_av (av));
537 perl4caml_get_hv (value optcreate, value name)
539 CAMLparam2 (optcreate, name);
543 create = unoption (optcreate, Val_false);
544 hv = get_hv (String_val (name), create == Val_true ? TRUE : FALSE);
545 if (hv == NULL) raise_not_found ();
547 CAMLreturn (Val_hv (hv));
551 check_perl_failure ()
553 SV *errsv = get_sv ("@", TRUE);
555 if (SvTRUE (errsv)) /* Equivalent of $@ in Perl. */
559 const char *err = SvPV (errsv, n_a);
561 errv = copy_string (err);
563 raise_with_arg (*caml_named_value ("perl4caml_perl_failure"), errv);
568 perl4caml_call (value optsv, value optfnname, value arglist)
570 CAMLparam3 (optsv, optfnname, arglist);
574 CAMLlocal3 (errv, svv, fnname);
579 /* Push the parameter list. */
582 /* Iteration over the linked list. */
583 for (; arglist != Val_int (0); arglist = Field (arglist, 1))
585 svv = Field (arglist, 0);
587 XPUSHs (sv_2mortal (newSVsv (sv)));
592 if (optsv != Val_int (0))
594 svv = unoption (optsv, Val_false);
596 count = call_sv (sv, G_EVAL|G_SCALAR);
598 else if (optfnname != Val_int (0))
600 fnname = unoption (optfnname, Val_false);
601 count = call_pv (String_val (fnname), G_EVAL|G_SCALAR);
606 "Perl.call: must supply either 'sv' or 'fn' parameters.");
612 assert (count == 1); /* Pretty sure it should never be anything else. */
614 /* Pop return value off the stack. Note that the return value on the
615 * stack is mortal, so we need to take a copy.
622 check_perl_failure ();
629 perl4caml_call_array (value optsv, value optfnname, value arglist)
631 CAMLparam3 (optsv, optfnname, arglist);
635 CAMLlocal5 (errv, svv, fnname, list, cons);
640 /* Push the parameter list. */
643 /* Iteration over the linked list. */
644 for (; arglist != Val_int (0); arglist = Field (arglist, 1))
646 svv = Field (arglist, 0);
648 XPUSHs (sv_2mortal (newSVsv (sv)));
653 if (optsv != Val_int (0))
655 svv = unoption (optsv, Val_false);
657 count = call_sv (sv, G_EVAL|G_ARRAY);
659 else if (optfnname != Val_int (0))
661 fnname = unoption (optfnname, Val_false);
662 count = call_pv (String_val (fnname), G_EVAL|G_ARRAY);
667 "Perl.call_array: must supply either 'sv' or 'fn' parameters.");
673 /* Pop all the return values off the stack into a list. Values on the
674 * stack are mortal, so we must copy them.
677 for (i = 0; i < count; ++i) {
681 Field (cons, 1) = list;
684 Field (cons, 0) = Val_sv (sv);
687 /* Restore the stack. */
692 check_perl_failure ();
698 perl4caml_call_void (value optsv, value optfnname, value arglist)
700 CAMLparam3 (optsv, optfnname, arglist);
704 CAMLlocal3 (errv, svv, fnname);
709 /* Push the parameter list. */
712 /* Iteration over the linked list. */
713 for (; arglist != Val_int (0); arglist = Field (arglist, 1))
715 svv = Field (arglist, 0);
717 XPUSHs (sv_2mortal (newSVsv (sv)));
722 if (optsv != Val_int (0))
724 svv = unoption (optsv, Val_false);
726 count = call_sv (sv, G_EVAL|G_VOID);
728 else if (optfnname != Val_int (0))
730 fnname = unoption (optfnname, Val_false);
731 count = call_pv (String_val (fnname), G_EVAL|G_VOID|G_DISCARD);
736 "Perl.call_void: must supply either 'sv' or 'fn' parameters.");
744 /* Restore the stack. */
749 check_perl_failure ();
751 CAMLreturn (Val_unit);
755 perl4caml_eval (value expr)
760 CAMLlocal2 (errv, svv);
762 sv = eval_pv (String_val (expr), G_SCALAR);
764 check_perl_failure ();
771 perl4caml_call_method (value ref, value name, value arglist)
773 CAMLparam3 (ref, name, arglist);
777 CAMLlocal2 (errv, svv);
782 /* Push the parameter list. */
786 XPUSHs (sv_2mortal (newSVsv (sv)));
788 /* Iteration over the linked list. */
789 for (; arglist != Val_int (0); arglist = Field (arglist, 1))
791 svv = Field (arglist, 0);
793 XPUSHs (sv_2mortal (newSVsv (sv)));
798 count = call_method (String_val (name), G_EVAL|G_SCALAR);
802 assert (count == 1); /* Pretty sure it should never be anything else. */
804 /* Pop return value off the stack. Note that the return value on the
805 * stack is mortal, so we need to take a copy.
812 check_perl_failure ();
819 perl4caml_call_method_array (value ref, value name, value arglist)
821 CAMLparam3 (ref, name, arglist);
825 CAMLlocal4 (errv, svv, list, cons);
830 /* Push the parameter list. */
834 XPUSHs (sv_2mortal (newSVsv (sv)));
836 /* Iteration over the linked list. */
837 for (; arglist != Val_int (0); arglist = Field (arglist, 1))
839 svv = Field (arglist, 0);
841 XPUSHs (sv_2mortal (newSVsv (sv)));
846 count = call_method (String_val (name), G_EVAL|G_ARRAY);
850 /* Pop all return values off the stack. Note that the return values on the
851 * stack are mortal, so we need to take a copy.
854 for (i = 0; i < count; ++i) {
858 Field (cons, 1) = list;
861 Field (cons, 0) = Val_sv (sv);
864 /* Restore the stack. */
869 check_perl_failure ();
875 perl4caml_call_method_void (value ref, value name, value arglist)
877 CAMLparam3 (ref, name, arglist);
881 CAMLlocal2 (errv, svv);
886 /* Push the parameter list. */
890 XPUSHs (sv_2mortal (newSVsv (sv)));
892 /* Iteration over the linked list. */
893 for (; arglist != Val_int (0); arglist = Field (arglist, 1))
895 svv = Field (arglist, 0);
897 XPUSHs (sv_2mortal (newSVsv (sv)));
902 count = call_method (String_val (name), G_EVAL|G_VOID|G_DISCARD);
908 /* Restore the stack. */
913 check_perl_failure ();
915 CAMLreturn (Val_unit);
919 perl4caml_call_class_method (value classname, value name, value arglist)
921 CAMLparam3 (classname, name, arglist);
925 CAMLlocal2 (errv, svv);
930 /* Push the parameter list. */
933 XPUSHs (sv_2mortal (newSVpv (String_val (classname), 0)));
935 /* Iteration over the linked list. */
936 for (; arglist != Val_int (0); arglist = Field (arglist, 1))
938 svv = Field (arglist, 0);
940 XPUSHs (sv_2mortal (newSVsv (sv)));
945 count = call_method (String_val (name), G_EVAL|G_SCALAR);
949 assert (count == 1); /* Pretty sure it should never be anything else. */
951 /* Pop return value off the stack. Note that the return value on the
952 * stack is mortal, so we need to take a copy.
959 check_perl_failure ();
966 perl4caml_call_class_method_array (value classname, value name, value arglist)
968 CAMLparam3 (classname, name, arglist);
972 CAMLlocal4 (errv, svv, list, cons);
977 /* Push the parameter list. */
980 XPUSHs (sv_2mortal (newSVpv (String_val (classname), 0)));
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) {
1001 cons = alloc (2, 0);
1002 Field (cons, 1) = list;
1004 Field (cons, 0) = Val_sv (newSVsv (POPs));
1007 /* Restore the stack. */
1012 check_perl_failure ();
1018 perl4caml_call_class_method_void (value classname, value name, value arglist)
1020 CAMLparam3 (classname, name, arglist);
1024 CAMLlocal2 (errv, svv);
1029 /* Push the parameter list. */
1032 XPUSHs (sv_2mortal (newSVpv (String_val (classname), 0)));
1034 /* Iteration over the linked list. */
1035 for (; arglist != Val_int (0); arglist = Field (arglist, 1))
1037 svv = Field (arglist, 0);
1039 XPUSHs (sv_2mortal (newSVsv (sv)));
1044 count = call_method (String_val (name), G_EVAL|G_VOID|G_DISCARD);
1048 assert (count == 0);
1050 /* Restore the stack. */
1055 check_perl_failure ();
1057 CAMLreturn (Val_unit);
1061 Val_voidptr (void *ptr)
1065 rv = alloc (1, Abstract_tag);
1066 Field(rv, 0) = (value) ptr;
1070 #if PERL4CAML_REFCOUNTING_EXPERIMENTAL
1073 xv_finalize (value v)
1075 SvREFCNT_dec ((SV *) Xv_val (v));
1078 static struct custom_operations xv_custom_operations = {
1079 "xv_custom_operations",
1081 custom_compare_default,
1082 custom_hash_default,
1083 custom_serialize_default,
1084 custom_deserialize_default
1092 rv = alloc_custom (&xv_custom_operations, sizeof (void *), 0, 1);
1097 #endif /* PERL4CAML_REFCOUNTING_EXPERIMENTAL */
1100 unoption (value option, value deflt)
1102 if (option == Val_int (0)) /* "None" */
1104 else /* "Some 'a" */
1105 return Field (option, 0);