sci icon indicating copy to clipboard operation
sci copied to clipboard

Support this-as in CLJS

Open borkdude opened this issue 4 years ago • 2 comments

Example:

cljs.user> (def o #js {:a (fn [] (this-as this this))})
#'cljs.user/o
cljs.user> (.a o)
#js {:a #object[a]}

borkdude avatar May 24 '21 21:05 borkdude

This is possible as a workaround:

user=> (def thisAs (new js/Function "return this"))
#'user/thisAs
user=> (def obj #js {:fn thisAs})
#'user/obj
user=> (.fn obj)
#js {:fn #object[anonymous]}

We could leverage this workaround in SCI by tracking the local that was introduced by this-as and everywhere the local is used, we just call the special thisAs function instead.

This doesn't work:

(sci/eval-form (sci/init {:bindings {'thisAs (js/Function. "return this") }}) (do (def obj (clj->js {:fn (fn [] (thisAs))})) (.fn obj)))

but this does work:

(def obj (sci/eval-form (sci/init {:bindings {'thisAs (js/Function. "return this") }}) (do (def obj (clj->js {:fn thisAs})) (.fn obj))))

borkdude avatar Jun 08 '23 21:06 borkdude

Another alternative is to wrap every function (in fns.cljc) with this-as (not sure about a performance impact).

borkdude avatar Jun 08 '23 21:06 borkdude