-USING: alien alien.c-types windows.com.syntax
+USING: alien alien.c-types windows.com.syntax init
 windows.com.syntax.private windows.com continuations kernel
 namespaces windows.ole32 libc vocabs assocs accessors arrays
 sequences quotations combinators math words compiler.units
-destructors fry math.parser generalizations ;
+destructors fry math.parser generalizations sets ;
 IN: windows.com.wrapper
 
-TUPLE: com-wrapper vtbls disposed ;
+TUPLE: com-wrapper callbacks vtbls disposed ;
 
 <PRIVATE
 
 SYMBOL: +wrapped-objects+
 +wrapped-objects+ get-global
 [ H{ } +wrapped-objects+ set-global ]
+unless
+
+SYMBOL: +live-wrappers+
++live-wrappers+ get-global
+[ V{ } +live-wrappers+ set-global ]
 unless
 
 SYMBOL: +vtbl-counter+

            first2 (finish-thunk)
        ] bi*
        "stdcall" swap compile-alien-callback
    ] 2map >c-void*-array
    (byte-array-to-malloced-buffer) ;

: (make-vtbls) ( implementations -- vtbls )
    dup [ first ] map (make-iunknown-methods)
    [ >r >r first2 r> r> swap (make-vtbl) ] curry map-index ;

: (malloc-wrapped-object) ( wrapper -- wrapped-object )
    vtbls>> length "void*" heap-size *

    [ dup empty? [ 2drop [ ] ] [ swap 1- '[ , , ndip ] ] if ]
    dip compose ;

: (make-interface-callbacks) ( interface-name quots iunknown-methods n -- words )
    (thunk) (thunked-quots)
    swap [ find-com-interface-definition family-tree-functions ]
    keep (next-vtbl-counter) '[
