1 /* Interface to Perl from OCaml.
2 * Copyright (C) 2003 Merjis Ltd.
3 * $Id: perl_c.c,v 1.9 2003-10-26 11:22:38 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_scalarref (value svv)
248 SV *sv = Sv_val (svv);
249 rsvv = Val_sv (newRV_inc (sv));
254 perl4caml_arrayref (value avv)
258 AV *av = Av_val (avv);
259 rsvv = Val_sv (newRV_inc ((SV *) av));
264 perl4caml_hashref (value hvv)
268 HV *hv = Hv_val (hvv);
269 rsvv = Val_sv (newRV_inc ((SV *) hv));
274 perl4caml_deref (value svv)
278 SV *sv = Sv_val (svv);
280 if (SvTYPE (sv) != SVt_RV)
281 invalid_argument ("deref: SV is not a reference");
282 switch (SvTYPE (SvRV (sv))) {
290 invalid_argument ("deref: SV is not a reference to a scalar");
292 rsvv = Val_sv (SvRV (sv));
297 perl4caml_deref_array (value svv)
301 SV *sv = Sv_val (svv);
303 if (SvTYPE (sv) != SVt_RV)
304 invalid_argument ("deref_array: SV is not a reference");
305 switch (SvTYPE (SvRV (sv))) {
309 invalid_argument ("deref_array: SV is not a reference to an array");
311 ravv = Val_av ((AV *) SvRV (sv));
316 perl4caml_deref_hash (value svv)
320 SV *sv = Sv_val (svv);
322 if (SvTYPE (sv) != SVt_RV)
323 invalid_argument ("deref_array: SV is not a reference");
324 switch (SvTYPE (SvRV (sv))) {
328 invalid_argument ("deref_array: SV is not a reference to a hash");
330 rhvv = Val_hv ((HV *) SvRV (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 CAMLreturn (Val_sv (sv));
394 perl4caml_av_unshift (value avv, value svv)
396 CAMLparam2 (avv, svv);
397 AV *av = Av_val (avv);
398 SV *sv = Sv_val (svv);
401 if (av_store (av, 0, sv) == 0)
403 CAMLreturn (Val_unit);
407 perl4caml_av_shift (value avv)
410 AV *av = Av_val (avv);
411 SV *sv = av_shift (av);
412 CAMLreturn (Val_sv (sv));
416 perl4caml_av_length (value avv)
419 AV *av = Av_val (avv);
420 CAMLreturn (Val_int (av_len (av) + 1));
424 perl4caml_av_set (value avv, value i, value svv)
426 CAMLparam3 (avv, i, svv);
427 AV *av = Av_val (avv);
428 SV *sv = Sv_val (svv);
430 if (av_store (av, Int_val (i), sv) == 0)
432 CAMLreturn (Val_unit);
436 perl4caml_av_get (value avv, value i)
439 AV *av = Av_val (avv);
440 SV **svp = av_fetch (av, Int_val (i), 0);
441 if (svp == 0) invalid_argument ("av_get: index out of bounds");
442 CAMLreturn (Val_sv (*svp));
446 perl4caml_av_clear (value avv)
449 AV *av = Av_val (avv);
451 CAMLreturn (Val_unit);
455 perl4caml_av_undef (value avv)
458 AV *av = Av_val (avv);
460 CAMLreturn (Val_unit);
464 perl4caml_av_extend (value avv, value i)
467 AV *av = Av_val (avv);
468 av_extend (av, Int_val (i));
469 CAMLreturn (Val_unit);
473 perl4caml_hv_empty (value unit)
477 CAMLreturn (Val_hv (hv));
481 perl4caml_hv_set (value hvv, value key, value svv)
483 CAMLparam3 (hvv, key, svv);
484 HV *hv = Hv_val (hvv);
485 SV *sv = Sv_val (svv);
487 if (hv_store (hv, String_val (key), string_length (key), sv, 0) == 0)
489 CAMLreturn (Val_unit);
493 perl4caml_hv_get (value hvv, value key)
495 CAMLparam2 (hvv, key);
496 HV *hv = Hv_val (hvv);
497 SV **svp = hv_fetch (hv, String_val (key), string_length (key), 0);
498 if (svp == 0) raise_not_found ();
499 CAMLreturn (Val_sv (*svp));
503 perl4caml_hv_exists (value hvv, value key)
505 CAMLparam2 (hvv, key);
506 HV *hv = Hv_val (hvv);
507 bool r = hv_exists (hv, String_val (key), string_length (key));
508 CAMLreturn (r ? Val_true : Val_false);
512 perl4caml_hv_delete (value hvv, value key)
514 CAMLparam2 (hvv, key);
515 HV *hv = Hv_val (hvv);
516 hv_delete (hv, String_val (key), string_length (key), G_DISCARD);
517 CAMLreturn (Val_unit);
521 perl4caml_hv_clear (value hvv)
524 HV *hv = Hv_val (hvv);
526 CAMLreturn (Val_unit);
530 perl4caml_hv_undef (value hvv)
533 HV *hv = Hv_val (hvv);
535 CAMLreturn (Val_unit);
539 perl4caml_get_sv (value optcreate, value name)
541 CAMLparam2 (optcreate, name);
545 create = unoption (optcreate, Val_false);
546 sv = get_sv (String_val (name), create == Val_true ? TRUE : FALSE);
547 if (sv == NULL) raise_not_found ();
549 CAMLreturn (Val_sv (sv));
553 perl4caml_get_av (value optcreate, value name)
555 CAMLparam2 (optcreate, name);
559 create = unoption (optcreate, Val_false);
560 av = get_av (String_val (name), create == Val_true ? TRUE : FALSE);
561 if (av == NULL) raise_not_found ();
563 CAMLreturn (Val_av (av));
567 perl4caml_get_hv (value optcreate, value name)
569 CAMLparam2 (optcreate, name);
573 create = unoption (optcreate, Val_false);
574 hv = get_hv (String_val (name), create == Val_true ? TRUE : FALSE);
575 if (hv == NULL) raise_not_found ();
577 CAMLreturn (Val_hv (hv));
581 check_perl_failure ()
583 SV *errsv = get_sv ("@", TRUE);
585 if (SvTRUE (errsv)) /* Equivalent of $@ in Perl. */
589 const char *err = SvPV (errsv, n_a);
591 errv = copy_string (err);
593 raise_with_arg (*caml_named_value ("perl4caml_perl_failure"), errv);
598 perl4caml_call (value optsv, value optfnname, value arglist)
600 CAMLparam3 (optsv, optfnname, arglist);
604 CAMLlocal3 (errv, svv, fnname);
609 /* Push the parameter list. */
612 /* Iteration over the linked list. */
613 for (; arglist != Val_int (0); arglist = Field (arglist, 1))
615 svv = Field (arglist, 0);
617 XPUSHs (sv_2mortal (newSVsv (sv)));
622 if (optsv != Val_int (0))
624 svv = unoption (optsv, Val_false);
626 count = call_sv (sv, G_EVAL|G_SCALAR);
628 else if (optfnname != Val_int (0))
630 fnname = unoption (optfnname, Val_false);
631 count = call_pv (String_val (fnname), G_EVAL|G_SCALAR);
636 "Perl.call: must supply either 'sv' or 'fn' parameters.");
642 assert (count == 1); /* Pretty sure it should never be anything else. */
644 /* Pop return value off the stack. Note that the return value on the
645 * stack is mortal, so we need to take a copy.
652 check_perl_failure ();
659 perl4caml_call_array (value optsv, value optfnname, value arglist)
661 CAMLparam3 (optsv, optfnname, arglist);
665 CAMLlocal5 (errv, svv, fnname, list, cons);
670 /* Push the parameter list. */
673 /* Iteration over the linked list. */
674 for (; arglist != Val_int (0); arglist = Field (arglist, 1))
676 svv = Field (arglist, 0);
678 XPUSHs (sv_2mortal (newSVsv (sv)));
683 if (optsv != Val_int (0))
685 svv = unoption (optsv, Val_false);
687 count = call_sv (sv, G_EVAL|G_ARRAY);
689 else if (optfnname != Val_int (0))
691 fnname = unoption (optfnname, Val_false);
692 count = call_pv (String_val (fnname), G_EVAL|G_ARRAY);
697 "Perl.call_array: must supply either 'sv' or 'fn' parameters.");
703 /* Pop all the return values off the stack into a list. Values on the
704 * stack are mortal, so we must copy them.
707 for (i = 0; i < count; ++i) {
709 Field (cons, 1) = list;
711 Field (cons, 0) = Val_sv (newSVsv (POPs));
714 /* Restore the stack. */
719 check_perl_failure ();
725 perl4caml_call_void (value optsv, value optfnname, value arglist)
727 CAMLparam3 (optsv, optfnname, arglist);
731 CAMLlocal3 (errv, svv, fnname);
736 /* Push the parameter list. */
739 /* Iteration over the linked list. */
740 for (; arglist != Val_int (0); arglist = Field (arglist, 1))
742 svv = Field (arglist, 0);
744 XPUSHs (sv_2mortal (newSVsv (sv)));
749 if (optsv != Val_int (0))
751 svv = unoption (optsv, Val_false);
753 count = call_sv (sv, G_EVAL|G_VOID);
755 else if (optfnname != Val_int (0))
757 fnname = unoption (optfnname, Val_false);
758 count = call_pv (String_val (fnname), G_EVAL|G_VOID);
763 "Perl.call_void: must supply either 'sv' or 'fn' parameters.");
769 assert (count == 0); /* Pretty sure it should never be anything else. */
771 /* Restore the stack. */
776 check_perl_failure ();
778 CAMLreturn (Val_unit);
782 perl4caml_eval (value expr)
787 CAMLlocal2 (errv, svv);
789 sv = eval_pv (String_val (expr), G_SCALAR);
791 check_perl_failure ();
798 perl4caml_call_method (value ref, value name, value arglist)
800 CAMLparam3 (ref, name, arglist);
804 CAMLlocal2 (errv, svv);
809 /* Push the parameter list. */
813 XPUSHs (sv_2mortal (newSVsv (sv)));
815 /* Iteration over the linked list. */
816 for (; arglist != Val_int (0); arglist = Field (arglist, 1))
818 svv = Field (arglist, 0);
820 XPUSHs (sv_2mortal (newSVsv (sv)));
825 count = call_method (String_val (name), G_EVAL|G_SCALAR);
829 assert (count == 1); /* Pretty sure it should never be anything else. */
831 /* Pop return value off the stack. Note that the return value on the
832 * stack is mortal, so we need to take a copy.
839 check_perl_failure ();
846 perl4caml_call_method_array (value ref, value name, value arglist)
848 CAMLparam3 (ref, name, arglist);
852 CAMLlocal4 (errv, svv, list, cons);
857 /* Push the parameter list. */
861 XPUSHs (sv_2mortal (newSVsv (sv)));
863 /* Iteration over the linked list. */
864 for (; arglist != Val_int (0); arglist = Field (arglist, 1))
866 svv = Field (arglist, 0);
868 XPUSHs (sv_2mortal (newSVsv (sv)));
873 count = call_method (String_val (name), G_EVAL|G_ARRAY);
877 /* Pop all return values off the stack. Note that the return values on the
878 * stack are mortal, so we need to take a copy.
881 for (i = 0; i < count; ++i) {
883 Field (cons, 1) = list;
885 Field (cons, 0) = Val_sv (newSVsv (POPs));
888 /* Restore the stack. */
893 check_perl_failure ();
899 perl4caml_call_method_void (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_VOID);
930 assert (count == 0); /* Pretty sure it should never be anything else. */
932 /* Restore the stack. */
937 check_perl_failure ();
939 CAMLreturn (Val_unit);
943 perl4caml_call_class_method (value classname, value name, value arglist)
945 CAMLparam3 (classname, name, arglist);
949 CAMLlocal2 (errv, svv);
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_SCALAR);
973 assert (count == 1); /* Pretty sure it should never be anything else. */
975 /* Pop return value off the stack. Note that the return value on the
976 * stack is mortal, so we need to take a copy.
983 check_perl_failure ();
990 perl4caml_call_class_method_array (value classname, value name, value arglist)
992 CAMLparam3 (classname, name, arglist);
996 CAMLlocal4 (errv, svv, list, cons);
1001 /* Push the parameter list. */
1004 XPUSHs (sv_2mortal (newSVpv (String_val (classname), 0)));
1006 /* Iteration over the linked list. */
1007 for (; arglist != Val_int (0); arglist = Field (arglist, 1))
1009 svv = Field (arglist, 0);
1011 XPUSHs (sv_2mortal (newSVsv (sv)));
1016 count = call_method (String_val (name), G_EVAL|G_ARRAY);
1020 /* Pop all return values off the stack. Note that the return values on the
1021 * stack are mortal, so we need to take a copy.
1024 for (i = 0; i < count; ++i) {
1025 cons = alloc (2, 0);
1026 Field (cons, 1) = list;
1028 Field (cons, 0) = Val_sv (newSVsv (POPs));
1031 /* Restore the stack. */
1036 check_perl_failure ();
1042 perl4caml_call_class_method_void (value classname, value name, value arglist)
1044 CAMLparam3 (classname, name, arglist);
1048 CAMLlocal2 (errv, svv);
1053 /* Push the parameter list. */
1056 XPUSHs (sv_2mortal (newSVpv (String_val (classname), 0)));
1058 /* Iteration over the linked list. */
1059 for (; arglist != Val_int (0); arglist = Field (arglist, 1))
1061 svv = Field (arglist, 0);
1063 XPUSHs (sv_2mortal (newSVsv (sv)));
1068 count = call_method (String_val (name), G_EVAL|G_VOID);
1072 assert (count == 0); /* Pretty sure it should never be anything else. */
1074 /* Restore the stack. */
1079 check_perl_failure ();
1081 CAMLreturn (Val_unit);
1085 Val_voidptr (void *ptr)
1087 value rv = alloc (1, Abstract_tag); /* XXX Is this correct? */
1088 Field(rv, 0) = (value) ptr;
1093 unoption (value option, value deflt)
1095 if (option == Val_int (0)) /* "None" */
1097 else /* "Some 'a" */
1098 return Field (option, 0);