Passing ClojureScript objects to JavaScript functions

1,350 views
Skip to first unread message

Kevin Lynagh

unread,
Aug 10, 2011, 9:04:43 PM8/10/11
to Clojure
What is the best way to pass Clojure vectors and maps to JavaScript
functions?

Currently when I need to call a JavaScript function that takes an
array I do something like

(js/my_js_fn (.array (vector 1 2 3)))

and I pass Clojure maps like

(js/my_js_fn (.strobj (hash-map "a" 1 "b" 2)))

This feels a bit gross, since I'm digging into the implementation
details.

Is there a recommended way to do this yet?
Some kind of reader macro (say @@) would be okay, turning

(js/my_js_fn @@(vector 1 2 3))

into

(js/my_js_fn (.array (vector 1 2 3)))

at read time.

Ideally though, the compiler would automatically convert ClojureScript
objects into JavaScript native objects when they're passed to a
function in the js/ namespace.

Brenton

unread,
Aug 11, 2011, 12:27:10 PM8/11/11
to Clojure
There is an issue for this:

http://dev.clojure.org/jira/browse/CLJS-37

and work is being done to come up with a good solution. The comment in
the issue is outdated. There was some discussion last week about how
best to do this. I can't remember the details but I know that doing
anything automatically is out. I think it will either be a macro or a
reader-macro and will only create js objects out of literals or
expressions which can be evaluated to a literal.

Brenton

Kevin Lynagh

unread,
Aug 11, 2011, 5:02:13 PM8/11/11
to Clojure
Alright, thanks for the info. Do you know why an automatic solution is
out?

I'm trying to use D3 from ClojureScript, but right now all of the
clarity I get from Clojure's nicer data manipulation abstractions is
lost having to convert to/from JS objects everywhere.

Ken Wesson

unread,
Aug 11, 2011, 5:14:27 PM8/11/11
to clo...@googlegroups.com
On Thu, Aug 11, 2011 at 5:02 PM, Kevin Lynagh <kly...@gmail.com> wrote:
> Alright, thanks for the info. Do you know why an automatic solution is
> out?
>
> I'm trying to use D3 from ClojureScript, but right now all of the
> clarity I get from Clojure's nicer data manipulation abstractions is
> lost having to convert to/from JS objects everywhere.

At the very least there should be nice functions to call to convert
back and forth explicitly, if automatic conversion is out and a reader
macro will only work for compile time constants. So one could use
@@the-structure for constants, and (to-js the-structure) for anything
else, or something of the sort. Or is such a function already there,
but considered annoying enough to want to be able to hide it in
implicit conversions or short, pithy reader macros? Of course, in the
latter case, a reader macro that expands to the function call could
work on non-constants, though, as @foo already expands to (deref foo)
in normal Clojure and does not require foo to be a compile time
constant.

--
Protege: What is this seething mass of parentheses?!
Master: Your father's Lisp REPL. This is the language of a true
hacker. Not as clumsy or random as C++; a language for a more
civilized age.

Kevin Lynagh

unread,
Aug 11, 2011, 7:27:48 PM8/11/11
to Clojure
Yeah, I ended up writing two such functions for converting Clojure
sequentials into JS arrays and maps into JS objects:

https://gist.github.com/1141054

(Note that they won't convert a vector containing maps or vice versa
properly, so they're not quite "nice functions" yet.)


On Aug 11, 2:14 pm, Ken Wesson <kwess...@gmail.com> wrote:
Reply all
Reply to author
Forward
0 new messages