1 /* Interface to Perl from OCaml.
2 * Copyright (C) 2003 Merjis Ltd.
3 * $Id: perl_c.c,v 1.6 2003-10-15 16:51:12 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)))
47 perl4caml_init (value unit)
50 PERL_SYS_INIT3 (NULL, NULL, NULL);
55 perl4caml_current_interpreter (value unit)
58 if (my_perl == 0) raise_not_found ();
59 return Val_perl (my_perl);
65 char *file = __FILE__;
66 EXTERN_C void boot_DynaLoader (pTHX_ CV* cv);
68 newXS("DynaLoader::boot_DynaLoader", boot_DynaLoader, file);
72 perl4caml_create (value optargs, value unit)
74 CAMLparam2 (optargs, unit);
78 static char *no_args[] = { "", "-w", "-e", "0" };
80 /* Arguments given? */
81 if (optargs == Val_int (0)) /* "None" */
86 else /* "Some args" where args is a string array. */
88 args = Field (optargs, 0);
89 argc = Wosize_val (args);
90 argv = alloca (argc * sizeof (char *));
91 for (i = 0; i < argc; ++i) argv[i] = String_val (Field (args, i));
94 my_perl = perl_alloc ();
95 perl_construct (my_perl);
96 PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
97 perl_parse (my_perl, xs_init, argc, argv, NULL);
98 /*perl_run (my_perl);*/
100 CAMLreturn (Val_perl (my_perl));
104 perl4caml_destroy (value plv)
107 PerlInterpreter *pl = Perl_val (plv);
112 /* Current interpreter? */
113 if (my_perl == pl) my_perl = 0;
115 CAMLreturn (Val_unit);
119 perl4caml_set_context (value plv)
122 PerlInterpreter *pl = Perl_val (plv);
124 PERL_SET_CONTEXT (pl);
127 CAMLreturn (Val_unit);
131 perl4caml_int_of_sv (value svv)
134 SV *sv = Sv_val (svv);
135 CAMLreturn (Val_int (SvIV (sv)));
139 perl4caml_sv_of_int (value iv)
142 CAMLreturn (Val_sv (newSViv (Int_val (iv))));
146 perl4caml_float_of_sv (value svv)
149 SV *sv = Sv_val (svv);
151 f = copy_double (SvNV (sv));
156 perl4caml_sv_of_float (value fv)
159 CAMLreturn (Val_sv (newSViv (Double_val (fv))));
163 perl4caml_string_of_sv (value svv)
166 SV *sv = Sv_val (svv);
170 str = SvPV (sv, len);
171 /* XXX This won't work if the string contains NUL. */
172 strv = copy_string (str);
177 perl4caml_sv_of_string (value strv)
180 CAMLreturn (Val_sv (newSVpv (String_val (strv), string_length (strv))));
184 perl4caml_sv_is_true (value svv)
187 SV *sv = Sv_val (svv);
188 CAMLreturn (SvTRUE (sv) ? Val_true : Val_false);
192 perl4caml_sv_is_undef (value svv)
195 SV *sv = Sv_val (svv);
196 CAMLreturn (sv == &PL_sv_undef ? Val_true : Val_false);
200 perl4caml_sv_undef (value unit)
203 CAMLreturn (Val_sv (&PL_sv_undef));
207 perl4caml_sv_yes (value unit)
210 CAMLreturn (Val_sv (&PL_sv_yes));
214 perl4caml_sv_no (value unit)
217 CAMLreturn (Val_sv (&PL_sv_no));
221 perl4caml_sv_type (value svv)
224 SV *sv = Sv_val (svv);
228 case SVt_IV: CAMLreturn (Val_int (1));
229 case SVt_NV: CAMLreturn (Val_int (2));
230 case SVt_PV: CAMLreturn (Val_int (3));
231 case SVt_RV: CAMLreturn (Val_int (4));
232 case SVt_PVAV: CAMLreturn (Val_int (5));
233 case SVt_PVHV: CAMLreturn (Val_int (6));
234 case SVt_PVCV: CAMLreturn (Val_int (7));
235 case SVt_PVGV: CAMLreturn (Val_int (8));
236 case SVt_PVMG: CAMLreturn (Val_int (9));
237 default: CAMLreturn (Val_int (0));
242 perl4caml_deref (value svv)
246 SV *sv = Sv_val (svv);
248 if (SvTYPE (sv) != SVt_RV)
249 invalid_argument ("deref: SV is not a reference");
250 switch (SvTYPE (SvRV (sv))) {
258 invalid_argument ("deref: SV is not a reference to a scalar");
260 rsvv = Val_sv (SvRV (sv));
265 perl4caml_deref_array (value svv)
269 SV *sv = Sv_val (svv);
271 if (SvTYPE (sv) != SVt_RV)
272 invalid_argument ("deref_array: SV is not a reference");
273 switch (SvTYPE (SvRV (sv))) {
277 invalid_argument ("deref_array: SV is not a reference to an array");
279 ravv = Val_av ((AV *) SvRV (sv));
284 perl4caml_av_empty (value unit)
288 CAMLreturn (Val_av (av));
291 /* We don't know in advance how long the list will be, which makes this
295 perl4caml_av_of_sv_list (value svlistv)
297 CAMLparam1 (svlistv);
299 SV *sv, **svlist = 0;
300 int alloc = 0, size = 0;
303 for (; svlistv != Val_int (0); svlistv = Field (svlistv, 1))
305 svv = Field (svlistv, 0);
308 alloc = alloc == 0 ? 1 : alloc * 2;
309 svlist = realloc (svlist, alloc * sizeof (SV *));
314 av = av_make (size, svlist);
316 if (alloc > 0) free (svlist); /* Free memory allocated to SV list. */
318 CAMLreturn (Val_av (av));
321 /* XXX av_map would be faster if we also had sv_list_of_av. */
324 perl4caml_av_push (value avv, value svv)
326 CAMLparam2 (avv, svv);
327 AV *av = Av_val (avv);
328 SV *sv = Sv_val (svv);
330 CAMLreturn (Val_unit);
334 perl4caml_av_pop (value avv)
337 AV *av = Av_val (avv);
338 SV *sv = av_pop (av);
339 CAMLreturn (Val_sv (sv));
343 perl4caml_av_unshift (value avv, value svv)
345 CAMLparam2 (avv, svv);
346 AV *av = Av_val (avv);
347 SV *sv = Sv_val (svv);
350 if (av_store (av, 0, sv) == 0)
352 CAMLreturn (Val_unit);
356 perl4caml_av_shift (value avv)
359 AV *av = Av_val (avv);
360 SV *sv = av_shift (av);
361 CAMLreturn (Val_sv (sv));
365 perl4caml_av_length (value avv)
368 AV *av = Av_val (avv);
369 CAMLreturn (Val_int (av_len (av) + 1));
373 perl4caml_av_set (value avv, value i, value svv)
375 CAMLparam3 (avv, i, svv);
376 AV *av = Av_val (avv);
377 SV *sv = Sv_val (svv);
379 if (av_store (av, Int_val (i), sv) == 0)
381 CAMLreturn (Val_unit);
385 perl4caml_av_get (value avv, value i)
388 AV *av = Av_val (avv);
389 SV **svp = av_fetch (av, Int_val (i), 0);
390 if (svp == 0) invalid_argument ("av_get: index out of bounds");
391 CAMLreturn (Val_sv (*svp));
395 perl4caml_av_clear (value avv)
398 AV *av = Av_val (avv);
400 CAMLreturn (Val_unit);
404 perl4caml_av_undef (value avv)
407 AV *av = Av_val (avv);
409 CAMLreturn (Val_unit);
413 perl4caml_av_extend (value avv, value i)
416 AV *av = Av_val (avv);
417 av_extend (av, Int_val (i));
418 CAMLreturn (Val_unit);
422 perl4caml_get_sv (value optcreate, value name)
424 CAMLparam2 (optcreate, name);
428 create = unoption (optcreate, Val_false);
429 sv = get_sv (String_val (name), create == Val_true ? TRUE : FALSE);
430 if (sv == NULL) raise_not_found ();
432 CAMLreturn (Val_sv (sv));
436 perl4caml_get_av (value optcreate, value name)
438 CAMLparam2 (optcreate, name);
442 create = unoption (optcreate, Val_false);
443 av = get_av (String_val (name), create == Val_true ? TRUE : FALSE);
444 if (av == NULL) raise_not_found ();
446 CAMLreturn (Val_av (av));
450 check_perl_failure ()
452 SV *errsv = get_sv ("@", TRUE);
454 if (SvTRUE (errsv)) /* Equivalent of $@ in Perl. */
458 const char *err = SvPV (errsv, n_a);
460 errv = copy_string (err);
462 raise_with_arg (*caml_named_value ("perl4caml_perl_failure"), errv);
467 perl4caml_call (value optsv, value optfnname, value arglist)
469 CAMLparam3 (optsv, optfnname, arglist);
473 CAMLlocal3 (errv, svv, fnname);
478 /* Push the parameter list. */
481 /* Iteration over the linked list. */
482 for (; arglist != Val_int (0); arglist = Field (arglist, 1))
484 svv = Field (arglist, 0);
486 XPUSHs (sv_2mortal (newSVsv (sv)));
491 if (optsv != Val_int (0))
493 svv = unoption (optsv, Val_false);
495 count = call_sv (sv, G_EVAL|G_SCALAR);
497 else if (optfnname != Val_int (0))
499 fnname = unoption (optfnname, Val_false);
500 count = call_pv (String_val (fnname), G_EVAL|G_SCALAR);
505 "Perl.call: must supply either 'sv' or 'fn' parameters.");
511 assert (count == 1); /* Pretty sure it should never be anything else. */
513 /* Pop return value off the stack. Note that the return value on the
514 * stack is mortal, so we need to take a copy.
521 check_perl_failure ();
528 perl4caml_call_array (value optsv, value optfnname, value arglist)
530 CAMLparam3 (optsv, optfnname, arglist);
534 CAMLlocal5 (errv, svv, fnname, list, cons);
539 /* Push the parameter list. */
542 /* Iteration over the linked list. */
543 for (; arglist != Val_int (0); arglist = Field (arglist, 1))
545 svv = Field (arglist, 0);
547 XPUSHs (sv_2mortal (newSVsv (sv)));
552 if (optsv != Val_int (0))
554 svv = unoption (optsv, Val_false);
556 count = call_sv (sv, G_EVAL|G_ARRAY);
558 else if (optfnname != Val_int (0))
560 fnname = unoption (optfnname, Val_false);
561 count = call_pv (String_val (fnname), G_EVAL|G_ARRAY);
566 "Perl.call_array: must supply either 'sv' or 'fn' parameters.");
572 /* Pop all the return values off the stack into a list. Values on the
573 * stack are mortal, so we must copy them.
576 for (i = 0; i < count; ++i) {
578 Field (cons, 1) = list;
580 Field (cons, 0) = Val_sv (newSVsv (POPs));
583 /* Restore the stack. */
588 check_perl_failure ();
594 perl4caml_call_void (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_VOID);
624 else if (optfnname != Val_int (0))
626 fnname = unoption (optfnname, Val_false);
627 count = call_pv (String_val (fnname), G_EVAL|G_VOID);
632 "Perl.call_void: must supply either 'sv' or 'fn' parameters.");
638 assert (count == 0); /* Pretty sure it should never be anything else. */
640 /* Restore the stack. */
645 check_perl_failure ();
647 CAMLreturn (Val_unit);
651 perl4caml_eval (value expr)
656 CAMLlocal2 (errv, svv);
658 sv = eval_pv (String_val (expr), G_SCALAR);
660 check_perl_failure ();
667 perl4caml_call_method (value ref, value name, value arglist)
669 CAMLparam3 (ref, name, arglist);
673 CAMLlocal2 (errv, svv);
678 /* Push the parameter list. */
682 XPUSHs (sv_2mortal (newSVsv (sv)));
684 /* Iteration over the linked list. */
685 for (; arglist != Val_int (0); arglist = Field (arglist, 1))
687 svv = Field (arglist, 0);
689 XPUSHs (sv_2mortal (newSVsv (sv)));
694 count = call_method (String_val (name), G_EVAL|G_SCALAR);
698 assert (count == 1); /* Pretty sure it should never be anything else. */
700 /* Pop return value off the stack. Note that the return value on the
701 * stack is mortal, so we need to take a copy.
708 check_perl_failure ();
715 perl4caml_call_method_array (value ref, value name, value arglist)
717 CAMLparam3 (ref, name, arglist);
721 CAMLlocal4 (errv, svv, list, cons);
726 /* Push the parameter list. */
730 XPUSHs (sv_2mortal (newSVsv (sv)));
732 /* Iteration over the linked list. */
733 for (; arglist != Val_int (0); arglist = Field (arglist, 1))
735 svv = Field (arglist, 0);
737 XPUSHs (sv_2mortal (newSVsv (sv)));
742 count = call_method (String_val (name), G_EVAL|G_ARRAY);
746 /* Pop all return values off the stack. Note that the return values on the
747 * stack are mortal, so we need to take a copy.
750 for (i = 0; i < count; ++i) {
752 Field (cons, 1) = list;
754 Field (cons, 0) = Val_sv (newSVsv (POPs));
757 /* Restore the stack. */
762 check_perl_failure ();
768 perl4caml_call_method_void (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_VOID);
799 assert (count == 0); /* Pretty sure it should never be anything else. */
801 /* Restore the stack. */
806 check_perl_failure ();
808 CAMLreturn (Val_unit);
812 perl4caml_call_class_method (value classname, value name, value arglist)
814 CAMLparam3 (classname, name, arglist);
818 CAMLlocal2 (errv, svv);
823 /* Push the parameter list. */
826 XPUSHs (sv_2mortal (newSVpv (String_val (classname), 0)));
828 /* Iteration over the linked list. */
829 for (; arglist != Val_int (0); arglist = Field (arglist, 1))
831 svv = Field (arglist, 0);
833 XPUSHs (sv_2mortal (newSVsv (sv)));
838 count = call_method (String_val (name), G_EVAL|G_SCALAR);
842 assert (count == 1); /* Pretty sure it should never be anything else. */
844 /* Pop return value off the stack. Note that the return value on the
845 * stack is mortal, so we need to take a copy.
852 check_perl_failure ();
859 perl4caml_call_class_method_array (value classname, value name, value arglist)
861 CAMLparam3 (classname, name, arglist);
865 CAMLlocal4 (errv, svv, list, cons);
870 /* Push the parameter list. */
873 XPUSHs (sv_2mortal (newSVpv (String_val (classname), 0)));
875 /* Iteration over the linked list. */
876 for (; arglist != Val_int (0); arglist = Field (arglist, 1))
878 svv = Field (arglist, 0);
880 XPUSHs (sv_2mortal (newSVsv (sv)));
885 count = call_method (String_val (name), G_EVAL|G_ARRAY);
889 /* Pop all return values off the stack. Note that the return values on the
890 * stack are mortal, so we need to take a copy.
893 for (i = 0; i < count; ++i) {
895 Field (cons, 1) = list;
897 Field (cons, 0) = Val_sv (newSVsv (POPs));
900 /* Restore the stack. */
905 check_perl_failure ();
911 perl4caml_call_class_method_void (value classname, value name, value arglist)
913 CAMLparam3 (classname, name, arglist);
917 CAMLlocal2 (errv, svv);
922 /* Push the parameter list. */
925 XPUSHs (sv_2mortal (newSVpv (String_val (classname), 0)));
927 /* Iteration over the linked list. */
928 for (; arglist != Val_int (0); arglist = Field (arglist, 1))
930 svv = Field (arglist, 0);
932 XPUSHs (sv_2mortal (newSVsv (sv)));
937 count = call_method (String_val (name), G_EVAL|G_VOID);
941 assert (count == 0); /* Pretty sure it should never be anything else. */
943 /* Restore the stack. */
948 check_perl_failure ();
950 CAMLreturn (Val_unit);
954 Val_voidptr (void *ptr)
956 value rv = alloc (1, Abstract_tag); /* XXX Is this correct? */
957 Field(rv, 0) = (value) ptr;
962 unoption (value option, value deflt)
964 if (option == Val_int (0)) /* "None" */
967 return Field (option, 0);