Chromium Code Reviews| Index: runtime/bin/main.cc |
| diff --git a/runtime/bin/main.cc b/runtime/bin/main.cc |
| index 59501a952700fcd561ef120d1c5238f5a4682651..86186a4a1cc84e6efe18c1155ffb852ac8980407 100644 |
| --- a/runtime/bin/main.cc |
| +++ b/runtime/bin/main.cc |
| @@ -22,6 +22,7 @@ |
| #include "bin/process.h" |
| #include "bin/vmservice_impl.h" |
| #include "platform/globals.h" |
| +#include "platform/hashmap.h" |
| namespace dart { |
| namespace bin { |
| @@ -72,6 +73,8 @@ static bool start_vm_service = false; |
| static int vm_service_server_port = -1; |
| static const int DEFAULT_VM_SERVICE_SERVER_PORT = 8181; |
| +// The configuration provided through the command line using -D options. |
|
Ivan Posva
2013/10/30 20:44:43
Please change all mentions of "configuration" or "
Søren Gjesse
2013/10/30 21:24:34
Done. That should be the last, the raw patch file
|
| +static dart::HashMap* environment = NULL; |
| static bool IsValidFlag(const char* name, |
| const char* prefix, |
| @@ -132,6 +135,48 @@ static bool ProcessPackageRootOption(const char* arg) { |
| } |
| +static void* GetHashmapKeyFromString(char* key) { |
| + return reinterpret_cast<void*>(key); |
| +} |
| + |
| +static bool ProcessDefineOption(const char* arg) { |
| + ASSERT(arg != NULL); |
| + if (*arg == '\0') { |
| + // Ignore empty -D option. |
| + Log::PrintErr("No arguments given to -D option\n"); |
| + return true; |
| + } |
| + if (environment == NULL) { |
| + environment = new HashMap(&HashMap::SameStringValue, 4); |
| + } |
| + // Split the name=value part of the -Dname=value argument. |
| + char* name; |
| + char* value = NULL; |
| + const char* equals_pos = strchr(arg, '='); |
| + if (equals_pos == NULL) { |
| + // No equal sign (name without value) currently not supported. |
| + Log::PrintErr("No value given to -D option\n"); |
| + return false; |
| + } else { |
| + int name_len = equals_pos - arg; |
| + if (name_len == 0) { |
| + Log::PrintErr("No name given to -D option\n"); |
| + return false; |
| + } |
| + // Split name=value into name and value. |
| + name = reinterpret_cast<char*>(malloc(name_len + 1)); |
| + strncpy(name, arg, name_len); |
| + name[name_len] = '\0'; |
| + value = strdup(equals_pos + 1); |
| + } |
| + HashMap::Entry* entry = environment->Lookup( |
| + GetHashmapKeyFromString(name), HashMap::StringHash(name), true); |
| + ASSERT(entry != NULL); // Lookup adds an entry if key not found. |
| + entry->value = value; |
| + return true; |
| +} |
| + |
| + |
| static bool ProcessCompileAllOption(const char* arg) { |
| ASSERT(arg != NULL); |
| if (*arg != '\0') { |
| @@ -246,6 +291,7 @@ static struct { |
| { "--verbose", ProcessVerboseOption }, |
| { "-v", ProcessVerboseOption }, |
| { "--package-root=", ProcessPackageRootOption }, |
| + { "-D", ProcessDefineOption }, |
| // VM specific options to the standalone dart program. |
| { "--break-at=", ProcessBreakpointOption }, |
| { "--compile_all", ProcessCompileAllOption }, |
| @@ -433,6 +479,37 @@ static Dart_Handle SetupRuntimeOptions(CommandLineOptions* options) { |
| } \ |
| +static Dart_Handle ConfigCallback(Dart_Handle name) { |
| + uint8_t* utf8_array; |
| + intptr_t utf8_len; |
| + Dart_Handle result = Dart_Null(); |
| + Dart_Handle handle = Dart_StringToUTF8(name, &utf8_array, &utf8_len); |
| + if (Dart_IsError(handle)) { |
| + handle = Dart_ThrowException( |
| + DartUtils::NewDartArgumentError(Dart_GetError(handle))); |
| + } else { |
| + char* name_chars = reinterpret_cast<char*>(malloc(utf8_len + 1)); |
|
Ivan Posva
2013/10/30 20:44:43
LEAK
Søren Gjesse
2013/10/30 21:24:34
Done.
|
| + memmove(name_chars, utf8_array, utf8_len); |
| + name_chars[utf8_len] = '\0'; |
| + const char* value = NULL; |
| + if (environment != NULL) { |
| + HashMap::Entry* entry = environment->Lookup( |
| + GetHashmapKeyFromString(name_chars), |
| + HashMap::StringHash(name_chars), |
| + false); |
| + if (entry != NULL) { |
| + value = reinterpret_cast<char*>(entry->value); |
| + } |
| + } |
| + if (value != NULL) { |
| + result = Dart_NewStringFromUTF8(reinterpret_cast<const uint8_t*>(value), |
| + strlen(value)); |
| + } |
| + } |
| + return result; |
| +} |
| + |
| + |
| // Returns true on success, false on failure. |
| static Dart_Isolate CreateIsolateAndSetupHelper(const char* script_uri, |
| const char* main, |
| @@ -457,6 +534,9 @@ static Dart_Isolate CreateIsolateAndSetupHelper(const char* script_uri, |
| Dart_Handle result = Dart_SetLibraryTagHandler(DartUtils::LibraryTagHandler); |
| CHECK_RESULT(result); |
| + result = Dart_SetConfigCallback(ConfigCallback); |
| + CHECK_RESULT(result); |
| + |
| // Load the specified application script into the newly created isolate. |
| // Prepare builtin and its dependent libraries for use to resolve URIs. |