Skip to content

Commit

Permalink
Perlito5 - js - 0+ overload
Browse files Browse the repository at this point in the history
  • Loading branch information
fglock committed Oct 31, 2014
1 parent 5946b0f commit 4b51a26
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 4 deletions.
2 changes: 1 addition & 1 deletion perlito5.pl

Large diffs are not rendered by default.

14 changes: 12 additions & 2 deletions src5/lib/Perlito5/Javascript2/Runtime.pm
Expand Up @@ -662,7 +662,12 @@ p5str = function(o) {
// test for overload
var meth = p5method_lookup('(""', o._class_._ref_, {});
if (meth) {
return p5str(meth([o], 1));
return p5str(meth([o], 0));
}
// TODO - test the "fallback" flag
meth = p5method_lookup('(0+', o._class_._ref_, {});
if (meth) {
return p5str(meth([o], 0));
}
// no overload, strigify the reference instead
class_name = o._class_._ref_ + '=';
Expand Down Expand Up @@ -706,7 +711,12 @@ p5num = function(o) {
// test for overload
var meth = p5method_lookup('(0+', o._class_._ref_, {});
if (meth) {
return p5num(meth([o], 1));
return p5num(meth([o], 0));
}
// TODO - test the "fallback" flag
meth = p5method_lookup('(""', o._class_._ref_, {});
if (meth) {
return p5num(meth([o], 0));
}
}
}
Expand Down
30 changes: 29 additions & 1 deletion t5/01-perlito/510-string-overload.t
Expand Up @@ -2,7 +2,7 @@ use strict;
use warnings;
use feature ("say");

say "1..2";
say "1..6";

{ package S;
no strict 'refs';
Expand All @@ -22,3 +22,31 @@ $res = $v->$s;
print "not " if $res ne "123";
print "ok 2 - method call # $res\n";

# TODO - test if fallback is enabled
# $res = 0 + $v;
# print "not " if $res ne "123";
print "ok 3 - overload fallback # $res # TODO\n";


{ package N;
no strict 'refs';
*{'N::(0+'} = sub { 123 };
*{'N::()'} = sub {}; # Make it findable via fetchmethod
*{'N::()'} = \"fallback";
}

$v = bless {}, "N";
$res = 0 + $v;
print "not " if $res != 123;
print "ok 4 - overload # $res\n";

$s = '(0+';
$res = $v->$s;
print "not " if $res != 123;
print "ok 5 - method call # $res\n";

$res = "$v";
print "not " if $res ne "123";
print "ok 6 - overload fallback # $res\n";


0 comments on commit 4b51a26

Please sign in to comment.