From a4fd21900d907204900bd44767ff66b602b48aad Mon Sep 17 00:00:00 2001
From: Boris Smilga <boris.smilga@gmail.com>
Date: Wed, 17 Oct 2012 02:09:26 +0400
Subject: [PATCH] Method functions should be applied to their host objects as
 thisArgs, rather than to the callers' this.

Also, we might want a way to apply functions to whatever thisArg we like.
---
 src/macros.lisp  |   14 +++++++++++++-
 src/package.lisp |    1 +
 2 files changed, 14 insertions(+), 1 deletions(-)

diff --git a/src/macros.lisp b/src/macros.lisp
index d2f1d83..45764e8 100644
--- a/src/macros.lisp
+++ b/src/macros.lisp
@@ -445,7 +445,19 @@ lambda-list::=
   (let ((arglist (if (> (length args) 1)
                      `(append (list ,@(butlast args)) ,(car (last args)))
                      (first args))))
-    `(funcall (getprop ,fn 'apply) this ,arglist)))
+    (case (and (listp fn) (car fn))
+      ((getprop @ chain)
+       (with-ps-gensyms (obj method)
+         `(let* ((,obj ,(butlast fn))
+                 (,method (,(car fn) ,obj ,(car (last fn)))))
+            (funcall (getprop ,method 'apply) ,obj ,arglist))))
+      (t `(funcall (getprop ,fn 'apply) this ,arglist)))))
+
+(defpsmacro apply-to (this-arg fn &rest args)
+  (let ((arglist (if (> (length args) 1)
+                     `(append (list ,@(butlast args)) ,(car (last args)))
+                     (first args))))
+    `(funcall (getprop ,fn 'apply) ,this-arg ,arglist)))
 
 ;;; misc
 
diff --git a/src/package.lisp b/src/package.lisp
index 3f23183..e18dc64 100644
--- a/src/package.lisp
+++ b/src/package.lisp
@@ -201,6 +201,7 @@
    #:functionp
    #:append
    #:apply
+   #:apply-to
    #:destructuring-bind
 
    ;; js runtime utils
-- 
1.7.5.4

