Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Perl 5 methods can return a value.
  • Loading branch information
pmurias committed Oct 2, 2011
1 parent 50e0320 commit 2e3e4ba
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 10 deletions.
3 changes: 2 additions & 1 deletion examples/perl5.pl
Expand Up @@ -13,13 +13,14 @@ sub baz {
my ($self,$arg) = @_;
my $who = $$arg;
print "Just another $who\n";
return 44;
}
sub new {
bless {},"Foo";
}
PERL5
my $foo = eval(:lang<perl5>,'Foo->new');
$foo.baz(eval(:lang<perl5>,'\"Perl hacker"'));
say $foo.baz(eval(:lang<perl5>,'\"Perl hacker"'));
say eval(:lang<perl5>,"125");
say eval(:lang<perl5>,"13.5");
say eval(:lang<perl5>,"'Hello there'");
Expand Down
7 changes: 4 additions & 3 deletions lib/Perl5Interpreter.cs
Expand Up @@ -73,8 +73,8 @@ public class SVany : P6any {
[DllImport("obj/p5embed.so", EntryPoint="p5method_call")]
public static extern IntPtr MethodCall(
string name,
IntPtr[] foo,
int n
IntPtr[] arguments,
int argument_n
);

public static IntPtr VariableToSV(Variable var) {
Expand All @@ -93,7 +93,8 @@ int n
for (int i=0;i<pos.Length;i++) {
args[i] = VariableToSV(pos[i]);
}
MethodCall(name,args,args.Length);
IntPtr ret = MethodCall(name,args,args.Length);
caller.resultSlot = Perl5Interpreter.SVToVariable(ret);
return caller;
}

Expand Down
22 changes: 16 additions & 6 deletions lib/p5embed.c
Expand Up @@ -40,21 +40,31 @@ void p5embed_dispose()
PERL_SYS_TERM();
}

void p5method_call(char* name,SV** args,int n) {
dSP;

SV* p5method_call(char* name,SV** args,int args_count) {
dSP;

/*ENTER;
SAVETMPS;*/

PUSHMARK(SP);
int i;
for (i=0;i<n;i++) {
for (i=0;i<args_count;i++) {
XPUSHs(args[i]);
}
PUTBACK;

call_method(name,G_DISCARD);

int count = call_method(name,G_SCALAR);
SPAGAIN;
if (count != 1) croak("Big trouble\n");

SV* ret = POPs;

/* TODO should i do that? */
SvREFCNT_inc(ret);

PUTBACK;

return ret;

/*FREETMPS;
LEAVE;*/
Expand Down

0 comments on commit 2e3e4ba

Please sign in to comment.