<br><br><div class="gmail_quote">On Sun, Jul 26, 2009 at 2:25 PM, 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;">
As I've posted here before, we depend on having a facility in PS for symbols that do *not* get translated to the JS identifier rules. </blockquote><div><br>Could you please post an explicit example of why you need symbols that do not get translated according to normal symbol translation rules? I am in a different boat where my current libraries all expect keywords to translate with hypenated-to-camelcase rules. e.g.<br>
<br>(methcall :to-upper-case object)<br><br>Forms like that are all over my code (see <a href="http://common-lisp.net/project/suave/">http://common-lisp.net/project/suave/</a> for a list of Parenscript projects). I do not use a quoted symbol in the current package because I require that the symbols be translated without a package prefix, since they keywords correspond to Javascript-native identifiers. This is my use case. What is yours?<br>
</div><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">The current compromise in PS -- treat keywords differently from non-keyword symbols -- seems like a reasonable tradeoff. If the inconsistency of keywords in slot-value is an issue, I'd rather see slot-value itself modified to preserve the difference between keywords and other symbols. (That would produce syntax errors in the case of keywords with hyphens in them, but there's a fix ready at hand, namely use a non-keyword symbol.)</blockquote>
<div><br>I believe something like (slot-value object :key-with-hyphens) would properly translate to object['key-with-hyphens'], so there should not be any syntax errors for slot-value forms. For identifiers named by hyphenated keywords, you are correct.<br>
</div><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;"><br>
<br>The last time this subject came up, I promised to try replacing all of our symbols like '+ with keywords like :+ and see how it turned out. I meant to report back about this, but I don't think I did. We made the change, and it works fine. This allowed us to run against the latest PS. Unless I'm missing something, it looks like the proposed patch will break that.<br>
<br>The fundamental issue here is that the symbols in CL correspond to both identifiers and strings in JS. That means either we go with a one-to-many mapping (translating symbols to JS in more than one way) or translate all PS symbols to conform to the identifier rules. The latter is simpler but deprives PS of some expressiveness that we rely on. If we're going to have two different kinds of symbols in PS, the keyword/non-keyword distinction seems like the obvious choice, since that's CL's way of having two different kinds of symbols.<br>
</blockquote><div><br>The fundamental issue is there is no ideal translation of a Parenscript symbol into a Javascript equivalent; we are dealing with a language mismatch. One solution is to translate symbols not to strings but to some other form that is more useful. The symbol :to-upper-case, for example, could be translated into:<br>
<br>intern('toUpperCase', 'to-upper-case', 'keyword');<br><br>function intern(jsstring, lispName, package) {<br> if (lookup symbol in table)<br> return looked up symbol;<br> else<br> return new PSSymbol(jsstring, lispName, package);<br>
}<br><br><br>function PSSymbol(jsstring, lispName, package) {<br> this.jsstring = jsstring;<br> this.lispName = lispName;<br> this.package = package;<br> storeSymbol(this); <br> this.toString = function() { return this.jsstring; }<br>
return this; <br>}<br><br>This avoids the one-to-many headache and preserves consistency of how to reference symbols (the toString() method is used to serialize a value for slot store, e.g. (slot-value object (intern 'toUpperCase' ...)) == (slot-value object 'toUpperCase')). Unfortunately this makes Parenscript more complex on the runtime end.<br>
<br><br>If we do not adopt this fancy, semantically superior translation scheme, I think we should stick to a consistent translator across all packages.<br><br><br>Best,<br>Red<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;">
<br>Daniel<br><br><div class="gmail_quote"><div><div></div><div class="h5">On Sun, Jul 26, 2009 at 2:52 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">
Dear Parenscripters,<br><br>I found an inconsistency in how symbols are translated into Javascript. The issue is similar to an earlier issue:<br><br>CL-USER> (ps:ps <br>
(let ((sym :my-function)) <br> (slot-value object sym) <br>
(slot-value object :my-function)))<br>"var sym = 'my-function'; <br>
object[sym]; <br>object.myFunction;"<br>
<br>One would expect object[sym] and object.myFunction to evaluate to the same value, but right now they do not. This bug only affects keywords:<br><br>CL-USER> (ps:ps <br>
(let ((sym 'my-function)) <br> (slot-value object sym) <br>
(slot-value object 'my-function)))<br>"var sym = 'myFunction'; <br>
object[sym]; <br>object.myFunction;" <br>
<br>The above output is sensible. A patch is attached to fix this issue. A few test cases have also been added to prevent future breakage.<br><br>The old DEFUN-KEYWORD4 test function failed with this patch because it expected a keyword to translate to 'my-name-key'. I patched it to expect 'myNameKey'<br>
<br>Best,<br>Red<br><br>From 904be1cc2eee598491557132e8ed1569a90f27a3 Mon Sep 17 00:00:00 2001<br>From: Red Daly <<a href="mailto:reddaly@gmail.com" target="_blank">reddaly@gmail.com</a>><br>Date: Sun, 26 Jul 2009 20:22:54 +0000<br>
Subject: [PATCH] Fixed eval-when special form and added tests to prevent future breakage.<br><br>---<br> src/compiler.lisp | 50 ++++++++++++++++++++++++++++----------------------<br> t/ps-tests.lisp | 40 ++++++++++++++++++++++++++++++++++++++++<br>
2 files changed, 68 insertions(+), 22 deletions(-)<br><br>diff --git a/src/compiler.lisp b/src/compiler.lisp<br>index 4fed094..e72fb4e 100644<br>--- a/src/compiler.lisp<br>+++ b/src/compiler.lisp<br>@@ -162,17 +162,20 @@ compiled to an :expression (the default), a :statement, or a<br>
:symbol."))<br> <br> (defun adjust-ps-compilation-level (form level)<br>- (cond ((or (and (consp form) (eq 'progn (car form)))<br>- (and (symbolp form) (eq :toplevel level)))<br>- level)<br>
- ((eq :toplevel level) :inside-toplevel-form)))<br>+ "Given the current *ps-compilation-level*, LEVEL, and the fully macroexpanded<br>+form, FORM, returns the new value for *ps-compilation-level*."<br>
+ (cond ((or (and (consp form) (member (car form)<br>
+ '(progn locally macrolet symbol-macrolet compile-file)))<br>+ (and (symbolp form) (eq :toplevel level)))<br>+ level)<br>+ ((eq :toplevel level) :inside-toplevel-form)))<br>+<br>
<br>
(defmethod compile-parenscript-form :around (form &key expecting)<br> (assert (if expecting (member expecting '(:expression :statement :symbol)) t))<br> (if (eq expecting :symbol)<br> (compile-to-symbol form)<br>
- (let ((*ps-compilation-level* (adjust-ps-compilation-level form *ps-compilation-level*)))<br>- (call-next-method))))<br>+ (call-next-method)))<br> <br> (defun compile-to-symbol (form)<br> "Compiles the given Parenscript form and guarantees that the<br>
@@ -226,22 +229,25 @@ the form cannot be compiled to a symbol."<br> (defmethod compile-parenscript-form ((form cons) &key (expecting :statement))<br> (multiple-value-bind (form expanded-p)<br> (ps-macroexpand form)<br>
- (cond (expanded-p (compile-parenscript-form form :expecting expecting))<br>- ((ps-special-form-p form) (apply (get-ps-special-form (car form)) (cons expecting (cdr form))))<br>- ((op-form-p form)<br>
- `(js:operator ,(ps-convert-op-name (compile-parenscript-form (car form) :expecting :symbol))<br>- ,@(mapcar (lambda (form)<br>- (compile-parenscript-form (ps-macroexpand form) :expecting :expression))<br>
- (cdr form))))<br>- ((funcall-form-p form)<br>- `(js:funcall ,(compile-parenscript-form (if (symbolp (car form))<br>- (maybe-rename-local-function (car form))<br>
- (ps-macroexpand (car form)))<br>- :expecting :expression)<br>- ,@(mapcar (lambda (arg)<br>- (compile-parenscript-form (ps-macroexpand arg) :expecting :expression))<br>
- (cdr form))))<br>- (t (error "Cannot compile ~S to a ParenScript form." form)))))<br>+ (let ((*ps-compilation-level* (if expanded-p<br>+ *ps-compilation-level*<br>
+ (adjust-ps-compilation-level form *ps-compilation-level*))))<br>+ (cond (expanded-p (compile-parenscript-form form :expecting expecting))<br>+ ((ps-special-form-p form) (apply (get-ps-special-form (car form)) (cons expecting (cdr form))))<br>
+ ((op-form-p form)<br>+ `(js:operator ,(ps-convert-op-name (compile-parenscript-form (car form) :expecting :symbol))<br>+ ,@(mapcar (lambda (form)<br>+ (compile-parenscript-form (ps-macroexpand form) :expecting :expression))<br>
+ (cdr form))))<br>+ ((funcall-form-p form)<br>+ `(js:funcall ,(compile-parenscript-form (if (symbolp (car form))<br>+ (maybe-rename-local-function (car form))<br>
+ (ps-macroexpand (car form)))<br>+ :expecting :expression)<br>+ ,@(mapcar (lambda (arg)<br>+ (compile-parenscript-form (ps-macroexpand arg) :expecting :expression))<br>
+ (cdr form))))<br>+ (t (error "Cannot compile ~S to a ParenScript form." form))))))<br> <br> (defvar *ps-gensym-counter* 0)<br> <br>diff --git a/t/ps-tests.lisp b/t/ps-tests.lisp<br>index cd9d4f2..0324c09 100644<br>
--- a/t/ps-tests.lisp<br>+++ b/t/ps-tests.lisp<br>@@ -1129,3 +1129,43 @@ x1 - x1;<br> --x1;<br> ++x1;")<br> <br>+(test-ps-js eval-when-ps-side<br>+ (eval-when (:execute)<br>+ 5)<br>+ "5;")<br>+<br>+(defvar *lisp-output* nil)<br>
+<br>+(test eval-when-lisp-side ()<br>+ (setf *lisp-output* 'original-value)<br>+ (let ((js-output (normalize-js-code <br>+ (ps-doc* `(eval-when (:compile-toplevel)<br>+ (setf *lisp-output* 'it-works))))))<br>
+ (is (eql 'it-works *lisp-output*))<br>+ (is (string= "" js-output))))<br>+<br>+(defpsmacro my-in-package (package-name)<br>+ `(eval-when (:compile-toplevel)<br>+ (setf *lisp-output* ,package-name)))<br>
+<br>+(test eval-when-macro-expansion ()<br>+ (setf *lisp-output* 'original-value)<br>+ (let ((js-output (normalize-js-code <br>+ (ps-doc* `(progn<br>+ (my-in-package :cl-user)<br>
+ 3)))))<br>
+ (declare (ignore js-output))<br>+ (is (eql :cl-user *lisp-output*))))<br>+ ;(is (string= "" js-output))))<br>+<br>+(test eval-when-macrolet-expansion ()<br>+ (setf *lisp-output* 'original-value)<br>
+ (let ((js-output (normalize-js-code <br>+ (ps-doc* `(macrolet ((my-in-package2 (package-name)<br>+ `(eval-when (:compile-toplevel)<br>+ (setf *lisp-output* ,package-name))))<br>
+ (my-in-package2 :cl-user)<br>+ 3)))))<br>+ (declare (ignore js-output))<br>+ (is (eql :cl-user *lisp-output*))))<br>+ ;(is (string= "" js-output))))<br>-- <br>
1.5.4.3<br>
<br><br>From d6748a5cfc8c4b9300884e9e5061db19cb0407cd Mon Sep 17 00:00:00 2001<br>From: Red Daly <<a href="mailto:reddaly@gmail.com" target="_blank">reddaly@gmail.com</a>><br>Date: Sun, 26 Jul 2009 20:49:59 +0000<br>
Subject: [PATCH] Fixed keyword translation inconsistency between (slot-value object :key-thing) and :key-thing.<br>
<br>---<br> src/printer.lisp | 2 +-<br> src/special-forms.lisp | 2 +-<br> t/ps-tests.lisp | 18 +++++++++++++++++-<br> 3 files changed, 19 insertions(+), 3 deletions(-)<br><br>diff --git a/src/printer.lisp b/src/printer.lisp<br>
index b649dae..edf74b2 100644<br>--- a/src/printer.lisp<br>+++ b/src/printer.lisp<br>@@ -43,7 +43,7 @@ arguments, defines a printer for that form using the given body."<br> <br> (defmethod ps-print ((s symbol))<br> (assert (keywordp s))<br>
- (ps-print (string-downcase s)))<br>+ (ps-print (symbol-to-js-string s)))<br> <br> (defmethod ps-print ((compiled-form cons))<br> (ps-print% (car compiled-form) (cdr compiled-form)))<br>diff --git a/src/special-forms.lisp b/src/special-forms.lisp<br>
index 802dc28..e96a6d4 100644<br>--- a/src/special-forms.lisp<br>+++ b/src/special-forms.lisp<br>@@ -34,7 +34,7 @@<br> (typecase x<br> (cons `(array ,@(mapcar (lambda (x) (when x `',x)) x)))<br> (null '(array))<br>
- (keyword x)<br>+ (keyword (symbol-to-js-string x))<br> (symbol (symbol-to-js-string x))<br> (number x)<br> (string x))<br>diff --git a/t/ps-tests.lisp b/t/ps-tests.lisp<br>index 0324c09..540d1a6 100644<br>
--- a/t/ps-tests.lisp<br>+++ b/t/ps-tests.lisp<br>@@ -455,7 +455,7 @@ __setf_someThing(_js1, _js2, _js3);")<br> var _js2 = arguments.length;<br> for (var n1 = 0; n1 < _js2; n1 += 2) {<br> switch (arguments[n1]) {<br>
- case 'my-name-key':<br>+ case 'myNameKey':<br> {<br> myName = arguments[n1 + 1];<br> };<br>@@ -1169,3 +1169,19 @@ x1 - x1;<br> (declare (ignore js-output))<br>
(is (eql :cl-user *lisp-output*))))<br> ;(is (string= "" js-output))))<br>+<br>+(test-ps-js keyword-conversion1<br>+ :alpha-omega<br>+ "'alphaOmega';")<br>+<br>+(test-ps-js keyword-conversion2<br>
+ (slot-value object :alpha-omega)<br>+ "object.alphaOmega;")<br>+<br>+(test keyword-conversion2 ()<br>+ (let ((js-output1 (normalize-js-code <br>+ (ps-doc* '(slot-value object :alpha-omega))))<br>
+ (js-output2 (normalize-js-code<br>+ (ps-doc* :alpha-omega)))<br>+ (declare (ignore js-output))<br>+ (is (eql :cl-user *lisp-output*))))<br><font color="#888888">-- <br>1.5.4.3<br><br>
</font><br></div></div>_______________________________________________<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></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>