|
| 1 | +// Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file |
| 2 | +// for details. All rights reserved. Use of this source code is governed by a |
| 3 | +// BSD-style license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +import 'dart:ffi'; |
| 6 | +import 'dart:io'; |
| 7 | + |
| 8 | +import 'package:pub_semver/pub_semver.dart'; |
| 9 | +import 'package:test/test.dart'; |
| 10 | + |
| 11 | +import '../helpers.dart'; |
| 12 | +import 'helpers.dart'; |
| 13 | + |
| 14 | +const Timeout longTimeout = Timeout(Duration(minutes: 5)); |
| 15 | + |
| 16 | +void main() async { |
| 17 | + test('use_dart_api build', timeout: longTimeout, () async { |
| 18 | + await inTempDir((tempUri) async { |
| 19 | + await copyTestProjects(targetUri: tempUri); |
| 20 | + final packageUri = tempUri.resolve('use_dart_api/'); |
| 21 | + await runPubGet( |
| 22 | + workingDirectory: packageUri, |
| 23 | + logger: logger, |
| 24 | + ); |
| 25 | + |
| 26 | + // Assume we're run from Dart SDK and try to find the include dir. |
| 27 | + final includeDirectory = dartExecutable.resolve('../include/'); |
| 28 | + final versionFile = includeDirectory.resolve('dart_version.h'); |
| 29 | + final versionContents = |
| 30 | + await File(versionFile.toFilePath()).readAsString(); |
| 31 | + final regex = RegExp( |
| 32 | + r'#define DART_API_DL_MAJOR_VERSION (\d+)\s*#define DART_API_DL_MINOR_VERSION (\d+)'); |
| 33 | + final match = regex.firstMatch(versionContents)!; |
| 34 | + final major = int.parse(match.group(1)!); |
| 35 | + final minor = int.parse(match.group(2)!); |
| 36 | + final dartCApi = DartCApi( |
| 37 | + includeDirectory: includeDirectory, |
| 38 | + version: Version(major, minor, 0), |
| 39 | + ); |
| 40 | + |
| 41 | + // Run the build. |
| 42 | + final result = (await buildCodeAssets( |
| 43 | + packageUri, |
| 44 | + dartCApi: dartCApi, |
| 45 | + ))!; |
| 46 | + final codeAsset = CodeAsset.fromEncoded(result.encodedAssets.single); |
| 47 | + |
| 48 | + // Check that we can load the dylib and run the init. |
| 49 | + final dylib = DynamicLibrary.open(codeAsset.file!.toFilePath()); |
| 50 | + final initDartApiDl = dylib.lookupFunction<IntPtr Function(Pointer<Void>), |
| 51 | + int Function(Pointer<Void>)>('InitDartApiDL'); |
| 52 | + final initResult = initDartApiDl(NativeApi.initializeApiDLData); |
| 53 | + expect(initResult, 0); |
| 54 | + }); |
| 55 | + }); |
| 56 | +} |
0 commit comments