feat: free memory automatically (#5225)

This commit is contained in:
theanarkh 2026-01-19 06:39:52 +08:00 committed by GitHub
parent b12009a746
commit cd603fa981
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 163 additions and 0 deletions

View file

@ -1,5 +1,10 @@
import { C, Internal, assertInternal } from './constants';
import { Language } from './language';
import { newFinalizer } from './finalization_registry';
const finalizer = newFinalizer((address: number) => {
C._ts_lookahead_iterator_delete(address);
});
export class LookaheadIterator implements Iterable<string> {
/** @internal */
@ -13,6 +18,7 @@ export class LookaheadIterator implements Iterable<string> {
assertInternal(internal);
this[0] = address;
this.language = language;
finalizer?.register(this, address, this);
}
/** Get the current symbol of the lookahead iterator. */
@ -27,6 +33,7 @@ export class LookaheadIterator implements Iterable<string> {
/** Delete the lookahead iterator, freeing its resources. */
delete(): void {
finalizer?.unregister(this);
C._ts_lookahead_iterator_delete(this[0]);
this[0] = 0;
}