Commit bb2bbc8
n-api: add generic finalizer callback
Add `napi_add_finalizer()`, which provides the ability to attach data
to an arbitrary object and be notified when that object is garbage-
collected so as to have an opportunity to delete the data previously
attached.
This differs from `napi_wrap()` in that it does not use up the private
slot on the object, and is therefore neither removable, nor retrievable
after the call to `napi_add_finalizer()`. It is assumed that the data
is accessible by other means, yet it must be tied to the lifetime of
the object. This is the case for data passed to a dynamically created
function which is itself heap-allocated and must therefore be freed
along with the function.
Fixes: nodejs/abi-stable-node#313
PR-URL: #22244
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>1 parent cf95b61 commit bb2bbc8Copy full SHA for bb2bbc8
File tree
Expand file treeCollapse file tree
5 files changed
+215
-35
lines changedOpen diff view settings
Filter options
- doc/api
- src
- test/addons-napi/test_general
Expand file treeCollapse file tree
5 files changed
+215
-35
lines changedOpen diff view settings
Collapse file
+58Lines changed: 58 additions & 0 deletions
- Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| ||
3238 | 3238 | |
3239 | 3239 | |
3240 | 3240 | |
| 3241 | + |
| 3242 | + |
| 3243 | + |
| 3244 | + |
| 3245 | + |
3241 | 3246 | |
3242 | 3247 | |
3243 | 3248 | |
| ||
3375 | 3380 | |
3376 | 3381 | |
3377 | 3382 | |
| 3383 | + |
| 3384 | + |
| 3385 | + |
| 3386 | + |
| 3387 | + |
3378 | 3388 | |
3379 | 3389 | |
3380 | 3390 | |
| ||
3581 | 3591 | |
3582 | 3592 | |
3583 | 3593 | |
| 3594 | + |
| 3595 | + |
| 3596 | + |
| 3597 | + |
| 3598 | + |
| 3599 | + |
3584 | 3600 | |
3585 | 3601 | |
3586 | 3602 | |
| ||
3685 | 3701 | |
3686 | 3702 | |
3687 | 3703 | |
| 3704 | + |
| 3705 | + |
| 3706 | + |
| 3707 | + |
| 3708 | + |
| 3709 | + |
| 3710 | + |
| 3711 | + |
| 3712 | + |
| 3713 | + |
| 3714 | + |
| 3715 | + |
| 3716 | + |
| 3717 | + |
| 3718 | + |
| 3719 | + |
| 3720 | + |
| 3721 | + |
| 3722 | + |
| 3723 | + |
| 3724 | + |
| 3725 | + |
| 3726 | + |
| 3727 | + |
| 3728 | + |
| 3729 | + |
| 3730 | + |
| 3731 | + |
| 3732 | + |
| 3733 | + |
| 3734 | + |
| 3735 | + |
| 3736 | + |
| 3737 | + |
| 3738 | + |
| 3739 | + |
| 3740 | + |
| 3741 | + |
| 3742 | + |
| 3743 | + |
| 3744 | + |
3688 | 3745 | |
3689 | 3746 | |
3690 | 3747 | |
| ||
4559 | 4616 | |
4560 | 4617 | |
4561 | 4618 | |
| 4619 | + |
4562 | 4620 | |
4563 | 4621 | |
4564 | 4622 | |
|
Collapse file
+77-35Lines changed: 77 additions & 35 deletions
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| ||
1157 | 1157 | |
1158 | 1158 | |
1159 | 1159 | |
| 1160 | + |
| 1161 | + |
| 1162 | + |
| 1163 | + |
| 1164 | + |
| 1165 | + |
| 1166 | + |
| 1167 | + |
| 1168 | + |
| 1169 | + |
| 1170 | + |
| 1171 | + |
| 1172 | + |
| 1173 | + |
| 1174 | + |
| 1175 | + |
| 1176 | + |
| 1177 | + |
| 1178 | + |
| 1179 | + |
| 1180 | + |
| 1181 | + |
| 1182 | + |
| 1183 | + |
| 1184 | + |
| 1185 | + |
| 1186 | + |
| 1187 | + |
| 1188 | + |
| 1189 | + |
| 1190 | + |
| 1191 | + |
| 1192 | + |
| 1193 | + |
| 1194 | + |
| 1195 | + |
| 1196 | + |
| 1197 | + |
| 1198 | + |
| 1199 | + |
| 1200 | + |
| 1201 | + |
| 1202 | + |
| 1203 | + |
| 1204 | + |
| 1205 | + |
| 1206 | + |
| 1207 | + |
| 1208 | + |
| 1209 | + |
| 1210 | + |
| 1211 | + |
| 1212 | + |
| 1213 | + |
| 1214 | + |
| 1215 | + |
| 1216 | + |
1160 | 1217 | |
1161 | 1218 | |
1162 | 1219 | |
| ||
2859 | 2916 | |
2860 | 2917 | |
2861 | 2918 | |
2862 | | - |
2863 | | - |
2864 | | - |
2865 | | - |
2866 | | - |
2867 | | - |
2868 | | - |
2869 | | - |
2870 | | - |
2871 | | - |
2872 | | - |
2873 | | - |
2874 | | - |
2875 | | - |
2876 | | - |
2877 | | - |
2878 | | - |
2879 | | - |
2880 | | - |
2881 | | - |
2882 | | - |
2883 | | - |
2884 | | - |
2885 | | - |
2886 | | - |
2887 | | - |
2888 | | - |
2889 | | - |
2890 | | - |
2891 | | - |
2892 | | - |
2893 | | - |
2894 | | - |
2895 | | - |
2896 | | - |
| 2919 | + |
| 2920 | + |
| 2921 | + |
| 2922 | + |
| 2923 | + |
| 2924 | + |
2897 | 2925 | |
2898 | 2926 | |
2899 | 2927 | |
| ||
4138 | 4166 | |
4139 | 4167 | |
4140 | 4168 | |
| 4169 | + |
| 4170 | + |
| 4171 | + |
| 4172 | + |
| 4173 | + |
| 4174 | + |
| 4175 | + |
| 4176 | + |
| 4177 | + |
| 4178 | + |
| 4179 | + |
| 4180 | + |
| 4181 | + |
| 4182 | + |
Collapse file
+6Lines changed: 6 additions & 0 deletions
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| ||
695 | 695 | |
696 | 696 | |
697 | 697 | |
| 698 | + |
| 699 | + |
| 700 | + |
| 701 | + |
| 702 | + |
| 703 | + |
698 | 704 | |
699 | 705 | |
700 | 706 | |
|
Collapse file
test/addons-napi/test_general/testFinalizer.js
Copy file name to clipboard+33Lines changed: 33 additions & 0 deletions
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| ||
| 1 | + |
| 2 | + |
| 3 | + |
| 4 | + |
| 5 | + |
| 6 | + |
| 7 | + |
| 8 | + |
| 9 | + |
| 10 | + |
| 11 | + |
| 12 | + |
| 13 | + |
| 14 | + |
| 15 | + |
| 16 | + |
| 17 | + |
| 18 | + |
| 19 | + |
| 20 | + |
| 21 | + |
| 22 | + |
| 23 | + |
| 24 | + |
| 25 | + |
| 26 | + |
| 27 | + |
| 28 | + |
| 29 | + |
| 30 | + |
| 31 | + |
| 32 | + |
| 33 | + |
Collapse file
test/addons-napi/test_general/test_general.c
Copy file name to clipboardExpand all lines: test/addons-napi/test_general/test_general.c+41Lines changed: 41 additions & 0 deletions
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| ||
| 1 | + |
1 | 2 | |
2 | 3 | |
3 | 4 | |
| ||
177 | 178 | |
178 | 179 | |
179 | 180 | |
| 181 | + |
| 182 | + |
| 183 | + |
| 184 | + |
| 185 | + |
| 186 | + |
| 187 | + |
| 188 | + |
| 189 | + |
| 190 | + |
| 191 | + |
180 | 192 | |
181 | 193 | |
182 | 194 | |
| ||
232 | 244 | |
233 | 245 | |
234 | 246 | |
| 247 | + |
| 248 | + |
| 249 | + |
| 250 | + |
| 251 | + |
| 252 | + |
| 253 | + |
| 254 | + |
| 255 | + |
| 256 | + |
| 257 | + |
| 258 | + |
| 259 | + |
| 260 | + |
| 261 | + |
| 262 | + |
| 263 | + |
| 264 | + |
| 265 | + |
| 266 | + |
| 267 | + |
| 268 | + |
| 269 | + |
| 270 | + |
| 271 | + |
| 272 | + |
| 273 | + |
235 | 274 | |
236 | 275 | |
237 | 276 | |
| ||
246 | 285 | |
247 | 286 | |
248 | 287 | |
| 288 | + |
249 | 289 | |
| 290 | + |
250 | 291 | |
251 | 292 | |
252 | 293 | |
|
0 commit comments