@@ -1103,35 +1103,6 @@ impl<'a> Context<'a> {
1103
1103
let mut dst = format ! ( "class {} {{\n " , name) ;
1104
1104
let mut ts_dst = format ! ( "export {}" , dst) ;
1105
1105
1106
- let ( mkweakref, freeref) = if self . config . weak_refs {
1107
- // When weak refs are enabled we use them to automatically free the
1108
- // contents of an exported rust class when it's gc'd. Note that a
1109
- // manual `free` function still exists for deterministic
1110
- // destruction.
1111
- //
1112
- // This is implemented by using a `WeakRefGroup` to run finalizers
1113
- // for all `WeakRef` objects that it creates. Upon construction of
1114
- // a new wasm object we use `makeRef` with "holdings" of a thunk to
1115
- // free the wasm instance. Once the `this` (the instance we're
1116
- // creating) is gc'd then the finalizer will run with the
1117
- // `WeakRef`, and we'll pull out the `holdings`, our pointer.
1118
- //
1119
- // Note, though, that if manual finalization happens we want to
1120
- // cancel the `WeakRef`-generated finalization, so we retain the
1121
- // `WeakRef` in a global map. This global map is then used to
1122
- // `drop()` the `WeakRef` (cancel finalization) whenever it is
1123
- // finalized.
1124
- self . expose_cleanup_groups ( ) ;
1125
- let mk = format ! ( "addCleanup(this, this.ptr, free{});" , name) ;
1126
- let free = "
1127
- CLEANUPS_MAP.get(ptr).drop();
1128
- CLEANUPS_MAP.delete(ptr);
1129
- " ;
1130
- ( mk, free)
1131
- } else {
1132
- ( String :: new ( ) , "" )
1133
- } ;
1134
-
1135
1106
if self . config . debug && !class. has_constructor {
1136
1107
dst. push_str (
1137
1108
"
@@ -1165,29 +1136,52 @@ impl<'a> Context<'a> {
1165
1136
}}
1166
1137
" ,
1167
1138
name,
1168
- mkweakref. replace( "this" , "obj" ) ,
1139
+ if self . config. weak_refs {
1140
+ format!( "{}FinalizationGroup.register(obj, obj.ptr, obj.ptr);" , name)
1141
+ } else {
1142
+ String :: new( )
1143
+ } ,
1169
1144
) ) ;
1170
1145
}
1171
1146
1172
1147
self . global ( & format ! (
1173
1148
"
1174
1149
function free{}(ptr) {{
1175
- {}
1176
1150
wasm.{}(ptr);
1177
1151
}}
1178
1152
" ,
1179
1153
name,
1180
- freeref,
1181
1154
wasm_bindgen_shared:: free_function( & name)
1182
1155
) ) ;
1156
+
1157
+ if self . config . weak_refs {
1158
+ self . global ( & format ! (
1159
+ "
1160
+ const {}FinalizationGroup = new FinalizationGroup((items) => {{
1161
+ for (const ptr of items) {{
1162
+ free{}(ptr);
1163
+ }}
1164
+ }});
1165
+ " ,
1166
+ name,
1167
+ name,
1168
+ ) ) ;
1169
+ }
1170
+
1183
1171
dst. push_str ( & format ! (
1184
1172
"
1185
1173
free() {{
1186
1174
const ptr = this.ptr;
1187
1175
this.ptr = 0;
1176
+ {}
1188
1177
free{}(ptr);
1189
1178
}}
1190
1179
" ,
1180
+ if self . config. weak_refs {
1181
+ format!( "{}FinalizationGroup.unregister(ptr);" , name)
1182
+ } else {
1183
+ String :: new( )
1184
+ } ,
1191
1185
name,
1192
1186
) ) ;
1193
1187
ts_dst. push_str ( " free(): void;" ) ;
@@ -2279,23 +2273,6 @@ impl<'a> Context<'a> {
2279
2273
) ;
2280
2274
}
2281
2275
2282
- fn expose_cleanup_groups ( & mut self ) {
2283
- if !self . should_write_global ( "cleanup_groups" ) {
2284
- return ;
2285
- }
2286
- self . global (
2287
- "
2288
- const CLEANUPS = new WeakRefGroup(x => x.holdings());
2289
- const CLEANUPS_MAP = new Map();
2290
-
2291
- function addCleanup(obj, ptr, free) {
2292
- const ref = CLEANUPS.makeRef(obj, () => free(ptr));
2293
- CLEANUPS_MAP.set(ptr, ref);
2294
- }
2295
- " ,
2296
- ) ;
2297
- }
2298
-
2299
2276
fn describe ( & mut self , name : & str ) -> Option < Descriptor > {
2300
2277
let name = format ! ( "__wbindgen_describe_{}" , name) ;
2301
2278
let descriptor = self . interpreter . interpret_descriptor ( & name, self . module ) ?;
0 commit comments