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