Skip to content

Instantly share code, notes, and snippets.

@0racle
Last active November 9, 2017 13:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 0racle/c7e65b6946a30479cf8493fbafc04f53 to your computer and use it in GitHub Desktop.
Save 0racle/c7e65b6946a30479cf8493fbafc04f53 to your computer and use it in GitHub Desktop.
Readline / NativeCall encoding error

Readline / NativeCall encoding error

Issue occurs the first time I type any expression.

# perl6
To exit type 'exit' or '^D'
> []
Internal error: unhandled encoding
  in method CALL-ME at ... (NativeCall) line 587
  in method readline at ... (Readline) line 1391

> []
[]

Curiously, issue does not occur the first time if I run it really soon after the REPL starts, but the next time it will trigger.

See this asciinema

Relevant lines

NativeCall

571     method CALL-ME(|args) {
572         self.create-optimized-call() unless
573             $!is-clone # Clones and original would share the invokespec but not the $!do attribute
574             or $!any-optionals # the compiled code doesn't support optional parameters yet
575             or $*W;    # Avoid issues with compiling specialized version during BEGIN time
576         self!setup();
577 
578         my Mu $args := nqp::getattr(nqp::decont(args), Capture, '@!list');
579         if nqp::elems($args) != $!arity {
580             X::TypeCheck::Argument.new(
581                 :objname($.name),
582                 :arguments(args.list.map(*.^name))
583                 :signature(try $r.signature.gist),
584             ).throw
585         }
586 
587         nqp::nativecall($!rettype, self, $args)    # <----------
588     }

Readline

1387   sub readline( Str )
1388     is export
1389     returns Str
1390     is native( LIB ) { * }
1391   method readline( Str $prompt )    # <----------
1392     returns Str {
1393     readline( $prompt ) }
@0racle
Copy link
Author

0racle commented Nov 9, 2017

Upon doing a little digging, this error seems to be coming from within the depths of MoarVM, specifically nativecall.c

VMObject * MVM_nativecall_make_str(MVMThreadContext *tc, MVMObject *type, MVMint16 ret_type, char *cstring) {
    MVMObject *result = type;
    if (cstring && type) {
        MVMString *value;

        MVM_gc_root_temp_push(tc, (MVMCollectable **)&type);

        switch (ret_type & MVM_NATIVECALL_ARG_TYPE_MASK) {
            case MVM_NATIVECALL_ARG_ASCIISTR:
                value = MVM_string_ascii_decode(tc, tc->instance->VMString, cstring, strlen(cstring));
                break;
            case MVM_NATIVECALL_ARG_UTF8STR:
                value = MVM_string_utf8_decode(tc, tc->instance->VMString, cstring, strlen(cstring));
                break;
            case MVM_NATIVECALL_ARG_UTF16STR:
                value = MVM_string_utf16_decode(tc, tc->instance->VMString, cstring, strlen(cstring));
                break;
            default:
                MVM_exception_throw_adhoc(tc, "Internal error: unhandled encoding");    # <----------
        }

        MVM_gc_root_temp_pop(tc);
        result = MVM_repr_box_str(tc, type, value);
        if (ret_type & MVM_NATIVECALL_ARG_FREE_STR)
            MVM_free(cstring);
    }

    return result;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment