Index: mojo/dart/embedder/builtin.cc |
diff --git a/mojo/dart/embedder/builtin.cc b/mojo/dart/embedder/builtin.cc |
index dcbb33514b6f42f8867e747a8f93de9afd25cd3f..43a977e86266d4379502f459a429d53f07de2eda 100644 |
--- a/mojo/dart/embedder/builtin.cc |
+++ b/mojo/dart/embedder/builtin.cc |
@@ -4,6 +4,8 @@ |
#include <stdio.h> |
+#include "base/files/file_enumerator.h" |
+#include "base/files/file_path.h" |
#include "base/files/file_util.h" |
#include "base/i18n/icu_util.h" |
#include "dart/runtime/include/dart_api.h" |
@@ -13,11 +15,47 @@ |
namespace mojo { |
namespace dart { |
+struct ResourcesEntry { |
+ const char* path_; |
+ const char* resource_; |
+ int length_; |
+}; |
+ |
+extern ResourcesEntry __dart_embedder_patch_resources_[]; |
+ |
+// Returns -1 if resource could not be found. |
+static int FindResource(const char* path, const char** resource) { |
+ ResourcesEntry* table = &__dart_embedder_patch_resources_[0]; |
+ for (int i = 0; table[i].path_ != NULL; i++) { |
+ const ResourcesEntry& entry = table[i]; |
+ if (strcmp(path, entry.path_) == 0) { |
+ *resource = entry.resource_; |
+ DCHECK(entry.length_ > 0); |
+ return entry.length_; |
+ } |
+ } |
+ *resource = NULL; |
+ return -1; |
+} |
+ |
+const char* __mojo_core_patch_resources[] = { |
+ "/core/buffer_patch.dart", |
+ "/core/data_pipe_patch.dart", |
+ "/core/handle_patch.dart", |
+ "/core/handle_watcher_patch.dart", |
+ "/core/message_pipe_patch.dart", |
+ NULL, |
+}; |
+ |
Builtin::builtin_lib_props Builtin::builtin_libraries_[] = { |
- /* { url_, has_natives_, native_symbol_, native_resolver_ } */ |
- {"dart:mojo_builtin", true, Builtin::NativeSymbol, Builtin::NativeLookup}, |
- {"dart:mojo_bindings", false, nullptr, nullptr}, |
- {"dart:mojo_core", true, MojoNativeSymbol, MojoNativeLookup}, |
+ /* { url_, has_natives_, native_symbol_, native_resolver_, |
+ patch_url_, patch_paths_ } */ |
+ {"dart:mojo_builtin", true, Builtin::NativeSymbol, Builtin::NativeLookup, |
+ nullptr, nullptr }, |
+ {"dart:mojo_bindings", false, nullptr, nullptr, |
+ nullptr, nullptr }, |
+ {"dart:mojo_core", true, MojoNativeSymbol, MojoNativeLookup, |
+ "dart:mojo_core-patch", __mojo_core_patch_resources }, |
}; |
uint8_t Builtin::snapshot_magic_number[] = {0xf5, 0xf5, 0xdc, 0xdc}; |
@@ -25,7 +63,7 @@ uint8_t Builtin::snapshot_magic_number[] = {0xf5, 0xf5, 0xdc, 0xdc}; |
Dart_Handle Builtin::NewError(const char* format, ...) { |
va_list args; |
va_start(args, format); |
- intptr_t len = vsnprintf(NULL, 0, format, args); |
+ intptr_t len = vsnprintf(nullptr, 0, format, args); |
va_end(args); |
char* buffer = reinterpret_cast<char*>(Dart_ScopeAllocate(len + 1)); |
@@ -54,6 +92,34 @@ void Builtin::SetNativeResolver(BuiltinLibraryId id) { |
} |
} |
+Dart_Handle Builtin::GetStringResource(const char* resource_name) { |
+ const char* resource_string; |
+ int resource_length = FindResource(resource_name, &resource_string); |
+ if (resource_length > 0) { |
+ return Dart_NewStringFromCString(resource_string); |
+ } |
+ return NewError("Could not find resource %s", resource_name); |
+} |
+ |
+// Patch all the specified patch files in the array 'patch_resources' into the |
+// library specified in 'library'. |
+void Builtin::LoadPatchFiles(Dart_Handle library, |
+ const char* patch_uri, |
+ const char** patch_resources) { |
+ for (intptr_t j = 0; patch_resources[j] != NULL; j++) { |
+ Dart_Handle patch_src = GetStringResource(patch_resources[j]); |
+ DART_CHECK_VALID(patch_src); |
+ // Prepend the patch library URI to form a unique script URI for the patch. |
+ intptr_t len = snprintf(NULL, 0, "%s%s", patch_uri, patch_resources[j]); |
+ char* patch_filename = reinterpret_cast<char*>(malloc(len + 1)); |
+ snprintf(patch_filename, len + 1, "%s%s", patch_uri, patch_resources[j]); |
+ Dart_Handle patch_file_uri = Dart_NewStringFromCString(patch_filename); |
+ DART_CHECK_VALID(patch_file_uri); |
+ free(patch_filename); |
+ DART_CHECK_VALID(Dart_LibraryLoadPatch(library, patch_file_uri, patch_src)); |
+ } |
+} |
+ |
Dart_Handle Builtin::LoadAndCheckLibrary(BuiltinLibraryId id) { |
static_assert((sizeof(builtin_libraries_) / sizeof(builtin_lib_props)) == |
kInvalidLibrary, "Unexpected number of builtin libraries"); |
@@ -62,6 +128,13 @@ Dart_Handle Builtin::LoadAndCheckLibrary(BuiltinLibraryId id) { |
Dart_Handle url = Dart_NewStringFromCString(builtin_libraries_[id].url_); |
Dart_Handle library = Dart_LookupLibrary(url); |
DART_CHECK_VALID(library); |
+ SetNativeResolver(id); |
+ if (builtin_libraries_[id].patch_url_ != nullptr) { |
+ DCHECK(builtin_libraries_[id].patch_resources_ != nullptr); |
+ LoadPatchFiles(library, |
+ builtin_libraries_[id].patch_url_, |
+ builtin_libraries_[id].patch_resources_); |
+ } |
return library; |
} |