1 /* Interface to Perl from OCaml.
2 * Copyright (C) 2003 Merjis Ltd.
3 * $Id: perl_c.c,v 1.11 2003-10-26 12:57:11 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_undef (value unit)
197 CAMLreturn (Val_sv (newSV (0)));
201 perl4caml_sv_yes (value unit)
204 CAMLreturn (Val_sv (&PL_sv_yes));
208 perl4caml_sv_no (value unit)
211 CAMLreturn (Val_sv (&PL_sv_no));
215 perl4caml_sv_type (value svv)
218 SV *sv = Sv_val (svv);
222 case SVt_IV: CAMLreturn (Val_int (1));
223 case SVt_NV: CAMLreturn (Val_int (2));
224 case SVt_PV: CAMLreturn (Val_int (3));
225 case SVt_RV: CAMLreturn (Val_int (4));
226 case SVt_PVAV: CAMLreturn (Val_int (5));
227 case SVt_PVHV: CAMLreturn (Val_int (6));
228 case SVt_PVCV: CAMLreturn (Val_int (7));
229 case SVt_PVGV: CAMLreturn (Val_int (8));
230 case SVt_PVMG: CAMLreturn (Val_int (9));
231 default: CAMLreturn (Val_int (0));
236 perl4caml_scalarref (value svv)
240 SV *sv = Sv_val (svv);
241 rsvv = Val_sv (newRV_inc (sv));
246 perl4caml_arrayref (value avv)
250 AV *av = Av_val (avv);
251 rsvv = Val_sv (newRV_inc ((SV *) av));
256 perl4caml_hashref (value hvv)
260 HV *hv = Hv_val (hvv);
261 rsvv = Val_sv (newRV_inc ((SV *) hv));
266 perl4caml_deref (value svv)
270 SV *sv = Sv_val (svv);
272 if (SvTYPE (sv) != SVt_RV)
273 invalid_argument ("deref: SV is not a reference");
274 switch (SvTYPE (SvRV (sv))) {
282 invalid_argument ("deref: SV is not a reference to a scalar");
284 rsvv = Val_sv (SvRV (sv));
289 perl4caml_deref_array (value svv)
293 SV *sv = Sv_val (svv);
295 if (SvTYPE (sv) != SVt_RV)
296 invalid_argument ("deref_array: SV is not a reference");
297 switch (SvTYPE (SvRV (sv))) {
301 invalid_argument ("deref_array: SV is not a reference to an array");
303 ravv = Val_av ((AV *) SvRV (sv));
308 perl4caml_deref_hash (value svv)
312 SV *sv = Sv_val (svv);
314 if (SvTYPE (sv) != SVt_RV)
315 invalid_argument ("deref_array: SV is not a reference");
316 switch (SvTYPE (SvRV (sv))) {
320 invalid_argument ("deref_array: SV is not a reference to a hash");
322 rhvv = Val_hv ((HV *) SvRV (sv));
327 perl4caml_av_empty (value unit)
331 CAMLreturn (Val_av (av));
334 /* We don't know in advance how long the list will be, which makes this
338 perl4caml_av_of_sv_list (value svlistv)
340 CAMLparam1 (svlistv);
342 SV *sv, **svlist = 0;
343 int alloc = 0, size = 0;
346 for (; svlistv != Val_int (0); svlistv = Field (svlistv, 1))
348 svv = Field (svlistv, 0);
351 alloc = alloc == 0 ? 1 : alloc * 2;
352 svlist = realloc (svlist, alloc * sizeof (SV *));
357 av = av_make (size, svlist);
359 if (alloc > 0) free (svlist); /* Free memory allocated to SV list. */
361 CAMLreturn (Val_av (av));
364 /* XXX av_map would be faster if we also had sv_list_of_av. */
367 perl4caml_av_push (value avv, value svv)
369 CAMLparam2 (avv, svv);
370 AV *av = Av_val (avv);
371 SV *sv = Sv_val (svv);
373 CAMLreturn (Val_unit);
377 perl4caml_av_pop (value avv)
380 AV *av = Av_val (avv);
381 SV *sv = av_pop (av);
382 CAMLreturn (Val_sv (sv));
386 perl4caml_av_unshift (value avv, value svv)
388 CAMLparam2 (avv, svv);
389 AV *av = Av_val (avv);
390 SV *sv = Sv_val (svv);
393 if (av_store (av, 0, sv) == 0)
395 CAMLreturn (Val_unit);
399 perl4caml_av_shift (value avv)
402 AV *av = Av_val (avv);
403 SV *sv = av_shift (av);
404 CAMLreturn (Val_sv (sv));
408 perl4caml_av_length (value avv)
411 AV *av = Av_val (avv);
412 CAMLreturn (Val_int (av_len (av) + 1));
416 perl4caml_av_set (value avv, value i, value svv)
418 CAMLparam3 (avv, i, svv);
419 AV *av = Av_val (avv);
420 SV *sv = Sv_val (svv);
422 if (av_store (av, Int_val (i), sv) == 0)
424 CAMLreturn (Val_unit);
428 perl4caml_av_get (value avv, value i)
431 AV *av = Av_val (avv);
432 SV **svp = av_fetch (av, Int_val (i), 0);
433 if (svp == 0) invalid_argument ("av_get: index out of bounds");
434 CAMLreturn (Val_sv (*svp));
438 perl4caml_av_clear (value avv)
441 AV *av = Av_val (avv);
443 CAMLreturn (Val_unit);
447 perl4caml_av_undef (value avv)
450 AV *av = Av_val (avv);
452 CAMLreturn (Val_unit);
456 perl4caml_av_extend (value avv, value i)
459 AV *av = Av_val (avv);
460 av_extend (av, Int_val (i));
461 CAMLreturn (Val_unit);
465 perl4caml_hv_empty (value unit)
469 CAMLreturn (Val_hv (hv));
473 perl4caml_hv_set (value hvv, value key, value svv)
475 CAMLparam3 (hvv, key, svv);
476 HV *hv = Hv_val (hvv);
477 SV *sv = Sv_val (svv);
479 if (hv_store (hv, String_val (key), string_length (key), sv, 0) == 0)
481 CAMLreturn (Val_unit);
485 perl4caml_hv_get (value hvv, value key)
487 CAMLparam2 (hvv, key);
488 HV *hv = Hv_val (hvv);
489 SV **svp = hv_fetch (hv, String_val (key), string_length (key), 0);
490 if (svp == 0) raise_not_found ();
491 CAMLreturn (Val_sv (*svp));
495 perl4caml_hv_exists (value hvv, value key)
497 CAMLparam2 (hvv, key);
498 HV *hv = Hv_val (hvv);
499 bool r = hv_exists (hv, String_val (key), string_length (key));
500 CAMLreturn (r ? Val_true : Val_false);
504 perl4caml_hv_delete (value hvv, value key)
506 CAMLparam2 (hvv, key);
507 HV *hv = Hv_val (hvv);
508 hv_delete (hv, String_val (key), string_length (key), G_DISCARD);
509 CAMLreturn (Val_unit);
513 perl4caml_hv_clear (value hvv)
516 HV *hv = Hv_val (hvv);
518 CAMLreturn (Val_unit);
522 perl4caml_hv_undef (value hvv)
525 HV *hv = Hv_val (hvv);
527 CAMLreturn (Val_unit);
531 perl4caml_get_sv (value optcreate, value name)
533 CAMLparam2 (optcreate, name);
537 create = unoption (optcreate, Val_false);
538 sv = get_sv (String_val (name), create == Val_true ? TRUE : FALSE);
539 if (sv == NULL) raise_not_found ();
541 CAMLreturn (Val_sv (sv));
545 perl4caml_get_av (value optcreate, value name)
547 CAMLparam2 (optcreate, name);
551 create = unoption (optcreate, Val_false);
552 av = get_av (String_val (name), create == Val_true ? TRUE : FALSE);
553 if (av == NULL) raise_not_found ();
555 CAMLreturn (Val_av (av));
559 perl4caml_get_hv (value optcreate, value name)
561 CAMLparam2 (optcreate, name);
565 create = unoption (optcreate, Val_false);
566 hv = get_hv (String_val (name), create == Val_true ? TRUE : FALSE);
567 if (hv == NULL) raise_not_found ();
569 CAMLreturn (Val_hv (hv));
573 check_perl_failure ()
575 SV *errsv = get_sv ("@", TRUE);
577 if (SvTRUE (errsv)) /* Equivalent of $@ in Perl. */
581 const char *err = SvPV (errsv, n_a);
583 errv = copy_string (err);
585 raise_with_arg (*caml_named_value ("perl4caml_perl_failure"), errv);
590 perl4caml_call (value optsv, value optfnname, value arglist)
592 CAMLparam3 (optsv, optfnname, arglist);
596 CAMLlocal3 (errv, svv, fnname);
601 /* Push the parameter list. */
604 /* Iteration over the linked list. */
605 for (; arglist != Val_int (0); arglist = Field (arglist, 1))
607 svv = Field (arglist, 0);
609 XPUSHs (sv_2mortal (newSVsv (sv)));
614 if (optsv != Val_int (0))
616 svv = unoption (optsv, Val_false);
618 count = call_sv (sv, G_EVAL|G_SCALAR);
620 else if (optfnname != Val_int (0))
622 fnname = unoption (optfnname, Val_false);
623 count = call_pv (String_val (fnname), G_EVAL|G_SCALAR);
628 "Perl.call: must supply either 'sv' or 'fn' parameters.");
634 assert (count == 1); /* Pretty sure it should never be anything else. */
636 /* Pop return value off the stack. Note that the return value on the
637 * stack is mortal, so we need to take a copy.
644 check_perl_failure ();
651 perl4caml_call_array (value optsv, value optfnname, value arglist)
653 CAMLparam3 (optsv, optfnname, arglist);
657 CAMLlocal5 (errv, svv, fnname, list, cons);
662 /* Push the parameter list. */
665 /* Iteration over the linked list. */
666 for (; arglist != Val_int (0); arglist = Field (arglist, 1))
668 svv = Field (arglist, 0);
670 XPUSHs (sv_2mortal (newSVsv (sv)));
675 if (optsv != Val_int (0))
677 svv = unoption (optsv, Val_false);
679 count = call_sv (sv, G_EVAL|G_ARRAY);
681 else if (optfnname != Val_int (0))
683 fnname = unoption (optfnname, Val_false);
684 count = call_pv (String_val (fnname), G_EVAL|G_ARRAY);
689 "Perl.call_array: must supply either 'sv' or 'fn' parameters.");
695 /* Pop all the return values off the stack into a list. Values on the
696 * stack are mortal, so we must copy them.
699 for (i = 0; i < count; ++i) {
701 Field (cons, 1) = list;
703 Field (cons, 0) = Val_sv (newSVsv (POPs));
706 /* Restore the stack. */
711 check_perl_failure ();
717 perl4caml_call_void (value optsv, value optfnname, value arglist)
719 CAMLparam3 (optsv, optfnname, arglist);
723 CAMLlocal3 (errv, svv, fnname);
728 /* Push the parameter list. */
731 /* Iteration over the linked list. */
732 for (; arglist != Val_int (0); arglist = Field (arglist, 1))
734 svv = Field (arglist, 0);
736 XPUSHs (sv_2mortal (newSVsv (sv)));
741 if (optsv != Val_int (0))
743 svv = unoption (optsv, Val_false);
745 count = call_sv (sv, G_EVAL|G_VOID);
747 else if (optfnname != Val_int (0))
749 fnname = unoption (optfnname, Val_false);
750 count = call_pv (String_val (fnname), G_EVAL|G_VOID|G_DISCARD);
755 "Perl.call_void: must supply either 'sv' or 'fn' parameters.");
763 /* Restore the stack. */
768 check_perl_failure ();
770 CAMLreturn (Val_unit);
774 perl4caml_eval (value expr)
779 CAMLlocal2 (errv, svv);
781 sv = eval_pv (String_val (expr), G_SCALAR);
783 check_perl_failure ();
790 perl4caml_call_method (value ref, value name, value arglist)
792 CAMLparam3 (ref, name, arglist);
796 CAMLlocal2 (errv, svv);
801 /* Push the parameter list. */
805 XPUSHs (sv_2mortal (newSVsv (sv)));
807 /* Iteration over the linked list. */
808 for (; arglist != Val_int (0); arglist = Field (arglist, 1))
810 svv = Field (arglist, 0);
812 XPUSHs (sv_2mortal (newSVsv (sv)));
817 count = call_method (String_val (name), G_EVAL|G_SCALAR);
821 assert (count == 1); /* Pretty sure it should never be anything else. */
823 /* Pop return value off the stack. Note that the return value on the
824 * stack is mortal, so we need to take a copy.
831 check_perl_failure ();
838 perl4caml_call_method_array (value ref, value name, value arglist)
840 CAMLparam3 (ref, name, arglist);
844 CAMLlocal4 (errv, svv, list, cons);
849 /* Push the parameter list. */
853 XPUSHs (sv_2mortal (newSVsv (sv)));
855 /* Iteration over the linked list. */
856 for (; arglist != Val_int (0); arglist = Field (arglist, 1))
858 svv = Field (arglist, 0);
860 XPUSHs (sv_2mortal (newSVsv (sv)));
865 count = call_method (String_val (name), G_EVAL|G_ARRAY);
869 /* Pop all return values off the stack. Note that the return values on the
870 * stack are mortal, so we need to take a copy.
873 for (i = 0; i < count; ++i) {
875 Field (cons, 1) = list;
877 Field (cons, 0) = Val_sv (newSVsv (POPs));
880 /* Restore the stack. */
885 check_perl_failure ();
891 perl4caml_call_method_void (value ref, value name, value arglist)
893 CAMLparam3 (ref, name, arglist);
897 CAMLlocal2 (errv, svv);
902 /* Push the parameter list. */
906 XPUSHs (sv_2mortal (newSVsv (sv)));
908 /* Iteration over the linked list. */
909 for (; arglist != Val_int (0); arglist = Field (arglist, 1))
911 svv = Field (arglist, 0);
913 XPUSHs (sv_2mortal (newSVsv (sv)));
918 count = call_method (String_val (name), G_EVAL|G_VOID|G_DISCARD);
924 /* Restore the stack. */
929 check_perl_failure ();
931 CAMLreturn (Val_unit);
935 perl4caml_call_class_method (value classname, value name, value arglist)
937 CAMLparam3 (classname, name, arglist);
941 CAMLlocal2 (errv, svv);
946 /* Push the parameter list. */
949 XPUSHs (sv_2mortal (newSVpv (String_val (classname), 0)));
951 /* Iteration over the linked list. */
952 for (; arglist != Val_int (0); arglist = Field (arglist, 1))
954 svv = Field (arglist, 0);
956 XPUSHs (sv_2mortal (newSVsv (sv)));
961 count = call_method (String_val (name), G_EVAL|G_SCALAR);
965 assert (count == 1); /* Pretty sure it should never be anything else. */
967 /* Pop return value off the stack. Note that the return value on the
968 * stack is mortal, so we need to take a copy.
975 check_perl_failure ();
982 perl4caml_call_class_method_array (value classname, value name, value arglist)
984 CAMLparam3 (classname, name, arglist);
988 CAMLlocal4 (errv, svv, list, cons);
993 /* Push the parameter list. */
996 XPUSHs (sv_2mortal (newSVpv (String_val (classname), 0)));
998 /* Iteration over the linked list. */
999 for (; arglist != Val_int (0); arglist = Field (arglist, 1))
1001 svv = Field (arglist, 0);
1003 XPUSHs (sv_2mortal (newSVsv (sv)));
1008 count = call_method (String_val (name), G_EVAL|G_ARRAY);
1012 /* Pop all return values off the stack. Note that the return values on the
1013 * stack are mortal, so we need to take a copy.
1016 for (i = 0; i < count; ++i) {
1017 cons = alloc (2, 0);
1018 Field (cons, 1) = list;
1020 Field (cons, 0) = Val_sv (newSVsv (POPs));
1023 /* Restore the stack. */
1028 check_perl_failure ();
1034 perl4caml_call_class_method_void (value classname, value name, value arglist)
1036 CAMLparam3 (classname, name, arglist);
1040 CAMLlocal2 (errv, svv);
1045 /* Push the parameter list. */
1048 XPUSHs (sv_2mortal (newSVpv (String_val (classname), 0)));
1050 /* Iteration over the linked list. */
1051 for (; arglist != Val_int (0); arglist = Field (arglist, 1))
1053 svv = Field (arglist, 0);
1055 XPUSHs (sv_2mortal (newSVsv (sv)));
1060 count = call_method (String_val (name), G_EVAL|G_VOID|G_DISCARD);
1064 assert (count == 0);
1066 /* Restore the stack. */
1071 check_perl_failure ();
1073 CAMLreturn (Val_unit);
1077 Val_voidptr (void *ptr)
1079 value rv = alloc (1, Abstract_tag); /* XXX Is this correct? */
1080 Field(rv, 0) = (value) ptr;
1085 unoption (value option, value deflt)
1087 if (option == Val_int (0)) /* "None" */
1089 else /* "Some 'a" */
1090 return Field (option, 0);