make JSFunction Finalizable

This commit is contained in:
nyne
2024-11-11 15:10:44 +08:00
parent 8c26510bb6
commit 4ea1e95201
2 changed files with 12 additions and 3 deletions

View File

@@ -27,9 +27,14 @@ abstract class JSRef {
_refCount++; _refCount++;
} }
bool _released = false;
void free() { void free() {
_refCount--; _refCount--;
if (_refCount < 0) destroy(); if (_refCount < 0 && !_released){
_released = true;
destroy();
}
} }
void destroy(); void destroy();

View File

@@ -150,8 +150,12 @@ class _JSObject extends JSRef {
} }
/// JS function wrapper /// JS function wrapper
class _JSFunction extends _JSObject implements JSInvokable, _IsolateEncodable { class _JSFunction extends _JSObject implements JSInvokable, _IsolateEncodable, Finalizable {
_JSFunction(Pointer<JSContext> ctx, Pointer<JSValue> val) : super(ctx, val); _JSFunction(Pointer<JSContext> ctx, Pointer<JSValue> val) : super(ctx, val) {
_finalizer.attach(this, this);
}
static final _finalizer = Finalizer<_JSFunction>((f) => f.free());
@override @override
invoke(List<dynamic> arguments, [dynamic thisVal]) { invoke(List<dynamic> arguments, [dynamic thisVal]) {