Skip to content
This repository was archived by the owner on Jan 29, 2025. It is now read-only.

Guard against reloading native v8 library#15

Open
radhikalism wants to merge 1 commit intoCircleCI-Archived:masterfrom
radhikalism:library-reload
Open

Guard against reloading native v8 library#15
radhikalism wants to merge 1 commit intoCircleCI-Archived:masterfrom
radhikalism:library-reload

Conversation

@radhikalism
Copy link
Copy Markdown
Contributor

The v8wrapper native object only needs to be loaded once; in fact, it must not be reloaded (see magnars/optimus#47).

In Java-land, I understand the loading sequence is usually protected by referencing the library instance as a static property. In Clojure, something effectively similar can be accomplished by conditionally loading the library (only when LIBRARY is unbound), so namespace reloading is safe.

Thoughts?

@pbiggar
Copy link
Copy Markdown
Contributor

pbiggar commented Oct 17, 2015

I wonder would a defonce be better here?

@radhikalism
Copy link
Copy Markdown
Contributor Author

Hm, defonce wouldn't hurt, but the getInstance method is safe to re-run as it returns a singleton reference once the library has been loaded. The code path to guard is the one with System/loadLibrary or load-library-from-class-path calls.

How about something like this?

(defn get-v8-library []
  (try
    (com.sun.jna.NativeLibrary/getInstance "v8wrapper")
    (catch UnsatisfiedLinkError e
      (try
        (System/loadLibrary "v8wrapper")
        (catch UnsatisfiedLinkError e
          (load-library-from-class-path "libv8" ".clj-v8")
          (load-library-from-class-path "libv8wrapper" "")
          (System/setProperty "jna.library.path" (System/getProperty "java.io.tmpdir"))))
      (com.sun.jna.NativeLibrary/getInstance "v8wrapper"))))

(def LIBRARY (get-v8-library))

No need for a forward declaration or bound? check there.

@karneaud
Copy link
Copy Markdown

+bump was this ever solved? i'm using boot with optimus and get the subject error

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants