From e70690e4159169a70810f2c4cad6011a4ec60fba Mon Sep 17 00:00:00 2001 From: Alexander Aprelev Date: Thu, 1 Apr 2021 03:22:35 +0000 Subject: [PATCH] [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 Reviewed-by: Siva Annamalai --- runtime/bin/loader.cc | 6 +- .../test/sample_extension_test_helper.dart | 64 ++++++++++++------ .../test/sample_extension_test_helper.dart | 66 +++++++++++++------ 3 files changed, 93 insertions(+), 43 deletions(-) diff --git a/runtime/bin/loader.cc b/runtime/bin/loader.cc index e855baf975c..df166696d69 100644 --- a/runtime/bin/loader.cc +++ b/runtime/bin/loader.cc @@ -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); } diff --git a/samples/sample_extension/test/sample_extension_test_helper.dart b/samples/sample_extension/test/sample_extension_test_helper.dart index d4cae3da4b5..a4a78ced64a 100644 --- a/samples/sample_extension/test/sample_extension_test_helper.dart +++ b/samples/sample_extension/test/sample_extension_test_helper.dart @@ -44,6 +44,30 @@ Future run(String program, List 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, [ + ...Platform.executableArguments, + '--snapshot=$snapshot', + '--snapshot-kind=$snapshotKind', + script + ]); + } + + await run(program, [...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 args = new List.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 args = new List.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); } diff --git a/samples_2/sample_extension/test/sample_extension_test_helper.dart b/samples_2/sample_extension/test/sample_extension_test_helper.dart index 9c701bf3f84..cabbb28adf7 100644 --- a/samples_2/sample_extension/test/sample_extension_test_helper.dart +++ b/samples_2/sample_extension/test/sample_extension_test_helper.dart @@ -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 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, [ + ...Platform.executableArguments, + '--snapshot=$snapshot', + '--snapshot-kind=$snapshotKind', + script + ]); + } + + await run(program, [...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 args = new List.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 args = new List.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); }