[native_extensions] Convert uri to path so that on Windows native extension loading works.

Lookup of native extension library on Windows works when the library is next
to Platform.executable, this CL fixes lookup when it's next to the script.

Fixes https://github.com/dart-lang/sdk/issues/45370

TEST=updated sample_extension test

Change-Id: I91226be19ceec05e45cf90fd85428a62d95769c2
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/193741
Commit-Queue: Alexander Aprelev <aam@google.com>
Reviewed-by: Siva Annamalai <asiva@google.com>
This commit is contained in:
Alexander Aprelev
2021-04-01 03:22:35 +00:00
committed by commit-bot@chromium.org
parent e673c19820
commit e70690e415
3 changed files with 93 additions and 43 deletions
+4 -2
View File
@@ -79,7 +79,8 @@ Dart_Handle Loader::LoadImportExtension(const char* url_string,
char* lib_path = NULL;
if (strncmp(lib_uri_str, "file://", 7) == 0) {
lib_path = DartUtils::DirName(lib_uri_str + 7);
auto path = File::UriToPath(lib_uri_str);
lib_path = DartUtils::DirName(path.get());
} else {
lib_path = Utils::StrDup(lib_uri_str);
}
@@ -125,7 +126,8 @@ Dart_Handle Loader::ReloadNativeExtensions() {
char* lib_path = NULL;
if (strncmp(lib_uri, "file://", 7) == 0) {
lib_path = DartUtils::DirName(DartUtils::RemoveScheme(lib_uri));
auto path = File::UriToPath(lib_uri);
lib_path = DartUtils::DirName(path.get());
} else {
lib_path = Utils::StrDup(lib_uri);
}
@@ -44,6 +44,30 @@ Future run(String program, List<String> arguments) async {
}
}
Future runTests(
String program, String testDirectory, String? snapshotKind) async {
for (var test in [
'test_sample_synchronous_extension.dart',
'test_sample_asynchronous_extension.dart'
]) {
String script = join(testDirectory, test);
String snapshot;
if (snapshotKind == null) {
snapshot = script;
} else {
snapshot = join(testDirectory, "$test.snapshot");
await run(Platform.executable, <String>[
...Platform.executableArguments,
'--snapshot=$snapshot',
'--snapshot-kind=$snapshotKind',
script
]);
}
await run(program, <String>[...Platform.executableArguments, snapshot]);
}
}
Future testNativeExtensions(String? snapshotKind) async {
String buildDirectory = dirname(Platform.executable);
Directory tempDirectory =
@@ -63,27 +87,27 @@ Future testNativeExtensions(String? snapshotKind) async {
await copyFileToDirectory(join(sourceDirectory, file), testDirectory);
}
for (var test in [
'test_sample_synchronous_extension.dart',
'test_sample_asynchronous_extension.dart'
]) {
String script = join(testDirectory, test);
String snapshot;
if (snapshotKind == null) {
snapshot = script;
} else {
snapshot = join(testDirectory, "$test.snapshot");
List<String> args = new List<String>.from(Platform.executableArguments);
args.add('--snapshot=$snapshot');
args.add('--snapshot-kind=$snapshotKind');
args.add(script);
await run(Platform.executable, args);
}
// Test native library resolution when it's next to the binary
await runTests(Platform.executable, testDirectory, snapshotKind);
List<String> args = new List<String>.from(Platform.executableArguments);
args.add(snapshot);
await run(Platform.executable, args);
}
// Test native library resolution when it's next to the source
await copyFileToDirectory(
join(
buildDirectory,
(Platform.isWindows ? '' : 'lib') +
'sample_extension' +
(Platform.isWindows
? '.dll'
: Platform.isMacOS
? '.dylib'
: '.so')),
testDirectory);
Directory tempBinDirectory = Directory(join(tempDirectory.path, 'dart-bin'))
..createSync();
await copyFileToDirectory(Platform.executable, tempBinDirectory.path);
String copyPlatformExecutable =
join(tempBinDirectory.path, basename(Platform.executable));
await runTests(copyPlatformExecutable, testDirectory, snapshotKind);
} finally {
tempDirectory.deleteSync(recursive: true);
}
@@ -26,7 +26,7 @@ Future copyFileToDirectory(String file, String directory) async {
result = await Process.run('cmd.exe', ['/C', 'copy $src $dst']);
break;
default:
Expect.fail('Unknown operating system ${Platform.operatingSystem}');
throw 'Unknown operating system ${Platform.operatingSystem}';
}
if (result.exitCode != 0) {
print(result.stdout);
@@ -46,6 +46,30 @@ Future run(String program, List<String> arguments) async {
}
}
Future runTests(
String program, String testDirectory, String snapshotKind) async {
for (var test in [
'test_sample_synchronous_extension.dart',
'test_sample_asynchronous_extension.dart'
]) {
String script = join(testDirectory, test);
String snapshot;
if (snapshotKind == null) {
snapshot = script;
} else {
snapshot = join(testDirectory, "$test.snapshot");
await run(Platform.executable, <String>[
...Platform.executableArguments,
'--snapshot=$snapshot',
'--snapshot-kind=$snapshotKind',
script
]);
}
await run(program, <String>[...Platform.executableArguments, snapshot]);
}
}
Future testNativeExtensions(String snapshotKind) async {
String buildDirectory = dirname(Platform.executable);
Directory tempDirectory =
@@ -65,27 +89,27 @@ Future testNativeExtensions(String snapshotKind) async {
await copyFileToDirectory(join(sourceDirectory, file), testDirectory);
}
for (var test in [
'test_sample_synchronous_extension.dart',
'test_sample_asynchronous_extension.dart'
]) {
String script = join(testDirectory, test);
String snapshot;
if (snapshotKind == null) {
snapshot = script;
} else {
snapshot = join(testDirectory, "$test.snapshot");
List<String> args = new List<String>.from(Platform.executableArguments);
args.add('--snapshot=$snapshot');
args.add('--snapshot-kind=$snapshotKind');
args.add(script);
await run(Platform.executable, args);
}
// Test native library resolution when it's next to the binary
await runTests(Platform.executable, testDirectory, snapshotKind);
List<String> args = new List<String>.from(Platform.executableArguments);
args.add(snapshot);
await run(Platform.executable, args);
}
// Test native library resolution when it's next to the source
await copyFileToDirectory(
join(
buildDirectory,
(Platform.isWindows ? '' : 'lib') +
'sample_extension' +
(Platform.isWindows
? '.dll'
: Platform.isMacOS
? '.dylib'
: '.so')),
testDirectory);
Directory tempBinDirectory = Directory(join(tempDirectory.path, 'dart-bin'))
..createSync();
await copyFileToDirectory(Platform.executable, tempBinDirectory.path);
String copyPlatformExecutable =
join(tempBinDirectory.path, basename(Platform.executable));
await runTests(copyPlatformExecutable, testDirectory, snapshotKind);
} finally {
tempDirectory.deleteSync(recursive: true);
}