Skip to content

Commit d0b188c

Browse files
committed
Start adopting Dart 3 for new WASM VFS features
1 parent 279cd4c commit d0b188c

File tree

11 files changed

+44
-41
lines changed

11 files changed

+44
-41
lines changed

sqlite3/CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
## 2.0.0-dev
2+
3+
- __Breaking__: The WASM implementation no longer registers a default virtual
4+
file system. Instead, `registerVirtualFileSystem` needs to be used to add
5+
desired file system implementations.
6+
7+
18
## 1.11.1
29

310
- Fix user-defined functions returning text not supporting multi-byte utf8

sqlite3/analysis_options.yaml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,3 @@ analyzer:
44
language:
55
strict-casts: true
66
strict-raw-types: true
7-
8-
linter:
9-
rules:
10-
# We already enable `parameter_assignments`, so this doesn't provide much value.
11-
prefer_final_parameters: false

sqlite3/assets/wasm/sqlite_cfg.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
// Don't include the default VFS implementations, we write our own
44
#define SQLITE_OS_OTHER 1
55

6-
// Don't include locking code
6+
// Don't include locking code and WAL which require multiple threads to access
7+
// the same WASM module, something we can't do
78
#define SQLITE_THREADSAFE 0
9+
#define SQLITE_OMIT_WAL 1
810

911
// Our implementation of temporary files is also entirely in-memory,
1012
// so there really is no point in using temp files.
@@ -20,6 +22,10 @@
2022
#define SQLITE_USE_ALLOCA 1
2123
#define SQLITE_BYTEORDER 1234
2224

25+
// This apparently improves performance: https://github.com/sqlite/sqlite/blob/b8d689b6668437e220fa329300e0541ff64e4ef6/ext/wasm/api/sqlite3-wasm.c#L57-L77
26+
#define SQLITE_DEFAULT_CACHE_SIZE -16384
27+
#define SQLITE_DEFAULT_PAGE_SIZE 8192
28+
2329
// We have them, so we may as well let sqlite3 use them?
2430
#define HAVE_ISNAN 1
2531
#define HAVE_LOCALTIME_R 1

sqlite3/lib/src/ffi/sqlite3.g.dart

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1183,16 +1183,16 @@ class Bindings {
11831183
.asFunction<int Function(ffi.Pointer<ffi.Void>)>();
11841184
}
11851185

1186-
class sqlite3_char extends ffi.Opaque {}
1186+
final class sqlite3_char extends ffi.Opaque {}
11871187

1188-
class sqlite3 extends ffi.Opaque {}
1188+
final class sqlite3 extends ffi.Opaque {}
11891189

1190-
class sqlite3_stmt extends ffi.Opaque {}
1190+
final class sqlite3_stmt extends ffi.Opaque {}
11911191

1192-
class sqlite3_backup extends ffi.Opaque {}
1192+
final class sqlite3_backup extends ffi.Opaque {}
11931193

1194-
class sqlite3_api_routines extends ffi.Opaque {}
1194+
final class sqlite3_api_routines extends ffi.Opaque {}
11951195

1196-
class sqlite3_value extends ffi.Opaque {}
1196+
final class sqlite3_value extends ffi.Opaque {}
11971197

1198-
class sqlite3_context extends ffi.Opaque {}
1198+
final class sqlite3_context extends ffi.Opaque {}

