DelSkayn / rquickjs

High level bindings to the quickjs javascript engine
MIT License
434 stars 59 forks source link

Cache JS values in rust "globally"? #237

Open richarddd opened 8 months ago

richarddd commented 8 months ago

From many places in my system I'm accessing constructor objects and functions that cloud benefit from being cached so i dont have to get from either globals or create new objects every time.

My original plan was to create something like this, and populate when i initialize the runtime, and clear before i drop the runtime.

struct ObjectCache {
    array_ctor: Constructor<'static>,
// more stuff here
}

static OBJECT_CACHE: Lazy<RwLock<Option<ObjectCache>>> = Lazy::new(|| RwLock::new(None));

However this is not possible because:

NonNull<JSContext> cannot be shared between threads safely within Option<ObjectCache>, the trait Sync is not implemented for NonNull<JSContext>

Any other suggestion?