Skip to content

Commit

Permalink
Perlito5 - js2 - fix context in method calls
Browse files Browse the repository at this point in the history
  • Loading branch information
fglock committed Nov 22, 2012
1 parent 643adab commit 589722e
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 28 deletions.
12 changes: 6 additions & 6 deletions html/perlito5.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ function p5method_lookup(method, class_name, seen) {
}
}

function p5call(invocant, method, list) {
function p5call(invocant, method, list, p5want) {
list.unshift(invocant);

if (typeof invocant === "string") {
Expand All @@ -140,11 +140,11 @@ function p5call(invocant, method, list) {
if ( invocant.hasOwnProperty("_class_") ) {

if ( invocant._class_.hasOwnProperty(method) ) {
return invocant._class_[method](list)
return invocant._class_[method](list, p5want)
}
var m = p5method_lookup(method, invocant._class_._ref_, {});
if (m) {
return m(list)
return m(list, p5want)
}

// method can have an optional namespace
Expand All @@ -154,15 +154,15 @@ function p5call(invocant, method, list) {
pkg_name = pkg_name.join("::");
m = p5method_lookup(name, pkg_name, {});
if (m) {
return m(list)
return m(list, p5want)
}
p5pkg.CORE.die(["method not found: ", name, " in class ", pkg_name]);
}

pkg_name = p5get_class_for_method('AUTOLOAD', invocant._class_._ref_, {}) || p5get_class_for_method('AUTOLOAD', "UNIVERSAL", {});
if (pkg_name) {
p5pkg[pkg_name]["v_AUTOLOAD"] = invocant._class_._ref_ + "::" + method;
return p5pkg[pkg_name]["AUTOLOAD"](list);
return p5pkg[pkg_name]["AUTOLOAD"](list, p5want);
}

p5pkg.CORE.die(["method not found: ", method, " in class ", invocant._class_._ref_]);
Expand All @@ -173,7 +173,7 @@ function p5call(invocant, method, list) {

}

function p5call_sub(namespace, name, list) {
function p5call_sub(namespace, name, list, p5want) {
if(p5pkg[namespace].hasOwnProperty(name)) {
// TODO
}
Expand Down
4 changes: 2 additions & 2 deletions perlito5.pl

Large diffs are not rendered by default.

12 changes: 6 additions & 6 deletions src5/lib/Perlito5/Javascript2/Runtime.pm
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ function p5method_lookup(method, class_name, seen) {
}
}
function p5call(invocant, method, list) {
function p5call(invocant, method, list, p5want) {
list.unshift(invocant);
if (typeof invocant === "string") {
Expand All @@ -146,11 +146,11 @@ function p5call(invocant, method, list) {
if ( invocant.hasOwnProperty("_class_") ) {
if ( invocant._class_.hasOwnProperty(method) ) {
return invocant._class_[method](list)
return invocant._class_[method](list, p5want)
}
var m = p5method_lookup(method, invocant._class_._ref_, {});
if (m) {
return m(list)
return m(list, p5want)
}
// method can have an optional namespace
Expand All @@ -160,15 +160,15 @@ function p5call(invocant, method, list) {
pkg_name = pkg_name.join("::");
m = p5method_lookup(name, pkg_name, {});
if (m) {
return m(list)
return m(list, p5want)
}
p5pkg.CORE.die(["method not found: ", name, " in class ", pkg_name]);
}
pkg_name = p5get_class_for_method('AUTOLOAD', invocant._class_._ref_, {}) || p5get_class_for_method('AUTOLOAD', "UNIVERSAL", {});
if (pkg_name) {
p5pkg[pkg_name]["v_AUTOLOAD"] = invocant._class_._ref_ + "::" + method;
return p5pkg[pkg_name]["AUTOLOAD"](list);
return p5pkg[pkg_name]["AUTOLOAD"](list, p5want);
}
p5pkg.CORE.die(["method not found: ", method, " in class ", invocant._class_._ref_]);
Expand All @@ -179,7 +179,7 @@ function p5call(invocant, method, list) {
}
function p5call_sub(namespace, name, list) {
function p5call_sub(namespace, name, list, p5want) {
if(p5pkg[namespace].hasOwnProperty(name)) {
// TODO
}
Expand Down
22 changes: 16 additions & 6 deletions src5/lib/Perlito5/Javascript3/Runtime.pm
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ function p5method_lookup(method, class_name, seen) {
}
}
function p5call(invocant, method, list) {
function p5call(invocant, method, list, p5want) {
list.unshift(invocant);
if (invocant instanceof p5Scalar) {
Expand All @@ -155,11 +155,11 @@ function p5call(invocant, method, list) {
if ( invocant.hasOwnProperty("_class_") ) {
if ( invocant._class_.hasOwnProperty(method) ) {
return invocant._class_[method](list)
return invocant._class_[method](list, p5want)
}
var m = p5method_lookup(method, invocant._class_._ref_, {});
if (m) {
return m(list)
return m(list, p5want)
}
// method can have an optional namespace
Expand All @@ -169,15 +169,15 @@ function p5call(invocant, method, list) {
pkg_name = pkg_name.join("::");
m = p5method_lookup(name, pkg_name, {});
if (m) {
return m(list)
return m(list, p5want)
}
p5pkg.CORE.die(["method not found: ", name, " in class ", pkg_name]);
}
pkg_name = p5get_class_for_method('AUTOLOAD', invocant._class_._ref_, {}) || p5get_class_for_method('AUTOLOAD', "UNIVERSAL", {});
if (pkg_name) {
p5pkg[pkg_name]["v_AUTOLOAD"] = invocant._class_._ref_ + "::" + method;
return p5pkg[pkg_name]["AUTOLOAD"](list);
return p5pkg[pkg_name]["AUTOLOAD"](list, p5want);
}
p5pkg.CORE.die(["method not found: ", method, " in class ", invocant._class_._ref_]);
Expand All @@ -188,13 +188,23 @@ function p5call(invocant, method, list) {
if (typeof invocant === "string") {
var aclass = p5make_package(invocant);
return p5call(aclass, method, list);
return p5call(aclass, method, list, p5want);
}
p5pkg.CORE.die(["Can't call method ", method, " on unblessed reference"]);
}
function p5call_sub(namespace, name, list, p5want) {
if(p5pkg[namespace].hasOwnProperty(name)) {
// TODO
}
if(p5pkg[namespace].hasOwnProperty("AUTOLOAD")) {
// TODO
}
p5pkg.CORE.die(["Undefined subroutine &" + namespace + "::" + name]);
}
p5make_package("main");
p5pkg["main"]["v_@"] = new p5Scalar(""); // $@
p5pkg["main"]["v_|"] = new p5Scalar(0); // $|
Expand Down
25 changes: 21 additions & 4 deletions t5/01-perlito/31-autoload.t
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use feature 'say';
use strict;

say "1..3";
say "1..4";

{

package A;

our $AUTOLOAD;
Expand All @@ -15,12 +16,28 @@ say "1..3";
say "# $AUTOLOAD";
print "not " if $AUTOLOAD ne "A::a";
say "ok 2";
return 456;
return wantarray ? ( 4, 5 ) : 456;
}

my $v = a(123);
print "not " if $v != 456;
say "ok 3";
print "not " if $v != 456;
say "ok 3";

}

{

package C;

our $AUTOLOAD;

sub AUTOLOAD {
return wantarray ? ( 6, 7 ) : 456;
}

my @x = a(123);
print "not " if $x[0] != 6 || $x[1] != 7;
say "ok 4";

}

25 changes: 21 additions & 4 deletions t5/01-perlito/32-autoload-method.t
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use feature 'say';
use strict;

say "1..4";
say "1..5";

{

package A;

our $AUTOLOAD;
Expand All @@ -17,12 +18,28 @@ say "1..4";
say "# $AUTOLOAD";
print "not " if $AUTOLOAD ne "A::a";
say "ok 3";
return 456;
return wantarray ? ( 4, 5 ) : 456;
}

my $v = A->a(123);
print "not " if $v != 456;
say "ok 4";
print "not " if $v != 456;
say "ok 4";

}

{

package C;

our $AUTOLOAD;

sub AUTOLOAD {
return wantarray ? ( 6, 7 ) : 456;
}

my @x = C->a(123);
print "not " if $x[0] != 6 || $x[1] != 7;
say "ok 5";

}

0 comments on commit 589722e

Please sign in to comment.