babashka / sci

Configurable Clojure/Script interpreter suitable for scripting and Clojure DSLs
Eclipse Public License 1.0
1.21k stars 87 forks source link

Can we "add" to native CLJS protocols like ILookup without perf overhead? #639

Open borkdude opened 2 years ago

borkdude commented 2 years ago

SCI contains some protocols like IDeref that users are able to implement in scripts. However, this comes with some perf overhead. Protocols are currently represented as multi-methods in SCI (this is an implementation detail). It seems in CLJS you're able to add to a protocol at runtime, by manipulating objects.

cljs.user=> (extend-type string ILookup (-lookup [this k] k))
nil
cljs.user=> (goog.object/getKeys -lookup)
#js ["cljs$core$IFn$_invoke$arity$2" "cljs$core$IFn$_invoke$arity$3" "cljs$lang$maxFixedArity" "string"]
cljs.user=> (goog.object/getKeys ILookup)
#js ["string"]

If SCI, by permission of the SCI users of course, has access to the native protocol, perhaps we can support extending ILookup etc in user space without much perf overhead.

logseq-cldwalker commented 8 months ago

Hi. I'd vote for sci (and nbb) to be able to support this one day. An example extend-type that would benefit from supporting native CLJS protocols - https://github.com/logseq/logseq/blob/dddd937b535fe887777d6e330fd75cf503587103/deps/db/src/logseq/db/frontend/entity_plus.cljs#L28-L56