Commit a3a4157d authored by Vladimir Sedach's avatar Vladimir Sedach
Browse files

Made structures printed at toplevel be wrapped in parentheses to prevent...

Made structures printed at toplevel be wrapped in parentheses to prevent parsing bugs in JS implementations (thanks to Scott Bell for the bug report).
parent 72c36c63
......@@ -14,13 +14,16 @@ vice-versa.")
(defvar *psw-stream*)
(defvar %printer-toplevel?)
(defun parenscript-print (form immediate?)
(declare (special immediate?))
(let ((*indent-level* 0)
(*psw-stream* (if immediate?
*psw-stream*
(make-string-output-stream)))
(%psw-accumulator ()))
(%psw-accumulator ())
(%printer-toplevel? t))
(declare (special %psw-accumulator))
(with-standard-io-syntax
(if (and (listp form) (eq 'ps-js:block (car form))) ; ignore top-level block
......@@ -47,6 +50,9 @@ vice-versa.")
(defgeneric ps-print (form))
(defgeneric ps-print% (js-primitive args))
(defmethod ps-print :after (form)
(setf %printer-toplevel? nil))
(defmacro defprinter (js-primitive args &body body)
(if (listp js-primitive)
(cons 'progn (mapcar (lambda (p)
......@@ -160,6 +166,7 @@ vice-versa.")
(psw #\() (ps-print ps-form) (psw #\)))
(defun print-op-argument (op argument)
(setf %printer-toplevel? nil)
(let ((arg-op (when (listp argument) (car argument))))
(if (or (< (precedence op) (precedence arg-op))
(and (= (precedence op) (precedence arg-op))
......@@ -239,11 +246,17 @@ vice-versa.")
(ps-print body))
(defprinter ps-js:object (&rest slot-defs)
"{ "(loop for ((slot-name . slot-value) . remaining) on slot-defs do
(ps-print slot-name) (psw " : ") (if (and (consp slot-value) (eq 'ps-js:|,| (car slot-value)))
(parenthesize-print slot-value)
(ps-print slot-value))
(when remaining (psw ", ")))" }")
(let ((toplevel? %printer-toplevel?))
(when toplevel? (psw "("))
(psw "{ ")
(loop for ((slot-name . slot-value) . remaining) on slot-defs do
(ps-print slot-name) (psw " : ")
(if (and (consp slot-value) (eq 'ps-js:|,| (car slot-value)))
(parenthesize-print slot-value)
(ps-print slot-value))
(when remaining (psw ", ")))
(psw " }")
(when toplevel? (psw ")"))))
(defprinter ps-js:getprop (obj slot)
(print-op-argument op obj)"."(psw (symbol-to-js-string slot)))
......
......@@ -96,15 +96,15 @@
(test-ps-js object-literals-1
(create foo "bar" :blorg 1)
"{ foo : 'bar', 'blorg' : 1 };")
"({ foo : 'bar', 'blorg' : 1 });")
(test-ps-js object-literals-2
(create foo "hihi"
blorg (array 1 2 3)
another-object (create :schtrunz 1))
"{ foo : 'hihi',
"({ foo : 'hihi',
blorg : [ 1, 2, 3 ],
anotherObject : { 'schtrunz' : 1 } };")
anotherObject : { 'schtrunz' : 1 } });")
(test-ps-js object-literals-3
(getprop an-object 'foo)
......@@ -826,11 +826,11 @@ __setf_someThing('foo', 1, 2);")
(test-ps-js obj-literal-numbers
(create 1 "foo")
"{ 1 : 'foo' };")
"({ 1 : 'foo' });")
(test-ps-js obj-literal-strings
(create "foo" 2)
"{ 'foo' : 2 };")
"({ 'foo' : 2 });")
(test-ps-js getprop-string
(getprop foo "bar")
......@@ -850,11 +850,11 @@ __setf_someThing('foo', 1, 2);")
(test-ps-js create-blank
(create)
"{ };")
"({ });")
(test-ps-js blank-object-literal
{}
"{ };")
"({ });")
(test-ps-js array-literal1
[]
......@@ -1613,7 +1613,7 @@ x + x;")
(test-ps-js symbol-macro-obj
(symbol-macrolet ((x y))
(create x 1))
"{ x : 1 };")
"({ x : 1 });")
(test-ps-js symbol-macro-conditional1
(symbol-macrolet ((x y))
......@@ -1714,7 +1714,7 @@ x1 - x1;
(test-ps-js create-reserved-word
(create :default 1)
"{ 'default' : 1 };")
"({ 'default' : 1 });")
(test-ps-js getprop-reserved-word
(getprop foo :default)
......@@ -2778,7 +2778,7 @@ foo = 3;")
(test-ps-js create-let-exp
(create :abc (let ((x (+ 1 2)))
(if x 123 456)))
"{ 'abc' : (x = 1 + 2, x ? 123 : 456) };")
"({ 'abc' : (x = 1 + 2, x ? 123 : 456) });")
(test-ps-js eql-eql-eql-precedence
(unless (equal (= 3 3) (= 3 4))
......
......@@ -24,7 +24,7 @@
(test-ps-js uniform-symbol-handling1
(progn (create ps-test.my-library::foo 1)
(getprop foo 'ps-test.my-library::foo))
"{ my_library_foo : 1 };
"({ my_library_foo : 1 });
foo.my_library_foo;")
(let ((map (make-hash-table)))
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment