|
225 | 225 | :rty-new-host (b "wasmtime_component_resource_type_new_host" ADDR I32) |
226 | 226 | :rty-equal (b "wasmtime_component_resource_type_equal" ValueLayout/JAVA_BOOLEAN ADDR ADDR) |
227 | 227 | :rty-clone (b "wasmtime_component_resource_type_clone" ADDR ADDR) |
| 228 | + :rty-delete (b "wasmtime_component_resource_type_delete" nil ADDR) |
228 | 229 | :hres-new (b "wasmtime_component_resource_host_new" ADDR ValueLayout/JAVA_BOOLEAN I32 I32) |
229 | 230 | :hres-rep (b "wasmtime_component_resource_host_rep" I32 ADDR) |
230 | 231 | :hres-delete (b "wasmtime_component_resource_host_delete" nil ADDR) |
|
988 | 989 | (when (invoke nth-f ty (.-ptr e) (long i) pp lp item) |
989 | 990 | (let [nm (read-name pp lp) |
990 | 991 | k (item-kind item) |
991 | | - of (.get item ADDR (long ITEM-OF))] |
992 | | - (cond |
993 | | - (= ITEM-FUNC k) |
994 | | - [{:key (if prefix (str prefix "#" nm) nm) |
995 | | - :path (if prefix [prefix nm] [nm]) |
996 | | - :sig (reflect-func api arena of)}] |
997 | | - |
998 | | - ;; One level: WIT does not nest interfaces. |
999 | | - (and (= ITEM-IFACE k) (nil? prefix)) |
1000 | | - (walk of (:iface-nth api) (:iface-count api) nm)))))] |
| 992 | + of (.get item ADDR (long ITEM-OF)) |
| 993 | + r (cond |
| 994 | + (= ITEM-FUNC k) |
| 995 | + [{:key (if prefix (str prefix "#" nm) nm) |
| 996 | + :path (if prefix [prefix nm] [nm]) |
| 997 | + :sig (reflect-func api arena of)}] |
| 998 | + |
| 999 | + ;; One level: WIT does not nest interfaces. |
| 1000 | + (and (= ITEM-IFACE k) (nil? prefix)) |
| 1001 | + (walk of (:iface-nth api) (:iface-count api) nm))] |
| 1002 | + ;; Every _nth-filled item owns its union pointer |
| 1003 | + ;; and "must be deallocated" (types/component.h) |
| 1004 | + ;; — after `of` has been fully consumed above. |
| 1005 | + (invoke (:item-delete api) item) |
| 1006 | + r)))] |
1001 | 1007 | (into [] (comp (keep one) cat) |
1002 | 1008 | (range (invoke count-f ty (.-ptr e))))))] |
1003 | 1009 | (walk ct (:export-nth api) (:export-count api) nil))) |
|
1014 | 1020 | (when (invoke (:import-nth api) ct (.-ptr e) (long i) pp lp item) |
1015 | 1021 | (let [nm (read-name pp lp) |
1016 | 1022 | k (kind) |
1017 | | - of (.get item ADDR (long ITEM-OF))] |
1018 | | - (cond |
1019 | | - (= ITEM-FUNC k) [[nm (reflect-func api arena of)]] |
1020 | | - (= ITEM-IFACE k) |
1021 | | - (let [[ip il] (name-pair arena) |
1022 | | - sub ^MemorySegment (.allocate arena (long ITEM))] |
1023 | | - (into [] |
1024 | | - (keep (fn [j] |
1025 | | - (when (invoke (:iface-nth api) of (.-ptr e) (long j) ip il sub) |
1026 | | - (when (= ITEM-FUNC (bit-and (long (.get sub I8 (long 0))) 0xFF)) |
1027 | | - [(str nm "#" (read-name ip il)) |
1028 | | - (reflect-func api arena (.get sub ADDR (long ITEM-OF)))])))) |
1029 | | - (range (invoke (:iface-count api) of (.-ptr e))))) |
1030 | | - :else nil))))) |
| 1023 | + of (.get item ADDR (long ITEM-OF)) |
| 1024 | + r (cond |
| 1025 | + (= ITEM-FUNC k) [[nm (reflect-func api arena of)]] |
| 1026 | + (= ITEM-IFACE k) |
| 1027 | + (let [[ip il] (name-pair arena) |
| 1028 | + sub ^MemorySegment (.allocate arena (long ITEM))] |
| 1029 | + (into [] |
| 1030 | + (keep (fn [j] |
| 1031 | + (when (invoke (:iface-nth api) of (.-ptr e) (long j) ip il sub) |
| 1032 | + (let [f? (= ITEM-FUNC (bit-and (long (.get sub I8 (long 0))) 0xFF)) |
| 1033 | + r (when f? |
| 1034 | + [(str nm "#" (read-name ip il)) |
| 1035 | + (reflect-func api arena (.get sub ADDR (long ITEM-OF)))])] |
| 1036 | + (invoke (:item-delete api) sub) |
| 1037 | + r)))) |
| 1038 | + (range (invoke (:iface-count api) of (.-ptr e))))) |
| 1039 | + :else nil)] |
| 1040 | + (invoke (:item-delete api) item) |
| 1041 | + r)))) |
1031 | 1042 | cat) |
1032 | 1043 | (range (invoke (:import-count api) ct (.-ptr e)))))) |
1033 | 1044 |
|
|
1054 | 1065 | (comp |
1055 | 1066 | (keep (fn [i] |
1056 | 1067 | (when (invoke (:import-nth api) ct (.-ptr e) (long i) pp lp item) |
1057 | | - (let [nm (read-name pp lp)] |
1058 | | - (when (= ITEM-IFACE (bit-and (long (.get item I8 (long 0))) 0xFF)) |
1059 | | - (let [of (.get item ADDR (long ITEM-OF)) |
1060 | | - [ip il] (name-pair arena) |
1061 | | - sub ^MemorySegment (.allocate arena (long ITEM))] |
1062 | | - (into [] |
1063 | | - (keep (fn [j] |
1064 | | - (when (invoke (:iface-nth api) of (.-ptr e) (long j) ip il sub) |
1065 | | - (when (= ITEM-RESOURCE (bit-and (long (.get sub I8 (long 0))) 0xFF)) |
1066 | | - [(str nm "#" (read-name ip il)) |
1067 | | - (invoke (:rty-clone api) (.get sub ADDR (long ITEM-OF)))])))) |
1068 | | - (range (invoke (:iface-count api) of (.-ptr e)))))))))) |
| 1068 | + (let [nm (read-name pp lp) |
| 1069 | + r (when (= ITEM-IFACE (bit-and (long (.get item I8 (long 0))) 0xFF)) |
| 1070 | + (let [of (.get item ADDR (long ITEM-OF)) |
| 1071 | + [ip il] (name-pair arena) |
| 1072 | + sub ^MemorySegment (.allocate arena (long ITEM))] |
| 1073 | + (into [] |
| 1074 | + (keep (fn [j] |
| 1075 | + (when (invoke (:iface-nth api) of (.-ptr e) (long j) ip il sub) |
| 1076 | + (let [res? (= ITEM-RESOURCE (bit-and (long (.get sub I8 (long 0))) 0xFF)) |
| 1077 | + ;; Clone before the item goes: the clone is owned. |
| 1078 | + r (when res? |
| 1079 | + [(str nm "#" (read-name ip il)) |
| 1080 | + (invoke (:rty-clone api) (.get sub ADDR (long ITEM-OF)))])] |
| 1081 | + (invoke (:item-delete api) sub) |
| 1082 | + r)))) |
| 1083 | + (range (invoke (:iface-count api) of (.-ptr e))))))] |
| 1084 | + (invoke (:item-delete api) item) |
| 1085 | + r)))) |
1069 | 1086 | cat) |
1070 | 1087 | (range (invoke (:import-count api) ct (.-ptr e))))] |
1071 | 1088 | ;; Cluster: each entry is {:names [...] :type ptr}. |
|
1130 | 1147 | (cstr arena rname) |
1131 | 1148 | (long (count (.getBytes ^String rname "UTF-8"))) |
1132 | 1149 | rty stub MemorySegment/NULL MemorySegment/NULL)))) |
1133 | | - nil)))))))) |
| 1150 | + ;; add_resource borrows the type (c-api takes &ty and copies), |
| 1151 | + ;; so the host still owns it, and the header requires delete. |
| 1152 | + (invoke (:rty-delete api) rty) |
| 1153 | + nil)))) |
| 1154 | + (doseq [cls classes] |
| 1155 | + (invoke (:rty-delete api) (:type cls))))))) |
1134 | 1156 |
|
1135 | 1157 | (defn- define-imports! |
1136 | 1158 | "Registers each supplied Clojure function with the linker. Missing imports |
|
1234 | 1256 | rt {:ctx ctx :api api :registry registry :pending pending} |
1235 | 1257 | found (reflect-exports api arena e ct) |
1236 | 1258 | sigs (into {} (map (fn [x] [(:key x) (:sig x)])) found) |
| 1259 | + ;; The component type "must be deallocated" (component.h), and this is |
| 1260 | + ;; its last use. One leaked per instantiate is one per request. |
| 1261 | + _ (invoke (:type-delete api) ct) |
1237 | 1262 | index-of (fn [path] |
1238 | 1263 | (when (empty? path) |
1239 | 1264 | (throw (ex-info "an export with no path" {:cljwit/error :internal}))) |
|
1283 | 1308 | _ (closed! (.-closed e) "engine") |
1284 | 1309 | api (.-api e)] |
1285 | 1310 | (with-open [arena (Arena/ofConfined)] |
1286 | | - (let [ct (invoke (:comp-type api) (.-ptr art))] |
1287 | | - (into {} |
1288 | | - (map (fn [{:keys [key sig]}] [key sig])) |
1289 | | - (reflect-exports api arena e ct)))))) |
| 1311 | + (let [ct (invoke (:comp-type api) (.-ptr art)) |
| 1312 | + found (reflect-exports api arena e ct)] |
| 1313 | + (invoke (:type-delete api) ct) |
| 1314 | + (into {} (map (fn [{:keys [key sig]}] [key sig])) found))))) |
1290 | 1315 |
|
1291 | 1316 | (defn drop-failures |
1292 | 1317 | "Anything a `:drop` threw, and clears them. `0018` E: a destructor that |
|
0 commit comments