Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Perlito5 - js - string overload
  • Loading branch information
fglock committed May 15, 2014
1 parent 7afd967 commit 41e7b04
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 18 deletions.
1 change: 1 addition & 0 deletions TODO-perlito5
Expand Up @@ -473,6 +473,7 @@ TODO list for Perlito5
See: p5str_inc()

-- finish "overload" implementation
See: s5str

-- pack(), unpack()
-- y()()
Expand Down
11 changes: 10 additions & 1 deletion html/perlito5.js

Large diffs are not rendered by default.

16 changes: 0 additions & 16 deletions misc/overload.pl

This file was deleted.

4 changes: 3 additions & 1 deletion perlito5.pl

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions src5/lib/Perlito5/Grammar/Expression.pm
Expand Up @@ -76,6 +76,7 @@ sub pop_term {
my $v = pop @$num_stack;
if (ref($v) eq 'ARRAY') {
# say "# ** processing term ", $v->perl;
return $v->[1] if ref($v->[1]); # optimization - avoid strigifying objects
if ($v->[1] eq 'methcall_no_params') {
# say "# Perlito5::AST::Call ", ($v->[2])->perl;
$v = Perlito5::AST::Call->new( invocant => undef, method => $v->[2], arguments => [] );
Expand Down
6 changes: 6 additions & 0 deletions src5/lib/Perlito5/Javascript2/Runtime.pm
Expand Up @@ -599,6 +599,12 @@ p5str = function(o) {
var class_name = '';
if (o._class_ && typeof o._class_._ref_ === "string") {
// blessed reference
// test for overload
var meth = p5method_lookup('(""', o._class_._ref_, {});
if (meth) {
return p5str(meth([o], 1));
}
// no overload, strigify the reference instead
class_name = o._class_._ref_ + '=';
}
if (!o._id_) { o._id_ = p5id++ }
Expand Down
24 changes: 24 additions & 0 deletions t5/01-perlito/510-string-overload.t
@@ -0,0 +1,24 @@
use strict;
use warnings;
use feature ("say");

say "1..2";

{ package S;
no strict 'refs';
*{'S::(""'} = sub { "123" };
*{'S::()'} = sub { };
}

my $res;
my $s;
my $v = bless {}, "S";
$res = "$v";
print "not " if $res ne "123";
print "ok 1 - overload # $res\n";

$s = '(""';
$res = $v->$s;
print "not " if $res ne "123";
print "ok 2 - method call # $res\n";

0 comments on commit 41e7b04

Please sign in to comment.