<br><br><div class="gmail_quote">On Sun, Nov 1, 2009 at 9:50 AM, Daniel Gackle <span dir="ltr"><<a href="mailto:danielgackle@gmail.com">danielgackle@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
It might help to use a PS special variable and make MULTIPLE-VALUE-BIND responsible for cleanup.<br><br><span style="font-family: courier new,monospace;">(ps (defvar *mv* undefined)</span>)<br style="font-family: courier new,monospace;">
<br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">(defpsmacro values (main &rest additional)<br> (with-ps-gensyms (mv)<br> `(let ((,mv (list ,@additional)))<br> (when (defined *mv*)<br>
(setf *mv* ,mv))<br> (return ,main))))<br><br>(defpsmacro multiple-value-bind (vars expr &body body)<br> `(let ((*mv* '()))<br> (let ((,(car vars) ,expr))<br> (destructuring-bind ,(cdr vars) *mv*<br>
,@body))))<br></span><br>This works in the obvious cases. I'm not sure it handles Red's scenarios. Red, can you supply an example where this breaks?<br></blockquote><div><br><br>The main case I am concerned about is dealing with non-parenscript functions that will not manipulate the mv state. Something like the following would break the above code:<br>
<br>(defun foo ()<br> (let ((my-callback (lambda (x) (values 1 10)))<br> (multiple-value-bind (a b)<br> (add-to-result my-callback 2)))<br> <br>=><br><br>// imagine this is a non-Parenscript function that we cannot manipulate with the<br>
// Parenscript compiler<br>function addToResult(callback, x) { <br> // returns the result of adding x to the result of calling the callback<br> return callback() + x;<br>}<br><br>function foo () {<br> var myCallback = function (x) {<br>
if (MV !== undefined)<br>
MV = [10];<br>
return 1;<br>
};<br> <br> var old_MV = MV; // begin let<br> MV = null;<br> var a = addToResult(myCallback, 2);<br> var b = MV ? MV[0] : null;<br><br> // now b === 10 but it should be nil<br> // this is because addToResult did not reset MV<br>
<br> MV = old_MV; // end let<br> <br>}<br><br>This is why I think you might need to identify the callee in another special variable, and for emitted functions reset the MV_CALLEE variable before returning (and maybe in other places?).<br>
<br><br>Red<br><br><br> </div><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;"><font color="#888888"><br>Daniel<br></font><br>p.s. I took the easy way out of making VALUES always prepend RETURN, but once Vladimir bestows implicit RETURN upon us we can take that out ;)<br>
<br><br><div class="gmail_quote"><div><div></div><div class="h5">On Sat, Oct 31, 2009 at 9:53 PM, Red Daly <span dir="ltr"><<a href="mailto:reddaly@gmail.com" target="_blank">reddaly@gmail.com</a>></span> wrote:<br>
</div></div><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;"><div><div></div><div class="h5">I apologize for sending the first half of this email in error earlier:<br>
<br><div class="gmail_quote">
<div><div></div><div>On Sat, Oct 31, 2009 at 8:35 PM, Red Daly <span dir="ltr"><<a href="mailto:reddaly@gmail.com" target="_blank">reddaly@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">Hi Parenscripters,<br><br>As far as I can tell, multiple-value function calls are a unique feature of lisp. I would like the ability to perform multiple-value calls in Parenscript but I don't know if a sane solution exists or not.<br>
<br>Can anyone come up with a scheme for returning multiple values that translates well to Javascript? Ideally such a scheme would not introduce much overhead for usual functions that do not use or return multiple values (though perhaps setting some sort of global MV flag might be inexpensive enough). Functions that return multiple values should also only appear to return a single value when called by a function that expects only one return value (including native javascript functions).<br>
<br>(defun paren-mv-returner ()<br> (return (values 1 2 3)))<br><br>=><br><br><br>
function parenMvReturner() {<br>
/* do some magic with the values 2 and 3 */<br>
return 1; <br>
}<br><br>// one implementation might be<br>var mv = undefined;<br><br>function parenMvReturner() {<br>
mv = [2, 3];<br> return 1; <br>
}<br><br>// this scheme needs to adjust the return statement of every function so it might not be sufficient<br>// consider this other function<br><br>function parenMySingleReturner () {<br> var x = parenMvReturner();<br>
</blockquote></div></div><div>return x;<br>}<br><br>// parenMySingleReturner() will appear to return multiple values unless it modifies the mv value itself<br><br>// correction:<div><br>function parenMySingleReturner () {<br>
var x = parenMvReturner();<br></div>
mv = null;<br> return x;<br>
}<br> </div></div>But it seems like this solution will fall apart for calls to native Javascript functions over which we have no control. If we pass a multiple-value returning function as an argument to a native function, the native function will not perform the necessary mv-nulling when it returns.<br>
<br>someForeignJavascriptFunction( someMVReturningFunction)<br><br>will return whatever the someForeignJavascriptFunction should return, but it will also appear to return the other values that someMVReturningFunction set in the mv variable, since someForeignJavascriptFunction performs no cleanup.<br>
<br>Maybe this limitation can be avoided by having an mv-returning function A set a global variable "mvFunctionReturner" equal to the function A and a mv-receiver can check that mvFunctionReturner is set according to the function it called expecting multiple values. Does this scheme miss any cases?<br>
<br>Anyway I have thought a little bit about this and I thought I would pass it off to the rest of the Parenscripters as a thought experiment. Assume you can do a lot more semantic analysis than Parenscript currently does and transform the compiled source however you want. But any compiled functions must still be able to be treated as normal Javascript functions and all and only functions that should return multiple values appear to return them.<br>
<br>Cheers,<br><font color="#888888">Red<br>
</font><br></div></div><div class="im">_______________________________________________<br>
parenscript-devel mailing list<br>
<a href="mailto:parenscript-devel@common-lisp.net" target="_blank">parenscript-devel@common-lisp.net</a><br>
<a href="http://common-lisp.net/cgi-bin/mailman/listinfo/parenscript-devel" target="_blank">http://common-lisp.net/cgi-bin/mailman/listinfo/parenscript-devel</a><br>
<br></div></blockquote></div><br>
<br>_______________________________________________<br>
parenscript-devel mailing list<br>
<a href="mailto:parenscript-devel@common-lisp.net">parenscript-devel@common-lisp.net</a><br>
<a href="http://common-lisp.net/cgi-bin/mailman/listinfo/parenscript-devel" target="_blank">http://common-lisp.net/cgi-bin/mailman/listinfo/parenscript-devel</a><br>
<br></blockquote></div><br>