Index: runtime/bin/main.cc |
diff --git a/runtime/bin/main.cc b/runtime/bin/main.cc |
index 5e475390a4f13be0a411f8a975dd853aef2a8872..b6e8ee9ef049bddb900a00b4559c829e0b5de546 100644 |
--- a/runtime/bin/main.cc |
+++ b/runtime/bin/main.cc |
@@ -881,16 +881,32 @@ int main(int argc, char** argv) { |
} |
} else { |
// Lookup and invoke the top level main function. |
- Dart_Handle main_args[1]; |
+ // The top-level function may accept up to two arguments: |
+ // main(List<String> args, var message). |
+ // However most commonly it either accepts one (the args list) or |
+ // none. |
+ // If the message is optional, main(args, [message]), it is invoked with |
+ // one argument only. |
+ Dart_Handle main_args[2]; |
main_args[0] = CreateRuntimeOptions(&dart_options); |
+ main_args[1] = Dart_Null(); |
+ // First try with 1 argument. |
result = Dart_Invoke(library, DartUtils::NewString("main"), 1, main_args); |
// TODO(iposva): Return a special error type for mismatched argument |
// counts from Dart_Invoke to avoid the string comparison. |
const char* expected_error = "Dart_Invoke: wrong argument count for " |
- "function 'main': 1 passed, 0 expected."; |
+ "function 'main': "; |
+ intptr_t length = strlen(expected_error); |
if (Dart_IsError(result) && |
- strcmp(expected_error, Dart_GetError(result)) == 0) { |
- result = Dart_Invoke(library, DartUtils::NewString("main"), 0, NULL); |
+ strncmp(expected_error, Dart_GetError(result), length) == 0) { |
+ // Try with two arguments. |
+ result = |
+ Dart_Invoke(library, DartUtils::NewString("main"), 2, main_args); |
+ if (Dart_IsError(result) && |
+ strncmp(expected_error, Dart_GetError(result), length) == 0) { |
+ // Finally try with 0 arguments. |
+ result = Dart_Invoke(library, DartUtils::NewString("main"), 0, NULL); |
+ } |
} |
if (Dart_IsError(result)) { |
return DartErrorExit(result); |