1 /* Interface to Perl from OCaml.
2 * Copyright (C) 2003 Merjis Ltd.
3 * $Id: perl_c.c,v 1.13 2003-12-11 17:41:52 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 /* XXX This was required to avoid an error on my machine when loading the Perl
20 * headers. Not clear why this is missing.
22 #define off64_t __off64_t
24 /* XXX This is required by Perl >= 5.8.2. */
31 /* Perl requires the interpreter to be called literally 'my_perl'! */
32 static PerlInterpreter *my_perl;
34 /* Wrap up an arbitrary void pointer in an opaque OCaml object. */
35 static value Val_voidptr (void *ptr);
37 /* Wrap up an SV, AV or HV in a custom OCaml object which will decrement
38 * the reference count on finalization.
40 static value Val_xv (SV *sv);
42 /* Get the concrete value from an optional field. */
43 static value unoption (value option, value deflt);
45 /* Unwrap an arbitrary void pointer from an opaque OCaml object. */
46 #define Voidptr_val(type,rv) ((type *) Field ((rv), 0))
48 /* Unwrap a custom block. */
49 #define Xv_val(rv) (*((void **)Data_custom_val(rv)))
51 /* Hide Perl types in opaque OCaml objects. */
52 #define Val_perl(pl) (Val_voidptr ((pl)))
53 #define Perl_val(plv) (Voidptr_val (PerlInterpreter, (plv)))
54 #define Val_sv(sv) (Val_xv ((sv)))
55 #define Sv_val(svv) ((SV *) Xv_val (svv))
56 #define Val_av(av) (Val_xv ((SV *)(av)))
57 #define Av_val(avv) ((AV *) Xv_val (avv))
58 #define Val_hv(hv) (Val_xv ((SV *)(hv)))
59 #define Hv_val(hvv) ((HV *) Xv_val (hvv))
64 char *file = __FILE__;
65 EXTERN_C void boot_DynaLoader (pTHX_ CV* cv);
67 newXS("DynaLoader::boot_DynaLoader", boot_DynaLoader, file);
71 perl4caml_init (value unit)
75 static char *argv[] = { "", "-w", "-e", "0" };
77 PERL_SYS_INIT3 (NULL, NULL, NULL);
78 my_perl = perl_alloc ();
79 perl_construct (my_perl);
80 PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
81 perl_parse (my_perl, xs_init, argc, argv, NULL);
82 /*perl_run (my_perl);*/
84 CAMLreturn (Val_unit);
88 perl4caml_int_of_sv (value svv)
91 SV *sv = Sv_val (svv);
92 CAMLreturn (Val_int (SvIV (sv)));
96 perl4caml_sv_of_int (value iv)
99 CAMLreturn (Val_sv (newSViv (Int_val (iv))));
103 perl4caml_float_of_sv (value svv)
106 SV *sv = Sv_val (svv);
108 f = copy_double (SvNV (sv));
113 perl4caml_sv_of_float (value fv)
116 CAMLreturn (Val_sv (newSViv (Double_val (fv))));
120 perl4caml_string_of_sv (value svv)
123 SV *sv = Sv_val (svv);
127 str = SvPV (sv, len);
128 /* XXX This won't work if the string contains NUL. */
129 strv = copy_string (str);
134 perl4caml_sv_of_string (value strv)
137 CAMLreturn (Val_sv (newSVpv (String_val (strv), string_length (strv))));
141 perl4caml_sv_is_true (value svv)
144 SV *sv = Sv_val (svv);
145 CAMLreturn (SvTRUE (sv) ? Val_true : Val_false);
149 perl4caml_sv_undef (value unit)
152 CAMLreturn (Val_sv (newSV (0)));
156 perl4caml_sv_yes (value unit)
159 CAMLreturn (Val_sv (&PL_sv_yes));
163 perl4caml_sv_no (value unit)
166 CAMLreturn (Val_sv (&PL_sv_no));
170 perl4caml_sv_type (value svv)
173 SV *sv = Sv_val (svv);
177 case SVt_IV: CAMLreturn (Val_int (1));
178 case SVt_NV: CAMLreturn (Val_int (2));
179 case SVt_PV: CAMLreturn (Val_int (3));
180 case SVt_RV: CAMLreturn (Val_int (4));
181 case SVt_PVAV: CAMLreturn (Val_int (5));
182 case SVt_PVHV: CAMLreturn (Val_int (6));
183 case SVt_PVCV: CAMLreturn (Val_int (7));
184 case SVt_PVGV: CAMLreturn (Val_int (8));
185 case SVt_PVMG: CAMLreturn (Val_int (9));
186 default: CAMLreturn (Val_int (0));
191 perl4caml_scalarref (value svv)
195 SV *sv = Sv_val (svv);
196 rsvv = Val_sv (newRV_inc (sv));
201 perl4caml_arrayref (value avv)
205 AV *av = Av_val (avv);
206 rsvv = Val_sv (newRV_inc ((SV *) av));
211 perl4caml_hashref (value hvv)
215 HV *hv = Hv_val (hvv);
216 rsvv = Val_sv (newRV_inc ((SV *) hv));
221 perl4caml_deref (value svv)
225 SV *sv = Sv_val (svv);
227 if (SvTYPE (sv) != SVt_RV)
228 invalid_argument ("deref: SV is not a reference");
229 switch (SvTYPE (SvRV (sv))) {
237 invalid_argument ("deref: SV is not a reference to a scalar");
239 rsvv = Val_sv (SvRV (sv));
244 perl4caml_deref_array (value svv)
248 SV *sv = Sv_val (svv);
250 if (SvTYPE (sv) != SVt_RV)
251 invalid_argument ("deref_array: SV is not a reference");
252 switch (SvTYPE (SvRV (sv))) {
256 invalid_argument ("deref_array: SV is not a reference to an array");
258 ravv = Val_av ((AV *) SvRV (sv));
263 perl4caml_deref_hash (value svv)
267 SV *sv = Sv_val (svv);
269 if (SvTYPE (sv) != SVt_RV)
270 invalid_argument ("deref_array: SV is not a reference");
271 switch (SvTYPE (SvRV (sv))) {
275 invalid_argument ("deref_array: SV is not a reference to a hash");
277 rhvv = Val_hv ((HV *) SvRV (sv));
282 perl4caml_av_empty (value unit)
286 CAMLreturn (Val_av (av));
289 /* We don't know in advance how long the list will be, which makes this
293 perl4caml_av_of_sv_list (value svlistv)
295 CAMLparam1 (svlistv);
297 SV *sv, **svlist = 0;
298 int alloc = 0, size = 0;
301 for (; svlistv != Val_int (0); svlistv = Field (svlistv, 1))
303 svv = Field (svlistv, 0);
306 alloc = alloc == 0 ? 1 : alloc * 2;
307 svlist = realloc (svlist, alloc * sizeof (SV *));
312 av = av_make (size, svlist);
314 if (alloc > 0) free (svlist); /* Free memory allocated to SV list. */
316 CAMLreturn (Val_av (av));
319 /* XXX av_map would be faster if we also had sv_list_of_av. */
322 perl4caml_av_push (value avv, value svv)
324 CAMLparam2 (avv, svv);
325 AV *av = Av_val (avv);
326 SV *sv = Sv_val (svv);
328 CAMLreturn (Val_unit);
332 perl4caml_av_pop (value avv)
335 AV *av = Av_val (avv);
336 SV *sv = av_pop (av);
337 CAMLreturn (Val_sv (sv));
341 perl4caml_av_unshift (value avv, value svv)
343 CAMLparam2 (avv, svv);
344 AV *av = Av_val (avv);
345 SV *sv = Sv_val (svv);
348 if (av_store (av, 0, sv) == 0)
350 CAMLreturn (Val_unit);
354 perl4caml_av_shift (value avv)
357 AV *av = Av_val (avv);
358 SV *sv = av_shift (av);
359 CAMLreturn (Val_sv (sv));
363 perl4caml_av_length (value avv)
366 AV *av = Av_val (avv);
367 CAMLreturn (Val_int (av_len (av) + 1));
371 perl4caml_av_set (value avv, value i, value svv)
373 CAMLparam3 (avv, i, svv);
374 AV *av = Av_val (avv);
375 SV *sv = Sv_val (svv);
377 if (av_store (av, Int_val (i), sv) == 0)
379 CAMLreturn (Val_unit);
383 perl4caml_av_get (value avv, value i)
386 AV *av = Av_val (avv);
387 SV **svp = av_fetch (av, Int_val (i), 0);
388 if (svp == 0) invalid_argument ("av_get: index out of bounds");
389 CAMLreturn (Val_sv (*svp));
393 perl4caml_av_clear (value avv)
396 AV *av = Av_val (avv);
398 CAMLreturn (Val_unit);
402 perl4caml_av_undef (value avv)
405 AV *av = Av_val (avv);
407 CAMLreturn (Val_unit);
411 perl4caml_av_extend (value avv, value i)
414 AV *av = Av_val (avv);
415 av_extend (av, Int_val (i));
416 CAMLreturn (Val_unit);
420 perl4caml_hv_empty (value unit)
424 CAMLreturn (Val_hv (hv));
428 perl4caml_hv_set (value hvv, value key, value svv)
430 CAMLparam3 (hvv, key, svv);
431 HV *hv = Hv_val (hvv);
432 SV *sv = Sv_val (svv);
434 if (hv_store (hv, String_val (key), string_length (key), sv, 0) == 0)
436 CAMLreturn (Val_unit);
440 perl4caml_hv_get (value hvv, value key)
442 CAMLparam2 (hvv, key);
443 HV *hv = Hv_val (hvv);
444 SV **svp = hv_fetch (hv, String_val (key), string_length (key), 0);
445 if (svp == 0) raise_not_found ();
446 CAMLreturn (Val_sv (*svp));
450 perl4caml_hv_exists (value hvv, value key)
452 CAMLparam2 (hvv, key);
453 HV *hv = Hv_val (hvv);
454 bool r = hv_exists (hv, String_val (key), string_length (key));
455 CAMLreturn (r ? Val_true : Val_false);
459 perl4caml_hv_delete (value hvv, value key)
461 CAMLparam2 (hvv, key);
462 HV *hv = Hv_val (hvv);
463 hv_delete (hv, String_val (key), string_length (key), G_DISCARD);
464 CAMLreturn (Val_unit);
468 perl4caml_hv_clear (value hvv)
471 HV *hv = Hv_val (hvv);
473 CAMLreturn (Val_unit);
477 perl4caml_hv_undef (value hvv)
480 HV *hv = Hv_val (hvv);
482 CAMLreturn (Val_unit);
486 perl4caml_get_sv (value optcreate, value name)
488 CAMLparam2 (optcreate, name);
492 create = unoption (optcreate, Val_false);
493 sv = get_sv (String_val (name), create == Val_true ? TRUE : FALSE);
494 if (sv == NULL) raise_not_found ();
496 CAMLreturn (Val_sv (sv));
500 perl4caml_get_av (value optcreate, value name)
502 CAMLparam2 (optcreate, name);
506 create = unoption (optcreate, Val_false);
507 av = get_av (String_val (name), create == Val_true ? TRUE : FALSE);
508 if (av == NULL) raise_not_found ();
510 CAMLreturn (Val_av (av));
514 perl4caml_get_hv (value optcreate, value name)
516 CAMLparam2 (optcreate, name);
520 create = unoption (optcreate, Val_false);
521 hv = get_hv (String_val (name), create == Val_true ? TRUE : FALSE);
522 if (hv == NULL) raise_not_found ();
524 CAMLreturn (Val_hv (hv));
528 check_perl_failure ()
530 SV *errsv = get_sv ("@", TRUE);
532 if (SvTRUE (errsv)) /* Equivalent of $@ in Perl. */
536 const char *err = SvPV (errsv, n_a);
538 errv = copy_string (err);
540 raise_with_arg (*caml_named_value ("perl4caml_perl_failure"), errv);
545 perl4caml_call (value optsv, value optfnname, value arglist)
547 CAMLparam3 (optsv, optfnname, arglist);
551 CAMLlocal3 (errv, svv, fnname);
556 /* Push the parameter list. */
559 /* Iteration over the linked list. */
560 for (; arglist != Val_int (0); arglist = Field (arglist, 1))
562 svv = Field (arglist, 0);
564 XPUSHs (sv_2mortal (newSVsv (sv)));
569 if (optsv != Val_int (0))
571 svv = unoption (optsv, Val_false);
573 count = call_sv (sv, G_EVAL|G_SCALAR);
575 else if (optfnname != Val_int (0))
577 fnname = unoption (optfnname, Val_false);
578 count = call_pv (String_val (fnname), G_EVAL|G_SCALAR);
583 "Perl.call: must supply either 'sv' or 'fn' parameters.");
589 assert (count == 1); /* Pretty sure it should never be anything else. */
591 /* Pop return value off the stack. Note that the return value on the
592 * stack is mortal, so we need to take a copy.
599 check_perl_failure ();
606 perl4caml_call_array (value optsv, value optfnname, value arglist)
608 CAMLparam3 (optsv, optfnname, arglist);
612 CAMLlocal5 (errv, svv, fnname, list, cons);
617 /* Push the parameter list. */
620 /* Iteration over the linked list. */
621 for (; arglist != Val_int (0); arglist = Field (arglist, 1))
623 svv = Field (arglist, 0);
625 XPUSHs (sv_2mortal (newSVsv (sv)));
630 if (optsv != Val_int (0))
632 svv = unoption (optsv, Val_false);
634 count = call_sv (sv, G_EVAL|G_ARRAY);
636 else if (optfnname != Val_int (0))
638 fnname = unoption (optfnname, Val_false);
639 count = call_pv (String_val (fnname), G_EVAL|G_ARRAY);
644 "Perl.call_array: must supply either 'sv' or 'fn' parameters.");
650 /* Pop all the return values off the stack into a list. Values on the
651 * stack are mortal, so we must copy them.
654 for (i = 0; i < count; ++i) {
658 Field (cons, 1) = list;
661 Field (cons, 0) = Val_sv (sv);
664 /* Restore the stack. */
669 check_perl_failure ();
675 perl4caml_call_void (value optsv, value optfnname, value arglist)
677 CAMLparam3 (optsv, optfnname, arglist);
681 CAMLlocal3 (errv, svv, fnname);
686 /* Push the parameter list. */
689 /* Iteration over the linked list. */
690 for (; arglist != Val_int (0); arglist = Field (arglist, 1))
692 svv = Field (arglist, 0);
694 XPUSHs (sv_2mortal (newSVsv (sv)));
699 if (optsv != Val_int (0))
701 svv = unoption (optsv, Val_false);
703 count = call_sv (sv, G_EVAL|G_VOID);
705 else if (optfnname != Val_int (0))
707 fnname = unoption (optfnname, Val_false);
708 count = call_pv (String_val (fnname), G_EVAL|G_VOID|G_DISCARD);
713 "Perl.call_void: must supply either 'sv' or 'fn' parameters.");
721 /* Restore the stack. */
726 check_perl_failure ();
728 CAMLreturn (Val_unit);
732 perl4caml_eval (value expr)
737 CAMLlocal2 (errv, svv);
739 sv = eval_pv (String_val (expr), G_SCALAR);
741 check_perl_failure ();
748 perl4caml_call_method (value ref, value name, value arglist)
750 CAMLparam3 (ref, name, arglist);
754 CAMLlocal2 (errv, svv);
759 /* Push the parameter list. */
763 XPUSHs (sv_2mortal (newSVsv (sv)));
765 /* Iteration over the linked list. */
766 for (; arglist != Val_int (0); arglist = Field (arglist, 1))
768 svv = Field (arglist, 0);
770 XPUSHs (sv_2mortal (newSVsv (sv)));
775 count = call_method (String_val (name), G_EVAL|G_SCALAR);
779 assert (count == 1); /* Pretty sure it should never be anything else. */
781 /* Pop return value off the stack. Note that the return value on the
782 * stack is mortal, so we need to take a copy.
789 check_perl_failure ();
796 perl4caml_call_method_array (value ref, value name, value arglist)
798 CAMLparam3 (ref, name, arglist);
802 CAMLlocal4 (errv, svv, list, cons);
807 /* Push the parameter list. */
811 XPUSHs (sv_2mortal (newSVsv (sv)));
813 /* Iteration over the linked list. */
814 for (; arglist != Val_int (0); arglist = Field (arglist, 1))
816 svv = Field (arglist, 0);
818 XPUSHs (sv_2mortal (newSVsv (sv)));
823 count = call_method (String_val (name), G_EVAL|G_ARRAY);
827 /* Pop all return values off the stack. Note that the return values on the
828 * stack are mortal, so we need to take a copy.
831 for (i = 0; i < count; ++i) {
835 Field (cons, 1) = list;
838 Field (cons, 0) = Val_sv (sv);
841 /* Restore the stack. */
846 check_perl_failure ();
852 perl4caml_call_method_void (value ref, value name, value arglist)
854 CAMLparam3 (ref, name, arglist);
858 CAMLlocal2 (errv, svv);
863 /* Push the parameter list. */
867 XPUSHs (sv_2mortal (newSVsv (sv)));
869 /* Iteration over the linked list. */
870 for (; arglist != Val_int (0); arglist = Field (arglist, 1))
872 svv = Field (arglist, 0);
874 XPUSHs (sv_2mortal (newSVsv (sv)));
879 count = call_method (String_val (name), G_EVAL|G_VOID|G_DISCARD);
885 /* Restore the stack. */
890 check_perl_failure ();
892 CAMLreturn (Val_unit);
896 perl4caml_call_class_method (value classname, value name, value arglist)
898 CAMLparam3 (classname, name, arglist);
902 CAMLlocal2 (errv, svv);
907 /* Push the parameter list. */
910 XPUSHs (sv_2mortal (newSVpv (String_val (classname), 0)));
912 /* Iteration over the linked list. */
913 for (; arglist != Val_int (0); arglist = Field (arglist, 1))
915 svv = Field (arglist, 0);
917 XPUSHs (sv_2mortal (newSVsv (sv)));
922 count = call_method (String_val (name), G_EVAL|G_SCALAR);
926 assert (count == 1); /* Pretty sure it should never be anything else. */
928 /* Pop return value off the stack. Note that the return value on the
929 * stack is mortal, so we need to take a copy.
936 check_perl_failure ();
943 perl4caml_call_class_method_array (value classname, value name, value arglist)
945 CAMLparam3 (classname, name, arglist);
949 CAMLlocal4 (errv, svv, list, cons);
954 /* Push the parameter list. */
957 XPUSHs (sv_2mortal (newSVpv (String_val (classname), 0)));
959 /* Iteration over the linked list. */
960 for (; arglist != Val_int (0); arglist = Field (arglist, 1))
962 svv = Field (arglist, 0);
964 XPUSHs (sv_2mortal (newSVsv (sv)));
969 count = call_method (String_val (name), G_EVAL|G_ARRAY);
973 /* Pop all return values off the stack. Note that the return values on the
974 * stack are mortal, so we need to take a copy.
977 for (i = 0; i < count; ++i) {
979 Field (cons, 1) = list;
981 Field (cons, 0) = Val_sv (newSVsv (POPs));
984 /* Restore the stack. */
989 check_perl_failure ();
995 perl4caml_call_class_method_void (value classname, value name, value arglist)
997 CAMLparam3 (classname, name, arglist);
1001 CAMLlocal2 (errv, svv);
1006 /* Push the parameter list. */
1009 XPUSHs (sv_2mortal (newSVpv (String_val (classname), 0)));
1011 /* Iteration over the linked list. */
1012 for (; arglist != Val_int (0); arglist = Field (arglist, 1))
1014 svv = Field (arglist, 0);
1016 XPUSHs (sv_2mortal (newSVsv (sv)));
1021 count = call_method (String_val (name), G_EVAL|G_VOID|G_DISCARD);
1025 assert (count == 0);
1027 /* Restore the stack. */
1032 check_perl_failure ();
1034 CAMLreturn (Val_unit);
1038 Val_voidptr (void *ptr)
1042 rv = alloc (1, Abstract_tag);
1043 Field(rv, 0) = (value) ptr;
1048 xv_finalize (value v)
1050 SvREFCNT_dec ((SV *) Xv_val (v));
1053 static struct custom_operations xv_custom_operations = {
1054 "xv_custom_operations",
1056 custom_compare_default,
1057 custom_hash_default,
1058 custom_serialize_default,
1059 custom_deserialize_default
1067 rv = alloc_custom (&xv_custom_operations, sizeof (void *), 0, 1);
1073 unoption (value option, value deflt)
1075 if (option == Val_int (0)) /* "None" */
1077 else /* "Some 'a" */
1078 return Field (option, 0);