Can confirm this is still current behaviour with Rakudo 2019. I have came 
across exactly the same issue and found this ticket. I've noticed, If I change 
the example to catch exceptions. It then runs:

    sub start-element($, $elem, $attr)
    {
        say "open $elem".indent($depth * 4);
        say $attr.elems;
        ++$depth;
        CATCH { default { warn "whoops: $_" } }
    }

Produces:

    open calendar
    whoops: Don't know how many elements a C array returned from a library
      in block  at /tmp/tst.p6 line 27
    open advent
    whoops: Don't know how many elements a C array returned from a library
      in block  at /tmp/tst.p6 line 27
...etc

Underlying issue seems to be that any uncaught exception in a NativeCall Perl 
callback currently results in a unfriendly NativeCall MoarVM panic. The 
exception itself is lost.

On Tue, 27 Jun 2017 05:59:08 -0700, vladimir.ma...@oracle.com wrote:
> I took the NativeCall sample from here:
> 
> https://perl6advent.wordpress.com/2015/12/21/day-21-nativecall-backs-
> and-beyond-c/
> 
> --------------------------------------------------------------------------------
> use NativeCall;
> 
> sub XML_SetElementHandler(OpaquePointer $parser,
> &start (OpaquePointer, Str, CArray[Str]),
> &end   (OpaquePointer, Str))
> is native('expat') { ... }
> 
> sub XML_ParserCreate(Str --> OpaquePointer)               is
> native('expat') { ... }
> sub XML_ParserFree(OpaquePointer)                         is
> native('expat') { ... }
> sub XML_Parse(OpaquePointer, Buf, int32, int32 --> int32) is
> native('expat') { ... }
> 
> my $xml = q:to/XML/;
>     <calendar>
>         <advent day="21">
>             <topic title="NativeCall Bits and Pieces"/>
>         </advent>
>     </calendar>
>     XML
> 
> my $depth = 0;
> 
> sub start-element($, $elem, $attr)
> {
>     say "open $elem".indent($depth * 4);
>     ++$depth;
> }
> 
> sub end-element($, $elem)
> {
>     --$depth;
>     say "close $elem".indent($depth * 4);
> }
> 
> my $parser = XML_ParserCreate('UTF-8');
> XML_SetElementHandler($parser, &start-element, &end-element);
> 
> my $buf = $xml.encode('UTF-8');
> XML_Parse($parser, $buf, $buf.elems, 1);
> 
> XML_ParserFree($parser);
> --------------------------------------------------------------------------------
> 
> 
> All works fine, unless I try to use the $attr in start-element
> 
> sub start-element($, $elem, $attr)
> {
>     say "open $elem".indent($depth * 4);
>     say $attr.elems;
>     ++$depth;
> }
> 
> 
> The program then terminates with:
> 
> MoarVM panic: Internal error: Unwound entire stack and missed handler
> 
> 
> I have seen that on Solaris, but Linux has the same issue. In both
> cases
> custom build perl6 using 'rakudobrew build moar' so I suppose latest
> git revision.
> 
> Thank you

Reply via email to