cljs-clj interop

207 views
Skip to first unread message

Stuart Campbell

unread,
Dec 29, 2012, 8:22:00 PM12/29/12
to Clojure
Hi all,

I'm toying with a way to use Clojure objects from a Rhino-based ClojureScript environment (https://github.com/harto/rhino-bridge).

I've been able to export a Clojure function into the ClojureScript environment without too much difficulty. Ideally, I'd like to be able to call that function as if it were a regular ClojureScript function.

I was hoping I could do something like:

(extend-type js/Packages.clojure.lang.IFn
  IFn
  (-invoke [this] (.invoke this))
  (-invoke [this a] (.invoke this a))
  ;; etc
  )

However, this yields an error that I don't quite understand:

java.lang.UnsupportedOperationException: nth not supported on this type: Symbol
    at clojure.lang.RT.nthFrom(RT.java:846)
    at clojure.lang.RT.nth(RT.java:796)
    at cljs.core$extend_type$assign_impls__7365$fn__7377$adapt_params__7380.invoke(core.clj:486)
    at clojure.core$map$fn__4087.invoke(core.clj:2434)

In fact, I'm not sure this will work at all, since (type) doesn't appear to work with non-native JS objects.

Is the extend-type method feasible?

Cheers,
Stuart

David Nolen

unread,
Dec 29, 2012, 8:31:59 PM12/29/12
to clojure
I think you've just formatted your code incorrectly. Did you try something like this?

(extend-type js/Packages.clojure.lang.IFn
  IFn
  (-invoke 
    ([this] (.invoke this))
    ([this a] (.invoke this a)))
  )


--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@googlegroups.com
Note that posts from new members are moderated - please be patient with your first post.
To unsubscribe from this group, send email to
clojure+u...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Stuart Campbell

unread,
Dec 30, 2012, 5:41:45 PM12/30/12
to Clojure
Ah, you're right -- thanks. That leads me to another error:

ClojureScript:cljs.user> (extend-type js/Packages.clojure.lang.IFn IFn (-invoke ([this] (.invoke this))))         
"Error evaluating:" (extend-type js/Packages.clojure.lang.IFn IFn (-invoke ([this] (.invoke this)))) :as "Packages.clojure.lang.IFn.prototype.cljs$core$IFn$ = true;\nPackages.clojure.lang.IFn.prototype.call = (function (this_sym23394){\nvar this_sym23394__23395 = this;\nvar this__23396 = this_sym23394__23395;\nreturn this__23396.invoke();\n});\nPackages.clojure.lang.IFn.prototype.apply = (function (this_sym23392,args23393){\nreturn this_sym23392.call.apply(this_sym23392,[this_sym23392].concat(args23393.slice()));\n});\n"
org.mozilla.javascript.EcmaError: TypeError: Cannot set property "cljs$core$IFn$" of null to "true" (<cljs repl>#25)
    at <cljs repl>:25 (anonymous)
    at <cljs repl>:25 (anonymous)
    at <cljs repl>:25

I'll keep investigating.

Stuart Campbell

unread,
Dec 30, 2012, 5:56:08 PM12/30/12
to Clojure
OK, unless there's some way to set arbitrary properties on a Java object, I guess this isn't possible...

ClojureScript:cljs.user> (aset js/Packages.clojure.lang.IFn "prototype" (js/Object.))
"Error evaluating:" (aset js/Packages.clojure.lang.IFn "prototype" (js/Object.)) :as "(Packages.clojure.lang.IFn[\"prototype\"] = (new Object()));\n"
org.mozilla.javascript.EvaluatorException: Java class "clojure.lang.IFn" has no public instance field or method named "prototype". (<cljs repl>#9)
    at <cljs repl>:9 (anonymous)
    at <cljs repl>:9

Stuart Campbell

unread,
Jan 3, 2013, 4:00:11 PM1/3/13
to Clojure
In case anybody's interested, I'm messing around with a ClojureScript wrapper type that can implement various core protocols, e.g.:

(deftype ClojureObject [o]
  IFn
  (-invoke [_] (.invoke o))
  (-invoke [_ a] (.invoke o a))
  ;; etc
  )

Reply all
Reply to author
Forward
0 new messages