From 7fd166e9f6026e845958a132e1529c5f7fe78fe2 Mon Sep 17 00:00:00 2001 From: Ivan Dlugos Date: Sat, 23 Jan 2021 16:41:03 +0100 Subject: [PATCH 1/4] bindings - fix macOS & Windows library loading include relative and absolute paths where install.sh puts the lib --- lib/src/bindings/bindings.dart | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/lib/src/bindings/bindings.dart b/lib/src/bindings/bindings.dart index e75223fa0..e0e965a3f 100644 --- a/lib/src/bindings/bindings.dart +++ b/lib/src/bindings/bindings.dart @@ -11,8 +11,18 @@ ObjectBoxC loadObjectBoxLib() { var libName = 'objectbox'; if (Platform.isWindows) { libName += '.dll'; + try { + lib = DynamicLibrary.open(libName); + } on ArgumentError { + lib = DynamicLibrary.open('lib/' + libName); + } } else if (Platform.isMacOS) { libName = 'lib' + libName + '.dylib'; + try { + lib = DynamicLibrary.open(libName); + } on ArgumentError { + lib = DynamicLibrary.open('/usr/local/lib/' + libName); + } } else if (Platform.isIOS) { // this works in combination with `'OTHER_LDFLAGS' => '-framework ObjectBox'` in objectbox_flutter_libs.podspec lib = DynamicLibrary.process(); From a9251adae84b2954582e519f9108cd7ce0542733 Mon Sep 17 00:00:00 2001 From: Ivan Dlugos Date: Sat, 23 Jan 2021 18:01:30 +0100 Subject: [PATCH 2/4] FlatBuffers wrapper - fix windows - missing DynamicLibrary.process() --- lib/src/bindings/flatbuffers.dart | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/lib/src/bindings/flatbuffers.dart b/lib/src/bindings/flatbuffers.dart index 0fb0cded2..fda1a66e1 100644 --- a/lib/src/bindings/flatbuffers.dart +++ b/lib/src/bindings/flatbuffers.dart @@ -2,6 +2,7 @@ import 'dart:typed_data'; import 'package:ffi/ffi.dart' as f; import 'dart:ffi'; +import 'dart:io' show Platform; import '../../flatbuffers/flat_buffers.dart' as fb; @@ -80,8 +81,17 @@ class _Allocator extends fb.Allocator { @override void clear(ByteData data, bool _) { - _memset ??= DynamicLibrary.process().lookupFunction< - Void Function(Pointer, Int32, IntPtr), _dart_memset>('memset'); + if (_memset == null) { + DynamicLibrary lib; + if (Platform.isWindows) { + // DynamicLibrary.process() is not available on Windows + lib = DynamicLibrary.open('msvcr100.dll'); + } else { + lib = DynamicLibrary.process(); + } + _memset = lib.lookupFunction, Int32, IntPtr), + _dart_memset>('memset'); + } _memset(_allocs[data].cast(), 0, data.lengthInBytes); } } From fb6fb1042736e234b16f9323fe284ea8d069cd91 Mon Sep 17 00:00:00 2001 From: Ivan Dlugos Date: Sat, 23 Jan 2021 18:01:49 +0100 Subject: [PATCH 3/4] CI - test all supported platforms and versions --- .github/workflows/dart.yml | 34 ++++++++++++++++++++++++++-------- lib/src/query/query.dart | 2 +- pubspec.yaml | 7 ++++--- tool/pub.sh | 12 ++++++++++++ 4 files changed, 43 insertions(+), 12 deletions(-) create mode 100755 tool/pub.sh diff --git a/.github/workflows/dart.yml b/.github/workflows/dart.yml index a2611bf37..599854a92 100644 --- a/.github/workflows/dart.yml +++ b/.github/workflows/dart.yml @@ -19,18 +19,36 @@ jobs: run: ./generator/test.sh lib: - runs-on: ubuntu-20.04 - container: - image: google/dart:latest + strategy: + matrix: + os: + - windows-2019 + - macos-10.15 + - ubuntu-20.04 + dart: + - 2.10.5 + # - 2.9.3 - generator stuck. I remember there was an issue in some dependency but don't remember which one. + - 2.8.4 + - 2.7.2 + runs-on: ${{ matrix.os }} steps: - - uses: actions/checkout@v1 + # Note: dart-sdk from flutter doesn't work on linux, see https://github.com/flutter/flutter/issues/74599 + # - uses: subosito/flutter-action@v1 + # with: + # flutter-version: 1.22.x + # - run: flutter --version + - uses: cedx/setup-dart@v2 + with: + version: ${{ matrix.dart }} + - run: dart --version + - uses: actions/checkout@v2 - name: Install ObjectBox C-API run: ./install.sh - - run: pub get + - run: ./tool/pub.sh get - name: Generate ObjectBox models - run: pub run build_runner build + run: ./tool/pub.sh run build_runner build - name: Run tests - run: pub run test + run: ./tool/pub.sh run test valgrind: runs-on: ubuntu-20.04 @@ -47,4 +65,4 @@ jobs: run: | apt update apt install -y valgrind - - run: ./tool/valgrind.sh \ No newline at end of file + - run: ./tool/valgrind.sh diff --git a/lib/src/query/query.dart b/lib/src/query/query.dart index ad0fc906e..ec234d7c6 100644 --- a/lib/src/query/query.dart +++ b/lib/src/query/query.dart @@ -754,7 +754,7 @@ class Query { try { final idArray = idArrayPtr.ref; return idArray.count == 0 - ? List.empty() + ? List.filled(0, 0) : idArray.ids.asTypedList(idArray.count).toList(growable: false); } finally { C.id_array_free(idArrayPtr); diff --git a/pubspec.yaml b/pubspec.yaml index 4507d2341..1856ea771 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -6,7 +6,8 @@ description: ObjectBox is a super-fast NoSQL ACID compliant object database. environment: # sdk: '>=2.12.0-0 <3.0.0' - sdk: '>=2.6.0 <3.0.0' + # min 2.7.0 because of ffigen + sdk: '>=2.7.0 <3.0.0' dependencies: collection: ^1.14.11 @@ -20,8 +21,8 @@ dev_dependencies: build_runner: ^1.0.0 objectbox_generator: path: generator - pedantic: ^1.10.0-nullsafety.0 - test: ^1.16.0-nullsafety.0 + pedantic: ^1.9.0 + test: ^1.0.0 ffigen: ^1.1.0 flat_buffers: 1.12.0 diff --git a/tool/pub.sh b/tool/pub.sh new file mode 100755 index 000000000..b9ed4b573 --- /dev/null +++ b/tool/pub.sh @@ -0,0 +1,12 @@ +#!/usr/bin/env bash +set -euo pipefail + +# forward all arguments to an available version of `pub` + +if [[ `command -v pub` ]]; then + pub "$@" +elif [[ `command -v pub.bat` ]]; then + pub.bat "$@" +else + dart pub "$@" +fi From 7741f9a019b20e10979a7293008c6597c56d01fc Mon Sep 17 00:00:00 2001 From: Ivan Dlugos Date: Mon, 25 Jan 2021 16:08:09 +0100 Subject: [PATCH 4/4] flatbuffers builder - add memset alternative if we can't load the native version --- lib/src/bindings/flatbuffers.dart | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/lib/src/bindings/flatbuffers.dart b/lib/src/bindings/flatbuffers.dart index fda1a66e1..bc61549d3 100644 --- a/lib/src/bindings/flatbuffers.dart +++ b/lib/src/bindings/flatbuffers.dart @@ -82,15 +82,28 @@ class _Allocator extends fb.Allocator { @override void clear(ByteData data, bool _) { if (_memset == null) { - DynamicLibrary lib; - if (Platform.isWindows) { - // DynamicLibrary.process() is not available on Windows - lib = DynamicLibrary.open('msvcr100.dll'); - } else { - lib = DynamicLibrary.process(); + try { + DynamicLibrary lib; + if (Platform.isWindows) { + // DynamicLibrary.process() is not available on Windows, let's load a + // lib that defines 'memset()' it - should be mscvr100 or mscvrt DLL. + // mscvr100.dll is in the frequently installed MSVC Redistributable. + lib = DynamicLibrary.open('msvcr100.dll'); + } else { + lib = DynamicLibrary.process(); + } + _memset = lib.lookupFunction< + Void Function(Pointer, Int32, IntPtr), + _dart_memset>('memset'); + } catch (_) { + // fall back if we can't load a native memset() + _memset = (Pointer ptr, int byte, int size) { + final bytes = ptr.cast(); + for (var i = 0; i < size; i++) { + bytes[i] = byte; + } + }; } - _memset = lib.lookupFunction, Int32, IntPtr), - _dart_memset>('memset'); } _memset(_allocs[data].cast(), 0, data.lengthInBytes); }