BrowserUk has asked for the wisdom of the Perl Monks concerning the following question:

Is it possible (to use vec from XS code)?

If yes, does anyone have any examples?


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
"Too many [] have been sedated by an oppressive environment of political correctness and risk aversion."

Replies are listed 'Best First'.
Re: Using vec() from XS
by salva (Canon) on Sep 23, 2008 at 13:48 UTC
    Some ways to do it:
    • Write a wrapper function in Perl and call it.
    • There should be a way to create a sequence of OPs (containing the required pp_vec) and run it.
    • Copy & paste the source code for pp_vec and its helpers into your code changing the interface to suit your needs.
        I don't know the details, but it should be something like this:
        1. synthesize an OP o for vec
        2. set up the runtime environment: push the arguments on the stack, save PL_op and make it point to the synthesized OP o, set any other interpreter globals, stacks, etc.
        3. call CALLRUNOPS
        4. retrieve the result from o->op_targ
        5. clean up
        For and example, look at Perl_fold_constants from op.c from the perl source. Try running it inside a C debugger with a simple fold-able expression as print(1+2).

        The C function Perl_op_dump can be used to dump an OP tree once it has been properly build (otherwise it will crash).

        Most likely, you already know that Perl is compiled to a tree of ops. pp_vec is one such op type -- corresponding to the Perl vec() function.

        Now, you could create op structures from XS and inject them into the running program. B::Generate does stuff like that.

        The one example I'm aware of where this actually works well is List::Util::shuffle. Instead of repeating what's happening there, check for yourself: List::Util XS code.

        I tried to do something similar in order to get caller() in XS, but that experiment mostly failed. I would not suggest going down that route.

        Cheers,
        Steffen

Re: Using vec() from XS
by ysth (Canon) on Sep 23, 2008 at 15:37 UTC

      I'm not sure how that helps? vec doesn't do any inserts, just update in-place or grow if the offest calculates beyond the current end of string.

      It isn't too hard to re-code the math and bit-twiddling, the main problem is getting the memory management right. As vec does everything I need (and more), it would have been nice to find a way to re-use that code without having to either c&p or callback to perl in order to do it.


      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.