| Index: mojo/dart/embedder/builtin.cc
|
| diff --git a/mojo/dart/embedder/builtin.cc b/mojo/dart/embedder/builtin.cc
|
| index dcbb33514b6f42f8867e747a8f93de9afd25cd3f..63e158fa9224764a88275fcd7a61adde023d89f2 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,21 @@
|
| namespace mojo {
|
| namespace dart {
|
|
|
| +extern const uint8_t* snapshot_buffer;
|
| +
|
| +static bool EmbedderRunningFromSnapshot() {
|
| + return snapshot_buffer != nullptr;
|
| +}
|
| +
|
| 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_paths_ },
|
| };
|
|
|
| uint8_t Builtin::snapshot_magic_number[] = {0xf5, 0xf5, 0xdc, 0xdc};
|
| @@ -25,7 +37,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 +66,37 @@ void Builtin::SetNativeResolver(BuiltinLibraryId id) {
|
| }
|
| }
|
|
|
| +Dart_Handle Builtin::ReadStringFromFile(const char* file) {
|
| + base::FilePath path(base::FilePath::FromUTF8Unsafe(std::string(file)));
|
| + std::string source;
|
| + if (ReadFileToString(path, &source)) {
|
| + const uint8_t* text_buffer =
|
| + reinterpret_cast<const uint8_t*>(source.c_str());
|
| + const intptr_t len = source.length();
|
| + return Dart_NewStringFromUTF8(text_buffer, len);
|
| + }
|
| + return NewError("Could not load %s", file);
|
| +}
|
| +
|
| +// Patch all the specified patch files in the array 'patch_files' into the
|
| +// library specified in 'library'.
|
| +void Builtin::LoadPatchFiles(Dart_Handle library,
|
| + const char* patch_uri,
|
| + const char** patch_files) {
|
| + for (intptr_t j = 0; patch_files[j] != NULL; j += 2) {
|
| + Dart_Handle patch_src = ReadStringFromFile(patch_files[j + 1]);
|
| + 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_files[j]);
|
| + char* patch_filename = reinterpret_cast<char*>(malloc(len + 1));
|
| + snprintf(patch_filename, len + 1, "%s/%s", patch_uri, patch_files[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 +105,17 @@ 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 (EmbedderRunningFromSnapshot()) {
|
| + // Patch files are included in the snapshot.
|
| + return library;
|
| + }
|
| + if (builtin_libraries_[id].patch_url_ != nullptr) {
|
| + DCHECK(builtin_libraries_[id].patch_paths_ != nullptr);
|
| + LoadPatchFiles(library,
|
| + builtin_libraries_[id].patch_url_,
|
| + builtin_libraries_[id].patch_paths_);
|
| + }
|
| return library;
|
| }
|
|
|
|
|