sci
sci copied to clipboard
Support this-as in CLJS
Example:
cljs.user> (def o #js {:a (fn [] (this-as this this))})
#'cljs.user/o
cljs.user> (.a o)
#js {:a #object[a]}
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))))
Another alternative is to wrap every function (in fns.cljc) with this-as (not sure about a performance impact).