OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <stdio.h> | 5 #include <stdio.h> |
6 | 6 |
7 #include "base/files/file_util.h" | |
8 #include "base/i18n/icu_util.h" | 7 #include "base/i18n/icu_util.h" |
9 #include "dart/runtime/include/dart_api.h" | 8 #include "dart/runtime/include/dart_api.h" |
10 #include "mojo/dart/embedder/builtin.h" | 9 #include "mojo/dart/embedder/builtin.h" |
11 #include "mojo/dart/embedder/mojo_natives.h" | 10 #include "mojo/dart/embedder/mojo_natives.h" |
12 | 11 |
13 namespace mojo { | 12 namespace mojo { |
14 namespace dart { | 13 namespace dart { |
15 | 14 |
| 15 struct ResourcesEntry { |
| 16 const char* path_; |
| 17 const char* resource_; |
| 18 int length_; |
| 19 }; |
| 20 |
| 21 extern ResourcesEntry __dart_embedder_patch_resources_[]; |
| 22 |
| 23 // Returns -1 if resource could not be found. |
| 24 static int FindResource(const char* path, const uint8_t** resource) { |
| 25 ResourcesEntry* table = &__dart_embedder_patch_resources_[0]; |
| 26 for (int i = 0; table[i].path_ != NULL; i++) { |
| 27 const ResourcesEntry& entry = table[i]; |
| 28 if (strcmp(path, entry.path_) == 0) { |
| 29 *resource = reinterpret_cast<const uint8_t*>(entry.resource_); |
| 30 DCHECK(entry.length_ > 0); |
| 31 return entry.length_; |
| 32 } |
| 33 } |
| 34 *resource = NULL; |
| 35 return -1; |
| 36 } |
| 37 |
| 38 const char* Builtin::mojo_core_patch_resource_names_[] = { |
| 39 "/core/buffer_patch.dart", |
| 40 "/core/data_pipe_patch.dart", |
| 41 "/core/handle_patch.dart", |
| 42 "/core/handle_watcher_patch.dart", |
| 43 "/core/message_pipe_patch.dart", |
| 44 NULL, |
| 45 }; |
| 46 |
16 Builtin::builtin_lib_props Builtin::builtin_libraries_[] = { | 47 Builtin::builtin_lib_props Builtin::builtin_libraries_[] = { |
17 /* { url_, has_natives_, native_symbol_, native_resolver_ } */ | 48 /* { url_, has_natives_, native_symbol_, native_resolver_, |
18 {"dart:mojo_builtin", true, Builtin::NativeSymbol, Builtin::NativeLookup}, | 49 patch_url_, patch_paths_ } */ |
19 {"dart:mojo_bindings", false, nullptr, nullptr}, | 50 {"dart:mojo_builtin", true, Builtin::NativeSymbol, Builtin::NativeLookup, |
20 {"dart:mojo_core", true, MojoNativeSymbol, MojoNativeLookup}, | 51 nullptr, nullptr }, |
| 52 {"dart:mojo_bindings", false, nullptr, nullptr, |
| 53 nullptr, nullptr }, |
| 54 {"dart:mojo_core", true, MojoNativeSymbol, MojoNativeLookup, |
| 55 "dart:mojo_core-patch", mojo_core_patch_resource_names_ }, |
21 }; | 56 }; |
22 | 57 |
23 uint8_t Builtin::snapshot_magic_number[] = {0xf5, 0xf5, 0xdc, 0xdc}; | 58 uint8_t Builtin::snapshot_magic_number[] = {0xf5, 0xf5, 0xdc, 0xdc}; |
24 | 59 |
25 Dart_Handle Builtin::NewError(const char* format, ...) { | 60 Dart_Handle Builtin::NewError(const char* format, ...) { |
26 va_list args; | 61 va_list args; |
27 va_start(args, format); | 62 va_start(args, format); |
28 intptr_t len = vsnprintf(NULL, 0, format, args); | 63 intptr_t len = vsnprintf(nullptr, 0, format, args); |
29 va_end(args); | 64 va_end(args); |
30 | 65 |
31 char* buffer = reinterpret_cast<char*>(Dart_ScopeAllocate(len + 1)); | 66 char* buffer = reinterpret_cast<char*>(Dart_ScopeAllocate(len + 1)); |
32 va_list args2; | 67 va_list args2; |
33 va_start(args2, format); | 68 va_start(args2, format); |
34 vsnprintf(buffer, (len + 1), format, args2); | 69 vsnprintf(buffer, (len + 1), format, args2); |
35 va_end(args2); | 70 va_end(args2); |
36 | 71 |
37 return Dart_NewApiError(buffer); | 72 return Dart_NewApiError(buffer); |
38 } | 73 } |
39 | 74 |
40 void Builtin::SetNativeResolver(BuiltinLibraryId id) { | 75 Dart_Handle Builtin::GetStringResource(const char* resource_name) { |
| 76 const uint8_t* resource_string; |
| 77 int resource_length = FindResource(resource_name, &resource_string); |
| 78 if (resource_length > 0) { |
| 79 return Dart_NewStringFromUTF8(resource_string, resource_length); |
| 80 } |
| 81 return NewError("Could not find resource %s", resource_name); |
| 82 } |
| 83 |
| 84 // Patch all the specified patch files in the array 'patch_resources' into the |
| 85 // library specified in 'library'. |
| 86 void Builtin::LoadPatchFiles(Dart_Handle library, |
| 87 const char* patch_uri, |
| 88 const char** patch_resources) { |
| 89 for (intptr_t j = 0; patch_resources[j] != NULL; j++) { |
| 90 Dart_Handle patch_src = GetStringResource(patch_resources[j]); |
| 91 DART_CHECK_VALID(patch_src); |
| 92 // Prepend the patch library URI to form a unique script URI for the patch. |
| 93 intptr_t len = snprintf(NULL, 0, "%s%s", patch_uri, patch_resources[j]); |
| 94 char* patch_filename = reinterpret_cast<char*>(malloc(len + 1)); |
| 95 snprintf(patch_filename, len + 1, "%s%s", patch_uri, patch_resources[j]); |
| 96 Dart_Handle patch_file_uri = Dart_NewStringFromCString(patch_filename); |
| 97 DART_CHECK_VALID(patch_file_uri); |
| 98 free(patch_filename); |
| 99 DART_CHECK_VALID(Dart_LibraryLoadPatch(library, patch_file_uri, patch_src)); |
| 100 } |
| 101 } |
| 102 |
| 103 Dart_Handle Builtin::GetLibrary(BuiltinLibraryId id) { |
41 static_assert((sizeof(builtin_libraries_) / sizeof(builtin_lib_props)) == | 104 static_assert((sizeof(builtin_libraries_) / sizeof(builtin_lib_props)) == |
42 kInvalidLibrary, "Unexpected number of builtin libraries"); | 105 kInvalidLibrary, "Unexpected number of builtin libraries"); |
43 DCHECK_GE(id, kBuiltinLibrary); | 106 DCHECK_GE(id, kBuiltinLibrary); |
44 DCHECK_LT(id, kInvalidLibrary); | |
45 if (builtin_libraries_[id].has_natives_) { | |
46 Dart_Handle url = Dart_NewStringFromCString(builtin_libraries_[id].url_); | |
47 Dart_Handle library = Dart_LookupLibrary(url); | |
48 DCHECK(!Dart_IsError(library)); | |
49 // Setup the native resolver for built in library functions. | |
50 DART_CHECK_VALID( | |
51 Dart_SetNativeResolver(library, | |
52 builtin_libraries_[id].native_resolver_, | |
53 builtin_libraries_[id].native_symbol_)); | |
54 } | |
55 } | |
56 | |
57 Dart_Handle Builtin::LoadAndCheckLibrary(BuiltinLibraryId id) { | |
58 static_assert((sizeof(builtin_libraries_) / sizeof(builtin_lib_props)) == | |
59 kInvalidLibrary, "Unexpected number of builtin libraries"); | |
60 DCHECK_GE(id, kBuiltinLibrary); | |
61 DCHECK_LT(id, kInvalidLibrary); | 107 DCHECK_LT(id, kInvalidLibrary); |
62 Dart_Handle url = Dart_NewStringFromCString(builtin_libraries_[id].url_); | 108 Dart_Handle url = Dart_NewStringFromCString(builtin_libraries_[id].url_); |
63 Dart_Handle library = Dart_LookupLibrary(url); | 109 Dart_Handle library = Dart_LookupLibrary(url); |
64 DART_CHECK_VALID(library); | 110 DART_CHECK_VALID(library); |
65 return library; | 111 return library; |
66 } | 112 } |
67 | 113 |
| 114 void Builtin::PrepareLibrary(BuiltinLibraryId id) { |
| 115 Dart_Handle library = GetLibrary(id); |
| 116 DCHECK(!Dart_IsError(library)); |
| 117 if (builtin_libraries_[id].has_natives_) { |
| 118 // Setup the native resolver for built in library functions. |
| 119 DART_CHECK_VALID( |
| 120 Dart_SetNativeResolver(library, |
| 121 builtin_libraries_[id].native_resolver_, |
| 122 builtin_libraries_[id].native_symbol_)); |
| 123 } |
| 124 if (builtin_libraries_[id].patch_url_ != nullptr) { |
| 125 DCHECK(builtin_libraries_[id].patch_resources_ != nullptr); |
| 126 LoadPatchFiles(library, |
| 127 builtin_libraries_[id].patch_url_, |
| 128 builtin_libraries_[id].patch_resources_); |
| 129 } |
| 130 } |
| 131 |
68 } // namespace dart | 132 } // namespace dart |
69 } // namespace mojo | 133 } // namespace mojo |
OLD | NEW |