1 /* Interface to Perl from OCaml.
2 * Copyright (C) 2003 Merjis Ltd.
3 * $Id: perl_c.c,v 1.8 2003-10-18 12:36:09 rich Exp $
12 #include <caml/alloc.h>
13 #include <caml/callback.h>
14 #include <caml/fail.h>
15 #include <caml/memory.h>
16 #include <caml/mlvalues.h>
18 /* XXX This was required to avoid an error on my machine when loading the Perl
19 * headers. Not clear why this is missing.
21 #define off64_t __off64_t
26 /* Perl requires the interpreter to be called literally 'my_perl'! */
27 static PerlInterpreter *my_perl;
29 /* Wrap up an arbitrary void pointer in an opaque OCaml object. */
30 static value Val_voidptr (void *ptr);
32 /* Get the concrete value from an optional field. */
33 static value unoption (value option, value deflt);
35 /* Unwrap an arbitrary void pointer from an opaque OCaml object. */
36 #define Voidptr_val(type,rv) ((type *) Field ((rv), 0))
38 /* Hide Perl types in opaque OCaml objects. */
39 #define Val_perl(pl) (Val_voidptr ((pl)))
40 #define Perl_val(plv) (Voidptr_val (PerlInterpreter, (plv)))
41 #define Val_sv(sv) (Val_voidptr ((sv)))
42 #define Sv_val(svv) (Voidptr_val (SV, (svv)))
43 #define Val_av(av) (Val_voidptr ((av)))
44 #define Av_val(avv) (Voidptr_val (AV, (avv)))
45 #define Val_hv(hv) (Val_voidptr ((hv)))
46 #define Hv_val(hvv) (Voidptr_val (HV, (hvv)))
49 perl4caml_init (value unit)
52 PERL_SYS_INIT3 (NULL, NULL, NULL);
53 CAMLreturn (Val_unit);
57 perl4caml_current_interpreter (value unit)
60 if (my_perl == 0) raise_not_found ();
61 CAMLreturn (Val_perl (my_perl));
67 char *file = __FILE__;
68 EXTERN_C void boot_DynaLoader (pTHX_ CV* cv);
70 newXS("DynaLoader::boot_DynaLoader", boot_DynaLoader, file);
74 perl4caml_create (value optargs, value unit)
76 CAMLparam2 (optargs, unit);
80 static char *no_args[] = { "", "-w", "-e", "0" };
82 /* Arguments given? */
83 if (optargs == Val_int (0)) /* "None" */
88 else /* "Some args" where args is a string array. */
90 args = Field (optargs, 0);
91 argc = Wosize_val (args);
92 argv = alloca (argc * sizeof (char *));
93 for (i = 0; i < argc; ++i) argv[i] = String_val (Field (args, i));
96 my_perl = perl_alloc ();
97 perl_construct (my_perl);
98 PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
99 perl_parse (my_perl, xs_init, argc, argv, NULL);
100 /*perl_run (my_perl);*/
102 CAMLreturn (Val_perl (my_perl));
106 perl4caml_destroy (value plv)
109 PerlInterpreter *pl = Perl_val (plv);
114 /* Current interpreter? */
115 if (my_perl == pl) my_perl = 0;
117 CAMLreturn (Val_unit);
121 perl4caml_set_context (value plv)
124 PerlInterpreter *pl = Perl_val (plv);
126 PERL_SET_CONTEXT (pl);
129 CAMLreturn (Val_unit);
133 perl4caml_int_of_sv (value svv)
136 SV *sv = Sv_val (svv);
137 CAMLreturn (Val_int (SvIV (sv)));
141 perl4caml_sv_of_int (value iv)
144 CAMLreturn (Val_sv (newSViv (Int_val (iv))));
148 perl4caml_float_of_sv (value svv)
151 SV *sv = Sv_val (svv);
153 f = copy_double (SvNV (sv));
158 perl4caml_sv_of_float (value fv)
161 CAMLreturn (Val_sv (newSViv (Double_val (fv))));
165 perl4caml_string_of_sv (value svv)
168 SV *sv = Sv_val (svv);
172 str = SvPV (sv, len);
173 /* XXX This won't work if the string contains NUL. */
174 strv = copy_string (str);
179 perl4caml_sv_of_string (value strv)
182 CAMLreturn (Val_sv (newSVpv (String_val (strv), string_length (strv))));
186 perl4caml_sv_is_true (value svv)
189 SV *sv = Sv_val (svv);
190 CAMLreturn (SvTRUE (sv) ? Val_true : Val_false);
194 perl4caml_sv_is_undef (value svv)
197 SV *sv = Sv_val (svv);
198 CAMLreturn (SvLEN (sv) == 0 ? Val_true : Val_false);
202 perl4caml_sv_undef (value unit)
205 CAMLreturn (Val_sv (newSV (0)));
209 perl4caml_sv_yes (value unit)
212 CAMLreturn (Val_sv (&PL_sv_yes));
216 perl4caml_sv_no (value unit)
219 CAMLreturn (Val_sv (&PL_sv_no));
223 perl4caml_sv_type (value svv)
226 SV *sv = Sv_val (svv);
230 case SVt_IV: CAMLreturn (Val_int (1));
231 case SVt_NV: CAMLreturn (Val_int (2));
232 case SVt_PV: CAMLreturn (Val_int (3));
233 case SVt_RV: CAMLreturn (Val_int (4));
234 case SVt_PVAV: CAMLreturn (Val_int (5));
235 case SVt_PVHV: CAMLreturn (Val_int (6));
236 case SVt_PVCV: CAMLreturn (Val_int (7));
237 case SVt_PVGV: CAMLreturn (Val_int (8));
238 case SVt_PVMG: CAMLreturn (Val_int (9));
239 default: CAMLreturn (Val_int (0));
244 perl4caml_deref (value svv)
248 SV *sv = Sv_val (svv);
250 if (SvTYPE (sv) != SVt_RV)
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);
273 if (SvTYPE (sv) != SVt_RV)
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);
292 if (SvTYPE (sv) != SVt_RV)
293 invalid_argument ("deref_array: SV is not a reference");
294 switch (SvTYPE (SvRV (sv))) {
298 invalid_argument ("deref_array: 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) {
679 Field (cons, 1) = list;
681 Field (cons, 0) = Val_sv (newSVsv (POPs));
684 /* Restore the stack. */
689 check_perl_failure ();
695 perl4caml_call_void (value optsv, value optfnname, value arglist)
697 CAMLparam3 (optsv, optfnname, arglist);
701 CAMLlocal3 (errv, svv, fnname);
706 /* Push the parameter list. */
709 /* Iteration over the linked list. */
710 for (; arglist != Val_int (0); arglist = Field (arglist, 1))
712 svv = Field (arglist, 0);
714 XPUSHs (sv_2mortal (newSVsv (sv)));
719 if (optsv != Val_int (0))
721 svv = unoption (optsv, Val_false);
723 count = call_sv (sv, G_EVAL|G_VOID);
725 else if (optfnname != Val_int (0))
727 fnname = unoption (optfnname, Val_false);
728 count = call_pv (String_val (fnname), G_EVAL|G_VOID);
733 "Perl.call_void: must supply either 'sv' or 'fn' parameters.");
739 assert (count == 0); /* Pretty sure it should never be anything else. */
741 /* Restore the stack. */
746 check_perl_failure ();
748 CAMLreturn (Val_unit);
752 perl4caml_eval (value expr)
757 CAMLlocal2 (errv, svv);
759 sv = eval_pv (String_val (expr), G_SCALAR);
761 check_perl_failure ();
768 perl4caml_call_method (value ref, value name, value arglist)
770 CAMLparam3 (ref, name, arglist);
774 CAMLlocal2 (errv, svv);
779 /* Push the parameter list. */
783 XPUSHs (sv_2mortal (newSVsv (sv)));
785 /* Iteration over the linked list. */
786 for (; arglist != Val_int (0); arglist = Field (arglist, 1))
788 svv = Field (arglist, 0);
790 XPUSHs (sv_2mortal (newSVsv (sv)));
795 count = call_method (String_val (name), G_EVAL|G_SCALAR);
799 assert (count == 1); /* Pretty sure it should never be anything else. */
801 /* Pop return value off the stack. Note that the return value on the
802 * stack is mortal, so we need to take a copy.
809 check_perl_failure ();
816 perl4caml_call_method_array (value ref, value name, value arglist)
818 CAMLparam3 (ref, name, arglist);
822 CAMLlocal4 (errv, svv, list, cons);
827 /* Push the parameter list. */
831 XPUSHs (sv_2mortal (newSVsv (sv)));
833 /* Iteration over the linked list. */
834 for (; arglist != Val_int (0); arglist = Field (arglist, 1))
836 svv = Field (arglist, 0);
838 XPUSHs (sv_2mortal (newSVsv (sv)));
843 count = call_method (String_val (name), G_EVAL|G_ARRAY);
847 /* Pop all return values off the stack. Note that the return values on the
848 * stack are mortal, so we need to take a copy.
851 for (i = 0; i < count; ++i) {
853 Field (cons, 1) = list;
855 Field (cons, 0) = Val_sv (newSVsv (POPs));
858 /* Restore the stack. */
863 check_perl_failure ();
869 perl4caml_call_method_void (value ref, value name, value arglist)
871 CAMLparam3 (ref, name, arglist);
875 CAMLlocal2 (errv, svv);
880 /* Push the parameter list. */
884 XPUSHs (sv_2mortal (newSVsv (sv)));
886 /* Iteration over the linked list. */
887 for (; arglist != Val_int (0); arglist = Field (arglist, 1))
889 svv = Field (arglist, 0);
891 XPUSHs (sv_2mortal (newSVsv (sv)));
896 count = call_method (String_val (name), G_EVAL|G_VOID);
900 assert (count == 0); /* Pretty sure it should never be anything else. */
902 /* Restore the stack. */
907 check_perl_failure ();
909 CAMLreturn (Val_unit);
913 perl4caml_call_class_method (value classname, value name, value arglist)
915 CAMLparam3 (classname, name, arglist);
919 CAMLlocal2 (errv, svv);
924 /* Push the parameter list. */
927 XPUSHs (sv_2mortal (newSVpv (String_val (classname), 0)));
929 /* Iteration over the linked list. */
930 for (; arglist != Val_int (0); arglist = Field (arglist, 1))
932 svv = Field (arglist, 0);
934 XPUSHs (sv_2mortal (newSVsv (sv)));
939 count = call_method (String_val (name), G_EVAL|G_SCALAR);
943 assert (count == 1); /* Pretty sure it should never be anything else. */
945 /* Pop return value off the stack. Note that the return value on the
946 * stack is mortal, so we need to take a copy.
953 check_perl_failure ();
960 perl4caml_call_class_method_array (value classname, value name, value arglist)
962 CAMLparam3 (classname, name, arglist);
966 CAMLlocal4 (errv, svv, list, cons);
971 /* Push the parameter list. */
974 XPUSHs (sv_2mortal (newSVpv (String_val (classname), 0)));
976 /* Iteration over the linked list. */
977 for (; arglist != Val_int (0); arglist = Field (arglist, 1))
979 svv = Field (arglist, 0);
981 XPUSHs (sv_2mortal (newSVsv (sv)));
986 count = call_method (String_val (name), G_EVAL|G_ARRAY);
990 /* Pop all return values off the stack. Note that the return values on the
991 * stack are mortal, so we need to take a copy.
994 for (i = 0; i < count; ++i) {
996 Field (cons, 1) = list;
998 Field (cons, 0) = Val_sv (newSVsv (POPs));
1001 /* Restore the stack. */
1006 check_perl_failure ();
1012 perl4caml_call_class_method_void (value classname, value name, value arglist)
1014 CAMLparam3 (classname, name, arglist);
1018 CAMLlocal2 (errv, svv);
1023 /* Push the parameter list. */
1026 XPUSHs (sv_2mortal (newSVpv (String_val (classname), 0)));
1028 /* Iteration over the linked list. */
1029 for (; arglist != Val_int (0); arglist = Field (arglist, 1))
1031 svv = Field (arglist, 0);
1033 XPUSHs (sv_2mortal (newSVsv (sv)));
1038 count = call_method (String_val (name), G_EVAL|G_VOID);
1042 assert (count == 0); /* Pretty sure it should never be anything else. */
1044 /* Restore the stack. */
1049 check_perl_failure ();
1051 CAMLreturn (Val_unit);
1055 Val_voidptr (void *ptr)
1057 value rv = alloc (1, Abstract_tag); /* XXX Is this correct? */
1058 Field(rv, 0) = (value) ptr;
1063 unoption (value option, value deflt)
1065 if (option == Val_int (0)) /* "None" */
1067 else /* "Some 'a" */
1068 return Field (option, 0);