adoptTrustedNativeToken method
Claims one frozen handle published by a trusted native integration.
token must point to initialized, writable NativeBufferToken storage.
The native producer owns its release loop and resource lifetime; this
method neither registers callbacks nor dispatches them on a Dart thread.
ABI, identity and mutable-handle rejection leave the token unconsumed. Successful validation clears the token before constructing the owner; later construction failure releases that claimed handle exactly once.
Implementation
NativeOwnedBuffer adoptTrustedNativeToken(
ffi.Pointer<NativeBufferToken> token,
) {
_require();
if (!_externalSupported) {
throw UnsupportedError('Native producer-lease ABI v1 is unavailable');
}
if (token.address == 0) {
throw ArgumentError.value(token, 'token', 'Null native token');
}
final value = token.ref;
final id = value.handle;
if (id <= 0 || value.identity != _externalIdentity) {
throw ArgumentError('Invalid native token or different buffer library');
}
final info = _getInfoId(id);
if (info.writable != 0) {
throw StateError('Native token must reference an immutable buffer');
}
value.handle = 0;
value.identity = ffi.nullptr;
final handle = _OwnedHandle(this, id, externalSize: info.capacity);
try {
return NativeOwnedBuffer._(handle, info.length, 0, 0);
} catch (_) {
handle.dispose();
rethrow;
}
}