use gc_arena::Collect; use gc_arena::Gc; use gc_arena::Rootable; use piccolo::{Context, UserData, Value}; use std::rc::Rc; use wasm_bindgen::prelude::Closure; #[derive(Collect)] #[collect(require_static)] struct ClosureCollect { #[allow(unused)] inner: Rc>, } pub fn store_named_global_closure<'gc, F: 'static + ?Sized>( ctx: Context<'gc>, name: &'static str, closure: Closure, ) { ctx.set_global( name, UserData::new::>]>( &ctx, Gc::new( &ctx, ClosureCollect { inner: Rc::new(closure), }, ), ), ); } pub fn release_named_global_closure(ctx: Context, name: &'static str) { ctx.set_global(name, Value::Nil); }