sqlite3/lib/src/vfs.dart

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class Sqlite3Filename {
3939
/// consider extending [BaseVirtualFileSystem].
4040
///
4141
/// [vfs]: https://www.sqlite.org/c3ref/vfs.html
42-
abstract class VirtualFileSystem {
42+
abstract base class VirtualFileSystem {
4343
/// The name of this virtual file system.
4444
///
4545
/// This can be passed as an URI parameter when opening databases to select
@@ -78,18 +78,12 @@ abstract class VirtualFileSystem {
7878
}
7979

8080
/// The result of [VirtualFileSystem.xOpen].
81-
class XOpenResult {
82-
final int outFlags;
83-
final VirtualFileSystemFile file;
84-
85-
XOpenResult({required this.outFlags, required this.file});
86-
// todo: Turn into record
87-
}
81+
typedef XOpenResult = ({int outFlags, VirtualFileSystemFile file});
8882

8983
/// A file implemented by a VFS author and returned by [VirtualFileSystem.xOpen].
9084
///
9185
/// To avoid common pitfalls, consider extending [BaseVfsFile] instead.
92-
abstract class VirtualFileSystemFile {
86+
abstract interface class VirtualFileSystemFile {
9387
/// Close this file.
9488
void xClose();
9589

@@ -137,7 +131,7 @@ abstract class VirtualFileSystemFile {
137131

138132
/// A [VirtualFileSystem] implementation that uses a [Random] instance for
139133
/// [xRandomness] and [DateTime.now] for [xCurrentTime].
140-
abstract class BaseVirtualFileSystem extends VirtualFileSystem {
134+
abstract base class BaseVirtualFileSystem extends VirtualFileSystem {
141135
final Random random;
142136

143137
BaseVirtualFileSystem({Random? random, required String name})

sqlite3/lib/src/wasm/vfs/async_opfs/client.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import '../utils.dart';
1212
import 'sync_channel.dart';
1313
import 'worker.dart';
1414

15-
class WasmVfs extends BaseVirtualFileSystem {
15+
final class WasmVfs extends BaseVirtualFileSystem {
1616
final RequestResponseSynchronizer synchronizer;
1717
final MessageSerializer serializer;
1818

@@ -72,7 +72,7 @@ class WasmVfs extends BaseVirtualFileSystem {
7272

7373
final outFlags = result.flag0;
7474
final fd = result.flag1;
75-
return XOpenResult(outFlags: outFlags, file: WasmFile(this, fd));
75+
return (outFlags: outFlags, file: WasmFile(this, fd));
7676
}
7777

7878
@override

sqlite3/lib/src/wasm/vfs/async_opfs/worker.dart

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,7 @@ class VfsWorker {
8989
Future<_ResolvedPath> _resolvePath(String absolutePath,
9090
{bool createDirectories = false}) async {
9191
final fullPath = p.url.relative(absolutePath, from: '/');
92-
final directories = p.url.split(fullPath);
93-
final file = directories.removeLast();
92+
final [...directories, file] = p.url.split(fullPath);
9493

9594
var dirHandle = root;
9695
for (final entry in directories) {

sqlite3/lib/src/wasm/vfs/indexed_db.dart

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,7 @@ class _OffsetAndBuffer {
401401
///
402402
/// In the future, we may want to store individual blocks instead.
403403
404-
class IndexedDbFileSystem extends BaseVirtualFileSystem {
404+
final class IndexedDbFileSystem extends BaseVirtualFileSystem {
405405
final AsynchronousIndexedDbFileSystem _asynchronous;
406406

407407
var _isClosing = false;
@@ -567,8 +567,10 @@ class IndexedDbFileSystem extends BaseVirtualFileSystem {
567567
}
568568
}
569569

570-
return XOpenResult(
571-
outFlags: 0, file: _IndexedDbFile(this, inMemoryFile.file, pathStr));
570+
return (
571+
outFlags: 0,
572+
file: _IndexedDbFile(this, inMemoryFile.file, pathStr),
573+
);
572574
}
573575

574576
@override
@@ -577,7 +579,7 @@ class IndexedDbFileSystem extends BaseVirtualFileSystem {
577579
}
578580
}
579581

580-
class _IndexedDbFile extends VirtualFileSystemFile {
582+
class _IndexedDbFile implements VirtualFileSystemFile {
581583
final IndexedDbFileSystem vfs;
582584
final VirtualFileSystemFile memoryFile;
583585
final String path;
@@ -643,7 +645,7 @@ class _IndexedDbFile extends VirtualFileSystemFile {
643645
}
644646
}
645647

646-
abstract class _IndexedDbWorkItem with LinkedListEntry<_IndexedDbWorkItem> {
648+
sealed class _IndexedDbWorkItem extends LinkedListEntry<_IndexedDbWorkItem> {
647649
final Completer<void> completer = Completer.sync();
648650

649651
/// Insert this item into the [pending] list, returning whether the item was
@@ -664,7 +666,7 @@ abstract class _IndexedDbWorkItem with LinkedListEntry<_IndexedDbWorkItem> {
664666
FutureOr<void> run();
665667
}
666668

667-
class _FunctionWorkItem extends _IndexedDbWorkItem {
669+
final class _FunctionWorkItem extends _IndexedDbWorkItem {
668670
final FutureOr<void> Function() work;
669671
final String description;
670672

@@ -674,7 +676,7 @@ class _FunctionWorkItem extends _IndexedDbWorkItem {
674676
FutureOr<void> run() => work();
675677
}
676678

677-
class _DeleteFileWorkItem extends _IndexedDbWorkItem {
679+
final class _DeleteFileWorkItem extends _IndexedDbWorkItem {
678680
final IndexedDbFileSystem fileSystem;
679681
final String path;
680682

@@ -737,7 +739,7 @@ class _DeleteFileWorkItem extends _IndexedDbWorkItem {
737739
}
738740
}
739741

740-
class _CreateFileWorkItem extends _IndexedDbWorkItem {
742+
final class _CreateFileWorkItem extends _IndexedDbWorkItem {
741743
final IndexedDbFileSystem fileSystem;
742744
final String path;
743745

@@ -750,7 +752,7 @@ class _CreateFileWorkItem extends _IndexedDbWorkItem {
750752
}
751753
}
752754

753-
class _WriteFileWorkItem extends _IndexedDbWorkItem {
755+
final class _WriteFileWorkItem extends _IndexedDbWorkItem {
754756
final IndexedDbFileSystem fileSystem;
755757
final String path;
756758

sqlite3/lib/src/wasm/vfs/memory.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import '../../constants.dart';
77
import '../../vfs.dart';
88
import 'utils.dart';
99

10-
class InMemoryFileSystem extends BaseVirtualFileSystem {
10+
final class InMemoryFileSystem extends BaseVirtualFileSystem {
1111
final Map<String, Uint8List?> fileData = {};
1212

1313
InMemoryFileSystem({super.name = 'dart-memory', super.random});
@@ -40,7 +40,7 @@ class InMemoryFileSystem extends BaseVirtualFileSystem {
4040
}
4141
}
4242

43-
return XOpenResult(
43+
return (
4444
outFlags: 0,
4545
file: _InMemoryFile(
4646
this,

sqlite3/lib/src/wasm/vfs/simple_opfs.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ enum FileType {
4040
/// not in the JavaScript context for a tab or a shared web worker.
4141
///
4242
/// [file system access API]: https://developer.mozilla.org/en-US/docs/Web/API/File_System_Access_API
43-
class SimpleOpfsFileSystem extends BaseVirtualFileSystem {
43+
final class SimpleOpfsFileSystem extends BaseVirtualFileSystem {
4444
// The storage idea here is to open sync file handles at the beginning, so
4545
// that no new async open needs to happen when these callbacks are invoked by
4646
// sqlite3.
@@ -162,7 +162,7 @@ class SimpleOpfsFileSystem extends BaseVirtualFileSystem {
162162
}
163163
}
164164

165-
return XOpenResult(
165+
return (
166166
outFlags: 0,
167167
file: _SimpleOpfsFile(this, recognized, syncHandle, deleteOnClose),
168168
);

sqlite3/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ dev_dependencies:
3232
build_daemon: ^4.0.0
3333
build_runner: ^2.1.7
3434
build_web_compilers: ^4.0.3
35-
ffigen: ^7.0.0
35+
ffigen: ^8.0.1
3636
http: ^0.13.4
3737
lints: ^2.0.1
3838
shelf: ^1.4.0

0 commit comments

Comments
 (0)