Skip to content

Commit

Permalink
Implement calling parameterless P6 functions from P5
Browse files Browse the repository at this point in the history
  • Loading branch information
niner committed Feb 6, 2015
1 parent bd9e834 commit c57a1ec
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 5 deletions.
15 changes: 12 additions & 3 deletions Perl6.xs
Expand Up @@ -9,6 +9,7 @@
#include "Perl6.h"

SV *(*call_method_callback)(IV, char *);
SV *(*call_function_callback)(char *);
SV *(*eval_code_callback)(char *);
MVMInstance *instance;
MVMCompUnit *cu;
Expand All @@ -21,9 +22,10 @@ static void toplevel_initial_invoke(MVMThreadContext *tc, void *data) {
MVM_frame_invoke(tc, (MVMStaticFrame *)data, MVM_callsite_get_common(tc, MVM_CALLSITE_ID_NULL_ARGS), NULL, NULL, NULL, -1);
}

void init_callbacks(SV *(*eval_p6_code)(char *), SV *(*call_p6_method)(IV, char *)) {
void init_callbacks(SV *(*eval_p6_code)(char *), SV *(*call_p6_method)(IV, char *), SV *(*call_p6_function)(char *)) {
eval_code_callback = eval_p6_code;
call_method_callback = call_p6_method;
call_function_callback = call_p6_function;
}

SV *p5_int_to_sv(IV value) {
Expand Down Expand Up @@ -86,8 +88,6 @@ initialize()
MVMThreadContext *tc = instance->main_thread;
cu = MVM_cu_map_from_file(tc, filename);

call_method_callback = NULL;

MVMROOT(tc, cu, {
/* The call to MVM_string_utf8_decode() may allocate, invalidating the
location cu->body.filename */
Expand Down Expand Up @@ -143,6 +143,15 @@ run(code)
OUTPUT:
RETVAL

SV *
call(name)
char *name
CODE:
cur_my_perl = my_perl;
RETVAL = call_function_callback(name);
OUTPUT:
RETVAL

SV *
invoke(name)
char *name
Expand Down
9 changes: 7 additions & 2 deletions lib/Inline/Perl6Helper.pm6
Expand Up @@ -32,9 +32,14 @@ our $call_method = sub (Int $index, Str $name) returns OpaquePointer {
return OpaquePointer;
};

our $call_function = sub (Str $name) returns OpaquePointer {
return p6_to_p5(&::($name)());
};

sub init_callbacks(
&eval_code (Str --> OpaquePointer),
&call_method (Int, Str --> OpaquePointer)
&call_method (Int, Str --> OpaquePointer),
&call_function (Str --> OpaquePointer),
) is native(@*ARGS[0]) { * };

init_callbacks($eval_code, $call_method);
init_callbacks($eval_code, $call_method, $call_function);
9 changes: 9 additions & 0 deletions t/04call_function.t
@@ -0,0 +1,9 @@
use Test::More tests => 1;
use Inline::Perl6;

Inline::Perl6::initialize;
ok(1);
Inline::Perl6::call('exit');
ok(0);
Inline::Perl6::destroy;

0 comments on commit c57a1ec

Please sign in to comment.