> Date: Wed, 27 Nov 2024 16:01:59 +0100 > From: pertu...@free.fr > Cc: Gavin Smith <gavinsmith0...@gmail.com>, bug-texinfo@gnu.org > > > Can you please explain what you mean by "calling back into Perl"? > > What kind of call is made from the C code of an XS, and what functions > > in Perl does it call? > > It is possible to call any Perl function from C code using the Perl C > API and having a Perl interpreter available. It is described here: > https://perldoc.perl.org/perlcall > > The precise code is in tp/Texinfo/XS/main/call_perl_function.c, function > call_translations_translate_string. There is some code to setup the > Perl stack, convert C to SV * Perl objects and push on the Perl stack: > > PUSHs(sv_2mortal (newSVpv_utf8 (string, 0))); > PUSHs(sv_2mortal (newSVpv_utf8 (in_lang, 0))); > PUSHs(sv_2mortal (newSVpv_utf8 (translation_context, 0))); > > the call to the Perl function: > count = call_pv ( > "Texinfo::Translations::translate_string", > G_SCALAR); > > and getting the result from the return of the Perl function: > result_sv = POPs; > result_ret = SvPVutf8 (result_sv, len);
OK, thanks. This should work, if we avoid the cases where the caller allocates memory that Perl is supposed to free, or the other way around. That's because each side is most probably using a different implementation of malloc/free. > > And also, do you have any guesses for why calling gettext from XS > > doesn't work? > > My guess is that it is related to the switching of locale using calls to > getenv and searching for an existing locale to use the @documentlanguage, > as it has always been very fragile and prone to fail, both in pure Perl and > in C. It could also fail because this local switching is done from C > called from Perl, we had trouble with that previously and needed to call > some Perl functions related to threads to make things work, while it > worked well from C only. If the issue is changing the locale, then it isn't gettext per se, it's the locale change itself. E.g., switching locales on Windows needs to use Windows locale identifiers, which are similar but different from Posix ones. I don't know if Cygwin has some feature whereby it transparently maps one to the other, but if not, that might be the problem. > There is something going on that makes the C code fail to find the > translated strings, the workaround consist in calling the pure Perl > libintl-perl Gettext implementation from C through the mechanism > explained above to retrieve the translated string. So, we actually > avoid calling the C gettext to get the output we want. > > I think that it would be better to investigate why calling C gettext > from C code does not work, but it requires direct access to the > platforms. I suggest to ask Bruno Haible to help us out. He definitely knows more than enough about gettext and the possible issues with it on unusual systems, like Windows, including Cygwin.