ClojureScript can't call JavaScript functions relying on `this`

713 views
Skip to first unread message

Kevin Lynagh

unread,
Aug 28, 2011, 9:22:41 PM8/28/11
to Clojure
I am having trouble using ClojureScript to call JavaScript functions
that exploit prototype injection.
If I'm reading `defmethod :emit invoke` correctly,

https://github.com/clojure/clojurescript/blob/master/src/clj/cljs/compiler.clj#L513

ClojureScript always seems to compile f(x) JavaScript calls as

f.call(null, x)

which trip up any functions that rely on `this` to have certain
properties.
I have two questions:

1) why are function calls compiled to the `f.call(null, ...)` form
rather than just `f(...)`? Is it to support the Closure Compiler?

2) What is the appropriate way to use JavaScript functions that rely
on `this`?
Is there some way to emit `f.call(f, ...)`, or do I need to use `(js*
"f(...)")`?

For reference, here is a minimal JavaScript example of the JavaScript
prototype injection pattern:

var p_injection = function(){};
p_injection.one = function(){ return 1; };

var MyClass = function(){};

var x = new MyClass();
x.two = function(){ return this.one() + 1; };
x.__proto__ = p_injection;
x.two(); // 2
x.two.call(null); // error, object has no method "one()"




Chouser

unread,
Aug 29, 2011, 9:24:25 AM8/29/11
to clo...@googlegroups.com

Have you tried calling the method using the interop form?

(. x (two))

--Chouser

Kevin Lynagh

unread,
Aug 29, 2011, 10:54:13 AM8/29/11
to Clojure
Chouser,

Yes, that does it---I didn't even think about my use of (apply js/f
args), thanks!

Is there a way to use the interop form with variable-arity JavaScript
functions without using `apply`?
This issue came up with my ClojureScript wrapper for D3; I'm using
this macro

(defmacro shim [name]
"Define a proxy to native D3 method"
`(defn ~name [sel# & args#]
(apply (. sel# ~name) args#)))

to define proxy functions.
If I can't use `apply` because of the `this = null` problem, do I have
to write out a case for each arity?

thanks,
kevin

On Aug 29, 6:24 am, Chouser <chou...@gmail.com> wrote:
> On Sun, Aug 28, 2011 at 9:22 PM, Kevin Lynagh <klyn...@gmail.com> wrote:
> > I am having trouble using ClojureScript to call JavaScript functions
> > that exploit prototype injection.
> > If I'm reading `defmethod :emit invoke` correctly,
>
> >    https://github.com/clojure/clojurescript/blob/master/src/clj/cljs/com...
Reply all
Reply to author
Forward
0 new messages