Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(268)

Side by Side Diff: sky/engine/core/script/dart_controller.cc

Issue 938623005: Allow multiple dart <script> tags in .sky files (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: All works! Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 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 "sky/engine/config.h" 5 #include "sky/engine/config.h"
6 #include "sky/engine/core/script/dart_controller.h" 6 #include "sky/engine/core/script/dart_controller.h"
7 7
8 #include "base/bind.h" 8 #include "base/bind.h"
9 #include "base/logging.h" 9 #include "base/logging.h"
10 #include "base/single_thread_task_runner.h" 10 #include "base/single_thread_task_runner.h"
11 #include "dart/runtime/include/dart_mirrors_api.h"
11 #include "sky/engine/bindings/builtin.h" 12 #include "sky/engine/bindings/builtin.h"
12 #include "sky/engine/bindings/builtin_natives.h" 13 #include "sky/engine/bindings/builtin_natives.h"
13 #include "sky/engine/bindings/builtin_sky.h" 14 #include "sky/engine/bindings/builtin_sky.h"
14 #include "sky/engine/core/app/AbstractModule.h" 15 #include "sky/engine/core/app/AbstractModule.h"
15 #include "sky/engine/core/app/Module.h" 16 #include "sky/engine/core/app/Module.h"
16 #include "sky/engine/core/dom/Element.h" 17 #include "sky/engine/core/dom/Element.h"
17 #include "sky/engine/core/frame/LocalFrame.h" 18 #include "sky/engine/core/frame/LocalFrame.h"
18 #include "sky/engine/core/html/imports/HTMLImport.h" 19 #include "sky/engine/core/html/imports/HTMLImport.h"
19 #include "sky/engine/core/html/imports/HTMLImportChild.h" 20 #include "sky/engine/core/html/imports/HTMLImportChild.h"
20 #include "sky/engine/core/loader/FrameLoaderClient.h" 21 #include "sky/engine/core/loader/FrameLoaderClient.h"
(...skipping 15 matching lines...) Expand all
36 static const char* kCheckedModeArgs[] = { 37 static const char* kCheckedModeArgs[] = {
37 "--enable_asserts", 38 "--enable_asserts",
38 "--enable_type_checks", 39 "--enable_type_checks",
39 "--error_on_bad_type", 40 "--error_on_bad_type",
40 "--error_on_bad_override", 41 "--error_on_bad_override",
41 }; 42 };
42 #endif 43 #endif
43 44
44 extern const uint8_t* kDartSnapshotBuffer; 45 extern const uint8_t* kDartSnapshotBuffer;
45 46
46 DartController::DartController() : weak_factory_(this) { 47 DartController::DartController() {
47 } 48 }
48 49
49 DartController::~DartController() { 50 DartController::~DartController() {
50 } 51 }
51 52
52 void DartController::LoadModule(RefPtr<AbstractModule> module, 53 bool DartController::ImportChildLibraries(AbstractModule* module,
53 const String& source, 54 Dart_Handle library) {
54 const TextPosition& textPosition) { 55 // If the document has never seen an <import> tag, it won't have an import
56 // controller, and thus will return null for its root HTMLImport. We could
57 // remove this null-check by always creating an ImportController.
58 HTMLImport* root = module->document()->import();
59 if (!root)
60 return true;
61
62 // TODO(abarth): Why doesn't HTMLImport do these casts for us?
63 for (HTMLImportChild* child =
64 static_cast<HTMLImportChild*>(root->firstChild());
65 child; child = static_cast<HTMLImportChild*>(child->next())) {
66 if (Element* link = child->link()) {
67 String name = link->getAttribute(HTMLNames::asAttr);
68
69 Module* child_module = child->module();
70 if (!child_module)
71 continue;
72 for (auto entry : child_module->libraries()) {
abarth-chromium 2015/02/19 02:51:40 s/auto/const auto&/ You almost always want to use
73 if (entry.library()->is_empty())
74 continue;
75 if (LogIfError(Dart_LibraryImportLibrary(
76 library, entry.library()->dart_value(),
77 StringToDart(dart_state(), name))))
78 return false;
79 }
80 }
81 }
82 return true;
83 }
84
85 Dart_Handle DartController::CreateLibrary(AbstractModule* module,
86 const String& source,
87 const TextPosition& textPosition) {
88 Dart_Handle library = Dart_LoadLibrary(
89 StringToDart(dart_state(), module->UrlForLibraryAt(textPosition)),
90 StringToDart(dart_state(), source), textPosition.m_line.zeroBasedInt(),
91 textPosition.m_column.zeroBasedInt());
92
93 if (LogIfError(library))
94 return nullptr;
95
96 if (!ImportChildLibraries(module, library))
97 return nullptr;
98
99 return library;
100 }
101
102 void DartController::LoadScriptInModule(
103 AbstractModule* module,
104 const String& source,
105 const TextPosition& position,
106 const LoadFinishedCallback& finished_callback) {
55 DartIsolateScope isolate_scope(dart_state()->isolate()); 107 DartIsolateScope isolate_scope(dart_state()->isolate());
56 DartApiScope dart_api_scope; 108 DartApiScope dart_api_scope;
57 109
58 DartDependencyCatcher dependency_catcher(dart_state()->loader()); 110 DartDependencyCatcher dependency_catcher(dart_state()->loader());
111 Dart_Handle library_handle = CreateLibrary(module, source, position);
112 if (!library_handle)
113 return finished_callback.Run(nullptr, nullptr);
114 RefPtr<DartValue> library = DartValue::Create(dart_state(), library_handle);
115 module->AddLibrary(library, position);
59 116
60 Dart_Handle library = Dart_LoadLibrary( 117 // TODO(eseidel): Better if the library/module retained its dependencies and
61 StringToDart(dart_state(), module->url()), 118 // dependency waiting could be separate from library creation.
62 StringToDart(dart_state(), source), textPosition.m_line.zeroBasedInt(), 119 dart_state()->loader().WaitForDependencies(
63 textPosition.m_column.zeroBasedInt()); 120 dependency_catcher.dependencies(),
64 121 base::Bind(finished_callback, module, library));
65 if (LogIfError(library))
66 return;
67
68 if (HTMLImport* parent = module->document()->import()) {
69 for (HTMLImportChild* child = static_cast<HTMLImportChild*>(parent->firstChi ld());
70 child; child = static_cast<HTMLImportChild*>(child->next())) {
71 if (Element* link = child->link()) {
72 String name = link->getAttribute(HTMLNames::asAttr);
73
74 Module* childModule = child->module();
75 if (childModule
76 && childModule->library()
77 && !childModule->library()->is_empty()) {
78 if (LogIfError(Dart_LibraryImportLibrary(
79 library, childModule->library()->dart_value(),
80 StringToDart(dart_state(), name))))
81 return;
82 }
83 }
84 }
85 }
86
87 module->set_library(DartValue::Create(dart_state(), library));
88 const auto& dependencies = dependency_catcher.dependencies();
89
90 if (dependencies.isEmpty()) {
91 ExecuteModule(module);
92 } else {
93 dart_state()->loader().WaitForDependencies(
94 dependencies, base::Bind(&DartController::ExecuteModule,
95 weak_factory_.GetWeakPtr(), module));
96 }
97 } 122 }
98 123
99 void DartController::ExecuteModule(RefPtr<AbstractModule> module) { 124 void DartController::ExecuteLibraryInModule(AbstractModule* module,
125 Dart_Handle library) {
126 ASSERT(library);
100 DCHECK(Dart_CurrentIsolate() == dart_state()->isolate()); 127 DCHECK(Dart_CurrentIsolate() == dart_state()->isolate());
101 DartApiScope dart_api_scope; 128 DartApiScope dart_api_scope;
102 129
103 // Don't continue if we failed to load the module. 130 // Don't continue if we failed to load the module.
104 if (LogIfError(Dart_FinalizeLoading(true))) 131 if (LogIfError(Dart_FinalizeLoading(true)))
105 return; 132 return;
106 Dart_Handle library = module->library()->dart_value();
107 const char* name = module->isApplication() ? "main" : "init"; 133 const char* name = module->isApplication() ? "main" : "init";
108 Dart_Handle closure_name = Dart_NewStringFromCString(name);
109 Dart_Handle result = Dart_Invoke(library, closure_name, 0, nullptr);
110 134
111 if (module->isApplication()) { 135 // main() is required, but init() is not:
112 // TODO(dart): This will throw an API error if main() is absent. It would be 136 // TODO(rmacnak): Dart_LookupFunction won't find re-exports, etc.
113 // better to test whether main() is present first, then attempt to invoke it 137 Dart_Handle main = Dart_LookupFunction(library, ToDart(name));
abarth-chromium 2015/02/19 02:51:40 s/main/entry/ ? It's not always main.
114 // so as to capture & report other errors. 138 if (!Dart_IsFunction(main) && !module->isApplication())
115 LogIfError(result); 139 return;
116 } 140
141 Dart_Handle result = Dart_Invoke(library, ToDart(name), 0, nullptr);
142 LogIfError(result);
117 } 143 }
118 144
119 static void UnhandledExceptionCallback(Dart_Handle error) { 145 static void UnhandledExceptionCallback(Dart_Handle error) {
120 DCHECK(!Dart_IsError(error)); 146 DCHECK(!Dart_IsError(error));
121 LOG(ERROR) << Dart_GetError(error); 147 LOG(ERROR) << Dart_GetError(error);
122 } 148 }
123 149
124 static Dart_Handle LibraryTagHandler(Dart_LibraryTag tag, 150 static Dart_Handle LibraryTagHandler(Dart_LibraryTag tag,
125 Dart_Handle library, 151 Dart_Handle library,
126 Dart_Handle url) { 152 Dart_Handle url) {
(...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after
263 289
264 CHECK(Dart_SetVMFlags(argc, argv)); 290 CHECK(Dart_SetVMFlags(argc, argv));
265 CHECK(Dart_Initialize(IsolateCreateCallback, 291 CHECK(Dart_Initialize(IsolateCreateCallback,
266 nullptr, // Isolate interrupt callback. 292 nullptr, // Isolate interrupt callback.
267 UnhandledExceptionCallback, IsolateShutdownCallback, 293 UnhandledExceptionCallback, IsolateShutdownCallback,
268 // File IO callbacks. 294 // File IO callbacks.
269 nullptr, nullptr, nullptr, nullptr, nullptr)); 295 nullptr, nullptr, nullptr, nullptr, nullptr));
270 } 296 }
271 297
272 } // namespace blink 298 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698