[parenscript-devel] APPLY of method functions

Boris Smilga boris.smilga at gmail.com
Thu Oct 18 23:30:24 UTC 2012


Hello.

I have noticed that the macro APPLY in PS expands to a call to the  
function's apply method with an invariable “this” as its first  
argument (thisArg, per http://ecma-international.org/ecma-262/5.1/ 
#sec-15.3.4.3).  When the function is a method of an object, this may  
result in incorrect code.  Consider:

   (lambda (some-vector start del-count &rest elts)
     ...
     (apply (@ some-vector splice) start del-count elts)
     ...
   )

   =>

   (function (someVector, start, delCount) {
       var elts = [];
       for (var i120 = 0; i120 < arguments.length - 3; i120 += 1) {
           elts[i120] = arguments[i120 + 3];
       };
       ...
       someVector.splice.apply(this, [start, delCount].concat(elts));
       ...
   });

Whereas, to be correct, the apply part should go like

       someVector.splice.apply(someVector, [start, delCount].concat 
(elts));

Please find attached a patch which makes APPLY recognize property  
accessors in the function argument, separate them into object and  
method, and pass the object to the method's apply.  For cases which  
require more fine-grained control, the patch adds an APPLY-TO macro  
which provides for a completely custom thisArg.

There's also a separate patch with a couple of unit tests.

Yours,
  — B. Smilga.

-------------- next part --------------
A non-text attachment was scrubbed...
Name: 0001-Method-functions-should-be-applied-to-their-host-obj.patch
Type: application/octet-stream
Size: 1707 bytes
Desc: not available
URL: <https://mailman.common-lisp.net/pipermail/parenscript-devel/attachments/20121019/2c9e14ce/attachment.obj>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: 0001-Unit-tests-for-APPLY-as-used-with-object-methods.patch
Type: application/octet-stream
Size: 1122 bytes
Desc: not available
URL: <https://mailman.common-lisp.net/pipermail/parenscript-devel/attachments/20121019/2c9e14ce/attachment-0001.obj>


More information about the parenscript-devel mailing list