Skip to content

Instantly share code, notes, and snippets.

@dainiusjocas
Created March 12, 2021 11:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dainiusjocas/0d49f291ae3bf32cd42ff0fe6fbb367b to your computer and use it in GitHub Desktop.
Save dainiusjocas/0d49f291ae3bf32cd42ff0fe6fbb367b to your computer and use it in GitHub Desktop.
GraalVM Javascript context locking to enable multithreaded access
(import (org.graalvm.polyglot Value Context HostAccess)
(java.util Map)
(org.graalvm.polyglot.proxy ProxyObject))
(let [js-fn-constructor (fn [^String script]
(let [^Context ctx (.build (Context/newBuilder (into-array String ["js"])))
^Value f (.eval ctx "js" script)]
(fn [value]
(locking ctx
(.execute f (object-array [value]))))))
js-fn (js-fn-constructor "(val) => console.log(val)")]
(js-fn {:foo "bar"}))
@dainiusjocas
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment