fix: remove linux target for now
Signed-off-by: Sphericalkat <me@kat.bio>
This commit is contained in:
8 files changed
+188
-202
No files matched your search
@@ -0,0 +1,32 @@
|
||||
class Article {
|
||||
final String? title;
|
||||
final String? author;
|
||||
final int length;
|
||||
final String? excerpt;
|
||||
final String? siteName;
|
||||
final String? imageUrl;
|
||||
final String? faviconUrl;
|
||||
final String? content;
|
||||
final String? textContent;
|
||||
final String? language;
|
||||
final String? publishedTime;
|
||||
|
||||
Article({
|
||||
required this.title,
|
||||
required this.author,
|
||||
required this.length,
|
||||
required this.excerpt,
|
||||
required this.siteName,
|
||||
required this.imageUrl,
|
||||
required this.faviconUrl,
|
||||
required this.content,
|
||||
required this.textContent,
|
||||
required this.language,
|
||||
required this.publishedTime,
|
||||
});
|
||||
|
||||
@override
|
||||
String toString() {
|
||||
return 'Article{title: $title,\n author: $author,\n length: $length,\n excerpt: $excerpt,\n siteName: $siteName,\n imageUrl: $imageUrl,\n faviconUrl: $faviconUrl,\n content: $content,\n textContent: $textContent,\n language: $language,\n publishedTime: $publishedTime}';
|
||||
}
|
||||
}
|
||||
+73
-29
@@ -9,6 +9,8 @@ import 'dart:async';
|
||||
import 'dart:ffi';
|
||||
import 'dart:io';
|
||||
import 'dart:isolate';
|
||||
import 'package:ffi/ffi.dart';
|
||||
import 'package:readability/article.dart';
|
||||
|
||||
import 'readability_bindings_generated.dart';
|
||||
|
||||
@@ -17,7 +19,7 @@ import 'readability_bindings_generated.dart';
|
||||
/// For very short-lived functions, it is fine to call them on the main isolate.
|
||||
/// They will block the Dart execution while running the native function, so
|
||||
/// only do this for native functions which are guaranteed to be short-lived.
|
||||
int sum(int a, int b) => _bindings.sum(a, b);
|
||||
// int sum(int a, int b) => _bindings.sum(a, b);
|
||||
|
||||
/// A longer lived native function, which occupies the thread calling it.
|
||||
///
|
||||
@@ -29,12 +31,12 @@ int sum(int a, int b) => _bindings.sum(a, b);
|
||||
///
|
||||
/// 1. Reuse a single isolate for various different kinds of requests.
|
||||
/// 2. Use multiple helper isolates for parallel execution.
|
||||
Future<int> sumAsync(int a, int b) async {
|
||||
Future<ArticleResponse> parseAsync(String url) async {
|
||||
final SendPort helperIsolateSendPort = await _helperIsolateSendPort;
|
||||
final int requestId = _nextSumRequestId++;
|
||||
final _SumRequest request = _SumRequest(requestId, a, b);
|
||||
final Completer<int> completer = Completer<int>();
|
||||
_sumRequests[requestId] = completer;
|
||||
final int requestId = _nextParseRequestId++;
|
||||
final _ParseRequest request = _ParseRequest(url, requestId);
|
||||
final Completer<ArticleResponse> completer = Completer<ArticleResponse>();
|
||||
_parseRequests[requestId] = completer;
|
||||
helperIsolateSendPort.send(request);
|
||||
return completer.future;
|
||||
}
|
||||
@@ -58,33 +60,32 @@ final DynamicLibrary _dylib = () {
|
||||
/// The bindings to the native functions in [_dylib].
|
||||
final ReadabilityBindings _bindings = ReadabilityBindings(_dylib);
|
||||
|
||||
|
||||
/// A request to compute `sum`.
|
||||
///
|
||||
/// Typically sent from one isolate to another.
|
||||
class _SumRequest {
|
||||
class _ParseRequest {
|
||||
final String url;
|
||||
final int id;
|
||||
final int a;
|
||||
final int b;
|
||||
|
||||
const _SumRequest(this.id, this.a, this.b);
|
||||
const _ParseRequest(this.url, this.id);
|
||||
}
|
||||
|
||||
/// A response with the result of `sum`.
|
||||
///
|
||||
/// Typically sent from one isolate to another.
|
||||
class _SumResponse {
|
||||
class ArticleResponse {
|
||||
final Article article;
|
||||
final int id;
|
||||
final int result;
|
||||
|
||||
const _SumResponse(this.id, this.result);
|
||||
ArticleResponse({
|
||||
required this.article,
|
||||
required this.id,
|
||||
});
|
||||
}
|
||||
|
||||
/// Counter to identify [_SumRequest]s and [_SumResponse]s.
|
||||
int _nextSumRequestId = 0;
|
||||
/// Counter to identify [_ParseRequest]s and [ArticleResponse]s.
|
||||
int _nextParseRequestId = 0;
|
||||
|
||||
/// Mapping from [_SumRequest] `id`s to the completers corresponding to the correct future of the pending request.
|
||||
final Map<int, Completer<int>> _sumRequests = <int, Completer<int>>{};
|
||||
/// Mapping from [_ParseRequest] `id`s to the completers corresponding to the correct future of the pending request.
|
||||
final Map<int, Completer<ArticleResponse>> _parseRequests =
|
||||
<int, Completer<ArticleResponse>>{};
|
||||
|
||||
/// The SendPort belonging to the helper isolate.
|
||||
Future<SendPort> _helperIsolateSendPort = () async {
|
||||
@@ -103,11 +104,11 @@ Future<SendPort> _helperIsolateSendPort = () async {
|
||||
completer.complete(data);
|
||||
return;
|
||||
}
|
||||
if (data is _SumResponse) {
|
||||
if (data is ArticleResponse) {
|
||||
// The helper isolate sent us a response to a request we sent.
|
||||
final Completer<int> completer = _sumRequests[data.id]!;
|
||||
_sumRequests.remove(data.id);
|
||||
completer.complete(data.result);
|
||||
final Completer<ArticleResponse> completer = _parseRequests[data.id]!;
|
||||
_parseRequests.remove(data.id);
|
||||
completer.complete(data);
|
||||
return;
|
||||
}
|
||||
throw UnsupportedError('Unsupported message type: ${data.runtimeType}');
|
||||
@@ -118,10 +119,53 @@ Future<SendPort> _helperIsolateSendPort = () async {
|
||||
final ReceivePort helperReceivePort = ReceivePort()
|
||||
..listen((dynamic data) {
|
||||
// On the helper isolate listen to requests and respond to them.
|
||||
if (data is _SumRequest) {
|
||||
final int result = _bindings.sum_long_running(data.a, data.b);
|
||||
final _SumResponse response = _SumResponse(data.id, result);
|
||||
sendPort.send(response);
|
||||
if (data is _ParseRequest) {
|
||||
final urlPointer = data.url.toNativeUtf8();
|
||||
final CArticle article = _bindings.Parse(urlPointer);
|
||||
|
||||
// Free the native string.
|
||||
calloc.free(urlPointer);
|
||||
|
||||
// Convert the native article to a Dart article.
|
||||
final articleDart = Article(
|
||||
title:
|
||||
article.title == nullptr ? null : article.title.toDartString(),
|
||||
author: article.author == nullptr
|
||||
? null
|
||||
: article.author.toDartString(),
|
||||
length: article.length,
|
||||
excerpt: article.excerpt == nullptr
|
||||
? null
|
||||
: article.excerpt.toDartString(),
|
||||
siteName: article.site_name == nullptr
|
||||
? null
|
||||
: article.site_name.toDartString(),
|
||||
imageUrl: article.image_url == nullptr
|
||||
? null
|
||||
: article.image_url.toDartString(),
|
||||
faviconUrl: article.favicon_url == nullptr
|
||||
? null
|
||||
: article.favicon_url.toDartString(),
|
||||
content: article.content == nullptr
|
||||
? null
|
||||
: article.content.toDartString(),
|
||||
textContent: article.text_content == nullptr
|
||||
? null
|
||||
: article.text_content.toDartString(),
|
||||
language: article.language == nullptr
|
||||
? null
|
||||
: article.language.toDartString(),
|
||||
publishedTime: article.published_time == nullptr
|
||||
? null
|
||||
: article.published_time.toDartString(),
|
||||
);
|
||||
ArticleResponse articleDartResponse =
|
||||
ArticleResponse(article: articleDart, id: data.id);
|
||||
|
||||
// Free the native article.
|
||||
_bindings.FreeArticle(article);
|
||||
|
||||
sendPort.send(articleDartResponse);
|
||||
return;
|
||||
}
|
||||
throw UnsupportedError('Unsupported message type: ${data.runtimeType}');
|
||||
|
||||
@@ -8,6 +8,8 @@
|
||||
// ignore_for_file: type=lint
|
||||
import 'dart:ffi' as ffi;
|
||||
|
||||
import 'package:ffi/ffi.dart';
|
||||
|
||||
/// Bindings for `src/readability.h`.
|
||||
///
|
||||
/// Regenerate bindings with `dart run ffigen --config ffigen.yaml`.
|
||||
@@ -27,44 +29,92 @@ class ReadabilityBindings {
|
||||
lookup)
|
||||
: _lookup = lookup;
|
||||
|
||||
/// A very short-lived native function.
|
||||
///
|
||||
/// For very short-lived functions, it is fine to call them on the main isolate.
|
||||
/// They will block the Dart execution while running the native function, so
|
||||
/// only do this for native functions which are guaranteed to be short-lived.
|
||||
int sum(
|
||||
int a,
|
||||
int b,
|
||||
CArticle Parse(
|
||||
ffi.Pointer<Utf8> url,
|
||||
) {
|
||||
return _sum(
|
||||
a,
|
||||
b,
|
||||
return _Parse(
|
||||
url,
|
||||
);
|
||||
}
|
||||
|
||||
late final _sumPtr =
|
||||
_lookup<ffi.NativeFunction<ffi.Int Function(ffi.Int, ffi.Int)>>(
|
||||
'sum');
|
||||
late final _sum = _sumPtr.asFunction<int Function(int, int)>();
|
||||
late final _ParsePtr =
|
||||
_lookup<ffi.NativeFunction<CArticle Function(ffi.Pointer<Utf8>)>>(
|
||||
'Parse');
|
||||
late final _Parse =
|
||||
_ParsePtr.asFunction<CArticle Function(ffi.Pointer<Utf8>)>();
|
||||
|
||||
/// A longer lived native function, which occupies the thread calling it.
|
||||
///
|
||||
/// Do not call these kind of native functions in the main isolate. They will
|
||||
/// block Dart execution. This will cause dropped frames in Flutter applications.
|
||||
/// Instead, call these native functions on a separate isolate.
|
||||
int sum_long_running(
|
||||
int a,
|
||||
int b,
|
||||
void FreeArticle(
|
||||
CArticle article,
|
||||
) {
|
||||
return _sum_long_running(
|
||||
a,
|
||||
b,
|
||||
return _FreeArticle(
|
||||
article,
|
||||
);
|
||||
}
|
||||
|
||||
late final _sum_long_runningPtr =
|
||||
_lookup<ffi.NativeFunction<ffi.Int Function(ffi.Int, ffi.Int)>>(
|
||||
'sum_long_running');
|
||||
late final _sum_long_running =
|
||||
_sum_long_runningPtr.asFunction<int Function(int, int)>();
|
||||
late final _FreeArticlePtr =
|
||||
_lookup<ffi.NativeFunction<ffi.Void Function(CArticle)>>('FreeArticle');
|
||||
late final _FreeArticle =
|
||||
_FreeArticlePtr.asFunction<void Function(CArticle)>();
|
||||
}
|
||||
|
||||
final class _GoString_ extends ffi.Struct {
|
||||
external ffi.Pointer<Utf8> p;
|
||||
|
||||
@ptrdiff_t()
|
||||
external int n;
|
||||
}
|
||||
|
||||
typedef ptrdiff_t = ffi.Long;
|
||||
typedef Dartptrdiff_t = int;
|
||||
|
||||
final class CArticle extends ffi.Struct {
|
||||
external ffi.Pointer<Utf8> title;
|
||||
|
||||
external ffi.Pointer<Utf8> author;
|
||||
|
||||
@ffi.Int()
|
||||
external int length;
|
||||
|
||||
external ffi.Pointer<Utf8> excerpt;
|
||||
|
||||
external ffi.Pointer<Utf8> site_name;
|
||||
|
||||
external ffi.Pointer<Utf8> image_url;
|
||||
|
||||
external ffi.Pointer<Utf8> favicon_url;
|
||||
|
||||
/// HTML content
|
||||
external ffi.Pointer<Utf8> content;
|
||||
|
||||
/// text content
|
||||
external ffi.Pointer<Utf8> text_content;
|
||||
|
||||
external ffi.Pointer<Utf8> language;
|
||||
|
||||
external ffi.Pointer<Utf8> published_time;
|
||||
|
||||
external ffi.Pointer<Utf8> err;
|
||||
|
||||
@ffi.Int()
|
||||
external int success;
|
||||
}
|
||||
|
||||
final class GoInterface extends ffi.Struct {
|
||||
external ffi.Pointer<ffi.Void> t;
|
||||
|
||||
external ffi.Pointer<ffi.Void> v;
|
||||
}
|
||||
|
||||
final class GoSlice extends ffi.Struct {
|
||||
external ffi.Pointer<ffi.Void> data;
|
||||
|
||||
@GoInt()
|
||||
external int len;
|
||||
|
||||
@GoInt()
|
||||
external int cap;
|
||||
}
|
||||
|
||||
typedef GoInt = GoInt64;
|
||||
typedef GoInt64 = ffi.LongLong;
|
||||
typedef DartGoInt64 = int;
|
||||
Reference in new issue
Block a user