Skip to content

Commit 04bf879

Browse files
author
George Wright
authored
Add plumbing to grab dart entrypoint args on macOS (flutter#21789)
1 parent 9b75279 commit 04bf879

File tree

3 files changed

+22
-0
lines changed

3 files changed

+22
-0
lines changed

shell/platform/darwin/macos/framework/Headers/FlutterDartProject.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,16 @@ FLUTTER_EXPORT
3636
*/
3737
@property(nonatomic) bool enableMirrors;
3838

39+
/**
40+
* An NSArray of NSStrings to be passed as command line arguments to the Dart entrypoint.
41+
*
42+
* If this is not explicitly set, this will default to the contents of
43+
* [NSProcessInfo arguments], without the binary name.
44+
*
45+
* Set this to nil to pass no arguments to the Dart entrypoint.
46+
*/
47+
@property(nonatomic, nullable) NSArray<NSString*>* dartEntrypointArguments;
48+
3949
@end
4050

4151
#endif // FLUTTER_FLUTTERDARTPROJECT_H_

shell/platform/darwin/macos/framework/Source/FlutterDartProject.mm

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ - (instancetype)initWithPrecompiledDartBundle:(NSBundle*)bundle {
2727
NSAssert(self, @"Super init cannot be nil");
2828

2929
_dartBundle = bundle ?: [NSBundle bundleWithIdentifier:kAppBundleIdentifier];
30+
_dartEntrypointArguments = [[NSProcessInfo processInfo] arguments];
31+
// Remove the first element as it's the binary name
32+
_dartEntrypointArguments = [_dartEntrypointArguments
33+
subarrayWithRange:NSMakeRange(1, _dartEntrypointArguments.count - 1)];
3034
return self;
3135
}
3236

shell/platform/darwin/macos/framework/Source/FlutterEngine.mm

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,11 @@ - (BOOL)runWithEntrypoint:(NSString*)entrypoint {
263263
std::transform(switches.begin(), switches.end(), std::back_inserter(argv),
264264
[](const std::string& arg) -> const char* { return arg.c_str(); });
265265

266+
std::vector<const char*> dartEntrypointArgs;
267+
for (NSString* argument in [_project dartEntrypointArguments]) {
268+
dartEntrypointArgs.push_back([argument UTF8String]);
269+
}
270+
266271
FlutterProjectArgs flutterArguments = {};
267272
flutterArguments.struct_size = sizeof(FlutterProjectArgs);
268273
flutterArguments.assets_path = _project.assetsPath.UTF8String;
@@ -272,6 +277,9 @@ - (BOOL)runWithEntrypoint:(NSString*)entrypoint {
272277
flutterArguments.platform_message_callback = (FlutterPlatformMessageCallback)OnPlatformMessage;
273278
flutterArguments.custom_dart_entrypoint = entrypoint.UTF8String;
274279
flutterArguments.shutdown_dart_vm_when_done = true;
280+
flutterArguments.dart_entrypoint_argc = dartEntrypointArgs.size();
281+
flutterArguments.dart_entrypoint_argv = dartEntrypointArgs.data();
282+
275283
static size_t sTaskRunnerIdentifiers = 0;
276284
const FlutterTaskRunnerDescription cocoa_task_runner_description = {
277285
.struct_size = sizeof(FlutterTaskRunnerDescription),

0 commit comments

Comments
 (0)