1 /* Interface to Perl from OCaml.
2 * Copyright (C) 2003 Merjis Ltd.
3 * $Id: perl_c.c,v 1.4 2003-10-12 17:33:14 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)))
45 perl4caml_init (value unit)
47 static char *argv[] = { "", "-w", "-e", "0" };
48 int argc = sizeof argv / sizeof argv[0];
50 PERL_SYS_INIT3 (NULL, NULL, NULL);
52 /* Create a default interpreter. */
53 my_perl = perl_alloc ();
54 perl_construct (my_perl);
55 PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
56 perl_parse (my_perl, NULL, argc, argv, NULL);
57 /*perl_run (my_perl);*/
63 perl4caml_create (value optargs, value unit)
65 CAMLparam2 (optargs, unit);
69 static char *no_args[] = { "", "-e", "0" };
71 /* Arguments given? */
72 if (optargs == Val_int (0)) /* "None" */
77 else /* "Some args" where args is a string array. */
79 args = Field (optargs, 0);
80 argc = Wosize_val (args);
81 argv = alloca (argc * sizeof (char *));
82 for (i = 0; i < argc; ++i) argv[i] = String_val (Field (args, i));
85 my_perl = perl_alloc ();
86 perl_construct (my_perl);
87 PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
88 perl_parse (my_perl, NULL, argc, argv, NULL);
89 /*perl_run (my_perl);*/
91 CAMLreturn (Val_perl (my_perl));
95 perl4caml_destroy (value unit)
99 perl_destruct (my_perl);
102 /* Force a segfault if someone tries to use a Perl function without
103 * creating another interpreter first.
107 CAMLreturn (Val_unit);
111 perl4caml_set_context (value plv)
114 PerlInterpreter *pl = Perl_val (plv);
116 PERL_SET_CONTEXT (pl);
119 CAMLreturn (Val_unit);
123 perl4caml_int_of_sv (value svv)
126 SV *sv = Sv_val (svv);
127 CAMLreturn (Val_int (SvIV (sv)));
131 perl4caml_sv_of_int (value iv)
134 CAMLreturn (Val_sv (newSViv (Int_val (iv))));
138 perl4caml_float_of_sv (value svv)
141 SV *sv = Sv_val (svv);
143 f = copy_double (SvNV (sv));
148 perl4caml_sv_of_float (value fv)
151 CAMLreturn (Val_sv (newSViv (Double_val (fv))));
155 perl4caml_string_of_sv (value svv)
158 SV *sv = Sv_val (svv);
162 str = SvPV (sv, len);
163 /* XXX This won't work if the string contains NUL. */
164 strv = copy_string (str);
169 perl4caml_sv_of_string (value strv)
172 CAMLreturn (Val_sv (newSVpv (String_val (strv), string_length (strv))));
176 perl4caml_sv_is_true (value svv)
179 SV *sv = Sv_val (svv);
180 CAMLreturn (SvTRUE (sv) ? Val_true : Val_false);
184 perl4caml_sv_is_undef (value svv)
187 SV *sv = Sv_val (svv);
188 CAMLreturn (sv == &PL_sv_undef ? Val_true : Val_false);
192 perl4caml_sv_undef (value unit)
195 CAMLreturn (Val_sv (&PL_sv_undef));
199 perl4caml_sv_yes (value unit)
202 CAMLreturn (Val_sv (&PL_sv_yes));
206 perl4caml_sv_no (value unit)
209 CAMLreturn (Val_sv (&PL_sv_no));
213 perl4caml_sv_type (value svv)
216 SV *sv = Sv_val (svv);
220 case SVt_IV: CAMLreturn (Val_int (1));
221 case SVt_NV: CAMLreturn (Val_int (2));
222 case SVt_PV: CAMLreturn (Val_int (3));
223 case SVt_RV: CAMLreturn (Val_int (4));
224 case SVt_PVAV: CAMLreturn (Val_int (5));
225 case SVt_PVHV: CAMLreturn (Val_int (6));
226 case SVt_PVCV: CAMLreturn (Val_int (7));
227 case SVt_PVGV: CAMLreturn (Val_int (8));
228 case SVt_PVMG: CAMLreturn (Val_int (9));
229 default: CAMLreturn (Val_int (0));
234 perl4caml_deref (value svv)
238 SV *sv = Sv_val (svv);
240 if (SvTYPE (sv) != SVt_RV)
241 invalid_argument ("deref: SV is not a reference");
242 switch (SvTYPE (SvRV (sv))) {
250 invalid_argument ("deref: SV is not a reference to a scalar");
252 rsvv = Val_sv (SvRV (sv));
257 perl4caml_get_sv (value optcreate, value name)
259 CAMLparam2 (optcreate, name);
263 create = unoption (optcreate, Val_false);
264 sv = get_sv (String_val (name), create == Val_true ? TRUE : FALSE);
265 if (sv == NULL) raise_not_found ();
267 CAMLreturn (Val_sv (sv));
271 check_perl_failure ()
273 SV *errsv = get_sv ("@", TRUE);
275 if (SvTRUE (errsv)) /* Equivalent of $@ in Perl. */
279 const char *err = SvPV (errsv, n_a);
281 errv = copy_string (err);
283 raise_with_arg (*caml_named_value ("perl4caml_perl_failure"), errv);
288 perl4caml_call (value optsv, value optfnname, value arglist)
290 CAMLparam3 (optsv, optfnname, arglist);
294 CAMLlocal3 (errv, svv, fnname);
299 /* Push the parameter list. */
302 /* Iteration over the linked list. */
303 for (; arglist != Val_int (0); arglist = Field (arglist, 1))
305 svv = Field (arglist, 0);
307 XPUSHs (sv_2mortal (newSVsv (sv)));
312 if (optsv != Val_int (0))
314 svv = unoption (optsv, Val_false);
316 count = call_sv (sv, G_EVAL|G_SCALAR);
318 else if (optfnname != Val_int (0))
320 fnname = unoption (optfnname, Val_false);
321 count = call_pv (String_val (fnname), G_EVAL|G_SCALAR);
326 "Perl.call: must supply either 'sv' or 'fn' parameters.");
332 assert (count == 1); /* Pretty sure it should never be anything else. */
334 /* Pop return value off the stack. Note that the return value on the
335 * stack is mortal, so we need to take a copy.
342 check_perl_failure ();
349 perl4caml_call_array (value optsv, value optfnname, value arglist)
351 CAMLparam3 (optsv, optfnname, arglist);
355 CAMLlocal5 (errv, svv, fnname, list, cons);
360 /* Push the parameter list. */
363 /* Iteration over the linked list. */
364 for (; arglist != Val_int (0); arglist = Field (arglist, 1))
366 svv = Field (arglist, 0);
368 XPUSHs (sv_2mortal (newSVsv (sv)));
373 if (optsv != Val_int (0))
375 svv = unoption (optsv, Val_false);
377 count = call_sv (sv, G_EVAL|G_ARRAY);
379 else if (optfnname != Val_int (0))
381 fnname = unoption (optfnname, Val_false);
382 count = call_pv (String_val (fnname), G_EVAL|G_ARRAY);
387 "Perl.call_array: must supply either 'sv' or 'fn' parameters.");
393 /* Pop all the return values off the stack into a list. Values on the
394 * stack are mortal, so we must copy them.
397 for (i = 0; i < count; ++i) {
399 Field (cons, 1) = list;
401 Field (cons, 0) = Val_sv (newSVsv (POPs));
404 /* Restore the stack. */
409 check_perl_failure ();
415 perl4caml_call_void (value optsv, value optfnname, value arglist)
417 CAMLparam3 (optsv, optfnname, arglist);
421 CAMLlocal3 (errv, svv, fnname);
426 /* Push the parameter list. */
429 /* Iteration over the linked list. */
430 for (; arglist != Val_int (0); arglist = Field (arglist, 1))
432 svv = Field (arglist, 0);
434 XPUSHs (sv_2mortal (newSVsv (sv)));
439 if (optsv != Val_int (0))
441 svv = unoption (optsv, Val_false);
443 count = call_sv (sv, G_EVAL|G_VOID);
445 else if (optfnname != Val_int (0))
447 fnname = unoption (optfnname, Val_false);
448 count = call_pv (String_val (fnname), G_EVAL|G_VOID);
453 "Perl.call_void: must supply either 'sv' or 'fn' parameters.");
459 assert (count == 0); /* Pretty sure it should never be anything else. */
461 /* Restore the stack. */
466 check_perl_failure ();
468 CAMLreturn (Val_unit);
472 perl4caml_eval (value expr)
477 CAMLlocal2 (errv, svv);
479 sv = eval_pv (String_val (expr), G_SCALAR);
481 check_perl_failure ();
488 perl4caml_call_method (value ref, value name, value arglist)
490 CAMLparam3 (ref, name, arglist);
494 CAMLlocal2 (errv, svv);
499 /* Push the parameter list. */
503 XPUSHs (sv_2mortal (newSVsv (sv)));
505 /* Iteration over the linked list. */
506 for (; arglist != Val_int (0); arglist = Field (arglist, 1))
508 svv = Field (arglist, 0);
510 XPUSHs (sv_2mortal (newSVsv (sv)));
515 count = call_method (String_val (name), G_EVAL|G_SCALAR);
519 assert (count == 1); /* Pretty sure it should never be anything else. */
521 /* Pop return value off the stack. Note that the return value on the
522 * stack is mortal, so we need to take a copy.
529 check_perl_failure ();
536 perl4caml_call_method_array (value ref, value name, value arglist)
538 CAMLparam3 (ref, name, arglist);
542 CAMLlocal4 (errv, svv, list, cons);
547 /* Push the parameter list. */
551 XPUSHs (sv_2mortal (newSVsv (sv)));
553 /* Iteration over the linked list. */
554 for (; arglist != Val_int (0); arglist = Field (arglist, 1))
556 svv = Field (arglist, 0);
558 XPUSHs (sv_2mortal (newSVsv (sv)));
563 count = call_method (String_val (name), G_EVAL|G_ARRAY);
567 /* Pop all return values off the stack. Note that the return values on the
568 * stack are mortal, so we need to take a copy.
571 for (i = 0; i < count; ++i) {
573 Field (cons, 1) = list;
575 Field (cons, 0) = Val_sv (newSVsv (POPs));
578 /* Restore the stack. */
583 check_perl_failure ();
589 perl4caml_call_method_void (value ref, value name, value arglist)
591 CAMLparam3 (ref, name, arglist);
595 CAMLlocal2 (errv, svv);
600 /* Push the parameter list. */
604 XPUSHs (sv_2mortal (newSVsv (sv)));
606 /* Iteration over the linked list. */
607 for (; arglist != Val_int (0); arglist = Field (arglist, 1))
609 svv = Field (arglist, 0);
611 XPUSHs (sv_2mortal (newSVsv (sv)));
616 count = call_method (String_val (name), G_EVAL|G_VOID);
620 assert (count == 0); /* Pretty sure it should never be anything else. */
622 /* Restore the stack. */
627 check_perl_failure ();
629 CAMLreturn (Val_unit);
633 perl4caml_call_class_method (value classname, value name, value arglist)
635 CAMLparam3 (classname, name, arglist);
639 CAMLlocal2 (errv, svv);
644 /* Push the parameter list. */
647 XPUSHs (sv_2mortal (newSVpv (String_val (classname), 0)));
649 /* Iteration over the linked list. */
650 for (; arglist != Val_int (0); arglist = Field (arglist, 1))
652 svv = Field (arglist, 0);
654 XPUSHs (sv_2mortal (newSVsv (sv)));
659 count = call_method (String_val (name), G_EVAL|G_SCALAR);
663 assert (count == 1); /* Pretty sure it should never be anything else. */
665 /* Pop return value off the stack. Note that the return value on the
666 * stack is mortal, so we need to take a copy.
673 check_perl_failure ();
680 perl4caml_call_class_method_array (value classname, value name, value arglist)
682 CAMLparam3 (classname, name, arglist);
686 CAMLlocal4 (errv, svv, list, cons);
691 /* Push the parameter list. */
694 XPUSHs (sv_2mortal (newSVpv (String_val (classname), 0)));
696 /* Iteration over the linked list. */
697 for (; arglist != Val_int (0); arglist = Field (arglist, 1))
699 svv = Field (arglist, 0);
701 XPUSHs (sv_2mortal (newSVsv (sv)));
706 count = call_method (String_val (name), G_EVAL|G_ARRAY);
710 /* Pop all return values off the stack. Note that the return values on the
711 * stack are mortal, so we need to take a copy.
714 for (i = 0; i < count; ++i) {
716 Field (cons, 1) = list;
718 Field (cons, 0) = Val_sv (newSVsv (POPs));
721 /* Restore the stack. */
726 check_perl_failure ();
732 perl4caml_call_class_method_void (value classname, value name, value arglist)
734 CAMLparam3 (classname, name, arglist);
738 CAMLlocal2 (errv, svv);
743 /* Push the parameter list. */
746 XPUSHs (sv_2mortal (newSVpv (String_val (classname), 0)));
748 /* Iteration over the linked list. */
749 for (; arglist != Val_int (0); arglist = Field (arglist, 1))
751 svv = Field (arglist, 0);
753 XPUSHs (sv_2mortal (newSVsv (sv)));
758 count = call_method (String_val (name), G_EVAL|G_VOID);
762 assert (count == 0); /* Pretty sure it should never be anything else. */
764 /* Restore the stack. */
769 check_perl_failure ();
771 CAMLreturn (Val_unit);
775 Val_voidptr (void *ptr)
777 value rv = alloc (1, Abstract_tag); /* XXX Is this correct? */
778 Field(rv, 0) = (value) ptr;
783 unoption (value option, value deflt)
785 if (option == Val_int (0)) /* "None" */
788 return Field (option, 0);