1 /* Interface to Perl from OCaml.
2 * Copyright (C) 2003 Merjis Ltd.
3 * $Id: perl_c.c,v 1.12 2003-11-19 16:28:22 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
23 /* XXX This is required by Perl >= 5.8.2. */
30 /* Perl requires the interpreter to be called literally 'my_perl'! */
31 static PerlInterpreter *my_perl;
33 /* Wrap up an arbitrary void pointer in an opaque OCaml object. */
34 static value Val_voidptr (void *ptr);
36 /* Get the concrete value from an optional field. */
37 static value unoption (value option, value deflt);
39 /* Unwrap an arbitrary void pointer from an opaque OCaml object. */
40 #define Voidptr_val(type,rv) ((type *) Field ((rv), 0))
42 /* Hide Perl types in opaque OCaml objects. */
43 #define Val_perl(pl) (Val_voidptr ((pl)))
44 #define Perl_val(plv) (Voidptr_val (PerlInterpreter, (plv)))
45 #define Val_sv(sv) (Val_voidptr ((sv)))
46 #define Sv_val(svv) (Voidptr_val (SV, (svv)))
47 #define Val_av(av) (Val_voidptr ((av)))
48 #define Av_val(avv) (Voidptr_val (AV, (avv)))
49 #define Val_hv(hv) (Val_voidptr ((hv)))
50 #define Hv_val(hvv) (Voidptr_val (HV, (hvv)))
53 perl4caml_init (value unit)
56 PERL_SYS_INIT3 (NULL, NULL, NULL);
57 CAMLreturn (Val_unit);
61 perl4caml_current_interpreter (value unit)
64 if (my_perl == 0) raise_not_found ();
65 CAMLreturn (Val_perl (my_perl));
71 char *file = __FILE__;
72 EXTERN_C void boot_DynaLoader (pTHX_ CV* cv);
74 newXS("DynaLoader::boot_DynaLoader", boot_DynaLoader, file);
78 perl4caml_create (value optargs, value unit)
80 CAMLparam2 (optargs, unit);
84 static char *no_args[] = { "", "-w", "-e", "0" };
86 /* Arguments given? */
87 if (optargs == Val_int (0)) /* "None" */
92 else /* "Some args" where args is a string array. */
94 args = Field (optargs, 0);
95 argc = Wosize_val (args);
96 argv = alloca (argc * sizeof (char *));
97 for (i = 0; i < argc; ++i) argv[i] = String_val (Field (args, i));
100 my_perl = perl_alloc ();
101 perl_construct (my_perl);
102 PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
103 perl_parse (my_perl, xs_init, argc, argv, NULL);
104 /*perl_run (my_perl);*/
106 CAMLreturn (Val_perl (my_perl));
110 perl4caml_destroy (value plv)
113 PerlInterpreter *pl = Perl_val (plv);
118 /* Current interpreter? */
119 if (my_perl == pl) my_perl = 0;
121 CAMLreturn (Val_unit);
125 perl4caml_set_context (value plv)
128 PerlInterpreter *pl = Perl_val (plv);
130 PERL_SET_CONTEXT (pl);
133 CAMLreturn (Val_unit);
137 perl4caml_int_of_sv (value svv)
140 SV *sv = Sv_val (svv);
141 CAMLreturn (Val_int (SvIV (sv)));
145 perl4caml_sv_of_int (value iv)
148 CAMLreturn (Val_sv (newSViv (Int_val (iv))));
152 perl4caml_float_of_sv (value svv)
155 SV *sv = Sv_val (svv);
157 f = copy_double (SvNV (sv));
162 perl4caml_sv_of_float (value fv)
165 CAMLreturn (Val_sv (newSViv (Double_val (fv))));
169 perl4caml_string_of_sv (value svv)
172 SV *sv = Sv_val (svv);
176 str = SvPV (sv, len);
177 /* XXX This won't work if the string contains NUL. */
178 strv = copy_string (str);
183 perl4caml_sv_of_string (value strv)
186 CAMLreturn (Val_sv (newSVpv (String_val (strv), string_length (strv))));
190 perl4caml_sv_is_true (value svv)
193 SV *sv = Sv_val (svv);
194 CAMLreturn (SvTRUE (sv) ? Val_true : Val_false);
198 perl4caml_sv_undef (value unit)
201 CAMLreturn (Val_sv (newSV (0)));
205 perl4caml_sv_yes (value unit)
208 CAMLreturn (Val_sv (&PL_sv_yes));
212 perl4caml_sv_no (value unit)
215 CAMLreturn (Val_sv (&PL_sv_no));
219 perl4caml_sv_type (value svv)
222 SV *sv = Sv_val (svv);
226 case SVt_IV: CAMLreturn (Val_int (1));
227 case SVt_NV: CAMLreturn (Val_int (2));
228 case SVt_PV: CAMLreturn (Val_int (3));
229 case SVt_RV: CAMLreturn (Val_int (4));
230 case SVt_PVAV: CAMLreturn (Val_int (5));
231 case SVt_PVHV: CAMLreturn (Val_int (6));
232 case SVt_PVCV: CAMLreturn (Val_int (7));
233 case SVt_PVGV: CAMLreturn (Val_int (8));
234 case SVt_PVMG: CAMLreturn (Val_int (9));
235 default: CAMLreturn (Val_int (0));
240 perl4caml_scalarref (value svv)
244 SV *sv = Sv_val (svv);
245 rsvv = Val_sv (newRV_inc (sv));
250 perl4caml_arrayref (value avv)
254 AV *av = Av_val (avv);
255 rsvv = Val_sv (newRV_inc ((SV *) av));
260 perl4caml_hashref (value hvv)
264 HV *hv = Hv_val (hvv);
265 rsvv = Val_sv (newRV_inc ((SV *) hv));
270 perl4caml_deref (value svv)
274 SV *sv = Sv_val (svv);
276 if (SvTYPE (sv) != SVt_RV)
277 invalid_argument ("deref: SV is not a reference");
278 switch (SvTYPE (SvRV (sv))) {
286 invalid_argument ("deref: SV is not a reference to a scalar");
288 rsvv = Val_sv (SvRV (sv));
293 perl4caml_deref_array (value svv)
297 SV *sv = Sv_val (svv);
299 if (SvTYPE (sv) != SVt_RV)
300 invalid_argument ("deref_array: SV is not a reference");
301 switch (SvTYPE (SvRV (sv))) {
305 invalid_argument ("deref_array: SV is not a reference to an array");
307 ravv = Val_av ((AV *) SvRV (sv));
312 perl4caml_deref_hash (value svv)
316 SV *sv = Sv_val (svv);
318 if (SvTYPE (sv) != SVt_RV)
319 invalid_argument ("deref_array: SV is not a reference");
320 switch (SvTYPE (SvRV (sv))) {
324 invalid_argument ("deref_array: SV is not a reference to a hash");
326 rhvv = Val_hv ((HV *) SvRV (sv));
331 perl4caml_av_empty (value unit)
335 CAMLreturn (Val_av (av));
338 /* We don't know in advance how long the list will be, which makes this
342 perl4caml_av_of_sv_list (value svlistv)
344 CAMLparam1 (svlistv);
346 SV *sv, **svlist = 0;
347 int alloc = 0, size = 0;
350 for (; svlistv != Val_int (0); svlistv = Field (svlistv, 1))
352 svv = Field (svlistv, 0);
355 alloc = alloc == 0 ? 1 : alloc * 2;
356 svlist = realloc (svlist, alloc * sizeof (SV *));
361 av = av_make (size, svlist);
363 if (alloc > 0) free (svlist); /* Free memory allocated to SV list. */
365 CAMLreturn (Val_av (av));
368 /* XXX av_map would be faster if we also had sv_list_of_av. */
371 perl4caml_av_push (value avv, value svv)
373 CAMLparam2 (avv, svv);
374 AV *av = Av_val (avv);
375 SV *sv = Sv_val (svv);
377 CAMLreturn (Val_unit);
381 perl4caml_av_pop (value avv)
384 AV *av = Av_val (avv);
385 SV *sv = av_pop (av);
386 CAMLreturn (Val_sv (sv));
390 perl4caml_av_unshift (value avv, value svv)
392 CAMLparam2 (avv, svv);
393 AV *av = Av_val (avv);
394 SV *sv = Sv_val (svv);
397 if (av_store (av, 0, sv) == 0)
399 CAMLreturn (Val_unit);
403 perl4caml_av_shift (value avv)
406 AV *av = Av_val (avv);
407 SV *sv = av_shift (av);
408 CAMLreturn (Val_sv (sv));
412 perl4caml_av_length (value avv)
415 AV *av = Av_val (avv);
416 CAMLreturn (Val_int (av_len (av) + 1));
420 perl4caml_av_set (value avv, value i, value svv)
422 CAMLparam3 (avv, i, svv);
423 AV *av = Av_val (avv);
424 SV *sv = Sv_val (svv);
426 if (av_store (av, Int_val (i), sv) == 0)
428 CAMLreturn (Val_unit);
432 perl4caml_av_get (value avv, value i)
435 AV *av = Av_val (avv);
436 SV **svp = av_fetch (av, Int_val (i), 0);
437 if (svp == 0) invalid_argument ("av_get: index out of bounds");
438 CAMLreturn (Val_sv (*svp));
442 perl4caml_av_clear (value avv)
445 AV *av = Av_val (avv);
447 CAMLreturn (Val_unit);
451 perl4caml_av_undef (value avv)
454 AV *av = Av_val (avv);
456 CAMLreturn (Val_unit);
460 perl4caml_av_extend (value avv, value i)
463 AV *av = Av_val (avv);
464 av_extend (av, Int_val (i));
465 CAMLreturn (Val_unit);
469 perl4caml_hv_empty (value unit)
473 CAMLreturn (Val_hv (hv));
477 perl4caml_hv_set (value hvv, value key, value svv)
479 CAMLparam3 (hvv, key, svv);
480 HV *hv = Hv_val (hvv);
481 SV *sv = Sv_val (svv);
483 if (hv_store (hv, String_val (key), string_length (key), sv, 0) == 0)
485 CAMLreturn (Val_unit);
489 perl4caml_hv_get (value hvv, value key)
491 CAMLparam2 (hvv, key);
492 HV *hv = Hv_val (hvv);
493 SV **svp = hv_fetch (hv, String_val (key), string_length (key), 0);
494 if (svp == 0) raise_not_found ();
495 CAMLreturn (Val_sv (*svp));
499 perl4caml_hv_exists (value hvv, value key)
501 CAMLparam2 (hvv, key);
502 HV *hv = Hv_val (hvv);
503 bool r = hv_exists (hv, String_val (key), string_length (key));
504 CAMLreturn (r ? Val_true : Val_false);
508 perl4caml_hv_delete (value hvv, value key)
510 CAMLparam2 (hvv, key);
511 HV *hv = Hv_val (hvv);
512 hv_delete (hv, String_val (key), string_length (key), G_DISCARD);
513 CAMLreturn (Val_unit);
517 perl4caml_hv_clear (value hvv)
520 HV *hv = Hv_val (hvv);
522 CAMLreturn (Val_unit);
526 perl4caml_hv_undef (value hvv)
529 HV *hv = Hv_val (hvv);
531 CAMLreturn (Val_unit);
535 perl4caml_get_sv (value optcreate, value name)
537 CAMLparam2 (optcreate, name);
541 create = unoption (optcreate, Val_false);
542 sv = get_sv (String_val (name), create == Val_true ? TRUE : FALSE);
543 if (sv == NULL) raise_not_found ();
545 CAMLreturn (Val_sv (sv));
549 perl4caml_get_av (value optcreate, value name)
551 CAMLparam2 (optcreate, name);
555 create = unoption (optcreate, Val_false);
556 av = get_av (String_val (name), create == Val_true ? TRUE : FALSE);
557 if (av == NULL) raise_not_found ();
559 CAMLreturn (Val_av (av));
563 perl4caml_get_hv (value optcreate, value name)
565 CAMLparam2 (optcreate, name);
569 create = unoption (optcreate, Val_false);
570 hv = get_hv (String_val (name), create == Val_true ? TRUE : FALSE);
571 if (hv == NULL) raise_not_found ();
573 CAMLreturn (Val_hv (hv));
577 check_perl_failure ()
579 SV *errsv = get_sv ("@", TRUE);
581 if (SvTRUE (errsv)) /* Equivalent of $@ in Perl. */
585 const char *err = SvPV (errsv, n_a);
587 errv = copy_string (err);
589 raise_with_arg (*caml_named_value ("perl4caml_perl_failure"), errv);
594 perl4caml_call (value optsv, value optfnname, value arglist)
596 CAMLparam3 (optsv, optfnname, arglist);
600 CAMLlocal3 (errv, svv, fnname);
605 /* Push the parameter list. */
608 /* Iteration over the linked list. */
609 for (; arglist != Val_int (0); arglist = Field (arglist, 1))
611 svv = Field (arglist, 0);
613 XPUSHs (sv_2mortal (newSVsv (sv)));
618 if (optsv != Val_int (0))
620 svv = unoption (optsv, Val_false);
622 count = call_sv (sv, G_EVAL|G_SCALAR);
624 else if (optfnname != Val_int (0))
626 fnname = unoption (optfnname, Val_false);
627 count = call_pv (String_val (fnname), G_EVAL|G_SCALAR);
632 "Perl.call: must supply either 'sv' or 'fn' parameters.");
638 assert (count == 1); /* Pretty sure it should never be anything else. */
640 /* Pop return value off the stack. Note that the return value on the
641 * stack is mortal, so we need to take a copy.
648 check_perl_failure ();
655 perl4caml_call_array (value optsv, value optfnname, value arglist)
657 CAMLparam3 (optsv, optfnname, arglist);
661 CAMLlocal5 (errv, svv, fnname, list, cons);
666 /* Push the parameter list. */
669 /* Iteration over the linked list. */
670 for (; arglist != Val_int (0); arglist = Field (arglist, 1))
672 svv = Field (arglist, 0);
674 XPUSHs (sv_2mortal (newSVsv (sv)));
679 if (optsv != Val_int (0))
681 svv = unoption (optsv, Val_false);
683 count = call_sv (sv, G_EVAL|G_ARRAY);
685 else if (optfnname != Val_int (0))
687 fnname = unoption (optfnname, Val_false);
688 count = call_pv (String_val (fnname), G_EVAL|G_ARRAY);
693 "Perl.call_array: must supply either 'sv' or 'fn' parameters.");
699 /* Pop all the return values off the stack into a list. Values on the
700 * stack are mortal, so we must copy them.
703 for (i = 0; i < count; ++i) {
705 Field (cons, 1) = list;
707 Field (cons, 0) = Val_sv (newSVsv (POPs));
710 /* Restore the stack. */
715 check_perl_failure ();
721 perl4caml_call_void (value optsv, value optfnname, value arglist)
723 CAMLparam3 (optsv, optfnname, arglist);
727 CAMLlocal3 (errv, svv, fnname);
732 /* Push the parameter list. */
735 /* Iteration over the linked list. */
736 for (; arglist != Val_int (0); arglist = Field (arglist, 1))
738 svv = Field (arglist, 0);
740 XPUSHs (sv_2mortal (newSVsv (sv)));
745 if (optsv != Val_int (0))
747 svv = unoption (optsv, Val_false);
749 count = call_sv (sv, G_EVAL|G_VOID);
751 else if (optfnname != Val_int (0))
753 fnname = unoption (optfnname, Val_false);
754 count = call_pv (String_val (fnname), G_EVAL|G_VOID|G_DISCARD);
759 "Perl.call_void: must supply either 'sv' or 'fn' parameters.");
767 /* Restore the stack. */
772 check_perl_failure ();
774 CAMLreturn (Val_unit);
778 perl4caml_eval (value expr)
783 CAMLlocal2 (errv, svv);
785 sv = eval_pv (String_val (expr), G_SCALAR);
787 check_perl_failure ();
794 perl4caml_call_method (value ref, value name, value arglist)
796 CAMLparam3 (ref, name, arglist);
800 CAMLlocal2 (errv, svv);
805 /* Push the parameter list. */
809 XPUSHs (sv_2mortal (newSVsv (sv)));
811 /* Iteration over the linked list. */
812 for (; arglist != Val_int (0); arglist = Field (arglist, 1))
814 svv = Field (arglist, 0);
816 XPUSHs (sv_2mortal (newSVsv (sv)));
821 count = call_method (String_val (name), G_EVAL|G_SCALAR);
825 assert (count == 1); /* Pretty sure it should never be anything else. */
827 /* Pop return value off the stack. Note that the return value on the
828 * stack is mortal, so we need to take a copy.
835 check_perl_failure ();
842 perl4caml_call_method_array (value ref, value name, value arglist)
844 CAMLparam3 (ref, name, arglist);
848 CAMLlocal4 (errv, svv, list, cons);
853 /* Push the parameter list. */
857 XPUSHs (sv_2mortal (newSVsv (sv)));
859 /* Iteration over the linked list. */
860 for (; arglist != Val_int (0); arglist = Field (arglist, 1))
862 svv = Field (arglist, 0);
864 XPUSHs (sv_2mortal (newSVsv (sv)));
869 count = call_method (String_val (name), G_EVAL|G_ARRAY);
873 /* Pop all return values off the stack. Note that the return values on the
874 * stack are mortal, so we need to take a copy.
877 for (i = 0; i < count; ++i) {
879 Field (cons, 1) = list;
881 Field (cons, 0) = Val_sv (newSVsv (POPs));
884 /* Restore the stack. */
889 check_perl_failure ();
895 perl4caml_call_method_void (value ref, value name, value arglist)
897 CAMLparam3 (ref, name, arglist);
901 CAMLlocal2 (errv, svv);
906 /* Push the parameter list. */
910 XPUSHs (sv_2mortal (newSVsv (sv)));
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_VOID|G_DISCARD);
928 /* Restore the stack. */
933 check_perl_failure ();
935 CAMLreturn (Val_unit);
939 perl4caml_call_class_method (value classname, value name, value arglist)
941 CAMLparam3 (classname, name, arglist);
945 CAMLlocal2 (errv, svv);
950 /* Push the parameter list. */
953 XPUSHs (sv_2mortal (newSVpv (String_val (classname), 0)));
955 /* Iteration over the linked list. */
956 for (; arglist != Val_int (0); arglist = Field (arglist, 1))
958 svv = Field (arglist, 0);
960 XPUSHs (sv_2mortal (newSVsv (sv)));
965 count = call_method (String_val (name), G_EVAL|G_SCALAR);
969 assert (count == 1); /* Pretty sure it should never be anything else. */
971 /* Pop return value off the stack. Note that the return value on the
972 * stack is mortal, so we need to take a copy.
979 check_perl_failure ();
986 perl4caml_call_class_method_array (value classname, value name, value arglist)
988 CAMLparam3 (classname, name, arglist);
992 CAMLlocal4 (errv, svv, list, cons);
997 /* Push the parameter list. */
1000 XPUSHs (sv_2mortal (newSVpv (String_val (classname), 0)));
1002 /* Iteration over the linked list. */
1003 for (; arglist != Val_int (0); arglist = Field (arglist, 1))
1005 svv = Field (arglist, 0);
1007 XPUSHs (sv_2mortal (newSVsv (sv)));
1012 count = call_method (String_val (name), G_EVAL|G_ARRAY);
1016 /* Pop all return values off the stack. Note that the return values on the
1017 * stack are mortal, so we need to take a copy.
1020 for (i = 0; i < count; ++i) {
1021 cons = alloc (2, 0);
1022 Field (cons, 1) = list;
1024 Field (cons, 0) = Val_sv (newSVsv (POPs));
1027 /* Restore the stack. */
1032 check_perl_failure ();
1038 perl4caml_call_class_method_void (value classname, value name, value arglist)
1040 CAMLparam3 (classname, name, arglist);
1044 CAMLlocal2 (errv, svv);
1049 /* Push the parameter list. */
1052 XPUSHs (sv_2mortal (newSVpv (String_val (classname), 0)));
1054 /* Iteration over the linked list. */
1055 for (; arglist != Val_int (0); arglist = Field (arglist, 1))
1057 svv = Field (arglist, 0);
1059 XPUSHs (sv_2mortal (newSVsv (sv)));
1064 count = call_method (String_val (name), G_EVAL|G_VOID|G_DISCARD);
1068 assert (count == 0);
1070 /* Restore the stack. */
1075 check_perl_failure ();
1077 CAMLreturn (Val_unit);
1081 Val_voidptr (void *ptr)
1083 value rv = alloc (1, Abstract_tag); /* XXX Is this correct? */
1084 Field(rv, 0) = (value) ptr;
1089 unoption (value option, value deflt)
1091 if (option == Val_int (0)) /* "None" */
1093 else /* "Some 'a" */
1094 return Field (option, 0);