如何用Parenscript将Lisp列表设为JS变量?大列表报错解决
The root of your problem lies in how you're constructing the Parenscript form for the array. When you use (ps:lisp (cons 'list *VERTICES*)), you're creating a list function call with every element of your VERTICES list as an individual argument. Both Parenscript's compiler and most Lisp implementations have hard limits on the number of arguments a single function call can accept—hence the "Too many arguments" error when VERTICES grows large.
Solution: Use ps:raw to emit a direct array literal
Instead of relying on Parenscript to compile a huge list call, generate the JavaScript array literal as a string in Lisp and insert it directly into your Parenscript code using ps:raw. This bypasses the argument limit entirely because you're not creating a function call—just a raw string that becomes a valid JS array.
Here's the revised code:
(ps:ps (ps:var vertices (ps:raw (format nil "[~{~a~^, ~}]" *VERTICES*))))
How this works:
- The Lisp
formatfunction converts your flat list of numbers into a properly formatted string like"[0.0, -200.0, 0, ..., 0.4, 40]". ps:rawtells Parenscript to insert this string directly into the generated JavaScript, no compilation or function call required.
This approach is efficient (JS parses array literals quickly) and safe for your use case since VERTICES is a list of numbers—no risk of JavaScript injection here.
Alternative: Build the array incrementally (for cases where raw strings aren't preferred)
If you want to avoid raw strings, you can generate JavaScript code that builds the array by pushing elements one at a time. This avoids the argument limit but will generate more code and may be slower for extremely large lists:
(ps:ps (ps:var vertices (ps:array)) ;; Generate a push call for each vertex in Lisp (ps:lisp (dolist (v *VERTICES*) (ps:ps (ps:chain vertices (push ,v))))))
This loops through your VERTICES list in Lisp and emits a vertices.push(v) line for each element. While this works, it's less efficient than the array literal method for large datasets.
内容的提问来源于stack exchange,提问作者Capstone

